weile-global.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * 时间格式化
  3. *
  4. * 用法: (new Date).format('Y-m-d H:i:s');
  5. * @link http://jacwright.com/projects/javascript/date_format/
  6. */
  7. Date.prototype.format=function(e){var t="";var n=Date.replaceChars;for(var r=0;r<e.length;r++){var i=e.charAt(r);if(r-1>=0&&e.charAt(r-1)=="\\"){t+=i}else if(n[i]){t+=n[i].call(this)}else if(i!="\\"){t+=i}}return t};Date.replaceChars={shortMonths:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],longMonths:["January","February","March","April","May","June","July","August","September","October","November","December"],shortDays:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],longDays:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],d:function(){return(this.getDate()<10?"0":"")+this.getDate()},D:function(){return Date.replaceChars.shortDays[this.getDay()]},j:function(){return this.getDate()},l:function(){return Date.replaceChars.longDays[this.getDay()]},N:function(){return this.getDay()+1},S:function(){return this.getDate()%10==1&&this.getDate()!=11?"st":this.getDate()%10==2&&this.getDate()!=12?"nd":this.getDate()%10==3&&this.getDate()!=13?"rd":"th"},w:function(){return this.getDay()},z:function(){var e=new Date(this.getFullYear(),0,1);return Math.ceil((this-e)/864e5)},W:function(){var e=new Date(this.getFullYear(),0,1);return Math.ceil(((this-e)/864e5+e.getDay()+1)/7)},F:function(){return Date.replaceChars.longMonths[this.getMonth()]},m:function(){return(this.getMonth()<9?"0":"")+(this.getMonth()+1)},M:function(){return Date.replaceChars.shortMonths[this.getMonth()]},n:function(){return this.getMonth()+1},t:function(){var e=new Date;return(new Date(e.getFullYear(),e.getMonth(),0)).getDate()},L:function(){var e=this.getFullYear();return e%400==0||e%100!=0&&e%4==0},o:function(){var e=new Date(this.valueOf());e.setDate(e.getDate()-(this.getDay()+6)%7+3);return e.getFullYear()},Y:function(){return this.getFullYear()},y:function(){return(""+this.getFullYear()).substr(2)},a:function(){return this.getHours()<12?"am":"pm"},A:function(){return this.getHours()<12?"AM":"PM"},B:function(){return Math.floor(((this.getUTCHours()+1)%24+this.getUTCMinutes()/60+this.getUTCSeconds()/3600)*1e3/24)},g:function(){return this.getHours()%12||12},G:function(){return this.getHours()},h:function(){return((this.getHours()%12||12)<10?"0":"")+(this.getHours()%12||12)},H:function(){return(this.getHours()<10?"0":"")+this.getHours()},i:function(){return(this.getMinutes()<10?"0":"")+this.getMinutes()},s:function(){return(this.getSeconds()<10?"0":"")+this.getSeconds()},u:function(){var e=this.getMilliseconds();return(e<10?"00":e<100?"0":"")+e},e:function(){return"Not Yet Supported"},I:function(){var e=null;for(var t=0;t<12;++t){var n=new Date(this.getFullYear(),t,1);var r=n.getTimezoneOffset();if(e===null)e=r;else if(r<e){e=r;break}else if(r>e)break}return this.getTimezoneOffset()==e|0},O:function(){return(-this.getTimezoneOffset()<0?"-":"+")+(Math.abs(this.getTimezoneOffset()/60)<10?"0":"")+Math.abs(this.getTimezoneOffset()/60)+"00"},P:function(){return(-this.getTimezoneOffset()<0?"-":"+")+(Math.abs(this.getTimezoneOffset()/60)<10?"0":"")+Math.abs(this.getTimezoneOffset()/60)+":00"},T:function(){var e=this.getMonth();this.setMonth(0);var t=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,"$1");this.setMonth(e);return t},Z:function(){return-this.getTimezoneOffset()*60},c:function(){return this.format("Y-m-d\\TH:i:sP")},r:function(){return this.toString()},U:function(){return this.getTime()/1e3}};
  8. /**
  9. * 微乐游戏后台核心类定义
  10. *
  11. * @author DongHai Hsing <xingdonghai@gmail.com>
  12. * @link http://www.xingdonghai.cn/
  13. * @copyright Copyright &copy; 2016 WeiLe Inc.
  14. * @package com.weile
  15. * @since 0.1
  16. * @version 0.1
  17. */
  18. (function(window) {
  19. var WeiLe = function() {
  20. return WeiLe.prototype.init();
  21. };
  22. WeiLe.prototype = {
  23. V : { //全局变量, 调用方式 weile.V.variable
  24. },
  25. F : { //函数库, 调用方式 weile.F.func()
  26. },
  27. D : { //Dom操作函数, 调用方式 weile.D.func()
  28. },
  29. C : { //类库, 调用方式 weile.C.class
  30. },
  31. init : function() { //初始化
  32. return this;
  33. }
  34. };
  35. window.weile = new WeiLe();
  36. })(window);
  37. /**
  38. * 微乐游戏后台全局函数定义类
  39. *
  40. * @author DongHai Hsing <xingdonghai@gmail.com>
  41. * @link http://www.xingdonghai.cn/
  42. * @copyright Copyright &copy; 2013 WeiLe Inc.
  43. * @package com.weile.admin
  44. * @since 0.1
  45. * @version 0.1
  46. */
  47. weile.F = (function() {
  48. //JSON响应状态解析提示数组
  49. var _JSON_RESPONSE_MSG_DEFINE = {};
  50. return {
  51. modalConfig : {remote : false},
  52. /**
  53. * alert提示框
  54. * 该方法可传入最多4个参数:
  55. * 第一个字符串参数为提示信息
  56. * 第二个字符串参数为提示框标题
  57. * 传入的布尔值参数为返回值
  58. * 传入的可调用函数为点击OK或隐藏时触发的回调事件
  59. *
  60. * @author DongHai Hsing <xingdonghai@gmail.com>
  61. * @return boolean
  62. */
  63. alert : function() {
  64. var msg = '',
  65. title = '系统提示',
  66. ret = false,
  67. callback = function() {};
  68. for (var i = 0, len = arguments.length; i < len; i++) {
  69. switch (typeof arguments[i]) {
  70. case 'string':
  71. if ('' == msg)
  72. msg = arguments[i];
  73. else
  74. title = arguments[i];
  75. break;
  76. case 'boolean':
  77. ret = arguments[i];
  78. break;
  79. case 'function':
  80. callback = arguments[i];
  81. break;
  82. }
  83. }
  84. if ('undefined' == typeof jQuery.fn.modalmanager) {
  85. var html = '<div id="_global_modal_alert" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="_global_modal_alert_label" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button><h3 id="_global_modal_alert_label">' + title + '</h3></div><div class="modal-body"><p>' + msg + '</p></div><div class="modal-footer"><button data-dismiss="modal" class="btn green">OK</button></div></div></div></div>';
  86. $(html).modal(this.modalConfig).on('shown.bs.modal', function() {
  87. var radios = $('#_global_modal_alert input[type=radio]:not(.toggle, .star, .make-switch)');
  88. if (radios.size() > 0) {
  89. radios.each(function () {
  90. if (0 == $(this).parents('.checker').size()) {
  91. $(this).show();
  92. $(this).uniform();
  93. }
  94. });
  95. }
  96. }).on('hidden.bs.modal', function() {$('#_global_modal_alert').remove();}).one('hide', function() {$(this).focus();callback();});
  97. } else {
  98. var html = '<div id="_global_modal_alert" class="modal fade" tabindex="-1" data-focus-on="input:first"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button><h4 class="modal-title">' + title + '</h4></div><div class="modal-body">' + msg + '</div><div class="modal-footer"><button type="button" data-dismiss="modal" class="btn green">OK</button></div></div>';
  99. $(html).modal(this.modalConfig).one('hide', callback);
  100. }
  101. return ret;
  102. },
  103. /**
  104. * confirm提示框
  105. *
  106. * 属性 _CONFIRM_STATE 用于保存confirm提示框的点击结果
  107. *
  108. * @author DongHai Hsing <xingdonghai@gmail.com>
  109. * @param function func 单击确定时执行的回调函数
  110. * @param string s 提示信息字符串
  111. */
  112. //_CONFIRM_STATE : false,
  113. confirm : function(func, s) {
  114. s = s || '确认要执行该操作吗?';
  115. if ('string' == typeof func)
  116. func = new Function(func);
  117. bootbox.confirm({
  118. message : s,
  119. callback : (function(func) {
  120. return function(result) {
  121. if (result)
  122. func(result);
  123. };
  124. })(func),
  125. buttons: {
  126. confirm: {
  127. label: '确定'
  128. },
  129. cancel: {
  130. label: '取消'
  131. }
  132. },
  133. backdrop : false
  134. });
  135. /*this._CONFIRM_STATE = false;
  136. s = s || '确认要执行该操作吗?';
  137. var html = '<div id="static" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-body"><p>' + s + '</p></div><div class="modal-footer"><button type="button" data-dismiss="modal" class="btn">取消</button><button type="button" data-dismiss="modal" class="btn green" onclick="weile.F._CONFIRM_STATE = true;">确定</button></div></div></div></div>';
  138. if ('string' == typeof func)
  139. func = new Function(func);
  140. $(html).modal({remote : false}).one('hide', (function(o, func) {
  141. return function() {
  142. $(o).focus();
  143. if (weile.F._CONFIRM_STATE && func)
  144. func();
  145. };
  146. })(this, func));*/
  147. },
  148. /**
  149. * 在指定区间内随机出一个整数
  150. *
  151. * @author DongHai Hsing <xingdonghai@gmail.com>
  152. * @param integer min 最小值
  153. * @param integer max 最大值
  154. * @return integer
  155. */
  156. rangeRandom : function(min, max) {
  157. return Math.floor(Math.random() * (max - min + 1) + min);
  158. },
  159. /**
  160. * 解析JSON字符串
  161. *
  162. * @author DongHai Hsing <xingdonghai@gmail.com>
  163. * @return object
  164. */
  165. parseJSON : function(data) {
  166. try {
  167. return (new Function('return ' + data))();
  168. } catch (e) {
  169. return {};
  170. }
  171. },
  172. /**
  173. * JSON通信信息解析
  174. *
  175. * @author DongHai Hsing <xingdonghai@gmail.com>
  176. * @param json data
  177. * @return json
  178. */
  179. parseJsonResponse : function(data) {
  180. if (!data || 'object' != typeof data || !data.state)
  181. return data;
  182. _JSON_RESPONSE_MSG_DEFINE = $.extend({
  183. 51 : 'system error',
  184. 501 : 'parameter error',
  185. 502 : 'missing parameter',
  186. 511 : 'invalid app_id',
  187. 512 : 'api not exist',
  188. 513 : 'permission failed',
  189. 514 : 'api has been deprecated',
  190. 515 : 'invalid version',
  191. 521 : 'auth faild',
  192. 522 : 'request has expired',
  193. 523 : 'not open',
  194. 524 : 'invalid token',
  195. 611 : 'failed',
  196. 621 : ''
  197. }, _JSON_RESPONSE_MSG_DEFINE);
  198. if ('undefined' != typeof _JSON_RESPONSE_MSG_DEFINE[data.state] && '' != _JSON_RESPONSE_MSG_DEFINE[data.state])
  199. data.msg = _JSON_RESPONSE_MSG_DEFINE[data.state];
  200. switch (data.state) {
  201. case 501:
  202. if (data.data && 'undefined' != typeof data.data['name'])
  203. data.msg += ' : ' + data.data['name'];
  204. break;
  205. case 502:
  206. if (data.data && 'undefined' != typeof data.data['name'])
  207. data.msg += ' : ' + data.data['name'];
  208. break;
  209. default:
  210. }
  211. return data;
  212. },
  213. /**
  214. * 设置JSON响应状态解析提示数组
  215. *
  216. * @author DongHai Hsing <xingdonghai@gmail.com>
  217. * @param json data
  218. * @return json
  219. */
  220. setJsonResponseMsgDefine : function(data) {
  221. if ('obect' == typeof data)
  222. _JSON_RESPONSE_MSG_DEFINE = $.extend(_JSON_RESPONSE_MSG_DEFINE, data);
  223. },
  224. /**
  225. * JavaScript打开新窗口
  226. *
  227. * @access public
  228. */
  229. windowOpen : function(url) {
  230. if (!/^http[s]?:/.test(url))
  231. url = window.location.protocol + '//' + window.location.host + url;
  232. return window.open(url, '_blank', 'left=0,top=0,width='+ (screen.availWidth - 10) +',height='+ (screen.availHeight-50) +',scrollbars,resizable=yes,fullscreen=yes');
  233. },
  234. /**
  235. * 创建option元素
  236. *
  237. * @access public
  238. */
  239. createOption : function(value, text, selected) {
  240. var option = document.createElement('option');
  241. option.appendChild(document.createTextNode(text));
  242. option.setAttribute('value', value);
  243. option.setAttribute('title', text);
  244. if (selected)
  245. option.setAttribute('selected', true);
  246. return option;
  247. },
  248. /**
  249. * 地区筛选器
  250. *
  251. * @access public
  252. * @param string url 获取数据的url
  253. * @param string type_or_region 当前地区值的类型 或 6位地区代码. 当为类型时, 值可以是province_id或是city_id; 当值为6位地区代码时, 表示通过该地区代码自动联动选择值
  254. * @param string default_value 默认值, 6位地区代码
  255. * @param string parent
  256. */
  257. regionSelecter: function(url, type_or_region, default_value, parent) {
  258. //设置参数
  259. var the = this,
  260. _def_value = -1;
  261. parent = parent || document;
  262. //如果type_or_region传入的是一个6位地区代码, 那么表示该调用为初始化调用, 重新为实参赋值
  263. if (/^\d{6}$/.test(type_or_region)) {
  264. default_value = type_or_region;
  265. type_or_region = 'province_id';
  266. //设置省份列表默认选中值
  267. var _tmp = default_value.substr(0, 2) + '0000';
  268. $('select[name=province_id]', parent).val(_tmp);
  269. if ('function' == typeof $('select[name=province_id]', parent).select2)
  270. $('select[name=province_id]', parent).select2('val', _tmp);
  271. } else if (0 == type_or_region) {
  272. $('select[name=province_id]', parent).val(0);
  273. $('select[name=city_id]', parent).val(0);
  274. $('select[name=area_id]', parent).val(0);
  275. if ('function' == typeof $('select[name=province_id]', parent).select2)
  276. $('select[name=province_id]', parent).select2('val', 0);
  277. if ('function' == typeof $('select[name=city_id]', parent).select2)
  278. $('select[name=city_id]', parent).select2('val', 0);
  279. if ('function' == typeof $('select[name=area_id]', parent).select2)
  280. $('select[name=area_id]', parent).select2('val', 0);
  281. return ;
  282. }
  283. //如果定义了选项默认值则在生成表单列表时自动选中
  284. if ('undefined' != typeof default_value) {
  285. switch (type_or_region) {
  286. case 'province_id':
  287. _def_value = default_value.substr(0, 4) + '00';
  288. break;
  289. case 'city_id':
  290. _def_value = default_value;
  291. break;
  292. }
  293. }
  294. //定义请求参数
  295. var data = {type : type_or_region};
  296. data[type_or_region] = $('select[name=' + type_or_region + ']', parent).val();
  297. if (!data[type_or_region])
  298. data[type_or_region] = '0';
  299. //处理默认值
  300. $.ajax({
  301. 'url': url,
  302. 'type': 'post',
  303. 'dataType': 'json',
  304. 'cache': false,
  305. 'data': data,
  306. success: function (data) {
  307. if (data) {
  308. if (data.state) {
  309. bootbox.alert(data.msg);
  310. } else {
  311. var D = data.data;
  312. for (var name in D) {
  313. var select = $('select[name=' + name + ']', parent),
  314. _selected = -1;
  315. if (select.length) {
  316. select = select[0];
  317. select.length = 0;
  318. select.innerHTML = '';
  319. if ('city_id' == name) {
  320. if ($('select[name=area_id]', parent).length) {
  321. $('select[name=area_id]', parent)[0].length = 0;
  322. $('select[name=area_id]', parent)[0].innerHTML = '';
  323. $('select[name=area_id]', parent)[0].appendChild(the.createOption(0, ' 地区 ', true));
  324. $('select[name=area_id]', parent).select2({
  325. placeholder: '请选择',
  326. allowClear: true
  327. });
  328. }
  329. select.appendChild(the.createOption(0, ' 城市 '));
  330. } else {
  331. select.appendChild(the.createOption(0, ' 地区 '));
  332. }
  333. for (var i in D[name]) {
  334. select.appendChild(the.createOption(i, '[' + i + '] ' + D[name][i], _def_value == i));
  335. if (_def_value == i) //如果某值被选中, 那么将该值缓存
  336. _selected = i;
  337. }
  338. $('select[name=' + name + ']', parent).select2({
  339. placeholder : '请选择',
  340. allowClear : true
  341. });
  342. //如果有被选中的值且当前为城市列表则自动联选下一级
  343. if (-1 != _selected && 'city_id' == name) {
  344. the.regionSelecter(url, 'city_id', default_value);
  345. }
  346. }
  347. break;
  348. }
  349. }
  350. } else {
  351. bootbox.alert('加载失败');
  352. }
  353. },
  354. });
  355. },
  356. /**
  357. * 取消文本选择
  358. */
  359. emptySelection : function() {
  360. if (window.getSelection) {
  361. if (window.getSelection().empty) { // Chrome
  362. window.getSelection().empty();
  363. } else if (window.getSelection().removeAllRanges) { // Firefox
  364. window.getSelection().removeAllRanges();
  365. }
  366. } else if (document.selection) { // IE?
  367. document.selection.empty();
  368. }
  369. }
  370. };
  371. })();
  372. /**
  373. * 微乐游戏后台全局函数定义类
  374. *
  375. * @author DongHai Hsing <xingdonghai@gmail.com>
  376. * @link http://www.xingdonghai.cn/
  377. * @copyright Copyright &copy; 2016 WeiLe Inc.
  378. * @package com.weile
  379. * @since 0.1
  380. * @version 0.1
  381. */
  382. weile.D = (function() {
  383. return {
  384. };
  385. })();