ry-ui.js 46 KB

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