控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //封装基本操作 部分参考EasyAdmin和fastadmin
  2. layui.define(['layer','table', 'notice', 'addons'], function(exports) {
  3. var layer = layui.layer,
  4. $ = layui.$,
  5. table = layui.table,
  6. notice = layui.notice;
  7. var MOD_NAME = 'yzn';
  8. yzn = {
  9. config: {
  10. shade: [0.02, '#000'],
  11. },
  12. checkAuth: function (node,ele) {
  13. /*if (CONFIG.IS_SUPER_ADMIN) {
  14. return true;
  15. }*/
  16. var attr = $(ele).data("auth-" + node);
  17. if (typeof attr === 'undefined' || attr) {
  18. return true;
  19. }
  20. if(node.indexOf('?')>=0) node = node.replace(/([?#])[^'"]*/, ''); //去除参数
  21. if ($(ele).attr('data-auth-' + node.toLowerCase()) === '1') {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. },
  27. open: function(title, url, width, height, options, isResize) {
  28. isResize = isResize === undefined ? true : isResize;
  29. var area = yzn.config.openArea != undefined ? yzn.config.openArea : [$(window).width() > width ? width + 'px' : '95%', $(window).height() > height ? height + 'px' : '95%'];
  30. options = $.extend({
  31. title: title,
  32. type: 2,
  33. area: area,
  34. content: url + (url.indexOf("?") > -1 ? "&" : "?") + "dialog=1",
  35. maxmin: true,
  36. moveOut: true,
  37. success: function(layero, index) {
  38. var that = this;
  39. $(layero).data("callback", that.callback);
  40. layer.setTop(layero);
  41. try {
  42. var frame = layer.getChildFrame('body', index);
  43. var layerfooter = frame.find(".layer-footer");
  44. yzn.layerfooter(layero, index, that);
  45. //绑定事件
  46. if (layerfooter.size() > 0) {
  47. // 监听窗口内的元素及属性变化
  48. // Firefox和Chrome早期版本中带有前缀
  49. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  50. if (MutationObserver) {
  51. // 选择目标节点
  52. var target = layerfooter[0];
  53. // 创建观察者对象
  54. var observer = new MutationObserver(function(mutations) {
  55. yzn.layerfooter(layero, index, that);
  56. mutations.forEach(function(mutation) {});
  57. });
  58. // 配置观察选项:
  59. var config = { attributes: true, childList: true, characterData: true, subtree: true }
  60. // 传入目标节点和观察选项
  61. observer.observe(target, config);
  62. // 随后,你还可以停止观察
  63. // observer.disconnect();
  64. }
  65. }
  66. } catch (e) {
  67. }
  68. if (frame && frame.length > 0) {
  69. $.each(frame, function(i, v) {
  70. // todo 优化弹出层背景色修改
  71. $(v).before('<style>\n' +
  72. 'html, body {\n' +
  73. ' background: #ffffff !important;\n' +
  74. '}\n' +
  75. '</style>');
  76. });
  77. }
  78. if (yzn.checkMobile() || width === undefined || height === undefined) {
  79. layer.full(index);
  80. }
  81. if (isResize) {
  82. $(window).on("resize", function() {
  83. //layer.full(index);
  84. layer.style(index, {
  85. top: 0,
  86. height: $(window).height()
  87. })
  88. })
  89. }
  90. }
  91. }, options ? options : {})
  92. return layer.open(options);
  93. },
  94. //关闭窗口并回传数据
  95. close: function(data) {
  96. var index = parent.layer.getFrameIndex(window.name);
  97. var callback = parent.$("#layui-layer" + index).data("callback");
  98. //再执行关闭
  99. parent.layer.close(index);
  100. //再调用回传函数
  101. if (typeof callback === 'function') {
  102. callback.call(undefined, data);
  103. }
  104. },
  105. layerfooter: function(layero, index, that) {
  106. var frame = layer.getChildFrame('html', index);
  107. var layerfooter = frame.find(".layer-footer");
  108. //表单按钮
  109. if (layerfooter.size() > 0) {
  110. $(".layui-layer-footer", layero).remove();
  111. var footer = $("<div />").addClass('layui-layer-btn layui-layer-footer');
  112. footer.html(layerfooter.html());
  113. footer.insertAfter(layero.find('.layui-layer-content'));
  114. //绑定事件
  115. footer.on("click", ".layui-btn", function() {
  116. if ($(this).hasClass("disabled") || $(this).parent().hasClass("disabled")) {
  117. return;
  118. }
  119. var index = footer.find('.layui-btn').index(this);
  120. $(".layui-btn:eq(" + index + ")", layerfooter).trigger("click");
  121. });
  122. var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
  123. var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
  124. //重设iframe高度
  125. $("iframe", layero).height(layero.height() - titHeight - btnHeight);
  126. }
  127. },
  128. checkMobile: function() {
  129. var userAgentInfo = navigator.userAgent;
  130. var mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  131. var mobile_flag = false;
  132. //根据userAgent判断是否是手机
  133. for (var v = 0; v < mobileAgents.length; v++) {
  134. if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
  135. mobile_flag = true;
  136. break;
  137. }
  138. }
  139. var screen_width = window.screen.width;
  140. var screen_height = window.screen.height;
  141. //根据屏幕分辨率判断是否是手机
  142. if (screen_width < 600 && screen_height < 800) {
  143. mobile_flag = true;
  144. }
  145. return mobile_flag;
  146. },
  147. formatDateTime: function(timeStamp) {
  148. var date = new Date();
  149. date.setTime(timeStamp * 1000);
  150. var y = date.getFullYear();
  151. var m = date.getMonth() + 1;
  152. m = m < 10 ? ('0' + m) : m;
  153. var d = date.getDate();
  154. d = d < 10 ? ('0' + d) : d;
  155. var h = date.getHours();
  156. h = h < 10 ? ('0' + h) : h;
  157. var minute = date.getMinutes();
  158. var second = date.getSeconds();
  159. minute = minute < 10 ? ('0' + minute) : minute;
  160. second = second < 10 ? ('0' + second) : second;
  161. return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  162. },
  163. parame: function(param, defaultParam) {
  164. return param !== undefined ? param : defaultParam;
  165. },
  166. events: {
  167. //请求成功的回调
  168. onAjaxSuccess: function(ret, onAjaxSuccess) {
  169. var data = typeof ret.data !== 'undefined' ? ret.data : null;
  170. var msg = typeof ret.msg !== 'undefined' && ret.msg ? ret.msg : '操作成功!';
  171. if (typeof onAjaxSuccess === 'function') {
  172. var result = onAjaxSuccess.call(this, data, ret);
  173. if (result === false)
  174. return;
  175. }
  176. },
  177. //请求错误的回调
  178. onAjaxError: function(ret, onAjaxError) {
  179. var data = typeof ret.data !== 'undefined' ? ret.data : null;
  180. if (typeof onAjaxError === 'function') {
  181. var result = onAjaxError.call(this, data, ret);
  182. if (result === false) {
  183. return;
  184. }
  185. }
  186. },
  187. //服务器响应数据后
  188. onAjaxResponse: function(response) {
  189. try {
  190. var ret = typeof response === 'object' ? response : JSON.parse(response);
  191. if (!ret.hasOwnProperty('code')) {
  192. $.extend(ret, { code: -2, msg: response, data: null });
  193. }
  194. } catch (e) {
  195. var ret = { code: -1, msg: e.message, data: null };
  196. }
  197. return ret;
  198. }
  199. },
  200. request: {
  201. //修复URL
  202. fixurl: function(url) {
  203. if (url.substr(0, 1) !== "/") {
  204. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  205. if (!r.test(url)) {
  206. url = "/" + url;
  207. }
  208. }
  209. return url;
  210. },
  211. post: function(options, success, error) {
  212. return yzn.request.ajax('post', options, success, error);
  213. },
  214. get: function(options, success, error) {
  215. return yzn.request.ajax('get', options, success, error);
  216. },
  217. ajax: function(type, options, success, error) {
  218. options = typeof options === 'string' ? { url: options } : options;
  219. var index;
  220. if (typeof options.loading === 'undefined' || options.loading) {
  221. index = layer.load(options.loading || 0);
  222. }
  223. options = $.extend({
  224. type: type || 'get',
  225. dataType: "json",
  226. url: options.url || '',
  227. data: options.data || {},
  228. xhrFields: {
  229. withCredentials: true
  230. },
  231. complete: function(xhr, textStatus) {
  232. var token = xhr.getResponseHeader('__token__');
  233. if (token) {
  234. $("input[name='__token__']").val(token);
  235. }
  236. },
  237. success: function(ret) {
  238. index && layer.close(index);
  239. //刷新客户端token
  240. if (ret && typeof ret === 'object' && typeof ret.token !== 'undefined') {
  241. $("input[name='__token__']").val(data.token);
  242. }
  243. ret = yzn.events.onAjaxResponse(ret);
  244. if (ret.code === 1) {
  245. yzn.events.onAjaxSuccess(ret, success);
  246. } else {
  247. yzn.events.onAjaxError(ret, error);
  248. }
  249. },
  250. error: function(xhr) {
  251. index && layer.close(index);
  252. var ret = { code: xhr.status, msg: xhr.statusText, data: null };
  253. yzn.events.onAjaxError(ret, error);
  254. }
  255. }, options);
  256. return $.ajax(options);
  257. }
  258. },
  259. notice: {
  260. // 成功消息
  261. success: function(msg) {
  262. var index = notice.success({ message: msg });
  263. return index;
  264. },
  265. // 失败消息
  266. error: function(msg) {
  267. var index = notice.error({ message: msg });
  268. return index;
  269. },
  270. // 警告消息框
  271. warning: function(msg) {
  272. var index = notice.warning({ message: msg });
  273. return index;
  274. },
  275. // 消息提示
  276. info: function(msg) {
  277. var index = notice.info({ message: msg });
  278. return index;
  279. },
  280. },
  281. msg: {
  282. // 成功消息
  283. success: function(msg, callback) {
  284. if (callback === undefined) {
  285. callback = function() {}
  286. }
  287. var index = layer.msg(msg, { icon: 1, shade: yzn.config.shade, scrollbar: false, time: 2000, shadeClose: true }, callback);
  288. return index;
  289. },
  290. // 失败消息
  291. error: function(msg, callback) {
  292. if (callback === undefined) {
  293. callback = function() {}
  294. }
  295. var index = layer.msg(msg, { icon: 2, shade: yzn.config.shade, scrollbar: false, time: 3000, shadeClose: true }, callback);
  296. return index;
  297. },
  298. // 警告消息框
  299. alert: function(msg, callback) {
  300. var index = layer.alert(msg, { end: callback, scrollbar: false });
  301. return index;
  302. },
  303. // 对话框
  304. confirm: function(msg, ok, no) {
  305. var index = layer.confirm(msg, { title: '操作确认', btn: ['确认', '取消'] }, function() {
  306. typeof ok === 'function' && ok.call(this);
  307. }, function() {
  308. typeof no === 'function' && no.call(this);
  309. self.close(index);
  310. });
  311. return index;
  312. },
  313. // 消息提示
  314. tips: function(msg, time, callback) {
  315. var index = layer.msg(msg, { time: (time || 3) * 1000, shade: this.shade, end: callback, shadeClose: true });
  316. return index;
  317. },
  318. // 加载中提示
  319. loading: function(msg, callback) {
  320. 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 });
  321. return index;
  322. },
  323. // 关闭消息框
  324. close: function(index) {
  325. return layer.close(index);
  326. }
  327. },
  328. cache: {
  329. setStorage: function(key, value) {
  330. if (value != null && value !== "undefined") {
  331. layui.data(key, {
  332. key: key,
  333. value: value
  334. })
  335. } else {
  336. layui.data(key, {
  337. key: key,
  338. remove: true
  339. })
  340. }
  341. },
  342. getStorage: function(key) {
  343. var array = layui.data(key);
  344. if (array) {
  345. return array[key]
  346. } else {
  347. return false
  348. }
  349. }
  350. },
  351. init: function() {
  352. //修复含有fixed-footer类的body边距
  353. if ($(".fixed-footer").size() > 0) {
  354. $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
  355. }
  356. //修复不在iframe时layer-footer隐藏的问题
  357. if ($(".layer-footer").size() > 0 && self === top) {
  358. $(".layer-footer").show();
  359. }
  360. var tips_index = 0;
  361. $(document).on('mouseenter', '[lay-tips]', function () {
  362. tips_index = layer.tips($(this).attr('lay-tips'), this, {
  363. tips: 1,
  364. time: 0
  365. });
  366. }).on('mouseleave', '[lay-tips]', function(){
  367. layer.close(tips_index);
  368. });
  369. // 对相对地址进行处理
  370. $.ajaxSetup({
  371. beforeSend: function(xhr, setting) {
  372. setting.url = yzn.request.fixurl(setting.url);
  373. }
  374. });
  375. layer.config({
  376. skin: 'layui-layer-yzn'
  377. });
  378. notice.settings({
  379. timeout: 3000, //消失时间
  380. theme: 'dark', // 主题 dark light
  381. position: 'topRight', // 位置 bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center
  382. displayMode: 0, //0无限制 1同类型存在不显示 2同类型存在先移除
  383. progressBar: true, //进度条
  384. });
  385. // 放大图片
  386. $('body').on('click', '[data-image]', function() {
  387. var title = $(this).attr('data-image'),
  388. src = $(this).attr('src'),
  389. alt = $(this).attr('alt');
  390. var photos = {
  391. "title": title,
  392. "id": Math.random(),
  393. "data": [{
  394. "alt": alt,
  395. "pid": Math.random(),
  396. "src": src,
  397. "thumb": src
  398. }]
  399. };
  400. layer.photos({
  401. photos: photos,
  402. anim: 5
  403. });
  404. return false;
  405. });
  406. // 绑定ESC关闭窗口事件
  407. $(window).keyup(function (e) {
  408. if (e.keyCode == 27) {
  409. if ($(".layui-layer").length > 0) {
  410. var index = 0;
  411. $(".layui-layer").each(function () {
  412. index = Math.max(index, parseInt($(this).attr("times")));
  413. });
  414. if (index) {
  415. layer.close(index);
  416. }
  417. }
  418. }
  419. });
  420. //点击包含.btn-addtabs的元素时新增选项卡
  421. $(document).on('click', '.btn-addtabs,.addtabsit', function (e) {
  422. var that = this;
  423. var options = $.extend({}, $(that).data() || {});
  424. var id = $(that).data("menu-id") || Math.round(Math.random() * (99999-10000) + 10000);
  425. var url = $(that).data("url") || $(that).attr('href');
  426. var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
  427. if (!$(this).attr('data-menu-id')) {
  428. $(this).attr('data-menu-id', id);
  429. }
  430. if(parent.layui.admin){
  431. parent.layui.admin.addTab(id, title,url);
  432. }else if(window.top.layui.admin){
  433. window.top.layui.admin.addTab(id, title,url);
  434. }
  435. })
  436. // 监听弹出层的打开
  437. $(document).on('click', '[data-open]', function() {
  438. var clienWidth = $(this).attr('data-width') || 800,
  439. clientHeight = $(this).attr('data-height') || 600,
  440. dataFull = $(this).attr('data-full'),
  441. checkbox = $(this).attr('data-checkbox'),
  442. url = $(this).attr('data-open'),
  443. title = $(this).attr("title") || $(this).data("title"),
  444. tableId = $(this).attr('data-table');
  445. if (checkbox === 'true') {
  446. tableId = tableId || init.table_render_id;
  447. var checkStatus = table.checkStatus(tableId),
  448. data = checkStatus.data;
  449. if (data.length <= 0) {
  450. yzn.msg.error('请勾选需要操作的数据');
  451. return false;
  452. }
  453. var ids = [];
  454. $.each(data, function(i, v) {
  455. ids.push(v.id);
  456. });
  457. if (url.indexOf("?") === -1) {
  458. url += '?id=' + ids.join(',');
  459. } else {
  460. url += '&id=' + ids.join(',');
  461. }
  462. }
  463. if (dataFull === 'true') {
  464. clienWidth = '100%';
  465. clientHeight = '100%';
  466. }
  467. yzn.open(title, url, clienWidth, clientHeight);
  468. });
  469. }
  470. }
  471. yzn.init();
  472. exports(MOD_NAME, yzn);
  473. });