ry-ui.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * 通用方法封装处理
  3. * Copyright (c) 2018 ruoyi
  4. */
  5. (function ($) {
  6. $.extend({
  7. // 表格封装处理
  8. table: {
  9. _option: {},
  10. _params: {},
  11. // 初始化表格
  12. init: function(options) {
  13. $.table._option = options;
  14. $.table._params = $.common.isEmpty(options.queryParams) ? $.table.queryParams : options.queryParams;
  15. _sortOrder = $.common.isEmpty(options.sortOrder) ? "asc" : options.sortOrder;
  16. _sortName = $.common.isEmpty(options.sortName) ? "" : options.sortName;
  17. $('#bootstrap-table').bootstrapTable({
  18. url: options.url, // 请求后台的URL(*)
  19. contentType: "application/x-www-form-urlencoded", // 编码类型
  20. method: 'post', // 请求方式(*)
  21. cache: false, // 是否使用缓存
  22. sortable: true, // 是否启用排序
  23. sortStable: true, // 设置为 true 将获得稳定的排序
  24. sortName: _sortName, // 排序列名称
  25. sortOrder: _sortOrder, // 排序方式 asc 或者 desc
  26. pagination: $.common.visible(options.pagination), // 是否显示分页(*)
  27. pageNumber: 1, // 初始化加载第一页,默认第一页
  28. pageSize: 10, // 每页的记录行数(*)
  29. pageList: [10, 25, 50], // 可供选择的每页的行数(*)
  30. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  31. toolbar: '#toolbar', // 指定工作栏
  32. sidePagination: "server", // 启用服务端分页
  33. search: $.common.visible(options.search), // 是否显示搜索框功能
  34. showRefresh: $.common.visible(options.showRefresh), // 是否显示刷新按钮
  35. showColumns: $.common.visible(options.showColumns), // 是否显示隐藏某列下拉框
  36. showToggle: $.common.visible(options.showToggle), // 是否显示详细视图和列表视图的切换按钮
  37. showExport: $.common.visible(options.showExport), // 是否支持导出文件
  38. queryParams: $.table._params, // 传递参数(*)
  39. columns: options.columns // 显示列信息(*)
  40. });
  41. },
  42. // 查询条件
  43. queryParams: function(params) {
  44. return {
  45. // 传递参数查询参数
  46. pageSize: params.limit,
  47. pageNum: params.offset / params.limit + 1,
  48. searchValue: params.search,
  49. orderByColumn: params.sort,
  50. isAsc: params.order
  51. };
  52. },
  53. // 搜索
  54. search: function(formId) {
  55. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  56. var params = $("#bootstrap-table").bootstrapTable('getOptions');
  57. params.queryParams = function(params) {
  58. var search = {};
  59. $.each($("#" + currentId).serializeArray(), function(i, field) {
  60. search[field.name] = field.value;
  61. });
  62. search.pageSize = params.limit;
  63. search.pageNum = params.offset / params.limit + 1;
  64. search.searchValue = params.search;
  65. search.orderByColumn = params.sort;
  66. search.isAsc = params.order;
  67. return search;
  68. }
  69. $("#bootstrap-table").bootstrapTable('refresh', params);
  70. },
  71. // 下载
  72. exportExcel: function(formId) {
  73. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  74. $.modal.loading("正在导出数据,请稍后...");
  75. $.post($.table._option.exportUrl, $("#" + currentId).serializeArray(), function(result) {
  76. if (result.code == web_status.SUCCESS) {
  77. window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + true;
  78. } else {
  79. $.modal.alertError(result.msg);
  80. }
  81. $.modal.closeLoading();
  82. });
  83. },
  84. // 刷新
  85. refresh: function() {
  86. $("#bootstrap-table").bootstrapTable('refresh', {
  87. url: $.table._option.url,
  88. silent: true
  89. });
  90. },
  91. // 查询选中列值
  92. selectColumns: function(column) {
  93. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  94. return row[column];
  95. });
  96. },
  97. // 查询选中首列值
  98. selectFirstColumns: function() {
  99. return $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
  100. return row[$.table._option.columns[1].field];
  101. });
  102. },
  103. // 回显数据字典
  104. selectDictLabel: function(_datas, _value) {
  105. var actions = [];
  106. $.each(_datas, function(index, dict) {
  107. if (dict.dictValue == _value) {
  108. actions.push("<span class='badge badge-" + dict.listClass + "'>" + dict.dictLabel + "</span>");
  109. return false;
  110. }
  111. });
  112. return actions.join('');
  113. }
  114. },
  115. // 表格树封装处理
  116. treeTable: {
  117. _option: {},
  118. _treeTable: {},
  119. // 初始化表格
  120. init: function(options) {
  121. $.table._option = options;
  122. var treeTable = $('#bootstrap-table').bootstrapTreeTable({
  123. code : options.id, // 用于设置父子关系
  124. parentCode : options.parentId, // 用于设置父子关系
  125. type: 'get', // 请求方式(*)
  126. url: options.url, // 请求后台的URL(*)
  127. ajaxParams : {}, // 请求数据的ajax的data属性
  128. expandColumn : '0', // 在哪一列上面显示展开按钮
  129. striped : false, // 是否各行渐变色
  130. bordered : true, // 是否显示边框
  131. expandAll : $.common.visible(options.expandAll), // 是否全部展开
  132. columns: options.columns
  133. });
  134. $.treeTable._treeTable = treeTable;
  135. },
  136. // 条件查询
  137. search: function(formId) {
  138. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  139. var params = {};
  140. $.each($("#" + currentId).serializeArray(), function(i, field) {
  141. params[field.name] = field.value;
  142. });
  143. $.treeTable._treeTable.bootstrapTreeTable('refresh', params);
  144. },
  145. // 刷新
  146. refresh: function() {
  147. $.treeTable._treeTable.bootstrapTreeTable('refresh');
  148. },
  149. },
  150. // 表单封装处理
  151. form: {
  152. // 获取选中复选框项
  153. selectCheckeds: function(name) {
  154. var checkeds = "";
  155. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  156. if (0 == i) {
  157. checkeds = $(this).val();
  158. } else {
  159. checkeds += ("," + $(this).val());
  160. }
  161. });
  162. return checkeds;
  163. },
  164. // 获取选中下拉框项
  165. selectSelects: function(name) {
  166. var selects = "";
  167. $('#' + name + ' option:selected').each(function (i) {
  168. if (0 == i) {
  169. selects = $(this).val();
  170. } else {
  171. selects += ("," + $(this).val());
  172. }
  173. });
  174. return selects;
  175. }
  176. },
  177. // 弹出层封装处理
  178. modal: {
  179. // 显示图标
  180. icon: function(type) {
  181. var icon = "";
  182. if (type == modal_status.WARNING) {
  183. icon = 0;
  184. } else if (type == modal_status.SUCCESS) {
  185. icon = 1;
  186. } else if (type == modal_status.FAIL) {
  187. icon = 2;
  188. } else {
  189. icon = 3;
  190. }
  191. return icon;
  192. },
  193. // 消息提示
  194. msg: function(content, type) {
  195. if (type != undefined) {
  196. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  197. } else {
  198. layer.msg(content);
  199. }
  200. },
  201. // 错误消息
  202. msgError: function(content) {
  203. $.modal.msg(content, modal_status.FAIL);
  204. },
  205. // 成功消息
  206. msgSuccess: function(content) {
  207. $.modal.msg(content, modal_status.SUCCESS);
  208. },
  209. // 警告消息
  210. msgWarning: function(content) {
  211. $.modal.msg(content, modal_status.WARNING);
  212. },
  213. // 弹出提示
  214. alert: function(content, type) {
  215. layer.alert(content, {
  216. icon: $.modal.icon(type),
  217. title: "系统提示",
  218. btn: ['确认'],
  219. btnclass: ['btn btn-primary'],
  220. });
  221. },
  222. // 消息提示并刷新父窗体
  223. msgReload: function(msg, type) {
  224. layer.msg(msg, {
  225. icon: $.modal.icon(type),
  226. time: 500,
  227. shade: [0.1, '#8F8F8F']
  228. },
  229. function() {
  230. $.modal.reload();
  231. });
  232. },
  233. // 错误提示
  234. alertError: function(content) {
  235. $.modal.alert(content, modal_status.FAIL);
  236. },
  237. // 成功提示
  238. alertSuccess: function(content) {
  239. $.modal.alert(content, modal_status.SUCCESS);
  240. },
  241. // 警告提示
  242. alertWarning: function(content) {
  243. $.modal.alert(content, modal_status.WARNING);
  244. },
  245. // 关闭窗体
  246. close: function () {
  247. var index = parent.layer.getFrameIndex(window.name);
  248. parent.layer.close(index);
  249. },
  250. // 确认窗体
  251. confirm: function (content, callBack) {
  252. layer.confirm(content, {
  253. icon: 3,
  254. title: "系统提示",
  255. btn: ['确认', '取消'],
  256. btnclass: ['btn btn-primary', 'btn btn-danger'],
  257. }, function (index) {
  258. layer.close(index);
  259. callBack(true);
  260. });
  261. },
  262. // 弹出层指定宽度
  263. open: function (title, url, width, height) {
  264. if ($.common.isEmpty(title)) {
  265. title = false;
  266. };
  267. if ($.common.isEmpty(url)) {
  268. url = "404.html";
  269. };
  270. if ($.common.isEmpty(width)) {
  271. width = 800;
  272. };
  273. if ($.common.isEmpty(height)) {
  274. height = ($(window).height() - 50);
  275. };
  276. layer.open({
  277. type: 2,
  278. area: [width + 'px', height + 'px'],
  279. fix: false,
  280. //不固定
  281. maxmin: true,
  282. shade: 0.3,
  283. title: title,
  284. content: url
  285. });
  286. },
  287. // 弹出层全屏
  288. openFull: function (title, url, width, height) {
  289. if ($.common.isEmpty(title)) {
  290. title = false;
  291. };
  292. if ($.common.isEmpty(url)) {
  293. url = "404.html";
  294. };
  295. if ($.common.isEmpty(width)) {
  296. width = 800;
  297. };
  298. if ($.common.isEmpty(height)) {
  299. height = ($(window).height() - 50);
  300. };
  301. var index = layer.open({
  302. type: 2,
  303. area: [width + 'px', height + 'px'],
  304. fix: false,
  305. //不固定
  306. maxmin: true,
  307. shade: 0.3,
  308. title: title,
  309. content: url
  310. });
  311. layer.full(index);
  312. },
  313. // 打开遮罩层
  314. loading: function (message) {
  315. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  316. },
  317. // 关闭遮罩层
  318. closeLoading: function () {
  319. setTimeout(function(){
  320. $.unblockUI();
  321. }, 50);
  322. },
  323. // 重新加载
  324. reload: function () {
  325. parent.location.reload();
  326. }
  327. },
  328. // 操作封装处理
  329. operate: {
  330. // 提交数据
  331. submit: function(url, type, dataType, data) {
  332. $.modal.loading("正在处理中,请稍后...");
  333. var config = {
  334. url: url,
  335. type: type,
  336. dataType: dataType,
  337. data: data,
  338. success: function(result) {
  339. $.operate.ajaxSuccess(result);
  340. }
  341. };
  342. $.ajax(config)
  343. },
  344. // post请求传输
  345. post: function(url, data) {
  346. $.operate.submit(url, "post", "json", data);
  347. },
  348. // 删除信息
  349. remove: function(id) {
  350. $.modal.confirm("确定删除该条" + $.table._option.modalName + "信息吗?", function() {
  351. var url = $.common.isEmpty(id) ? $.table._option.removeUrl : $.table._option.removeUrl.replace("{id}", id);
  352. var data = { "ids": id };
  353. $.operate.submit(url, "post", "json", data);
  354. });
  355. },
  356. // 批量删除信息
  357. batRemove: function() {
  358. var rows = $.common.isEmpty($.table._option.id) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.id);
  359. if (rows.length == 0) {
  360. $.modal.alertWarning("请至少选择一条记录");
  361. return;
  362. }
  363. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  364. var url = $.table._option.removeUrl;
  365. var data = { "ids": rows.join() };
  366. $.operate.submit(url, "post", "json", data);
  367. });
  368. },
  369. // 添加信息
  370. add: function(id) {
  371. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  372. $.modal.open("添加" + $.table._option.modalName, url);
  373. },
  374. // 修改信息
  375. edit: function(id) {
  376. var url = $.table._option.updateUrl.replace("{id}", id);
  377. $.modal.open("修改" + $.table._option.modalName, url);
  378. },
  379. // 添加信息 全屏
  380. addFull: function(id) {
  381. var url = $.common.isEmpty(id) ? $.table._option.createUrl : $.table._option.createUrl.replace("{id}", id);
  382. $.modal.openFull("添加" + $.table._option.modalName, url);
  383. },
  384. // 修改信息 全屏
  385. editFull: function(id) {
  386. var url = $.table._option.updateUrl.replace("{id}", id);
  387. $.modal.openFull("修改" + $.table._option.modalName, url);
  388. },
  389. // 保存信息
  390. save: function(url, data) {
  391. $.modal.loading("正在处理中,请稍后...");
  392. var config = {
  393. url: url,
  394. type: "post",
  395. dataType: "json",
  396. data: data,
  397. success: function(result) {
  398. $.operate.saveSuccess(result);
  399. }
  400. };
  401. $.ajax(config)
  402. },
  403. // 保存结果弹出msg刷新table表格
  404. ajaxSuccess: function (result) {
  405. if (result.code == web_status.SUCCESS) {
  406. $.modal.msgSuccess(result.msg);
  407. $.table.refresh();
  408. } else {
  409. $.modal.alertError(result.msg);
  410. }
  411. $.modal.closeLoading();
  412. },
  413. // 保存结果提示msg
  414. saveSuccess: function (result) {
  415. if (result.code == web_status.SUCCESS) {
  416. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  417. } else {
  418. $.modal.alertError(result.msg);
  419. }
  420. $.modal.closeLoading();
  421. }
  422. },
  423. // 通用方法封装处理
  424. common: {
  425. // 判断字符串是否为空
  426. isEmpty: function (value) {
  427. if (value == null || this.trim(value) == "") {
  428. return true;
  429. }
  430. return false;
  431. },
  432. // 是否显示数据 为空默认为显示
  433. visible: function (value) {
  434. if ($.common.isEmpty(value) || value == true) {
  435. return true;
  436. }
  437. return false;
  438. },
  439. // 空格截取
  440. trim: function (value) {
  441. if (value == null) {
  442. return "";
  443. }
  444. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  445. },
  446. // 指定随机数返回
  447. random: function (min, max) {
  448. return Math.floor((Math.random() * max) + min);
  449. }
  450. }
  451. });
  452. })(jQuery);
  453. /** 消息状态码 */
  454. web_status = {
  455. SUCCESS: 0,
  456. FAIL: 500
  457. };
  458. /** 弹窗状态码 */
  459. modal_status = {
  460. SUCCESS: "success",
  461. FAIL: "error",
  462. WARNING: "warning"
  463. };