ry-ui.js 46 KB

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