ry-ui.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. (function ($) {
  6. $.extend({
  7. _treeTable: {},
  8. _tree: {},
  9. // 表格封装处理
  10. table: {
  11. _option: {},
  12. // 初始化表格参数
  13. init: function(options) {
  14. var defaults = {
  15. id: "bootstrap-table",
  16. type: 0, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  17. height: undefined,
  18. sidePagination: "server",
  19. sortName: "",
  20. sortOrder: "asc",
  21. pagination: true,
  22. pageSize: 10,
  23. toolbar: "toolbar",
  24. striped: false,
  25. escape: false,
  26. showFooter: false,
  27. search: false,
  28. showSearch: true,
  29. showRefresh: true,
  30. showColumns: true,
  31. showToggle: true,
  32. showExport: false,
  33. fixedColumns: false,
  34. fixedNumber: 0,
  35. rightFixedColumns: false,
  36. rightFixedNumber: 0,
  37. queryParams: $.table.queryParams,
  38. };
  39. var options = $.extend(defaults, options);
  40. $.table._option = options;
  41. $('#' + options.id).bootstrapTable({
  42. url: options.url, // 请求后台的URL(*)
  43. contentType: "application/x-www-form-urlencoded", // 编码类型
  44. method: 'post', // 请求方式(*)
  45. cache: false, // 是否使用缓存
  46. height: options.height, // 表格的高度
  47. striped: options.striped, // 是否显示行间隔色
  48. sortable: true, // 是否启用排序
  49. sortStable: true, // 设置为 true 将获得稳定的排序
  50. sortName: options.sortName, // 排序列名称
  51. sortOrder: options.sortOrder, // 排序方式 asc 或者 desc
  52. pagination: options.pagination, // 是否显示分页(*)
  53. pageNumber: 1, // 初始化加载第一页,默认第一页
  54. pageSize: options.pageSize, // 每页的记录行数(*)
  55. pageList: [10, 25, 50], // 可供选择的每页的行数(*)
  56. escape: options.escape, // 转义HTML字符串
  57. showFooter: options.showFooter, // 是否显示表尾
  58. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  59. toolbar: '#' + options.toolbar, // 指定工作栏
  60. sidePagination: options.sidePagination, // server启用服务端分页client客户端分页
  61. search: options.search, // 是否显示搜索框功能
  62. showSearch: options.showSearch, // 是否显示检索信息
  63. showRefresh: options.showRefresh, // 是否显示刷新按钮
  64. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  65. showToggle: options.showToggle, // 是否显示详细视图和列表视图的切换按钮
  66. showExport: options.showExport, // 是否支持导出文件
  67. fixedColumns: options.fixedColumns, // 是否启用冻结列(左侧)
  68. fixedNumber: options.fixedNumber, // 列冻结的个数(左侧)
  69. rightFixedColumns: options.rightFixedColumns, // 是否启用冻结列(右侧)
  70. rightFixedNumber: options.rightFixedNumber, // 列冻结的个数(右侧)
  71. queryParams: options.queryParams, // 传递参数(*)
  72. columns: options.columns, // 显示列信息(*)
  73. responseHandler: $.table.responseHandler, // 在加载服务器发送来的数据之前处理函数
  74. onLoadSuccess: $.table.onLoadSuccess, // 当所有数据被加载时触发处理函数
  75. });
  76. },
  77. // 查询条件
  78. queryParams: function(params) {
  79. return {
  80. // 传递参数查询参数
  81. pageSize: params.limit,
  82. pageNum: params.offset / params.limit + 1,
  83. searchValue: params.search,
  84. orderByColumn: params.sort,
  85. isAsc: params.order
  86. };
  87. },
  88. // 请求获取数据后处理回调函数
  89. responseHandler: function(res) {
  90. if (res.code == 0) {
  91. if ($.common.isNotEmpty($.table._option.sidePagination) && $.table._option.sidePagination == 'client') {
  92. return res.rows;
  93. } else {
  94. return { rows: res.rows, total: res.total };
  95. }
  96. } else {
  97. $.modal.alertWarning(res.msg);
  98. return { rows: [], total: 0 };
  99. }
  100. },
  101. // 当所有数据被加载时触发
  102. onLoadSuccess: function(data) {
  103. // 浮动提示框特效
  104. $("[data-toggle='tooltip']").tooltip();
  105. // 触发行点击事件
  106. $("#" + $.table._option.id).on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table", function () {
  107. var ids = $("#" + $.table._option.id).bootstrapTable("getSelections");
  108. $('#' + $.table._option.toolbar + ' .btn-del').toggleClass('disabled', !ids.length);
  109. $('#' + $.table._option.toolbar + ' .btn-edit').toggleClass('disabled', ids.length!=1);
  110. });
  111. },
  112. // 序列号生成
  113. serialNumber: function (index) {
  114. var table = $('#' + $.table._option.id).bootstrapTable('getOptions');
  115. var pageSize = table.pageSize;
  116. var pageNumber = table.pageNumber;
  117. return pageSize * (pageNumber - 1) + index + 1;
  118. },
  119. // 列超出指定长度浮动提示
  120. tooltip: function (value, length) {
  121. var _length = $.common.isEmpty(length) ? 20 : length;
  122. var _text = "";
  123. var _value = $.common.nullToStr(value);
  124. if (_value.length > _length) {
  125. _text = _value.substr(0, _length) + "...";
  126. } else {
  127. _text = _value;
  128. }
  129. return '<a href="#" class="tooltip-show" data-toggle="tooltip" title="' + _value + '">' + _text +'</a>';
  130. },
  131. // 下拉按钮切换
  132. dropdownToggle: function (value) {
  133. var actions = [];
  134. actions.push('<div class="btn-group">');
  135. actions.push('<button type="button" class="btn btn-xs dropdown-toggle" data-toggle="dropdown" aria-expanded="false">');
  136. actions.push('<i class="fa fa-cog"></i>&nbsp;<span class="fa fa-chevron-down"></span></button>');
  137. actions.push('<ul class="dropdown-menu">');
  138. actions.push(value.replace(/<a/g,"<li><a").replace(/<\/a>/g,"</a></li>"));
  139. actions.push('</ul>');
  140. actions.push('</div>');
  141. return actions.join('');
  142. },
  143. // 搜索-默认第一个form
  144. search: function(formId) {
  145. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  146. var params = $("#" + $.table._option.id).bootstrapTable('getOptions');
  147. params.queryParams = function(params) {
  148. var search = {};
  149. $.each($("#" + currentId).serializeArray(), function(i, field) {
  150. search[field.name] = field.value;
  151. });
  152. search.pageSize = params.limit;
  153. search.pageNum = params.offset / params.limit + 1;
  154. search.searchValue = params.search;
  155. search.orderByColumn = params.sort;
  156. search.isAsc = params.order;
  157. return search;
  158. }
  159. $("#" + $.table._option.id).bootstrapTable('refresh', params);
  160. },
  161. // 导出数据
  162. exportExcel: function(formId) {
  163. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  164. $.modal.loading("正在导出数据,请稍后...");
  165. $.post($.table._option.exportUrl, $("#" + currentId).serializeArray(), function(result) {
  166. if (result.code == web_status.SUCCESS) {
  167. window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + true;
  168. } else {
  169. $.modal.alertError(result.msg);
  170. }
  171. $.modal.closeLoading();
  172. });
  173. },
  174. // 下载模板
  175. importTemplate: function() {
  176. $.get($.table._option.importTemplateUrl, function(result) {
  177. if (result.code == web_status.SUCCESS) {
  178. window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + true;
  179. } else {
  180. $.modal.alertError(result.msg);
  181. }
  182. });
  183. },
  184. // 导入数据
  185. importExcel: function(formId) {
  186. var currentId = $.common.isEmpty(formId) ? 'importForm' : formId;
  187. $.form.reset(currentId);
  188. layer.open({
  189. type: 1,
  190. area: ['400px', '230px'],
  191. fix: false,
  192. //不固定
  193. maxmin: true,
  194. shade: 0.3,
  195. title: '导入' + $.table._option.modalName + '数据',
  196. content: $('#' + currentId),
  197. btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
  198. // 弹层外区域关闭
  199. shadeClose: true,
  200. btn1: function(index, layero){
  201. var file = layero.find('#file').val();
  202. if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
  203. $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
  204. return false;
  205. }
  206. var index = layer.load(2, {shade: false});
  207. $.modal.disable();
  208. var formData = new FormData();
  209. formData.append("file", $('#file')[0].files[0]);
  210. formData.append("updateSupport", $("input[name='updateSupport']").is(':checked'));
  211. $.ajax({
  212. url: $.table._option.importUrl,
  213. data: formData,
  214. cache: false,
  215. contentType: false,
  216. processData: false,
  217. type: 'POST',
  218. success: function (result) {
  219. if (result.code == web_status.SUCCESS) {
  220. $.modal.closeAll();
  221. $.modal.alertSuccess(result.msg);
  222. $.table.refresh();
  223. } else {
  224. layer.close(index);
  225. $.modal.enable();
  226. $.modal.alertError(result.msg);
  227. }
  228. }
  229. });
  230. }
  231. });
  232. },
  233. // 刷新表格
  234. refresh: function() {
  235. $("#" + $.table._option.id).bootstrapTable('refresh', {
  236. silent: true
  237. });
  238. },
  239. // 查询表格指定列值
  240. selectColumns: function(column) {
  241. return $.map($('#' + $.table._option.id).bootstrapTable('getSelections'), function (row) {
  242. return row[column];
  243. });
  244. },
  245. // 查询表格首列值
  246. selectFirstColumns: function() {
  247. return $.map($('#' + $.table._option.id).bootstrapTable('getSelections'), function (row) {
  248. return row[$.table._option.columns[1].field];
  249. });
  250. },
  251. // 回显数据字典
  252. selectDictLabel: function(datas, value) {
  253. var actions = [];
  254. $.each(datas, function(index, dict) {
  255. if (dict.dictValue == value) {
  256. actions.push("<span class='badge badge-" + dict.listClass + "'>" + dict.dictLabel + "</span>");
  257. return false;
  258. }
  259. });
  260. return actions.join('');
  261. },
  262. // 显示表格指定列
  263. showColumn: function(column) {
  264. $("#" + $.table._option.id).bootstrapTable('showColumn', column);
  265. },
  266. // 隐藏表格指定列
  267. hideColumn: function(column) {
  268. $("#" + $.table._option.id).bootstrapTable('hideColumn', column);
  269. }
  270. },
  271. // 表格树封装处理
  272. treeTable: {
  273. // 初始化表格
  274. init: function(options) {
  275. var defaults = {
  276. id: "bootstrap-tree-table",
  277. type: 1, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  278. height: 0,
  279. ajaxParams: {},
  280. toolbar: "toolbar",
  281. striped: false,
  282. expandColumn: 1,
  283. showRefresh: true,
  284. showColumns: true,
  285. expandAll: true,
  286. expandFirst: true
  287. };
  288. var options = $.extend(defaults, options);
  289. $.table._option = options;
  290. var treeTable = $('#' + options.id).bootstrapTreeTable({
  291. code: options.code, // 用于设置父子关系
  292. parentCode: options.parentCode, // 用于设置父子关系
  293. type: 'get', // 请求方式(*)
  294. url: options.url, // 请求后台的URL(*)
  295. ajaxParams: options.ajaxParams, // 请求数据的ajax的data属性
  296. height: options.height, // 表格树的高度
  297. expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
  298. striped: options.striped, // 是否显示行间隔色
  299. bordered: true, // 是否显示边框
  300. toolbar: '#' + options.toolbar, // 指定工作栏
  301. showRefresh: options.showRefresh, // 是否显示刷新按钮
  302. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  303. expandAll: options.expandAll, // 是否全部展开
  304. expandFirst: options.expandFirst, // 是否默认第一级展开--expandAll为false时生效
  305. columns: options.columns
  306. });
  307. $._treeTable = treeTable;
  308. },
  309. // 条件查询
  310. search: function(formId) {
  311. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  312. var params = {};
  313. $.each($("#" + currentId).serializeArray(), function(i, field) {
  314. params[field.name] = field.value;
  315. });
  316. $._treeTable.bootstrapTreeTable('refresh', params);
  317. },
  318. // 刷新
  319. refresh: function() {
  320. $._treeTable.bootstrapTreeTable('refresh');
  321. },
  322. },
  323. // 表单封装处理
  324. form: {
  325. // 表单重置
  326. reset: function(formId) {
  327. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  328. $("#" + currentId)[0].reset();
  329. },
  330. // 获取选中复选框项
  331. selectCheckeds: function(name) {
  332. var checkeds = "";
  333. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  334. if (0 == i) {
  335. checkeds = $(this).val();
  336. } else {
  337. checkeds += ("," + $(this).val());
  338. }
  339. });
  340. return checkeds;
  341. },
  342. // 获取选中下拉框项
  343. selectSelects: function(name) {
  344. var selects = "";
  345. $('#' + name + ' option:selected').each(function (i) {
  346. if (0 == i) {
  347. selects = $(this).val();
  348. } else {
  349. selects += ("," + $(this).val());
  350. }
  351. });
  352. return selects;
  353. }
  354. },
  355. // 弹出层封装处理
  356. modal: {
  357. // 显示图标
  358. icon: function(type) {
  359. var icon = "";
  360. if (type == modal_status.WARNING) {
  361. icon = 0;
  362. } else if (type == modal_status.SUCCESS) {
  363. icon = 1;
  364. } else if (type == modal_status.FAIL) {
  365. icon = 2;
  366. } else {
  367. icon = 3;
  368. }
  369. return icon;
  370. },
  371. // 消息提示
  372. msg: function(content, type) {
  373. if (type != undefined) {
  374. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  375. } else {
  376. layer.msg(content);
  377. }
  378. },
  379. // 错误消息
  380. msgError: function(content) {
  381. $.modal.msg(content, modal_status.FAIL);
  382. },
  383. // 成功消息
  384. msgSuccess: function(content) {
  385. $.modal.msg(content, modal_status.SUCCESS);
  386. },
  387. // 警告消息
  388. msgWarning: function(content) {
  389. $.modal.msg(content, modal_status.WARNING);
  390. },
  391. // 弹出提示
  392. alert: function(content, type) {
  393. layer.alert(content, {
  394. icon: $.modal.icon(type),
  395. title: "系统提示",
  396. btn: ['确认'],
  397. btnclass: ['btn btn-primary'],
  398. });
  399. },
  400. // 消息提示并刷新父窗体
  401. msgReload: function(msg, type) {
  402. layer.msg(msg, {
  403. icon: $.modal.icon(type),
  404. time: 500,
  405. shade: [0.1, '#8F8F8F']
  406. },
  407. function() {
  408. $.modal.reload();
  409. });
  410. },
  411. // 错误提示
  412. alertError: function(content) {
  413. $.modal.alert(content, modal_status.FAIL);
  414. },
  415. // 成功提示
  416. alertSuccess: function(content) {
  417. $.modal.alert(content, modal_status.SUCCESS);
  418. },
  419. // 警告提示
  420. alertWarning: function(content) {
  421. $.modal.alert(content, modal_status.WARNING);
  422. },
  423. // 关闭窗体
  424. close: function () {
  425. var index = parent.layer.getFrameIndex(window.name);
  426. parent.layer.close(index);
  427. },
  428. // 关闭全部窗体
  429. closeAll: function () {
  430. layer.closeAll();
  431. },
  432. // 确认窗体
  433. confirm: function (content, callBack) {
  434. layer.confirm(content, {
  435. icon: 3,
  436. title: "系统提示",
  437. btn: ['确认', '取消'],
  438. btnclass: ['btn btn-primary', 'btn btn-danger'],
  439. }, function (index) {
  440. layer.close(index);
  441. callBack(true);
  442. });
  443. },
  444. // 弹出层指定宽度
  445. open: function (title, url, width, height, callback) {
  446. //如果是移动端,就使用自适应大小弹窗
  447. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  448. width = 'auto';
  449. height = 'auto';
  450. }
  451. if ($.common.isEmpty(title)) {
  452. title = false;
  453. };
  454. if ($.common.isEmpty(url)) {
  455. url = "/404.html";
  456. };
  457. if ($.common.isEmpty(width)) {
  458. width = 800;
  459. };
  460. if ($.common.isEmpty(height)) {
  461. height = ($(window).height() - 50);
  462. };
  463. if ($.common.isEmpty(callback)) {
  464. callback = function(index, layero) {
  465. var iframeWin = layero.find('iframe')[0];
  466. iframeWin.contentWindow.submitHandler();
  467. }
  468. }
  469. layer.open({
  470. type: 2,
  471. area: [width + 'px', height + 'px'],
  472. fix: false,
  473. //不固定
  474. maxmin: true,
  475. shade: 0.3,
  476. title: title,
  477. content: url,
  478. btn: ['确定', '关闭'],
  479. // 弹层外区域关闭
  480. shadeClose: true,
  481. yes: callback,
  482. cancel: function(index) {
  483. return true;
  484. }
  485. });
  486. },
  487. // 弹出层指定参数选项
  488. openOptions: function (options) {
  489. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  490. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  491. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  492. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  493. layer.open({
  494. type: 2,
  495. maxmin: true,
  496. shade: 0.3,
  497. title: _title,
  498. fix: false,
  499. area: [_width + 'px', _height + 'px'],
  500. content: _url,
  501. shadeClose: true,
  502. btn: ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'],
  503. yes: function (index, layero) {
  504. options.callBack(index, layero)
  505. }, cancel: function () {
  506. return true;
  507. }
  508. });
  509. },
  510. // 弹出层全屏
  511. openFull: function (title, url, width, height) {
  512. //如果是移动端,就使用自适应大小弹窗
  513. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  514. width = 'auto';
  515. height = 'auto';
  516. }
  517. if ($.common.isEmpty(title)) {
  518. title = false;
  519. };
  520. if ($.common.isEmpty(url)) {
  521. url = "/404.html";
  522. };
  523. if ($.common.isEmpty(width)) {
  524. width = 800;
  525. };
  526. if ($.common.isEmpty(height)) {
  527. height = ($(window).height() - 50);
  528. };
  529. var index = layer.open({
  530. type: 2,
  531. area: [width + 'px', height + 'px'],
  532. fix: false,
  533. //不固定
  534. maxmin: true,
  535. shade: 0.3,
  536. title: title,
  537. content: url,
  538. btn: ['确定', '关闭'],
  539. // 弹层外区域关闭
  540. shadeClose: true,
  541. yes: function(index, layero) {
  542. var iframeWin = layero.find('iframe')[0];
  543. iframeWin.contentWindow.submitHandler();
  544. },
  545. cancel: function(index) {
  546. return true;
  547. }
  548. });
  549. layer.full(index);
  550. },
  551. // 选卡页方式打开
  552. openTab: function (title, url) {
  553. createMenuItem(url, title);
  554. },
  555. // 禁用按钮
  556. disable: function() {
  557. var doc = window.top == window.parent ? window.document : window.parent.document;
  558. $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
  559. },
  560. // 启用按钮
  561. enable: function() {
  562. var doc = window.top == window.parent ? window.document : window.parent.document;
  563. $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
  564. },
  565. // 打开遮罩层
  566. loading: function (message) {
  567. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  568. },
  569. // 关闭遮罩层
  570. closeLoading: function () {
  571. setTimeout(function(){
  572. $.unblockUI();
  573. }, 50);
  574. },
  575. // 重新加载
  576. reload: function () {
  577. parent.location.reload();
  578. }
  579. },
  580. // 操作封装处理
  581. operate: {
  582. // 提交数据
  583. submit: function(url, type, dataType, data) {
  584. var config = {
  585. url: url,
  586. type: type,
  587. dataType: dataType,
  588. data: data,
  589. beforeSend: function () {
  590. $.modal.loading("正在处理中,请稍后...");
  591. },
  592. success: function(result) {
  593. $.operate.ajaxSuccess(result);
  594. }
  595. };
  596. $.ajax(config)
  597. },
  598. // post请求传输
  599. post: function(url, data) {
  600. $.operate.submit(url, "post", "json", data);
  601. },
  602. // get请求传输
  603. get: function(url) {
  604. $.operate.submit(url, "get", "json", "");
  605. },
  606. // 详细信息
  607. detail: function(id, width, height) {
  608. var _url = $.common.isEmpty(id) ? $.table._option.detailUrl : $.table._option.detailUrl.replace("{id}", id);
  609. var _width = $.common.isEmpty(width) ? "800" : width;
  610. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  611. //如果是移动端,就使用自适应大小弹窗
  612. if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
  613. _width = 'auto';
  614. _height = 'auto';
  615. }
  616. layer.open({
  617. type: 2,
  618. area: [_width + 'px', _height + 'px'],
  619. fix: false,
  620. //不固定
  621. maxmin: true,
  622. shade: 0.3,
  623. title: $.table._option.modalName + "详细",
  624. content: _url,
  625. btn: ['关闭'],
  626. // 弹层外区域关闭
  627. shadeClose: true,
  628. cancel: function(index){
  629. return true;
  630. }
  631. });
  632. },
  633. // 删除信息
  634. remove: function(id) {
  635. $.modal.confirm("确定删除该条" + $.table._option.modalName + "信息吗?", function() {
  636. var url = $.common.isEmpty(id) ? $.table._option.removeUrl : $.table._option.removeUrl.replace("{id}", id);
  637. if($.table._option.type == table_type.bootstrapTreeTable) {
  638. $.operate.get(url);
  639. } else {
  640. var data = { "ids": id };
  641. $.operate.submit(url, "post", "json", data);
  642. }
  643. });
  644. },
  645. // 批量删除信息
  646. removeAll: function() {
  647. var rows = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  648. if (rows.length == 0) {
  649. $.modal.alertWarning("请至少选择一条记录");
  650. return;
  651. }
  652. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  653. var url = $.table._option.removeUrl;
  654. var data = { "ids": rows.join() };
  655. $.operate.submit(url, "post", "json", data);
  656. });
  657. },
  658. // 清空信息
  659. clean: function() {
  660. $.modal.confirm("确定清空所有" + $.table._option.modalName + "吗?", function() {
  661. var url = $.table._option.cleanUrl;
  662. $.operate.submit(url, "post", "json", "");
  663. });
  664. },
  665. // 添加信息
  666. add: function(id) {
  667. $.modal.open("添加" + $.table._option.modalName, $.operate.addUrl(id));
  668. },
  669. // 添加信息,以tab页展现
  670. addTab: function (id) {
  671. $.modal.openTab("添加" + $.table._option.modalName, $.operate.addUrl(id));
  672. },
  673. // 添加信息 全屏
  674. addFull: function(id) {
  675. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  676. $.modal.openFull("添加" + $.table._option.modalName, url);
  677. },
  678. // 添加访问地址
  679. addUrl: function(id) {
  680. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  681. return url;
  682. },
  683. // 修改信息
  684. edit: function(id) {
  685. if($.common.isEmpty(id) && $.table._option.type == table_type.bootstrapTreeTable) {
  686. var row = $('#' + $.table._option.id).bootstrapTreeTable('getSelections')[0];
  687. if ($.common.isEmpty(row)) {
  688. $.modal.alertWarning("请至少选择一条记录");
  689. return;
  690. }
  691. var url = $.table._option.updateUrl.replace("{id}", row[$.table._option.uniqueId]);
  692. $.modal.open("修改" + $.table._option.modalName, url);
  693. } else {
  694. $.modal.open("修改" + $.table._option.modalName, $.operate.editUrl(id));
  695. }
  696. },
  697. // 修改信息,以tab页展现
  698. editTab: function(id) {
  699. $.modal.openTab("修改" + $.table._option.modalName, $.operate.editUrl(id));
  700. },
  701. // 修改信息 全屏
  702. editFull: function(id) {
  703. var url = "/404.html";
  704. if ($.common.isNotEmpty(id)) {
  705. url = $.table._option.updateUrl.replace("{id}", id);
  706. } else {
  707. var row = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  708. url = $.table._option.updateUrl.replace("{id}", row);
  709. }
  710. $.modal.openFull("修改" + $.table._option.modalName, url);
  711. },
  712. // 修改访问地址
  713. editUrl: function(id) {
  714. var url = "/404.html";
  715. if ($.common.isNotEmpty(id)) {
  716. url = $.table._option.updateUrl.replace("{id}", id);
  717. } else {
  718. var id = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
  719. if (id.length == 0) {
  720. $.modal.alertWarning("请至少选择一条记录");
  721. return;
  722. }
  723. url = $.table._option.updateUrl.replace("{id}", id);
  724. }
  725. return url;
  726. },
  727. // 保存信息
  728. save: function(url, data) {
  729. var config = {
  730. url: url,
  731. type: "post",
  732. dataType: "json",
  733. data: data,
  734. beforeSend: function () {
  735. $.modal.loading("正在处理中,请稍后...");
  736. $.modal.disable();
  737. },
  738. success: function(result) {
  739. $.operate.successCallback(result);
  740. }
  741. };
  742. $.ajax(config)
  743. },
  744. // 保存选项卡信息
  745. saveTab: function(url, data) {
  746. var config = {
  747. url: url,
  748. type: "post",
  749. dataType: "json",
  750. data: data,
  751. beforeSend: function () {
  752. $.modal.loading("正在处理中,请稍后...");
  753. },
  754. success: function(result) {
  755. $.operate.successTabCallback(result);
  756. }
  757. };
  758. $.ajax(config)
  759. },
  760. // 保存结果弹出msg刷新table表格
  761. ajaxSuccess: function (result) {
  762. if (result.code == web_status.SUCCESS && $.table._option.type == table_type.bootstrapTable) {
  763. $.modal.msgSuccess(result.msg);
  764. $.table.refresh();
  765. } else if (result.code == web_status.SUCCESS && $.table._option.type == table_type.bootstrapTreeTable) {
  766. $.modal.msgSuccess(result.msg);
  767. $.treeTable.refresh();
  768. } else {
  769. $.modal.alertError(result.msg);
  770. }
  771. $.modal.closeLoading();
  772. },
  773. // 成功结果提示msg(父窗体全局更新)
  774. saveSuccess: function (result) {
  775. if (result.code == web_status.SUCCESS) {
  776. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  777. } else {
  778. $.modal.alertError(result.msg);
  779. }
  780. $.modal.closeLoading();
  781. },
  782. // 成功回调执行事件(父窗体静默更新)
  783. successCallback: function(result) {
  784. if (result.code == web_status.SUCCESS) {
  785. var parent = window.parent;
  786. if (parent.$.table._option.type == table_type.bootstrapTable) {
  787. $.modal.close();
  788. parent.$.modal.msgSuccess(result.msg);
  789. parent.$.table.refresh();
  790. } else if (parent.$.table._option.type == table_type.bootstrapTreeTable) {
  791. $.modal.close();
  792. parent.$.modal.msgSuccess(result.msg);
  793. parent.$.treeTable.refresh();
  794. } else {
  795. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  796. }
  797. } else {
  798. $.modal.alertError(result.msg);
  799. }
  800. $.modal.closeLoading();
  801. $.modal.enable();
  802. },
  803. // 选项卡成功回调执行事件(父窗体静默更新)
  804. successTabCallback: function(result) {
  805. if (result.code == web_status.SUCCESS) {
  806. var topWindow = $(window.parent.document);
  807. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
  808. var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
  809. $.modal.close();
  810. $contentWindow.$.modal.msgSuccess(result.msg);
  811. if ($contentWindow.$.table._option.type == table_type.bootstrapTable) {
  812. $contentWindow.$.table.refresh();
  813. } else if ($contentWindow.$.table._option.type == table_type.bootstrapTreeTable) {
  814. $contentWindow.$.treeTable.refresh();
  815. }
  816. closeItem();
  817. } else {
  818. $.modal.alertError(result.msg);
  819. }
  820. $.modal.closeLoading();
  821. }
  822. },
  823. // 校验封装处理
  824. validate: {
  825. // 判断返回标识是否唯一 false 不存在 true 存在
  826. unique: function (value) {
  827. if (value == "0") {
  828. return true;
  829. }
  830. return false;
  831. },
  832. // 表单验证
  833. form: function (formId) {
  834. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  835. return $("#" + currentId).validate().form();
  836. }
  837. },
  838. // 树插件封装处理
  839. tree: {
  840. _option: {},
  841. _lastValue: {},
  842. // 初始化树结构
  843. init: function(options) {
  844. var defaults = {
  845. id: "tree", // 属性ID
  846. expandLevel: 0, // 展开等级节点
  847. view: {
  848. selectedMulti: false, // 设置是否允许同时选中多个节点
  849. nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
  850. },
  851. check: {
  852. enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
  853. nocheckInherit: true, // 设置子节点是否自动继承
  854. },
  855. data: {
  856. key: {
  857. title: "title" // 节点数据保存节点提示信息的属性名称
  858. },
  859. simpleData: {
  860. enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
  861. }
  862. },
  863. };
  864. var options = $.extend(defaults, options);
  865. $.tree._option = options;
  866. // 树结构初始化加载
  867. var setting = {
  868. callback: {
  869. onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
  870. onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
  871. onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
  872. },
  873. check: options.check,
  874. view: options.view,
  875. data: options.data
  876. };
  877. $.get(options.url, function(data) {
  878. var treeId = $("#treeId").val();
  879. tree = $.fn.zTree.init($("#" + options.id), setting, data);
  880. $._tree = tree;
  881. var nodes = tree.getNodesByParam("level", options.expandLevel - 1);
  882. for (var i = 0; i < nodes.length; i++) {
  883. tree.expandNode(nodes[i], true, false, false);
  884. }
  885. var node = tree.getNodesByParam("id", treeId, null)[0];
  886. $.tree.selectByIdName(treeId, node);
  887. });
  888. },
  889. // 搜索节点
  890. searchNode: function() {
  891. // 取得输入的关键字的值
  892. var value = $.common.trim($("#keyword").val());
  893. if ($.tree._lastValue == value) {
  894. return;
  895. }
  896. // 保存最后一次搜索名称
  897. $.tree._lastValue = value;
  898. var nodes = $._tree.getNodes();
  899. // 如果要查空字串,就退出不查了。
  900. if (value == "") {
  901. $.tree.showAllNode(nodes);
  902. return;
  903. }
  904. $.tree.hideAllNode(nodes);
  905. // 根据搜索值模糊匹配
  906. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  907. },
  908. // 根据Id和Name选中指定节点
  909. selectByIdName: function(treeId, node) {
  910. if ($.common.isNotEmpty(treeId) && treeId == node.id) {
  911. $._tree.selectNode(node, true);
  912. }
  913. },
  914. // 显示所有节点
  915. showAllNode: function(nodes) {
  916. nodes = $._tree.transformToArray(nodes);
  917. for (var i = nodes.length - 1; i >= 0; i--) {
  918. if (nodes[i].getParentNode() != null) {
  919. $._tree.expandNode(nodes[i], true, false, false, false);
  920. } else {
  921. $._tree.expandNode(nodes[i], true, true, false, false);
  922. }
  923. $._tree.showNode(nodes[i]);
  924. $.tree.showAllNode(nodes[i].children);
  925. }
  926. },
  927. // 隐藏所有节点
  928. hideAllNode: function(nodes) {
  929. var tree = $.fn.zTree.getZTreeObj("tree");
  930. var nodes = $._tree.transformToArray(nodes);
  931. for (var i = nodes.length - 1; i >= 0; i--) {
  932. $._tree.hideNode(nodes[i]);
  933. }
  934. },
  935. // 显示所有父节点
  936. showParent: function(treeNode) {
  937. var parentNode;
  938. while ((parentNode = treeNode.getParentNode()) != null) {
  939. $._tree.showNode(parentNode);
  940. $._tree.expandNode(parentNode, true, false, false);
  941. treeNode = parentNode;
  942. }
  943. },
  944. // 显示所有孩子节点
  945. showChildren: function(treeNode) {
  946. if (treeNode.isParent) {
  947. for (var idx in treeNode.children) {
  948. var node = treeNode.children[idx];
  949. $._tree.showNode(node);
  950. $.tree.showChildren(node);
  951. }
  952. }
  953. },
  954. // 更新节点状态
  955. updateNodes: function(nodeList) {
  956. $._tree.showNodes(nodeList);
  957. for (var i = 0, l = nodeList.length; i < l; i++) {
  958. var treeNode = nodeList[i];
  959. $.tree.showChildren(treeNode);
  960. $.tree.showParent(treeNode)
  961. }
  962. },
  963. // 获取当前被勾选集合
  964. getCheckedNodes: function(column) {
  965. var _column = $.common.isEmpty(column) ? "id" : column;
  966. var nodes = $._tree.getCheckedNodes(true);
  967. return $.map(nodes, function (row) {
  968. return row[_column];
  969. }).join();
  970. },
  971. // 不允许根父节点选择
  972. notAllowParents: function(_tree) {
  973. var nodes = _tree.getSelectedNodes();
  974. for (var i = 0; i < nodes.length; i++) {
  975. if (nodes[i].level == 0) {
  976. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  977. return false;
  978. }
  979. if (nodes[i].isParent) {
  980. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  981. return false;
  982. }
  983. }
  984. return true;
  985. },
  986. // 不允许最后层级节点选择
  987. notAllowLastLevel: function(_tree) {
  988. var nodes = _tree.getSelectedNodes();
  989. for (var i = 0; i < nodes.length; i++) {
  990. if (nodes[i].level == nodes.length + 1) {
  991. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  992. return false;
  993. }
  994. }
  995. return true;
  996. },
  997. // 隐藏/显示搜索栏
  998. toggleSearch: function() {
  999. $('#search').slideToggle(200);
  1000. $('#btnShow').toggle();
  1001. $('#btnHide').toggle();
  1002. $('#keyword').focus();
  1003. },
  1004. // 折叠
  1005. collapse: function() {
  1006. $._tree.expandAll(false);
  1007. },
  1008. // 展开
  1009. expand: function() {
  1010. $._tree.expandAll(true);
  1011. }
  1012. },
  1013. // 通用方法封装处理
  1014. common: {
  1015. // 判断字符串是否为空
  1016. isEmpty: function (value) {
  1017. if (value == null || this.trim(value) == "") {
  1018. return true;
  1019. }
  1020. return false;
  1021. },
  1022. // 判断一个字符串是否为非空串
  1023. isNotEmpty: function (value) {
  1024. return !$.common.isEmpty(value);
  1025. },
  1026. // 空对象转字符串
  1027. nullToStr: function(value) {
  1028. if ($.common.isEmpty(value)) {
  1029. return "-";
  1030. }
  1031. return value;
  1032. },
  1033. // 是否显示数据 为空默认为显示
  1034. visible: function (value) {
  1035. if ($.common.isEmpty(value) || value == true) {
  1036. return true;
  1037. }
  1038. return false;
  1039. },
  1040. // 空格截取
  1041. trim: function (value) {
  1042. if (value == null) {
  1043. return "";
  1044. }
  1045. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  1046. },
  1047. // 指定随机数返回
  1048. random: function (min, max) {
  1049. return Math.floor((Math.random() * max) + min);
  1050. },
  1051. startWith: function(value, start) {
  1052. var reg = new RegExp("^" + start);
  1053. return reg.test(value)
  1054. },
  1055. endWith: function(value, end) {
  1056. var reg = new RegExp(end + "$");
  1057. return reg.test(value)
  1058. }
  1059. }
  1060. });
  1061. })(jQuery);
  1062. /** 表格类型 */
  1063. table_type = {
  1064. bootstrapTable: 0,
  1065. bootstrapTreeTable: 1
  1066. };
  1067. /** 消息状态码 */
  1068. web_status = {
  1069. SUCCESS: 0,
  1070. FAIL: 500
  1071. };
  1072. /** 弹窗状态码 */
  1073. modal_status = {
  1074. SUCCESS: "success",
  1075. FAIL: "error",
  1076. WARNING: "warning"
  1077. };