ry-ui.js 20 KB

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