admin.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // +----------------------------------------------------------------------
  2. // | ThinkAdmin
  3. // +----------------------------------------------------------------------
  4. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  5. // +----------------------------------------------------------------------
  6. // | 官方网站: https://thinkadmin.top
  7. // +----------------------------------------------------------------------
  8. // | 开源协议 ( https://mit-license.org )
  9. // +----------------------------------------------------------------------
  10. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  11. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  12. // +----------------------------------------------------------------------
  13. // Layui & jQuery
  14. if (typeof jQuery === 'undefined') window.$ = window.jQuery = layui.$;
  15. window.form = layui.form, window.layer = layui.layer, window.laydate = layui.laydate;
  16. window.appRoot = (function (src) {
  17. return src.pop(), src.pop(), src.join('/') + '/';
  18. })(document.scripts[document.scripts.length - 1].src.split('/'));
  19. window.baseRoot = (function (src) {
  20. return src.substring(0, src.lastIndexOf("/") + 1);
  21. })(document.scripts[document.scripts.length - 1].src);
  22. window.tapiRoot = window.tapiRoot || window.baseRoot + "?s=admin"
  23. // require 配置参数
  24. require.config({
  25. waitSeconds: 60,
  26. baseUrl: baseRoot,
  27. map: {'*': {css: baseRoot + 'plugs/require/css.js'}},
  28. paths: {
  29. 'md5': ['plugs/jquery/md5.min'],
  30. 'json': ['plugs/jquery/json.min'],
  31. 'michat': ['plugs/michat/michat'],
  32. 'base64': ['plugs/jquery/base64.min'],
  33. 'upload': [tapiRoot + '/api.upload?.js'],
  34. 'angular': ['plugs/angular/angular.min'],
  35. 'echarts': ['plugs/echarts/echarts.min'],
  36. 'ckeditor': ['plugs/ckeditor/ckeditor'],
  37. 'websocket': ['plugs/socket/websocket'],
  38. 'pcasunzips': ['plugs/jquery/pcasunzips'],
  39. 'jquery.ztree': ['plugs/ztree/ztree.all.min'],
  40. 'jquery.masonry': ['plugs/jquery/masonry.min'],
  41. 'jquery.autocompleter': ['plugs/jquery/autocompleter.min'],
  42. },
  43. shim: {
  44. 'websocket': {deps: [baseRoot + 'plugs/socket/swfobject.min.js']},
  45. 'jquery.ztree': {deps: ['jquery', 'css!' + baseRoot + 'plugs/ztree/zTreeStyle/zTreeStyle.css']},
  46. 'jquery.autocompleter': {deps: ['jquery', 'css!' + baseRoot + 'plugs/jquery/autocompleter.css']},
  47. }
  48. });
  49. // 注册jquery到require模块
  50. define('jquery', [], function () {
  51. return layui.$;
  52. });
  53. $(function () {
  54. window.$body = $('body');
  55. /*! 消息组件实例 */
  56. $.msg = new function (that) {
  57. that = this, this.idx = [], this.shade = [0.02, '#000'];
  58. // 关闭消息框
  59. this.close = function (index) {
  60. return layer.close(index);
  61. };
  62. // 弹出警告框
  63. this.alert = function (msg, callback) {
  64. var index = layer.alert(msg, {end: callback, scrollbar: false});
  65. return this.idx.push(index), index;
  66. };
  67. // 确认对话框
  68. this.confirm = function (msg, ok, no) {
  69. var index = layer.confirm(msg, {title: '操作确认', btn: ['确认', '取消']}, function () {
  70. typeof ok === 'function' && ok.call(this, index);
  71. }, function () {
  72. typeof no === 'function' && no.call(this, index);
  73. that.close(index);
  74. });
  75. return index;
  76. };
  77. // 显示成功类型的消息
  78. this.success = function (msg, time, callback) {
  79. var index = layer.msg(msg, {icon: 1, shade: this.shade, scrollbar: false, end: callback, time: (time || 2) * 1000, shadeClose: true});
  80. return this.idx.push(index), index;
  81. };
  82. // 显示失败类型的消息
  83. this.error = function (msg, time, callback) {
  84. var index = layer.msg(msg, {icon: 2, shade: this.shade, scrollbar: false, time: (time || 3) * 1000, end: callback, shadeClose: true});
  85. return this.idx.push(index), index;
  86. };
  87. // 状态消息提示
  88. this.tips = function (msg, time, callback) {
  89. var index = layer.msg(msg, {time: (time || 3) * 1000, shade: this.shade, end: callback, shadeClose: true});
  90. return this.idx.push(index), index;
  91. };
  92. // 显示正在加载中的提示
  93. this.loading = function (msg, callback) {
  94. var index = msg ? layer.msg(msg, {icon: 16, scrollbar: false, shade: this.shade, time: 0, end: callback}) : layer.load(2, {time: 0, scrollbar: false, shade: this.shade, end: callback});
  95. return this.idx.push(index), index;
  96. };
  97. // 自动处理显示Think返回的Json数据
  98. this.auto = function (ret, time) {
  99. var url = ret.url || (typeof ret.data === 'string' ? ret.data : '');
  100. var msg = ret.msg || (typeof ret.info === 'string' ? ret.info : '');
  101. if (parseInt(ret.code) === 1 && time === 'false') {
  102. return url ? (window.location.href = url) : $.form.reload();
  103. }
  104. return (parseInt(ret.code) === 1) ? this.success(msg, time, function () {
  105. url ? (window.location.href = url) : $.form.reload();
  106. for (var i in that.idx) layer.close(that.idx[i]);
  107. that.idx = [];
  108. }) : this.error(msg, 3, function () {
  109. url ? window.location.href = url : '';
  110. });
  111. };
  112. };
  113. /*! 表单自动化组件 */
  114. $.form = new function (that) {
  115. that = this;
  116. // 内容区选择器
  117. this.selecter = '.layui-layout-admin>.layui-body';
  118. // 刷新当前页面
  119. this.reload = function () {
  120. window.onhashchange.call(this);
  121. };
  122. // 内容区域动态加载后初始化
  123. this.reInit = function ($dom) {
  124. $.vali.listen(this);
  125. $dom = $dom || $(this.selecter);
  126. $dom.find('[required]').map(function ($parent) {
  127. if (($parent = $(this).parent()) && $parent.is('label')) {
  128. $parent.addClass('label-required-prev');
  129. } else {
  130. $parent.prevAll('label').addClass('label-required-next');
  131. }
  132. });
  133. $dom.find('input[data-date-range]').map(function () {
  134. this.setAttribute('autocomplete', 'off');
  135. laydate.render({
  136. type: this.getAttribute('data-date-range') || 'date',
  137. range: true, elem: this, done: function (value) {
  138. $(this.elem).val(value).trigger('change');
  139. }
  140. });
  141. });
  142. $dom.find('input[data-date-input]').map(function () {
  143. this.setAttribute('autocomplete', 'off');
  144. laydate.render({
  145. type: this.getAttribute('data-date-input') || 'date',
  146. range: false, elem: this, done: function (value) {
  147. $(this.elem).val(value).trigger('change');
  148. }
  149. });
  150. });
  151. $dom.find('[data-file]:not([data-inited])').map(function (index, elem, $this, field) {
  152. $this = $(elem), field = $this.attr('data-field') || 'file';
  153. if (!$this.data('input')) $this.data('input', $('[name="' + field + '"]').get(0));
  154. $this.uploadFile(function (url, file) {
  155. $($this.data('input')).data('file', file).val(url).trigger('change');
  156. });
  157. });
  158. };
  159. // 在内容区显示视图
  160. this.show = function (html) {
  161. $(this.selecter).html(html);
  162. this.reInit($(this.selecter));
  163. setTimeout(function () {
  164. that.reInit($(that.selecter));
  165. }, 500);
  166. };
  167. // 以HASH打开新网页
  168. this.href = function (url, obj) {
  169. if (url !== '#') window.location.href = '#' + $.menu.parseUri(url, obj);
  170. else if (obj && obj.getAttribute('data-menu-node')) {
  171. $('[data-menu-node^="' + obj.getAttribute('data-menu-node') + '-"][data-open!="#"]:first').trigger('click');
  172. }
  173. };
  174. // 异步加载的数据
  175. this.load = function (url, data, method, callback, loading, tips, time, headers) {
  176. var index = loading !== false ? $.msg.loading(tips) : 0;
  177. if (typeof data === 'object' && typeof data['_token_'] === 'string') {
  178. headers = headers || {}, headers['User-Form-Token'] = data['_token_'], delete data['_token_'];
  179. }
  180. $.ajax({
  181. data: data || {}, type: method || 'GET', url: $.menu.parseUri(url), beforeSend: function (xhr, i) {
  182. if (typeof Pace === 'object' && loading !== false) Pace.restart();
  183. if (typeof headers === 'object') for (i in headers) xhr.setRequestHeader(i, headers[i]);
  184. }, error: function (XMLHttpRequest, $dialog, dialogIdx, iframe) {
  185. if (parseInt(XMLHttpRequest.status) !== 200 && XMLHttpRequest.responseText.indexOf('Call Stack') > -1) try {
  186. dialogIdx = layer.open({title: XMLHttpRequest.status + ' - ' + XMLHttpRequest.statusText, type: 2, move: false, content: 'javascript:;'});
  187. layer.full(dialogIdx), $dialog = $('#layui-layer' + dialogIdx), iframe = $dialog.find('iframe').get(0);
  188. (iframe.contentDocument || iframe.contentWindow.document).write(XMLHttpRequest.responseText);
  189. $dialog.find('.layui-layer-setwin').css({right: '35px', top: '28px'}).find('a').css({marginLeft: 0});
  190. $dialog.find('.layui-layer-title').css({color: 'red', height: '70px', lineHeight: '70px', fontSize: '22px', textAlign: 'center', fontWeight: 700});
  191. } catch (e) {
  192. layer.close(dialogIdx);
  193. }
  194. layer.closeAll('loading');
  195. if (parseInt(XMLHttpRequest.status) !== 200) {
  196. $.msg.tips('E' + XMLHttpRequest.status + ' - 服务器繁忙,请稍候再试!');
  197. } else {
  198. this.success(XMLHttpRequest.responseText);
  199. }
  200. }, success: function (ret) {
  201. if (typeof callback === 'function' && callback.call(that, ret) === false) return false;
  202. return typeof ret === 'object' ? $.msg.auto(ret, time || ret.wait || undefined) : that.show(ret);
  203. }, complete: function () {
  204. $.msg.close(index);
  205. }
  206. });
  207. };
  208. // 加载HTML到目标位置
  209. this.open = function (url, data, callback, loading, tips) {
  210. this.load(url, data, 'get', function (ret) {
  211. return (typeof ret === 'object' ? $.msg.auto(ret) : that.show(ret)), false;
  212. }, loading, tips);
  213. };
  214. // 打开一个iframe窗口
  215. this.iframe = function (url, title, area) {
  216. return layer.open({title: title || '窗口', type: 2, area: area || ['800px', '580px'], fix: true, maxmin: false, content: url});
  217. };
  218. // 加载HTML到弹出层
  219. this.modal = function (url, data, title, callback, loading, tips) {
  220. this.load(url, data, 'GET', function (res, index) {
  221. if (typeof (res) === 'object') return $.msg.auto(res), false;
  222. index = layer.open({
  223. type: 1, btn: false, area: "800px", content: res, title: title || '', success: function (dom, index) {
  224. $(dom).find('[data-close]').off('click').on('click', function () {
  225. if ($(this).attr('data-confirm')) return $.msg.confirm($(this).attr('data-confirm'), function (_index) {
  226. layer.close(_index), layer.close(index);
  227. }), false;
  228. layer.close(index);
  229. });
  230. $.form.reInit($(dom));
  231. }
  232. });
  233. $.msg.idx.push(index);
  234. return (typeof callback === 'function') && callback.call(that);
  235. }, loading, tips);
  236. };
  237. };
  238. /*! 后台菜单辅助插件 */
  239. $.menu = new function (that) {
  240. that = this;
  241. // 计算URL地址中有效的URI
  242. this.getUri = function (uri) {
  243. uri = uri || window.location.href;
  244. uri = (uri.indexOf(window.location.host) > -1 ? uri.split(window.location.host)[1] : uri);
  245. return (uri.indexOf('#') > -1 ? uri.split('#')[1] : uri).split('?')[0];
  246. };
  247. // 通过URI查询最有可能的菜单NODE
  248. this.queryNode = function (url, node) {
  249. node = node || location.href.replace(/.*spm=([\d\-m]+).*/ig, '$1');
  250. if (!/^m-/.test(node)) {
  251. var $menu = $('[data-menu-node][data-open*="' + url.replace(/\.html$/ig, '') + '"]');
  252. return $menu.size() ? $menu.get(0).getAttribute('data-menu-node') : '';
  253. }
  254. return node;
  255. };
  256. // URL转URI
  257. this.parseUri = function (uri, obj) {
  258. var params = {};
  259. if (uri.indexOf('?') > -1) {
  260. var attrs = uri.split('?')[1].split('&');
  261. for (var i in attrs) if (attrs[i].indexOf('=') > -1) {
  262. var tmp = attrs[i].split('=').slice();
  263. if (typeof tmp[0] === 'string' && tmp[0].length > 0) {
  264. params[tmp[0]] = decodeURIComponent(tmp[1].replace(/%2B/ig, '%20'));
  265. }
  266. }
  267. }
  268. uri = this.getUri(uri);
  269. if (typeof params.spm !== 'string') {
  270. params.spm = obj && obj.getAttribute('data-menu-node') || this.queryNode(uri);
  271. }
  272. if (typeof params.spm !== 'string' || params.spm.length < 1) delete params.spm;
  273. // 生成新的 URL 参数
  274. var attrs = [];
  275. for (var i in params) attrs.push([i, params[i]].join('='));
  276. var query = '?' + attrs.join('&');
  277. return uri + (query === '?' ? '' : query);
  278. };
  279. // 后台菜单动作初始化
  280. this.listen = function () {
  281. /*! 初始化操作 */
  282. layui.form.on('switch(ThinkAdminDebug)', function (data) {
  283. jQuery.post(tapiRoot + '/api.plugs/debug', {state: data.elem.checked ? 1 : 0});
  284. });
  285. /*! 菜单模式切换 */
  286. (function ($menu, miniClass) {
  287. /*! Mini 菜单模式切换及显示 */
  288. if (layui.data('admin-menu-type')['type-mini']) $menu.addClass(miniClass);
  289. $body.on('click', '[data-target-menu-type]', function () {
  290. $menu.toggleClass(miniClass), layui.data('admin-menu-type', {key: 'type-mini', value: $menu.hasClass(miniClass)});
  291. }).on('resize', function () {
  292. $body.width() > 1000 ? (layui.data('admin-menu-type')['type-mini'] ? $menu.addClass(miniClass) : $menu.removeClass(miniClass)) : $menu.addClass(miniClass);
  293. }).trigger('resize');
  294. /*! Mini 菜单模式时TIPS文字显示 */
  295. $('[data-target-tips]').mouseenter(function () {
  296. if ($menu.hasClass(miniClass)) $(this).attr('index', layer.tips($(this).attr('data-target-tips') || '', this));
  297. }).mouseleave(function () {
  298. layer.close($(this).attr('index'));
  299. });
  300. })($('.layui-layout-admin'), 'layui-layout-left-mini');
  301. /*! 左则二级菜单展示 */
  302. $('[data-submenu-layout]>a').on('click', function () {
  303. that.syncOpenStatus(1);
  304. });
  305. /*! 同步二级菜单展示状态 */
  306. this.syncOpenStatus = function (mode) {
  307. $('[data-submenu-layout]').map(function (node) {
  308. node = $(this).attr('data-submenu-layout');
  309. if (mode === 1) {
  310. layui.data('admin-menu-stat', {key: node, value: $(this).hasClass('layui-nav-itemed') ? 2 : 1});
  311. } else if ((layui.data('admin-menu-stat')[node] || 2) === 2) {
  312. $(this).addClass('layui-nav-itemed');
  313. }
  314. });
  315. };
  316. window.onhashchange = function (hash, node) {
  317. hash = window.location.hash || '';
  318. if (hash.length < 1) return $('[data-menu-node][data-open!="#"]:first').trigger('click');
  319. $.form.load(hash), that.syncOpenStatus(2);
  320. // 菜单选择切换
  321. node = that.queryNode(that.getUri());
  322. if (/^m-/.test(node)) {
  323. var $all = $('a[data-menu-node]').parent(), tmp = node.split('-'), tmpNode = tmp.shift();
  324. while (tmp.length > 0) {
  325. tmpNode = tmpNode + '-' + tmp.shift();
  326. $all = $all.not($('a[data-menu-node="' + tmpNode + '"]').parent().addClass('layui-this'));
  327. }
  328. $all.removeClass('layui-this');
  329. // 菜单模式切换
  330. if (node.split('-').length > 2) {
  331. var _tmp = node.split('-'), _node = _tmp.shift() + '-' + _tmp.shift();
  332. $('[data-menu-layout]').not($('[data-menu-layout="' + _node + '"]').removeClass('layui-hide')).addClass('layui-hide');
  333. $('[data-menu-node="' + node + '"]').parent().parent().parent().addClass('layui-nav-itemed');
  334. $('.layui-layout-admin').removeClass('layui-layout-left-hide');
  335. } else $('.layui-layout-admin').addClass('layui-layout-left-hide');
  336. that.syncOpenStatus(1);
  337. }
  338. };
  339. // URI初始化动作
  340. window.onhashchange.call(this);
  341. };
  342. };
  343. /*! 注册对象到Jq */
  344. $.vali = function (form, callback, options) {
  345. return (new function (that) {
  346. that = this;
  347. // 表单元素
  348. this.tags = 'input,textarea,select';
  349. // 检测元素事件
  350. this.checkEvent = {change: true, blur: true, keyup: false};
  351. // 去除字符串两头的空格
  352. this.trim = function (str) {
  353. return str.replace(/(^\s*)|(\s*$)/g, '');
  354. };
  355. // 标签元素是否可见
  356. this.isVisible = function (ele) {
  357. return $(ele).is(':visible');
  358. };
  359. // 检测属性是否有定义
  360. this.hasProp = function (ele, prop) {
  361. if (typeof prop !== "string") return false;
  362. var attrProp = ele.getAttribute(prop);
  363. return (typeof attrProp !== 'undefined' && attrProp !== null && attrProp !== false);
  364. };
  365. // 判断表单元素是否为空
  366. this.isEmpty = function (ele, value) {
  367. var trim = this.trim(ele.value);
  368. value = value || ele.getAttribute('placeholder');
  369. return (trim === "" || trim === value);
  370. };
  371. // 正则验证表单元素
  372. this.isRegex = function (ele, regex, params) {
  373. var input = $(ele).val(), real = this.trim(input);
  374. regex = regex || ele.getAttribute('pattern');
  375. if (real === "" || !regex) return true;
  376. return new RegExp(regex, params || 'i').test(real);
  377. };
  378. // 检侧所的表单元素
  379. this.checkAllInput = function () {
  380. var isPass = true;
  381. $(form).find(this.tags).each(function () {
  382. if (that.checkInput(this) === false) return $(this).focus(), isPass = false;
  383. });
  384. return isPass;
  385. };
  386. // 检测表单单元
  387. this.checkInput = function (input) {
  388. var tag = input.tagName.toLowerCase(), need = this.hasProp(input, "required");
  389. var type = (input.getAttribute("type") || '').replace(/\W+/, "").toLowerCase();
  390. if (this.hasProp(input, 'data-auto-none')) return true;
  391. var ingoreTags = ['select'], ingoreType = ['radio', 'checkbox', 'submit', 'reset', 'image', 'file', 'hidden'];
  392. for (var i in ingoreTags) if (tag === ingoreTags[i]) return true;
  393. for (var i in ingoreType) if (type === ingoreType[i]) return true;
  394. if (need && this.isEmpty(input)) return this.remind(input);
  395. return this.isRegex(input) ? (this.hideError(input), true) : this.remind(input);
  396. };
  397. // 验证标志
  398. this.remind = function (input) {
  399. if (!this.isVisible(input)) return true;
  400. this.showError(input, input.getAttribute('title') || input.getAttribute('placeholder') || '输入错误');
  401. return false;
  402. };
  403. // 错误消息显示
  404. this.showError = function (ele, content) {
  405. $(ele).addClass('validate-error'), this.insertError(ele);
  406. $($(ele).data('input-info')).addClass('layui-anim layui-anim-fadein').css({width: 'auto'}).html(content);
  407. };
  408. // 错误消息消除
  409. this.hideError = function (ele) {
  410. $(ele).removeClass('validate-error'), this.insertError(ele);
  411. $($(ele).data('input-info')).removeClass('layui-anim-fadein').css({width: '30px'}).html('');
  412. };
  413. // 错误消息标签插入
  414. this.insertError = function (ele) {
  415. var $html = $('<span style="padding-right:12px;color:#a94442;position:absolute;right:0;font-size:12px;z-index:2;display:block;width:34px;text-align:center;pointer-events:none"></span>');
  416. $html.css({top: $(ele).position().top + 'px', paddingBottom: $(ele).css('paddingBottom'), lineHeight: $(ele).css('height')});
  417. $(ele).data('input-info') || $(ele).data('input-info', $html.insertAfter(ele));
  418. };
  419. // 表单验证入口
  420. this.check = function (form, callback) {
  421. $(form).attr("novalidate", "novalidate");
  422. $(form).find(that.tags).map(function () {
  423. this.bindEventMethod = function () {
  424. that.checkInput(this);
  425. };
  426. for (var e in that.checkEvent) if (that.checkEvent[e] === true) {
  427. $(this).off(e, this.bindEventMethod).on(e, this.bindEventMethod);
  428. }
  429. });
  430. $(form).bind("submit", function (event) {
  431. if (that.checkAllInput() && typeof callback === 'function') {
  432. if (typeof CKEDITOR === 'object' && typeof CKEDITOR.instances === 'object') {
  433. for (var i in CKEDITOR.instances) CKEDITOR.instances[i].updateElement();
  434. }
  435. callback.call(this, $(form).formToJson());
  436. }
  437. return event.preventDefault(), false;
  438. });
  439. $(form).find('[data-form-loaded]').map(function () {
  440. $(this).html(this.getAttribute('data-form-loaded') || this.innerHTML);
  441. $(this).removeAttr('data-form-loaded').removeClass('layui-disabled');
  442. });
  443. return $(form).data('validate', this);
  444. };
  445. }).check(form, callback, options);
  446. };
  447. /*! 自动监听规则内表单 */
  448. $.vali.listen = function () {
  449. $('form[data-auto]').map(function () {
  450. if ($(this).attr('data-listen') !== 'true') $(this).attr('data-listen', 'true').vali(function (data) {
  451. var call = $(this).attr('data-callback') || '_default_callback';
  452. var type = this.getAttribute('method') || 'POST', tips = this.getAttribute('data-tips') || undefined;
  453. var time = this.getAttribute('data-time') || undefined, href = this.getAttribute('action') || window.location.href;
  454. $.form.load(href, data, type, window[call] || undefined, true, tips, time);
  455. });
  456. });
  457. };
  458. /*! 注册对象到JqFn */
  459. $.fn.vali = function (callback, options) {
  460. return $.vali(this, callback, options);
  461. };
  462. /*! 表单转JSON */
  463. $.fn.formToJson = function () {
  464. var self = this, data = {}, push = {};
  465. var patterns = {"key": /[a-zA-Z0-9_]+|(?=\[\])/g, "push": /^$/, "fixed": /^\d+$/, "named": /^[a-zA-Z0-9_]+$/};
  466. this.build = function (base, key, value) {
  467. return (base[key] = value), base;
  468. };
  469. this.pushCounter = function (name) {
  470. if (push[name] === undefined) push[name] = 0;
  471. return push[name]++;
  472. };
  473. $.each($(this).serializeArray(), function () {
  474. var key, keys = this.name.match(patterns.key), merge = this.value, name = this.name;
  475. while ((key = keys.pop()) !== undefined) {
  476. name = name.replace(new RegExp("\\[" + key + "\\]$"), '');
  477. if (key.match(patterns.push)) { // push
  478. merge = self.build([], self.pushCounter(name), merge);
  479. } else if (key.match(patterns.fixed)) { // fixed
  480. merge = self.build([], key, merge);
  481. } else if (key.match(patterns.named)) { // named
  482. merge = self.build({}, key, merge);
  483. }
  484. }
  485. data = $.extend(true, data, merge);
  486. });
  487. return data;
  488. };
  489. /*! 全局文件上传入口 */
  490. $.fn.uploadFile = function (callback) {
  491. if (this.attr('data-inited')) return false;
  492. var that = this, mode = $(this).attr('data-file') || 'one';
  493. this.attr('data-inited', true).attr('data-multiple', (mode !== 'btn' && mode !== 'one') ? 1 : 0);
  494. require(['upload'], function (apply) {
  495. apply.call(this, that, callback);
  496. });
  497. };
  498. /*! 上传单张图片 */
  499. $.fn.uploadOneImage = function () {
  500. return this.each(function (input, template) {
  501. input = $(this), template = $('<a data-file="one" class="uploadimage"><span class="layui-icon">&#x1006;</span></a>');
  502. template.attr('data-type', input.data('type') || 'png,jpg,gif');
  503. template.attr('data-field', input.attr('name') || 'image').data('input', this);
  504. template.find('span').on('click', function (event) {
  505. event.stopPropagation(), template.attr('style', ''), input.val('');
  506. });
  507. input.attr('name', template.attr('data-field')).after(template).on('change', function () {
  508. if (this.value) template.css('backgroundImage', 'url(' + encodeURI(this.value) + ')');
  509. }).trigger('change');
  510. }), this;
  511. };
  512. /*! 上传多张图片 */
  513. $.fn.uploadMultipleImage = function () {
  514. return this.each(function () {
  515. var $button = $('<a class="uploadimage"></a>'), images = this.value ? this.value.split('|') : [];
  516. var $input = $(this), name = $input.attr('name') || 'umt-image', type = $input.data('type') || 'png,jpg,gif';
  517. $button.attr('data-type', type).attr('data-field', name).attr('data-file', 'mut').data('input', this);
  518. $input.attr('name', name).after($button), $button.uploadFile(function (src) {
  519. images.push(src), $input.val(images.join('|')), showImageContainer([src]);
  520. });
  521. if (images.length > 0) showImageContainer(images);
  522. function showImageContainer(srcs) {
  523. $(srcs).each(function (idx, src, $image) {
  524. $image = $('<div class="uploadimage uploadimagemtl"><a class="layui-icon margin-right-5">&#xe602;</a><a class="layui-icon margin-right-5">&#x1006;</a><a class="layui-icon margin-right-5">&#xe603;</a></div>');
  525. $image.attr('data-tips-image', encodeURI(src)).css('backgroundImage', 'url(' + encodeURI(src) + ')').on('click', 'a', function (event, index, prevs, $item) {
  526. event.stopPropagation(), $item = $(this).parent(), index = $(this).index(), prevs = $button.prevAll('div.uploadimage').length;
  527. if (index === 0 && $item.index() !== prevs) $item.next().after($item);
  528. else if (index === 2 && $item.index() > 1) $item.prev().before($item);
  529. else if (index === 1) $item.remove();
  530. images = [], $button.prevAll('.uploadimage').map(function () {
  531. images.push($(this).attr('data-tips-image'));
  532. });
  533. images.reverse(), $input.val(images.join('|'));
  534. }), $button.before($image);
  535. });
  536. };
  537. }), this;
  538. };
  539. /*! 注册 data-load 事件行为 */
  540. $body.on('click', '[data-load]', function () {
  541. var url = $(this).attr('data-load'), tips = $(this).attr('data-tips'), time = $(this).attr('data-time');
  542. if ($(this).attr('data-confirm')) return $.msg.confirm($(this).attr('data-confirm'), function () {
  543. $.form.load(url, {}, 'get', null, true, tips, time);
  544. });
  545. $.form.load(url, {}, 'get', null, true, tips, time);
  546. });
  547. /*! 注册 data-serach 表单搜索行为 */
  548. $body.on('submit', 'form.form-search', function () {
  549. var url = $(this).attr('action').replace(/&?page=\d+/g, ''), split = url.indexOf('?') === -1 ? '?' : '&';
  550. if ((this.method || 'get').toLowerCase() === 'get') {
  551. if (location.href.indexOf('spm=') > -1) {
  552. return window.location.href = '#' + $.menu.parseUri(url + split + $(this).serialize());
  553. } else {
  554. return window.location.href = $.menu.parseUri(url + split + $(this).serialize());
  555. }
  556. }
  557. $.form.load(url, this, 'post');
  558. });
  559. /*! 注册 data-modal 事件行为 */
  560. $body.on('click', '[data-modal]', function () {
  561. return $.form.modal($(this).attr('data-modal'), 'open_type=modal', $(this).attr('data-title') || $(this).text() || '编辑');
  562. });
  563. /*! 注册 data-open 事件行为 */
  564. $body.on('click', '[data-open]', function () {
  565. $.form.href($(this).attr('data-open'), this);
  566. });
  567. /*! 注册 data-dbclick 事件行为 */
  568. $body.on('dblclick', '[data-dbclick]', function () {
  569. $(this).find(this.getAttribute('data-dbclick') || '[data-dbclick]').trigger('click');
  570. });
  571. /*! 注册 data-reload 事件行为 */
  572. $body.on('click', '[data-reload]', function () {
  573. $.form.reload();
  574. });
  575. /*! 注册 data-check 事件行为 */
  576. $body.on('click', '[data-check-target]', function () {
  577. var checked = !!this.checked;
  578. $($(this).attr('data-check-target')).map(function () {
  579. (this.checked = checked), $(this).trigger('change');
  580. });
  581. });
  582. /*! 注册 data-action 事件行为 */
  583. $body.on('click', '[data-action]', function () {
  584. var $this = $(this), data = {}, time = $this.attr('data-time'), action = $this.attr('data-action');
  585. var loading = $this.attr('data-loading'), method = $this.attr('data-method') || 'post';
  586. var rule = $this.attr('data-value') || (function (rule, ids) {
  587. $($this.attr('data-target') || 'input[type=checkbox].list-check-box').map(function () {
  588. (this.checked) && ids.push(this.value);
  589. });
  590. return ids.length > 0 ? rule.replace('{key}', ids.join(',')) : '';
  591. }).call(this, $this.attr('data-rule') || '', []) || '';
  592. if (rule.length < 1) return $.msg.tips('请选择需要更改的数据!');
  593. var rules = rule.split(';');
  594. for (var i in rules) {
  595. if (rules[i].length < 2) return $.msg.tips('异常的数据操作规则,请修改规则!');
  596. data[rules[i].split('#')[0]] = rules[i].split('#')[1];
  597. }
  598. data['_token_'] = $this.attr('data-token') || $this.attr('data-csrf') || '--';
  599. var load = loading !== 'false', tips = typeof loading === 'string' ? loading : undefined;
  600. if (!$this.attr('data-confirm')) $.form.load(action, data, method, false, load, tips, time);
  601. else $.msg.confirm($this.attr('data-confirm'), function () {
  602. $.form.load(action, data, method, false, load, tips, time);
  603. });
  604. });
  605. /*! 表单元素失焦时提交 */
  606. $body.on('blur', '[data-action-blur]', function () {
  607. var data = {}, that = this, $this = $(this), action = $this.attr('data-action-blur');
  608. var time = $this.attr('data-time'), loading = $this.attr('data-loading') || false;
  609. var load = loading !== 'false', tips = typeof loading === 'string' ? loading : undefined;
  610. var method = $this.attr('data-method') || 'post', confirm = $this.attr('data-confirm');
  611. var attrs = $this.attr('data-value').replace('{value}', $this.val()).split(';');
  612. for (var i in attrs) {
  613. if (attrs[i].length < 2) return $.msg.tips('异常的数据操作规则,请修改规则!');
  614. data[attrs[i].split('#')[0]] = attrs[i].split('#')[1];
  615. }
  616. that.callback = function (ret) {
  617. return $this.css('border', (ret && ret.code) ? '1px solid #e6e6e6' : '1px solid red'), false;
  618. };
  619. data['_token_'] = $this.attr('data-token') || $this.attr('data-csrf') || '--';
  620. if (!confirm) return $.form.load(action, data, method, that.callback, load, tips, time);
  621. $.msg.confirm(confirm, function () {
  622. $.form.load(action, data, method, that.callback, load, tips, time);
  623. });
  624. });
  625. /*! 表单元素失去焦点时数字 */
  626. $body.on('blur', '[data-blur-number]', function (fiexd) {
  627. fiexd = this.getAttribute('data-blur-number') || 0;
  628. this.value = (parseFloat(this.value) || 0).toFixed(fiexd);
  629. });
  630. /*! 注册 data-href 事件行为 */
  631. $body.on('click', '[data-href]', function (href) {
  632. href = $(this).attr('data-href');
  633. if (href && href.indexOf('#') !== 0) window.location.href = href;
  634. });
  635. /*! 注册 data-iframe 事件行为 */
  636. $body.on('click', '[data-iframe]', function () {
  637. $(this).attr('data-index', $.form.iframe(
  638. $(this).attr('data-iframe'), $(this).attr('data-title') || '窗口',
  639. $(this).attr('data-area') || undefined)
  640. );
  641. });
  642. /*! 注册 data-icon 事件行为 */
  643. $body.on('click', '[data-icon]', function (field, location) {
  644. location = tapiRoot + '/api.plugs/icon';
  645. field = $(this).attr('data-icon') || $(this).attr('data-field') || 'icon';
  646. $.form.iframe(location + (location.indexOf('?') > -1 ? '&' : '?') + 'field=' + field, '图标选择');
  647. });
  648. /*! 注册 data-copy 事件行为 */
  649. $body.on('click', '[data-copy]', function () {
  650. $.copyToClipboard(this.getAttribute('data-copy'));
  651. });
  652. $.copyToClipboard = function (content, input) {
  653. input = document.createElement('textarea');
  654. input.style.position = 'absolute', input.style.left = '-100000px';
  655. input.style.width = '1px', input.style.height = '1px', input.innerText = content;
  656. document.body.appendChild(input), input.select(), setTimeout(function () {
  657. document.execCommand('Copy') ? $.msg.tips('复制成功') : $.msg.tips('复制失败,请使用鼠标操作复制!');
  658. document.body.removeChild(input);
  659. }, 100);
  660. };
  661. /*! 注册 data-tips-text 事件行为 */
  662. $body.on('mouseenter', '[data-tips-text]', function () {
  663. $(this).attr('index', layer.tips($(this).attr('data-tips-text'), this, {tips: [$(this).attr('data-tips-type') || 3, '#78BA32']}));
  664. }).on('mouseleave', '[data-tips-text]', function () {
  665. layer.close($(this).attr('index'));
  666. });
  667. /*! 注册 data-tips-image 事件行为 */
  668. $body.on('click', '[data-tips-image]', function () {
  669. $.previewImage(this.getAttribute('data-tips-image') || this.src, this.getAttribute('data-width'));
  670. });
  671. $.previewImage = function (src, area) {
  672. var img = new Image(), index = $.msg.loading();
  673. img.style.background = '#fff', img.style.display = 'none';
  674. img.style.height = 'auto', img.style.width = area || '480px';
  675. document.body.appendChild(img), img.onerror = function () {
  676. $.msg.close(index);
  677. }, img.onload = function () {
  678. layer.open({
  679. type: 1, shadeClose: true, success: img.onerror, content: $(img), title: false,
  680. area: area || '480px', closeBtn: 1, skin: 'layui-layer-nobg', end: function () {
  681. document.body.removeChild(img);
  682. }
  683. });
  684. };
  685. img.src = src;
  686. };
  687. /*! 注册 data-phone-view 事件行为 */
  688. $body.on('click', '[data-phone-view]', function () {
  689. $.previewPhonePage(this.getAttribute('data-phone-view') || this.href);
  690. });
  691. $.previewPhonePage = function (href, title, template) {
  692. template = '<div><div class="mobile-preview pull-left"><div class="mobile-header">_TITLE_</div><div class="mobile-body"><iframe id="phone-preview" src="_URL_" frameborder="0" marginheight="0" marginwidth="0"></iframe></div></div></div>';
  693. layer.style(layer.open({type: true, scrollbar: false, area: ['320px', '600px'], title: false, closeBtn: true, shadeClose: false, skin: 'layui-layer-nobg', content: $(template.replace('_TITLE_', title || '公众号').replace('_URL_', href)).html()}), {boxShadow: 'none'});
  694. };
  695. /*! 表单编辑返回操作 */
  696. $body.on('click', '[data-history-back]', function (title) {
  697. title = this.getAttribute('data-history-back') || '确定要返回上一页吗?';
  698. $.msg.confirm(title, function (index) {
  699. history.back(), $.msg.close(index);
  700. })
  701. });
  702. /*! 异步任务状态监听与展示 */
  703. $body.on('click', '[data-queue]', function (action) {
  704. action = this.getAttribute('data-queue') || '';
  705. if (action.length < 1) return $.msg.tips('任务地址不能为空!');
  706. this.doRuntime = function (index) {
  707. $.form.load(action, {}, 'post', function (ret) {
  708. if (typeof ret.data === 'string' && ret.data.indexOf('Q') === 0) {
  709. return $.loadQueue(ret.data, true), false;
  710. }
  711. }), $.msg.close(index);
  712. };
  713. $(this).attr('data-confirm') ? $.msg.confirm($(this).attr('data-confirm'), this.doRuntime) : this.doRuntime(0);
  714. });
  715. $.loadQueue = function (code, doScript, doAjax) {
  716. layer.open({
  717. type: 1, title: false, area: ['560px', '315px'], anim: 2, shadeClose: false, end: function () {
  718. doAjax = false;
  719. }, content: '' +
  720. '<div class="padding-30 padding-bottom-0" style="width:500px" data-queue-load="' + code + '">' +
  721. ' <div class="layui-elip nowrap" data-message-title></div>' +
  722. ' <div class="margin-top-15 layui-progress layui-progress-big" lay-showPercent="yes"><div class="layui-progress-bar transition" lay-percent="0.00%"></div></div>' +
  723. ' <div class="margin-top-15"><textarea class="layui-textarea layui-bg-black border-0" disabled style="resize:none;overflow:hidden;height:190px"></textarea></div>' +
  724. '</div>'
  725. });
  726. (function loadprocess(code, that) {
  727. that = this, this.$box = $('[data-queue-load=' + code + ']');
  728. if (doAjax === false || that.$box.length < 1) return false;
  729. this.$area = that.$box.find('textarea'), this.$title = that.$box.find('[data-message-title]');
  730. this.$percent = that.$box.find('.layui-progress div'), this.runCache = function (code, index, value) {
  731. this.ckey = code + '_' + index, this.ctype = 'admin-queue-script';
  732. return value !== undefined ? layui.data(this.ctype, {key: this.ckey, value: value}) : layui.data(this.ctype)[this.ckey] || 0;
  733. };
  734. this.setState = function (status, message) {
  735. if (message.indexOf('javascript:') === -1) if (status === 1) {
  736. that.$title.html('<b class="color-text">' + message + '</b>').addClass('text-center');
  737. that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
  738. } else if (status === 2) {
  739. if (message.indexOf('>>>') > -1) {
  740. that.$title.html('<b class="color-blue">' + message + '</b>').addClass('text-center');
  741. } else {
  742. that.$title.html('<b class="color-blue">正在处理:</b>' + message).removeClass('text-center');
  743. }
  744. that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
  745. } else if (status === 3) {
  746. that.$title.html('<b class="color-green">' + message + '</b>').addClass('text-center');
  747. that.$percent.addClass('layui-bg-green').removeClass('layui-bg-blue layui-bg-red');
  748. } else if (status === 4) {
  749. that.$title.html('<b class="color-red">' + message + '</b>').addClass('text-center');
  750. that.$percent.addClass('layui-bg-red').removeClass('layui-bg-blue layui-bg-green');
  751. }
  752. };
  753. $.form.load(tapiRoot + '/api.queue/progress', {code: code}, 'post', function (ret) {
  754. if (ret.code) {
  755. that.lines = [];
  756. for (this.lineIndex in ret.data.history) {
  757. this.line = ret.data.history[this.lineIndex], this.percent = '[ ' + this.line.progress + '% ] ';
  758. if (this.line.message.indexOf('javascript:') === -1) {
  759. that.lines.push(this.line.message.indexOf('>>>') > -1 ? this.line.message : this.percent + this.line.message);
  760. } else if (!that.runCache(code, this.lineIndex) && doScript !== false) {
  761. that.runCache(code, this.lineIndex, 1), location.href = this.line.message;
  762. }
  763. }
  764. that.$area.val(that.lines.join("\n")), that.$area.animate({scrollTop: that.$area[0].scrollHeight + 'px'}, 200);
  765. that.$percent.attr('lay-percent', (parseFloat(ret.data.progress || '0.00').toFixed(2)) + '%'), layui.element.render();
  766. if (ret.data.status > 0) that.setState(parseInt(ret.data.status), ret.data.message);
  767. else return that.setState(4, '获取任务详情失败!'), false;
  768. if (parseInt(ret.data.status) === 3 || parseInt(ret.data.status) === 4) return false;
  769. return setTimeout(function () {
  770. loadprocess(code);
  771. }, Math.floor(Math.random() * 200)), false;
  772. }
  773. }, false);
  774. })(code)
  775. };
  776. /*! 图片加载异常处理 */
  777. document.addEventListener('error', function (e, elem) {
  778. elem = e.target;
  779. if (elem.tagName.toLowerCase() === 'img') {
  780. elem.src = baseRoot + 'theme/img/404_icon.png';
  781. }
  782. }, true);
  783. /*! 初始化事件 */
  784. $.menu.listen();
  785. $.vali.listen();
  786. });