ry-ui.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. // 当前table相关信息
  6. var table = {
  7. config: {},
  8. // 当前实例配置
  9. options: {},
  10. // 设置实例配置
  11. set: function(id) {
  12. if($.common.getLength(table.config) > 1) {
  13. var tableId = $.common.isEmpty(id) ? $(event.currentTarget).parents(".bootstrap-table").find("table.table").attr("id") : id;
  14. if ($.common.isNotEmpty(tableId)) {
  15. table.options = table.get(tableId);
  16. }
  17. }
  18. },
  19. // 获取实例配置
  20. get: function(id) {
  21. return table.config[id];
  22. },
  23. // 记住选择实例组
  24. rememberSelecteds: {},
  25. // 记住选择ID组
  26. rememberSelectedIds: {}
  27. };
  28. (function ($) {
  29. $.extend({
  30. _tree: {},
  31. bttTable: {},
  32. // 表格封装处理
  33. table: {
  34. // 初始化表格参数
  35. init: function(options) {
  36. var defaults = {
  37. id: "bootstrap-table",
  38. type: 0, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  39. method: 'post',
  40. height: undefined,
  41. sidePagination: "server",
  42. sortName: "",
  43. sortOrder: "asc",
  44. pagination: true,
  45. paginationLoop: false,
  46. pageSize: 10,
  47. pageNumber: 1,
  48. pageList: [10, 25, 50],
  49. toolbar: "toolbar",
  50. loadingFontSize: 13,
  51. striped: false,
  52. escape: false,
  53. firstLoad: true,
  54. showFooter: false,
  55. search: false,
  56. showSearch: true,
  57. showPageGo: false,
  58. showRefresh: true,
  59. showColumns: true,
  60. showToggle: true,
  61. showExport: false,
  62. clickToSelect: false,
  63. singleSelect: false,
  64. mobileResponsive: true,
  65. maintainSelected: false,
  66. rememberSelected: false,
  67. fixedColumns: false,
  68. fixedNumber: 0,
  69. fixedRightNumber: 0,
  70. queryParams: $.table.queryParams,
  71. rowStyle: {},
  72. };
  73. var options = $.extend(defaults, options);
  74. table.options = options;
  75. table.config[options.id] = options;
  76. $.table.initEvent();
  77. $('#' + options.id).bootstrapTable({
  78. id: options.id,
  79. url: options.url, // 请求后台的URL(*)
  80. contentType: "application/x-www-form-urlencoded", // 编码类型
  81. method: options.method, // 请求方式(*)
  82. cache: false, // 是否使用缓存
  83. height: options.height, // 表格的高度
  84. striped: options.striped, // 是否显示行间隔色
  85. sortable: true, // 是否启用排序
  86. sortStable: true, // 设置为 true 将获得稳定的排序
  87. sortName: options.sortName, // 排序列名称
  88. sortOrder: options.sortOrder, // 排序方式 asc 或者 desc
  89. pagination: options.pagination, // 是否显示分页(*)
  90. paginationLoop: options.paginationLoop, // 是否启用分页条无限循环的功能
  91. pageNumber: 1, // 初始化加载第一页,默认第一页
  92. pageSize: options.pageSize, // 每页的记录行数(*)
  93. pageList: options.pageList, // 可供选择的每页的行数(*)
  94. firstLoad: options.firstLoad, // 是否首次请求加载数据,对于数据较大可以配置false
  95. escape: options.escape, // 转义HTML字符串
  96. showFooter: options.showFooter, // 是否显示表尾
  97. iconSize: 'outline', // 图标大小:undefined默认的按钮尺寸 xs超小按钮sm小按钮lg大按钮
  98. toolbar: '#' + options.toolbar, // 指定工作栏
  99. loadingFontSize: options.loadingFontSize, // 自定义加载文本的字体大小
  100. sidePagination: options.sidePagination, // server启用服务端分页client客户端分页
  101. search: options.search, // 是否显示搜索框功能
  102. searchText: options.searchText, // 搜索框初始显示的内容,默认为空
  103. showSearch: options.showSearch, // 是否显示检索信息
  104. showPageGo: options.showPageGo, // 是否显示跳转页
  105. showRefresh: options.showRefresh, // 是否显示刷新按钮
  106. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  107. showToggle: options.showToggle, // 是否显示详细视图和列表视图的切换按钮
  108. showExport: options.showExport, // 是否支持导出文件
  109. showHeader: options.showHeader, // 是否显示表头
  110. showFullscreen: options.showFullscreen, // 是否显示全屏按钮
  111. uniqueId: options.uniqueId, // 唯一的标识符
  112. clickToSelect: options.clickToSelect, // 是否启用点击选中行
  113. singleSelect: options.singleSelect, // 是否单选checkbox
  114. mobileResponsive: options.mobileResponsive, // 是否支持移动端适配
  115. cardView: options.cardView, // 是否启用显示卡片视图
  116. detailView: options.detailView, // 是否启用显示细节视图
  117. onCheck: options.onCheck, // 当选择此行时触发
  118. onUncheck: options.onUncheck, // 当取消此行时触发
  119. onCheckAll: options.onCheckAll, // 当全选行时触发
  120. onUncheckAll: options.onUncheckAll, // 当取消全选行时触发
  121. onClickRow: options.onClickRow, // 点击某行触发的事件
  122. onDblClickRow: options.onDblClickRow, // 双击某行触发的事件
  123. onClickCell: options.onClickCell, // 单击某格触发的事件
  124. onDblClickCell: options.onDblClickCell, // 双击某格触发的事件
  125. onEditableSave: options.onEditableSave, // 行内编辑保存的事件
  126. onExpandRow: options.onExpandRow, // 点击详细视图的事件
  127. maintainSelected: options.maintainSelected, // 前端翻页时保留所选行
  128. rememberSelected: options.rememberSelected, // 启用翻页记住前面的选择
  129. fixedColumns: options.fixedColumns, // 是否启用冻结列(左侧)
  130. fixedNumber: options.fixedNumber, // 列冻结的个数(左侧)
  131. fixedRightNumber: options.fixedRightNumber, // 列冻结的个数(右侧)
  132. onReorderRow: options.onReorderRow, // 当拖拽结束后处理函数
  133. queryParams: options.queryParams, // 传递参数(*)
  134. rowStyle: options.rowStyle, // 通过自定义函数设置行样式
  135. footerStyle: options.footerStyle, // 通过自定义函数设置页脚样式
  136. headerStyle: options.headerStyle, // 通过自定义函数设置标题样式
  137. columns: options.columns, // 显示列信息(*)
  138. data: options.data, // 被加载的数据
  139. responseHandler: $.table.responseHandler, // 在加载服务器发送来的数据之前处理函数
  140. onLoadSuccess: $.table.onLoadSuccess, // 当所有数据被加载时触发处理函数
  141. exportOptions: options.exportOptions, // 前端导出忽略列索引
  142. detailFormatter: options.detailFormatter, // 在行下面展示其他数据列表
  143. });
  144. },
  145. // 获取实例ID,如存在多个返回#id1,#id2 delimeter分隔符
  146. getOptionsIds: function(separator) {
  147. var _separator = $.common.isEmpty(separator) ? "," : separator;
  148. var optionsIds = "";
  149. $.each(table.config, function(key, value){
  150. optionsIds += "#" + key + _separator;
  151. });
  152. return optionsIds.substring(0, optionsIds.length - 1);
  153. },
  154. // 查询条件
  155. queryParams: function(params) {
  156. var curParams = {
  157. // 传递参数查询参数
  158. pageSize: params.limit,
  159. pageNum: params.offset / params.limit + 1,
  160. searchValue: params.search,
  161. orderByColumn: params.sort,
  162. isAsc: params.order
  163. };
  164. var currentId = $.common.isEmpty(table.options.formId) ? $('form').attr('id') : table.options.formId;
  165. return $.extend(curParams, $.common.formToJSON(currentId));
  166. },
  167. // 请求获取数据后处理回调函数
  168. responseHandler: function(res) {
  169. if (typeof table.get(this.id).responseHandler == "function") {
  170. table.get(this.id).responseHandler(res);
  171. }
  172. if (res.code == 0) {
  173. if ($.common.isNotEmpty(table.options.sidePagination) && table.options.sidePagination == 'client') {
  174. return res.rows;
  175. } else {
  176. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  177. var column = $.common.isEmpty(table.options.uniqueId) ? table.options.columns[1].field : table.options.uniqueId;
  178. $.each(res.rows, function(i, row) {
  179. row.state = $.inArray(row[column], table.rememberSelectedIds[table.options.id]) !== -1;
  180. })
  181. }
  182. return { rows: res.rows, total: res.total };
  183. }
  184. } else {
  185. $.modal.alertWarning(res.msg);
  186. return { rows: [], total: 0 };
  187. }
  188. },
  189. // 初始化事件
  190. initEvent: function() {
  191. // 实例ID信息
  192. var optionsIds = $.table.getOptionsIds();
  193. // 监听事件处理
  194. $(optionsIds).on(TABLE_EVENTS, function () {
  195. table.set($(this).attr("id"));
  196. });
  197. // 在表格体渲染完成,并在 DOM 中可见后触发(事件)
  198. $(optionsIds).on("post-body.bs.table", function (e, args) {
  199. // 浮动提示框特效
  200. $(".table [data-toggle='tooltip']").tooltip();
  201. });
  202. // 选中、取消、全部选中、全部取消(事件)
  203. $(optionsIds).on("check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table", function (e, rowsAfter, rowsBefore) {
  204. // 复选框分页保留保存选中数组
  205. var rows = $.common.equals("uncheck-all", e.type) ? rowsBefore : rowsAfter;
  206. var rowIds = $.table.affectedRowIds(rows);
  207. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  208. func = $.inArray(e.type, ['check', 'check-all']) > -1 ? 'union' : 'difference';
  209. var selectedIds = table.rememberSelectedIds[table.options.id];
  210. if($.common.isNotEmpty(selectedIds)) {
  211. table.rememberSelectedIds[table.options.id] = _[func](selectedIds, rowIds);
  212. } else {
  213. table.rememberSelectedIds[table.options.id] = _[func]([], rowIds);
  214. }
  215. var selectedRows = table.rememberSelecteds[table.options.id];
  216. if($.common.isNotEmpty(selectedRows)) {
  217. table.rememberSelecteds[table.options.id] = _[func](selectedRows, rows);
  218. } else {
  219. table.rememberSelecteds[table.options.id] = _[func]([], rows);
  220. }
  221. }
  222. });
  223. // 加载成功、选中、取消、全部选中、全部取消(事件)
  224. $(optionsIds).on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
  225. var toolbar = table.options.toolbar;
  226. var uniqueId = table.options.uniqueId;
  227. // 工具栏按钮控制
  228. var rows = $.common.isEmpty(uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(uniqueId);
  229. // 非多个禁用
  230. $('#' + toolbar + ' .multiple').toggleClass('disabled', !rows.length);
  231. // 非单个禁用
  232. $('#' + toolbar + ' .single').toggleClass('disabled', rows.length!=1);
  233. });
  234. // 图片预览事件
  235. $(optionsIds).off("click").on("click", '.img-circle', function() {
  236. var src = $(this).attr('src');
  237. var target = $(this).data('target');
  238. if($.common.equals("self", target)) {
  239. var height = $(this).data('height');
  240. var width = $(this).data('width');
  241. // 如果是移动端,就使用自适应大小弹窗
  242. if ($.common.isMobile()) {
  243. width = 'auto';
  244. height = 'auto';
  245. }
  246. layer.open({
  247. title: false,
  248. type: 1,
  249. closeBtn: true,
  250. shadeClose: true,
  251. area: ['auto', 'auto'],
  252. content: "<img src='" + src + "' height='" + height + "' width='" + width + "'/>"
  253. });
  254. } else if ($.common.equals("blank", target)) {
  255. window.open(src);
  256. }
  257. });
  258. // 单击tooltip事件
  259. $(optionsIds).on("click", '.tooltip-show', function() {
  260. var target = $(this).data('target');
  261. var input = $(this).prev();
  262. if ($.common.equals("copy", target)) {
  263. input.select();
  264. document.execCommand("copy");
  265. } else if ($.common.equals("open", target)) {
  266. parent.layer.alert(input.val(), {
  267. title: "信息内容",
  268. shadeClose: true,
  269. btn: ['确认'],
  270. btnclass: ['btn btn-primary'],
  271. });
  272. }
  273. });
  274. },
  275. // 当所有数据被加载时触发
  276. onLoadSuccess: function(data) {
  277. if (typeof table.options.onLoadSuccess == "function") {
  278. table.options.onLoadSuccess(data);
  279. }
  280. },
  281. // 表格销毁
  282. destroy: function (tableId) {
  283. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  284. $("#" + currentId).bootstrapTable('destroy');
  285. },
  286. // 序列号生成
  287. serialNumber: function (index, tableId) {
  288. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  289. var tableParams = $("#" + currentId).bootstrapTable('getOptions');
  290. var pageSize = $.common.isNotEmpty(tableParams.pageSize) ? tableParams.pageSize: table.options.pageSize;
  291. var pageNumber = $.common.isNotEmpty(tableParams.pageNumber) ? tableParams.pageNumber: table.options.pageNumber;
  292. return pageSize * (pageNumber - 1) + index + 1;
  293. },
  294. // 列超出指定长度浮动提示 target(copy单击复制文本 open弹窗打开文本)
  295. tooltip: function (value, length, target) {
  296. var _length = $.common.isEmpty(length) ? 20 : length;
  297. var _text = "";
  298. var _value = $.common.nullToStr(value);
  299. var _target = $.common.isEmpty(target) ? 'copy' : target;
  300. if (_value.length > _length) {
  301. _text = _value.substr(0, _length) + "...";
  302. _value = _value.replace(/\'/g,"&apos;");
  303. _value = _value.replace(/\"/g,"&quot;");
  304. var actions = [];
  305. actions.push($.common.sprintf('<input style="opacity: 0;position: absolute;width:5px;z-index:-1" type="text" value="%s"/>', _value));
  306. actions.push($.common.sprintf('<a href="###" class="tooltip-show" data-toggle="tooltip" data-target="%s" title="%s">%s</a>', _target, _value, _text));
  307. return actions.join('');
  308. } else {
  309. _text = _value;
  310. return _text;
  311. }
  312. },
  313. // 下拉按钮切换
  314. dropdownToggle: function (value) {
  315. var actions = [];
  316. actions.push('<div class="btn-group">');
  317. actions.push('<button type="button" class="btn btn-xs dropdown-toggle" data-toggle="dropdown" aria-expanded="false">');
  318. actions.push('<i class="fa fa-cog"></i>&nbsp;<span class="fa fa-chevron-down"></span></button>');
  319. actions.push('<ul class="dropdown-menu">');
  320. actions.push(value.replace(/<a/g,"<li><a").replace(/<\/a>/g,"</a></li>"));
  321. actions.push('</ul>');
  322. actions.push('</div>');
  323. return actions.join('');
  324. },
  325. // 图片预览
  326. imageView: function (value, height, width, target) {
  327. if ($.common.isEmpty(width)) {
  328. width = 'auto';
  329. }
  330. if ($.common.isEmpty(height)) {
  331. height = 'auto';
  332. }
  333. // blank or self
  334. var _target = $.common.isEmpty(target) ? 'self' : target;
  335. if ($.common.isNotEmpty(value)) {
  336. return $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s' src='%s'/>", height, width, _target, value);
  337. } else {
  338. return $.common.nullToStr(value);
  339. }
  340. },
  341. // 搜索-默认第一个form
  342. search: function(formId, tableId) {
  343. table.set(tableId);
  344. table.options.formId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  345. var params = $.common.isEmpty(tableId) ? $("#" + table.options.id).bootstrapTable('getOptions') : $("#" + tableId).bootstrapTable('getOptions');
  346. if($.common.isNotEmpty(tableId)){
  347. $("#" + tableId).bootstrapTable('refresh', params);
  348. } else{
  349. $("#" + table.options.id).bootstrapTable('refresh', params);
  350. }
  351. },
  352. // 导出数据
  353. exportExcel: function(formId) {
  354. table.set();
  355. $.modal.confirm("确定导出所有" + table.options.modalName + "吗?", function() {
  356. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  357. var params = $("#" + table.options.id).bootstrapTable('getOptions');
  358. var dataParam = $("#" + currentId).serializeArray();
  359. dataParam.push({ "name": "orderByColumn", "value": params.sortName });
  360. dataParam.push({ "name": "isAsc", "value": params.sortOrder });
  361. $.modal.loading("正在导出数据,请稍后...");
  362. $.post(table.options.exportUrl, dataParam, function(result) {
  363. if (result.code == web_status.SUCCESS) {
  364. window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
  365. } else if (result.code == web_status.WARNING) {
  366. $.modal.alertWarning(result.msg)
  367. } else {
  368. $.modal.alertError(result.msg);
  369. }
  370. $.modal.closeLoading();
  371. });
  372. });
  373. },
  374. // 下载模板
  375. importTemplate: function() {
  376. table.set();
  377. $.get(table.options.importTemplateUrl, function(result) {
  378. if (result.code == web_status.SUCCESS) {
  379. window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
  380. } else if (result.code == web_status.WARNING) {
  381. $.modal.alertWarning(result.msg)
  382. } else {
  383. $.modal.alertError(result.msg);
  384. }
  385. });
  386. },
  387. // 导入数据
  388. importExcel: function(formId, width, height) {
  389. table.set();
  390. var currentId = $.common.isEmpty(formId) ? 'importTpl' : formId;
  391. var _width = $.common.isEmpty(width) ? "400" : width;
  392. var _height = $.common.isEmpty(height) ? "230" : height;
  393. layer.open({
  394. type: 1,
  395. area: [_width + 'px', _height + 'px'],
  396. fix: false,
  397. //不固定
  398. maxmin: true,
  399. shade: 0.3,
  400. title: '导入' + table.options.modalName + '数据',
  401. content: $('#' + currentId).html(),
  402. btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
  403. // 弹层外区域关闭
  404. shadeClose: true,
  405. btn1: function(index, layero){
  406. var file = layero.find('#file').val();
  407. if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
  408. $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
  409. return false;
  410. }
  411. var index = layer.load(2, {shade: false});
  412. $.modal.disable();
  413. var formData = new FormData(layero.find('form')[0]);
  414. $.ajax({
  415. url: table.options.importUrl,
  416. data: formData,
  417. cache: false,
  418. contentType: false,
  419. processData: false,
  420. type: 'POST',
  421. success: function (result) {
  422. if (result.code == web_status.SUCCESS) {
  423. $.modal.closeAll();
  424. $.modal.alertSuccess(result.msg);
  425. $.table.refresh();
  426. } else if (result.code == web_status.WARNING) {
  427. layer.close(index);
  428. $.modal.enable();
  429. $.modal.alertWarning(result.msg)
  430. } else {
  431. layer.close(index);
  432. $.modal.enable();
  433. $.modal.alertError(result.msg);
  434. }
  435. }
  436. });
  437. }
  438. });
  439. },
  440. // 刷新表格
  441. refresh: function(tableId) {
  442. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  443. $("#" + currentId).bootstrapTable('refresh', {
  444. silent: true
  445. });
  446. },
  447. // 查询表格指定列值
  448. selectColumns: function(column) {
  449. var rows = $.map($("#" + table.options.id).bootstrapTable('getSelections'), function (row) {
  450. return $.common.getItemField(row, column);
  451. });
  452. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  453. var selectedRows = table.rememberSelecteds[table.options.id];
  454. if($.common.isNotEmpty(selectedRows)) {
  455. rows = $.map(table.rememberSelecteds[table.options.id], function (row) {
  456. return $.common.getItemField(row, column);
  457. });
  458. }
  459. }
  460. return $.common.uniqueFn(rows);
  461. },
  462. // 获取当前页选中或者取消的行ID
  463. affectedRowIds: function(rows) {
  464. var column = $.common.isEmpty(table.options.uniqueId) ? table.options.columns[1].field : table.options.uniqueId;
  465. var rowIds;
  466. if ($.isArray(rows)) {
  467. rowIds = $.map(rows, function(row) {
  468. return $.common.getItemField(row, column);
  469. });
  470. } else {
  471. rowIds = [rows[column]];
  472. }
  473. return rowIds;
  474. },
  475. // 查询表格首列值
  476. selectFirstColumns: function() {
  477. var rows = $.map($("#" + table.options.id).bootstrapTable('getSelections'), function (row) {
  478. return $.common.getItemField(row, table.options.columns[1].field);
  479. });
  480. if ($.common.isNotEmpty(table.options.rememberSelected) && table.options.rememberSelected) {
  481. var selectedRows = table.rememberSelecteds[table.options.id];
  482. if($.common.isNotEmpty(selectedRows)) {
  483. rows = $.map(selectedRows, function (row) {
  484. return $.common.getItemField(row, table.options.columns[1].field);
  485. });
  486. }
  487. }
  488. return $.common.uniqueFn(rows);
  489. },
  490. // 回显数据字典
  491. selectDictLabel: function(datas, value) {
  492. var actions = [];
  493. $.each(datas, function(index, dict) {
  494. if (dict.dictValue == ('' + value)) {
  495. var listClass = $.common.equals("default", dict.listClass) || $.common.isEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
  496. actions.push($.common.sprintf("<span class='%s'>%s</span>", listClass, dict.dictLabel));
  497. return false;
  498. }
  499. });
  500. return actions.join('');
  501. },
  502. // 回显数据字典(字符串数组)
  503. selectDictLabels: function(datas, value, separator) {
  504. if ($.common.isEmpty(value)) {
  505. return '';
  506. }
  507. var currentSeparator = $.common.isEmpty(separator) ? "," : separator;
  508. var actions = [];
  509. $.each(value.split(currentSeparator), function(i, val) {
  510. $.each(datas, function(index, dict) {
  511. if (dict.dictValue == ('' + val)) {
  512. var listClass = $.common.equals("default", dict.listClass) || $.common.isEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
  513. actions.push($.common.sprintf("<span class='%s'>%s </span>", listClass, dict.dictLabel));
  514. return false;
  515. }
  516. });
  517. });
  518. return actions.join('');
  519. },
  520. // 显示表格指定列
  521. showColumn: function(column, tableId) {
  522. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  523. $("#" + currentId).bootstrapTable('showColumn', column);
  524. },
  525. // 隐藏表格指定列
  526. hideColumn: function(column, tableId) {
  527. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  528. $("#" + currentId).bootstrapTable('hideColumn', column);
  529. },
  530. // 显示所有表格列
  531. showAllColumns: function(tableId) {
  532. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  533. $("#" + currentId).bootstrapTable('showAllColumns');
  534. },
  535. // 隐藏所有表格列
  536. hideAllColumns: function(tableId) {
  537. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  538. $("#" + currentId).bootstrapTable('hideAllColumns');
  539. }
  540. },
  541. // 表格树封装处理
  542. treeTable: {
  543. // 初始化表格
  544. init: function(options) {
  545. var defaults = {
  546. id: "bootstrap-tree-table",
  547. type: 1, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  548. height: 0,
  549. rootIdValue: null,
  550. ajaxParams: {},
  551. toolbar: "toolbar",
  552. striped: false,
  553. expandColumn: 1,
  554. showSearch: true,
  555. showRefresh: true,
  556. showColumns: true,
  557. expandAll: true,
  558. expandFirst: true
  559. };
  560. var options = $.extend(defaults, options);
  561. table.options = options;
  562. table.config[options.id] = options;
  563. $.table.initEvent();
  564. $.bttTable = $('#' + options.id).bootstrapTreeTable({
  565. code: options.code, // 用于设置父子关系
  566. parentCode: options.parentCode, // 用于设置父子关系
  567. type: 'post', // 请求方式(*)
  568. url: options.url, // 请求后台的URL(*)
  569. data: options.data, // 无url时用于渲染的数据
  570. ajaxParams: options.ajaxParams, // 请求数据的ajax的data属性
  571. rootIdValue: options.rootIdValue, // 设置指定根节点id值
  572. height: options.height, // 表格树的高度
  573. expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
  574. striped: options.striped, // 是否显示行间隔色
  575. bordered: false, // 是否显示边框
  576. toolbar: '#' + options.toolbar, // 指定工作栏
  577. showSearch: options.showSearch, // 是否显示检索信息
  578. showRefresh: options.showRefresh, // 是否显示刷新按钮
  579. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  580. expandAll: options.expandAll, // 是否全部展开
  581. expandFirst: options.expandFirst, // 是否默认第一级展开--expandAll为false时生效
  582. columns: options.columns, // 显示列信息(*)
  583. responseHandler: $.treeTable.responseHandler, // 在加载服务器发送来的数据之前处理函数
  584. onLoadSuccess: $.table.onLoadSuccess // 当所有数据被加载时触发处理函数
  585. });
  586. },
  587. // 条件查询
  588. search: function(formId) {
  589. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  590. var params = $.common.formToJSON(currentId);
  591. $.bttTable.bootstrapTreeTable('refresh', params);
  592. },
  593. // 刷新
  594. refresh: function() {
  595. $.bttTable.bootstrapTreeTable('refresh');
  596. },
  597. // 查询表格树指定列值
  598. selectColumns: function(column) {
  599. var rows = $.map($.bttTable.bootstrapTreeTable('getSelections'), function (row) {
  600. return $.common.getItemField(row, column);
  601. });
  602. return $.common.uniqueFn(rows);
  603. },
  604. // 请求获取数据后处理回调函数,校验异常状态提醒
  605. responseHandler: function(res) {
  606. if (typeof table.options.responseHandler == "function") {
  607. table.options.responseHandler(res);
  608. }
  609. if (res.code != undefined && res.code != 0) {
  610. $.modal.alertWarning(res.msg);
  611. return [];
  612. } else {
  613. return res;
  614. }
  615. },
  616. },
  617. // 表单封装处理
  618. form: {
  619. // 表单重置
  620. reset: function(formId, tableId) {
  621. table.set(tableId);
  622. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  623. $("#" + currentId)[0].reset();
  624. if (table.options.type == table_type.bootstrapTable) {
  625. if($.common.isEmpty(tableId)){
  626. $("#" + table.options.id).bootstrapTable('refresh');
  627. } else{
  628. $("#" + tableId).bootstrapTable('refresh');
  629. }
  630. } else if (table.options.type == table_type.bootstrapTreeTable) {
  631. if($.common.isEmpty(tableId)){
  632. $("#" + table.options.id).bootstrapTreeTable('refresh', []);
  633. } else{
  634. $("#" + tableId).bootstrapTreeTable('refresh', []);
  635. }
  636. }
  637. },
  638. // 获取选中复选框项
  639. selectCheckeds: function(name) {
  640. var checkeds = "";
  641. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  642. if (0 == i) {
  643. checkeds = $(this).val();
  644. } else {
  645. checkeds += ("," + $(this).val());
  646. }
  647. });
  648. return checkeds;
  649. },
  650. // 获取选中下拉框项
  651. selectSelects: function(name) {
  652. var selects = "";
  653. $('#' + name + ' option:selected').each(function (i) {
  654. if (0 == i) {
  655. selects = $(this).val();
  656. } else {
  657. selects += ("," + $(this).val());
  658. }
  659. });
  660. return selects;
  661. }
  662. },
  663. // 弹出层封装处理
  664. modal: {
  665. // 显示图标
  666. icon: function(type) {
  667. var icon = "";
  668. if (type == modal_status.WARNING) {
  669. icon = 0;
  670. } else if (type == modal_status.SUCCESS) {
  671. icon = 1;
  672. } else if (type == modal_status.FAIL) {
  673. icon = 2;
  674. } else {
  675. icon = 3;
  676. }
  677. return icon;
  678. },
  679. // 消息提示
  680. msg: function(content, type) {
  681. if (type != undefined) {
  682. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  683. } else {
  684. layer.msg(content);
  685. }
  686. },
  687. // 错误消息
  688. msgError: function(content) {
  689. $.modal.msg(content, modal_status.FAIL);
  690. },
  691. // 成功消息
  692. msgSuccess: function(content) {
  693. $.modal.msg(content, modal_status.SUCCESS);
  694. },
  695. // 警告消息
  696. msgWarning: function(content) {
  697. $.modal.msg(content, modal_status.WARNING);
  698. },
  699. // 弹出提示
  700. alert: function(content, type) {
  701. layer.alert(content, {
  702. icon: $.modal.icon(type),
  703. title: "系统提示",
  704. btn: ['确认'],
  705. btnclass: ['btn btn-primary'],
  706. });
  707. },
  708. // 消息提示并刷新父窗体
  709. msgReload: function(msg, type) {
  710. layer.msg(msg, {
  711. icon: $.modal.icon(type),
  712. time: 500,
  713. shade: [0.1, '#8F8F8F']
  714. },
  715. function() {
  716. $.modal.reload();
  717. });
  718. },
  719. // 错误提示
  720. alertError: function(content) {
  721. $.modal.alert(content, modal_status.FAIL);
  722. },
  723. // 成功提示
  724. alertSuccess: function(content) {
  725. $.modal.alert(content, modal_status.SUCCESS);
  726. },
  727. // 警告提示
  728. alertWarning: function(content) {
  729. $.modal.alert(content, modal_status.WARNING);
  730. },
  731. // 关闭窗体
  732. close: function (index) {
  733. if($.common.isEmpty(index)){
  734. var index = parent.layer.getFrameIndex(window.name);
  735. parent.layer.close(index);
  736. } else {
  737. layer.close(index);
  738. }
  739. },
  740. // 关闭全部窗体
  741. closeAll: function () {
  742. layer.closeAll();
  743. },
  744. // 确认窗体
  745. confirm: function (content, callBack) {
  746. layer.confirm(content, {
  747. icon: 3,
  748. title: "系统提示",
  749. btn: ['确认', '取消']
  750. }, function (index) {
  751. layer.close(index);
  752. callBack(true);
  753. });
  754. },
  755. // 弹出层指定宽度
  756. open: function (title, url, width, height, callback) {
  757. //如果是移动端,就使用自适应大小弹窗
  758. if ($.common.isMobile()) {
  759. width = 'auto';
  760. height = 'auto';
  761. }
  762. if ($.common.isEmpty(title)) {
  763. title = false;
  764. }
  765. if ($.common.isEmpty(url)) {
  766. url = "/404.html";
  767. }
  768. if ($.common.isEmpty(width)) {
  769. width = 800;
  770. }
  771. if ($.common.isEmpty(height)) {
  772. height = ($(window).height() - 50);
  773. }
  774. if ($.common.isEmpty(callback)) {
  775. callback = function(index, layero) {
  776. var iframeWin = layero.find('iframe')[0];
  777. iframeWin.contentWindow.submitHandler(index, layero);
  778. }
  779. }
  780. layer.open({
  781. type: 2,
  782. area: [width + 'px', height + 'px'],
  783. fix: false,
  784. //不固定
  785. maxmin: true,
  786. shade: 0.3,
  787. title: title,
  788. content: url,
  789. btn: ['确定', '关闭'],
  790. // 弹层外区域关闭
  791. shadeClose: true,
  792. yes: callback,
  793. cancel: function(index) {
  794. return true;
  795. }
  796. });
  797. },
  798. // 弹出层指定参数选项
  799. openOptions: function (options) {
  800. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  801. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  802. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  803. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  804. var _btn = ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'];
  805. if ($.common.isEmpty(options.yes)) {
  806. options.yes = function(index, layero) {
  807. options.callBack(index, layero);
  808. }
  809. }
  810. var btnCallback = {};
  811. if(options.btn instanceof Array){
  812. for (var i = 1, len = options.btn.length; i < len; i++) {
  813. var btn = options["btn" + (i + 1)];
  814. if (btn) {
  815. btnCallback["btn" + (i + 1)] = btn;
  816. }
  817. }
  818. }
  819. var index = layer.open($.extend({
  820. type: 2,
  821. maxmin: $.common.isEmpty(options.maxmin) ? true : options.maxmin,
  822. shade: 0.3,
  823. title: _title,
  824. fix: false,
  825. area: [_width + 'px', _height + 'px'],
  826. content: _url,
  827. shadeClose: $.common.isEmpty(options.shadeClose) ? true : options.shadeClose,
  828. skin: options.skin,
  829. btn: $.common.isEmpty(options.btn) ? _btn : options.btn,
  830. yes: options.yes,
  831. cancel: function () {
  832. return true;
  833. }
  834. }, btnCallback));
  835. if ($.common.isNotEmpty(options.full) && options.full === true) {
  836. layer.full(index);
  837. }
  838. },
  839. // 弹出层全屏
  840. openFull: function (title, url, width, height) {
  841. //如果是移动端,就使用自适应大小弹窗
  842. if ($.common.isMobile()) {
  843. width = 'auto';
  844. height = 'auto';
  845. }
  846. if ($.common.isEmpty(title)) {
  847. title = false;
  848. }
  849. if ($.common.isEmpty(url)) {
  850. url = "/404.html";
  851. }
  852. if ($.common.isEmpty(width)) {
  853. width = 800;
  854. }
  855. if ($.common.isEmpty(height)) {
  856. height = ($(window).height() - 50);
  857. }
  858. var index = layer.open({
  859. type: 2,
  860. area: [width + 'px', height + 'px'],
  861. fix: false,
  862. //不固定
  863. maxmin: true,
  864. shade: 0.3,
  865. title: title,
  866. content: url,
  867. btn: ['确定', '关闭'],
  868. // 弹层外区域关闭
  869. shadeClose: true,
  870. yes: function(index, layero) {
  871. var iframeWin = layero.find('iframe')[0];
  872. iframeWin.contentWindow.submitHandler(index, layero);
  873. },
  874. cancel: function(index) {
  875. return true;
  876. }
  877. });
  878. layer.full(index);
  879. },
  880. // 选卡页方式打开
  881. openTab: function (title, url) {
  882. createMenuItem(url, title);
  883. },
  884. // 选卡页同一页签打开
  885. parentTab: function (title, url) {
  886. var dataId = window.frameElement.getAttribute('data-id');
  887. createMenuItem(url, title);
  888. closeItem(dataId);
  889. },
  890. // 关闭选项卡
  891. closeTab: function (dataId) {
  892. closeItem(dataId);
  893. },
  894. // 禁用按钮
  895. disable: function() {
  896. var doc = window.top == window.parent ? window.document : window.parent.document;
  897. $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
  898. },
  899. // 启用按钮
  900. enable: function() {
  901. var doc = window.top == window.parent ? window.document : window.parent.document;
  902. $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
  903. },
  904. // 打开遮罩层
  905. loading: function (message) {
  906. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  907. },
  908. // 关闭遮罩层
  909. closeLoading: function () {
  910. setTimeout(function(){
  911. $.unblockUI();
  912. }, 50);
  913. },
  914. // 重新加载
  915. reload: function () {
  916. parent.location.reload();
  917. }
  918. },
  919. // 操作封装处理
  920. operate: {
  921. // 提交数据
  922. submit: function(url, type, dataType, data, callback) {
  923. var config = {
  924. url: url,
  925. type: type,
  926. dataType: dataType,
  927. data: data,
  928. beforeSend: function () {
  929. $.modal.loading("正在处理中,请稍后...");
  930. },
  931. success: function(result) {
  932. if (typeof callback == "function") {
  933. callback(result);
  934. }
  935. $.operate.ajaxSuccess(result);
  936. }
  937. };
  938. $.ajax(config)
  939. },
  940. // post请求传输
  941. post: function(url, data, callback) {
  942. $.operate.submit(url, "post", "json", data, callback);
  943. },
  944. // get请求传输
  945. get: function(url, callback) {
  946. $.operate.submit(url, "get", "json", "", callback);
  947. },
  948. // 详细信息
  949. detail: function(id, width, height) {
  950. table.set();
  951. var _url = $.operate.detailUrl(id);
  952. var _width = $.common.isEmpty(width) ? "800" : width;
  953. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  954. //如果是移动端,就使用自适应大小弹窗
  955. if ($.common.isMobile()) {
  956. _width = 'auto';
  957. _height = 'auto';
  958. }
  959. var options = {
  960. title: table.options.modalName + "详细",
  961. width: _width,
  962. height: _height,
  963. url: _url,
  964. skin: 'layui-layer-gray',
  965. btn: ['关闭'],
  966. yes: function (index, layero) {
  967. layer.close(index);
  968. }
  969. };
  970. $.modal.openOptions(options);
  971. },
  972. // 详细访问地址
  973. detailUrl: function(id) {
  974. var url = "/404.html";
  975. if ($.common.isNotEmpty(id)) {
  976. url = table.options.detailUrl.replace("{id}", id);
  977. } else {
  978. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  979. if (id.length == 0) {
  980. $.modal.alertWarning("请至少选择一条记录");
  981. return;
  982. }
  983. url = table.options.detailUrl.replace("{id}", id);
  984. }
  985. return url;
  986. },
  987. // 删除信息
  988. remove: function(id) {
  989. table.set();
  990. $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗?", function() {
  991. var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
  992. if(table.options.type == table_type.bootstrapTreeTable) {
  993. $.operate.get(url);
  994. } else {
  995. var data = { "ids": id };
  996. $.operate.submit(url, "post", "json", data);
  997. }
  998. });
  999. },
  1000. // 批量删除信息
  1001. removeAll: function() {
  1002. table.set();
  1003. var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1004. if (rows.length == 0) {
  1005. $.modal.alertWarning("请至少选择一条记录");
  1006. return;
  1007. }
  1008. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  1009. var url = table.options.removeUrl;
  1010. var data = { "ids": rows.join() };
  1011. $.operate.submit(url, "post", "json", data);
  1012. });
  1013. },
  1014. // 清空信息
  1015. clean: function() {
  1016. table.set();
  1017. $.modal.confirm("确定清空所有" + table.options.modalName + "吗?", function() {
  1018. var url = table.options.cleanUrl;
  1019. $.operate.submit(url, "post", "json", "");
  1020. });
  1021. },
  1022. // 添加信息
  1023. add: function(id) {
  1024. table.set();
  1025. $.modal.open("添加" + table.options.modalName, $.operate.addUrl(id));
  1026. },
  1027. // 添加信息,以tab页展现
  1028. addTab: function (id) {
  1029. table.set();
  1030. $.modal.openTab("添加" + table.options.modalName, $.operate.addUrl(id));
  1031. },
  1032. // 添加信息 全屏
  1033. addFull: function(id) {
  1034. table.set();
  1035. var url = $.common.isEmpty(id) ? table.options.createUrl : table.options.createUrl.replace("{id}", id);
  1036. $.modal.openFull("添加" + table.options.modalName, url);
  1037. },
  1038. // 添加访问地址
  1039. addUrl: function(id) {
  1040. var url = $.common.isEmpty(id) ? table.options.createUrl.replace("{id}", "") : table.options.createUrl.replace("{id}", id);
  1041. return url;
  1042. },
  1043. // 修改信息
  1044. edit: function(id) {
  1045. table.set();
  1046. if($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
  1047. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1048. if ($.common.isEmpty(row)) {
  1049. $.modal.alertWarning("请至少选择一条记录");
  1050. return;
  1051. }
  1052. var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1053. $.modal.open("修改" + table.options.modalName, url);
  1054. } else {
  1055. $.modal.open("修改" + table.options.modalName, $.operate.editUrl(id));
  1056. }
  1057. },
  1058. // 修改信息,以tab页展现
  1059. editTab: function(id) {
  1060. table.set();
  1061. $.modal.openTab("修改" + table.options.modalName, $.operate.editUrl(id));
  1062. },
  1063. // 修改信息 全屏
  1064. editFull: function(id) {
  1065. table.set();
  1066. var url = "/404.html";
  1067. if ($.common.isNotEmpty(id)) {
  1068. url = table.options.updateUrl.replace("{id}", id);
  1069. } else {
  1070. if(table.options.type == table_type.bootstrapTreeTable) {
  1071. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1072. if ($.common.isEmpty(row)) {
  1073. $.modal.alertWarning("请至少选择一条记录");
  1074. return;
  1075. }
  1076. url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1077. } else {
  1078. var row = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1079. url = table.options.updateUrl.replace("{id}", row);
  1080. }
  1081. }
  1082. $.modal.openFull("修改" + table.options.modalName, url);
  1083. },
  1084. // 修改访问地址
  1085. editUrl: function(id) {
  1086. var url = "/404.html";
  1087. if ($.common.isNotEmpty(id)) {
  1088. url = table.options.updateUrl.replace("{id}", id);
  1089. } else {
  1090. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1091. if (id.length == 0) {
  1092. $.modal.alertWarning("请至少选择一条记录");
  1093. return;
  1094. }
  1095. url = table.options.updateUrl.replace("{id}", id);
  1096. }
  1097. return url;
  1098. },
  1099. // 保存信息 刷新表格
  1100. save: function(url, data, callback) {
  1101. var config = {
  1102. url: url,
  1103. type: "post",
  1104. dataType: "json",
  1105. data: data,
  1106. beforeSend: function () {
  1107. $.modal.loading("正在处理中,请稍后...");
  1108. $.modal.disable();
  1109. },
  1110. success: function(result) {
  1111. if (typeof callback == "function") {
  1112. callback(result);
  1113. }
  1114. $.operate.successCallback(result);
  1115. }
  1116. };
  1117. $.ajax(config)
  1118. },
  1119. // 保存信息 弹出提示框
  1120. saveModal: function(url, data, callback) {
  1121. var config = {
  1122. url: url,
  1123. type: "post",
  1124. dataType: "json",
  1125. data: data,
  1126. beforeSend: function () {
  1127. $.modal.loading("正在处理中,请稍后...");
  1128. },
  1129. success: function(result) {
  1130. if (typeof callback == "function") {
  1131. callback(result);
  1132. }
  1133. if (result.code == web_status.SUCCESS) {
  1134. $.modal.alertSuccess(result.msg)
  1135. } else if (result.code == web_status.WARNING) {
  1136. $.modal.alertWarning(result.msg)
  1137. } else {
  1138. $.modal.alertError(result.msg);
  1139. }
  1140. $.modal.closeLoading();
  1141. }
  1142. };
  1143. $.ajax(config)
  1144. },
  1145. // 保存选项卡信息
  1146. saveTab: function(url, data, callback) {
  1147. var config = {
  1148. url: url,
  1149. type: "post",
  1150. dataType: "json",
  1151. data: data,
  1152. beforeSend: function () {
  1153. $.modal.loading("正在处理中,请稍后...");
  1154. },
  1155. success: function(result) {
  1156. if (typeof callback == "function") {
  1157. callback(result);
  1158. }
  1159. $.operate.successTabCallback(result);
  1160. }
  1161. };
  1162. $.ajax(config)
  1163. },
  1164. // 保存结果弹出msg刷新table表格
  1165. ajaxSuccess: function (result) {
  1166. if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTable) {
  1167. $.modal.msgSuccess(result.msg);
  1168. $.table.refresh();
  1169. } else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) {
  1170. $.modal.msgSuccess(result.msg);
  1171. $.treeTable.refresh();
  1172. } else if (result.code == web_status.SUCCESS && $.common.isEmpty(table.options.type)) {
  1173. $.modal.msgSuccess(result.msg)
  1174. } else if (result.code == web_status.WARNING) {
  1175. $.modal.alertWarning(result.msg)
  1176. } else {
  1177. $.modal.alertError(result.msg);
  1178. }
  1179. $.modal.closeLoading();
  1180. },
  1181. // 成功结果提示msg(父窗体全局更新)
  1182. saveSuccess: function (result) {
  1183. if (result.code == web_status.SUCCESS) {
  1184. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1185. } else if (result.code == web_status.WARNING) {
  1186. $.modal.alertWarning(result.msg)
  1187. } else {
  1188. $.modal.alertError(result.msg);
  1189. }
  1190. $.modal.closeLoading();
  1191. },
  1192. // 成功回调执行事件(父窗体静默更新)
  1193. successCallback: function(result) {
  1194. if (result.code == web_status.SUCCESS) {
  1195. var parent = window.parent;
  1196. if (parent.table.options.type == table_type.bootstrapTable) {
  1197. $.modal.close();
  1198. parent.$.modal.msgSuccess(result.msg);
  1199. parent.$.table.refresh();
  1200. } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
  1201. $.modal.close();
  1202. parent.$.modal.msgSuccess(result.msg);
  1203. parent.$.treeTable.refresh();
  1204. } else {
  1205. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1206. }
  1207. } else if (result.code == web_status.WARNING) {
  1208. $.modal.alertWarning(result.msg)
  1209. } else {
  1210. $.modal.alertError(result.msg);
  1211. }
  1212. $.modal.closeLoading();
  1213. $.modal.enable();
  1214. },
  1215. // 选项卡成功回调执行事件(父窗体静默更新)
  1216. successTabCallback: function(result) {
  1217. if (result.code == web_status.SUCCESS) {
  1218. var topWindow = $(window.parent.document);
  1219. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
  1220. var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
  1221. $.modal.close();
  1222. $contentWindow.$.modal.msgSuccess(result.msg);
  1223. $contentWindow.$(".layui-layer-padding").removeAttr("style");
  1224. if ($contentWindow.table.options.type == table_type.bootstrapTable) {
  1225. $contentWindow.$.table.refresh();
  1226. } else if ($contentWindow.table.options.type == table_type.bootstrapTreeTable) {
  1227. $contentWindow.$.treeTable.refresh();
  1228. }
  1229. $.modal.closeTab();
  1230. } else if (result.code == web_status.WARNING) {
  1231. $.modal.alertWarning(result.msg)
  1232. } else {
  1233. $.modal.alertError(result.msg);
  1234. }
  1235. $.modal.closeLoading();
  1236. }
  1237. },
  1238. // 校验封装处理
  1239. validate: {
  1240. // 判断返回标识是否唯一 false 不存在 true 存在
  1241. unique: function (value) {
  1242. if (value == "0") {
  1243. return true;
  1244. }
  1245. return false;
  1246. },
  1247. // 表单验证
  1248. form: function (formId) {
  1249. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1250. return $("#" + currentId).validate().form();
  1251. },
  1252. // 重置表单验证(清除提示信息)
  1253. reset: function (formId) {
  1254. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1255. return $("#" + currentId).validate().resetForm();
  1256. }
  1257. },
  1258. // 树插件封装处理
  1259. tree: {
  1260. _option: {},
  1261. _lastValue: {},
  1262. // 初始化树结构
  1263. init: function(options) {
  1264. var defaults = {
  1265. id: "tree", // 属性ID
  1266. expandLevel: 0, // 展开等级节点
  1267. view: {
  1268. selectedMulti: false, // 设置是否允许同时选中多个节点
  1269. nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
  1270. },
  1271. check: {
  1272. enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
  1273. nocheckInherit: true, // 设置子节点是否自动继承
  1274. chkboxType: { "Y": "ps", "N": "ps" } // 父子节点的关联关系
  1275. },
  1276. data: {
  1277. key: {
  1278. title: "title" // 节点数据保存节点提示信息的属性名称
  1279. },
  1280. simpleData: {
  1281. enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
  1282. }
  1283. },
  1284. };
  1285. var options = $.extend(defaults, options);
  1286. $.tree._option = options;
  1287. // 树结构初始化加载
  1288. var setting = {
  1289. callback: {
  1290. onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
  1291. onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
  1292. onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
  1293. },
  1294. check: options.check,
  1295. view: options.view,
  1296. data: options.data
  1297. };
  1298. $.get(options.url, function(data) {
  1299. var treeId = $("#treeId").val();
  1300. tree = $.fn.zTree.init($("#" + options.id), setting, data);
  1301. $._tree = tree;
  1302. for (var i = 0; i < options.expandLevel; i++) {
  1303. var nodes = tree.getNodesByParam("level", i);
  1304. for (var j = 0; j < nodes.length; j++) {
  1305. tree.expandNode(nodes[j], true, false, false);
  1306. }
  1307. }
  1308. var node = tree.getNodesByParam("id", treeId, null)[0];
  1309. $.tree.selectByIdName(treeId, node);
  1310. // 回调tree方法
  1311. if(typeof(options.callBack) === "function"){
  1312. options.callBack(tree);
  1313. }
  1314. });
  1315. },
  1316. // 搜索节点
  1317. searchNode: function() {
  1318. // 取得输入的关键字的值
  1319. var value = $.common.trim($("#keyword").val());
  1320. if ($.tree._lastValue == value) {
  1321. return;
  1322. }
  1323. // 保存最后一次搜索名称
  1324. $.tree._lastValue = value;
  1325. var nodes = $._tree.getNodes();
  1326. // 如果要查空字串,就退出不查了。
  1327. if (value == "") {
  1328. $.tree.showAllNode(nodes);
  1329. return;
  1330. }
  1331. $.tree.hideAllNode(nodes);
  1332. // 根据搜索值模糊匹配
  1333. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  1334. },
  1335. // 根据Id和Name选中指定节点
  1336. selectByIdName: function(treeId, node) {
  1337. if ($.common.isNotEmpty(treeId) && treeId == node.id) {
  1338. $._tree.selectNode(node, true);
  1339. }
  1340. },
  1341. // 显示所有节点
  1342. showAllNode: function(nodes) {
  1343. nodes = $._tree.transformToArray(nodes);
  1344. for (var i = nodes.length - 1; i >= 0; i--) {
  1345. if (nodes[i].getParentNode() != null) {
  1346. $._tree.expandNode(nodes[i], true, false, false, false);
  1347. } else {
  1348. $._tree.expandNode(nodes[i], true, true, false, false);
  1349. }
  1350. $._tree.showNode(nodes[i]);
  1351. $.tree.showAllNode(nodes[i].children);
  1352. }
  1353. },
  1354. // 隐藏所有节点
  1355. hideAllNode: function(nodes) {
  1356. var tree = $.fn.zTree.getZTreeObj("tree");
  1357. var nodes = $._tree.transformToArray(nodes);
  1358. for (var i = nodes.length - 1; i >= 0; i--) {
  1359. $._tree.hideNode(nodes[i]);
  1360. }
  1361. },
  1362. // 显示所有父节点
  1363. showParent: function(treeNode) {
  1364. var parentNode;
  1365. while ((parentNode = treeNode.getParentNode()) != null) {
  1366. $._tree.showNode(parentNode);
  1367. $._tree.expandNode(parentNode, true, false, false);
  1368. treeNode = parentNode;
  1369. }
  1370. },
  1371. // 显示所有孩子节点
  1372. showChildren: function(treeNode) {
  1373. if (treeNode.isParent) {
  1374. for (var idx in treeNode.children) {
  1375. var node = treeNode.children[idx];
  1376. $._tree.showNode(node);
  1377. $.tree.showChildren(node);
  1378. }
  1379. }
  1380. },
  1381. // 更新节点状态
  1382. updateNodes: function(nodeList) {
  1383. $._tree.showNodes(nodeList);
  1384. for (var i = 0, l = nodeList.length; i < l; i++) {
  1385. var treeNode = nodeList[i];
  1386. $.tree.showChildren(treeNode);
  1387. $.tree.showParent(treeNode)
  1388. }
  1389. },
  1390. // 获取当前被勾选集合
  1391. getCheckedNodes: function(column) {
  1392. var _column = $.common.isEmpty(column) ? "id" : column;
  1393. var nodes = $._tree.getCheckedNodes(true);
  1394. return $.map(nodes, function (row) {
  1395. return row[_column];
  1396. }).join();
  1397. },
  1398. // 不允许根父节点选择
  1399. notAllowParents: function(_tree) {
  1400. var nodes = _tree.getSelectedNodes();
  1401. if(nodes.length == 0){
  1402. $.modal.msgError("请选择节点后提交");
  1403. return false;
  1404. }
  1405. for (var i = 0; i < nodes.length; i++) {
  1406. if (nodes[i].level == 0) {
  1407. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  1408. return false;
  1409. }
  1410. if (nodes[i].isParent) {
  1411. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  1412. return false;
  1413. }
  1414. }
  1415. return true;
  1416. },
  1417. // 不允许最后层级节点选择
  1418. notAllowLastLevel: function(_tree) {
  1419. var nodes = _tree.getSelectedNodes();
  1420. for (var i = 0; i < nodes.length; i++) {
  1421. if (!nodes[i].isParent) {
  1422. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  1423. return false;
  1424. }
  1425. }
  1426. return true;
  1427. },
  1428. // 隐藏/显示搜索栏
  1429. toggleSearch: function() {
  1430. $('#search').slideToggle(200);
  1431. $('#btnShow').toggle();
  1432. $('#btnHide').toggle();
  1433. $('#keyword').focus();
  1434. },
  1435. // 折叠
  1436. collapse: function() {
  1437. $._tree.expandAll(false);
  1438. },
  1439. // 展开
  1440. expand: function() {
  1441. $._tree.expandAll(true);
  1442. }
  1443. },
  1444. // 通用方法封装处理
  1445. common: {
  1446. // 判断字符串是否为空
  1447. isEmpty: function (value) {
  1448. if (value == null || this.trim(value) == "") {
  1449. return true;
  1450. }
  1451. return false;
  1452. },
  1453. // 判断一个字符串是否为非空串
  1454. isNotEmpty: function (value) {
  1455. return !$.common.isEmpty(value);
  1456. },
  1457. // 空对象转字符串
  1458. nullToStr: function(value) {
  1459. if ($.common.isEmpty(value)) {
  1460. return "-";
  1461. }
  1462. return value;
  1463. },
  1464. // 是否显示数据 为空默认为显示
  1465. visible: function (value) {
  1466. if ($.common.isEmpty(value) || value == true) {
  1467. return true;
  1468. }
  1469. return false;
  1470. },
  1471. // 空格截取
  1472. trim: function (value) {
  1473. if (value == null) {
  1474. return "";
  1475. }
  1476. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  1477. },
  1478. // 比较两个字符串(大小写敏感)
  1479. equals: function (str, that) {
  1480. return str == that;
  1481. },
  1482. // 比较两个字符串(大小写不敏感)
  1483. equalsIgnoreCase: function (str, that) {
  1484. return String(str).toUpperCase() === String(that).toUpperCase();
  1485. },
  1486. // 将字符串按指定字符分割
  1487. split: function (str, sep, maxLen) {
  1488. if ($.common.isEmpty(str)) {
  1489. return null;
  1490. }
  1491. var value = String(str).split(sep);
  1492. return maxLen ? value.slice(0, maxLen - 1) : value;
  1493. },
  1494. // 字符串格式化(%s )
  1495. sprintf: function (str) {
  1496. var args = arguments, flag = true, i = 1;
  1497. str = str.replace(/%s/g, function () {
  1498. var arg = args[i++];
  1499. if (typeof arg === 'undefined') {
  1500. flag = false;
  1501. return '';
  1502. }
  1503. return arg == null ? '' : arg;
  1504. });
  1505. return flag ? str : '';
  1506. },
  1507. // 日期格式化 时间戳 -> yyyy-MM-dd HH-mm-ss
  1508. dateFormat: function(date, format) {
  1509. var that = this;
  1510. if (that.isEmpty(date)) return "";
  1511. if (!date) return;
  1512. if (!format) format = "yyyy-MM-dd";
  1513. switch (typeof date) {
  1514. case "string":
  1515. date = new Date(date.replace(/-/, "/"));
  1516. break;
  1517. case "number":
  1518. date = new Date(date);
  1519. break;
  1520. }
  1521. if (!date instanceof Date) return;
  1522. var dict = {
  1523. "yyyy": date.getFullYear(),
  1524. "M": date.getMonth() + 1,
  1525. "d": date.getDate(),
  1526. "H": date.getHours(),
  1527. "m": date.getMinutes(),
  1528. "s": date.getSeconds(),
  1529. "MM": ("" + (date.getMonth() + 101)).substr(1),
  1530. "dd": ("" + (date.getDate() + 100)).substr(1),
  1531. "HH": ("" + (date.getHours() + 100)).substr(1),
  1532. "mm": ("" + (date.getMinutes() + 100)).substr(1),
  1533. "ss": ("" + (date.getSeconds() + 100)).substr(1)
  1534. };
  1535. return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g,
  1536. function() {
  1537. return dict[arguments[0]];
  1538. });
  1539. },
  1540. // 获取节点数据,支持多层级访问
  1541. getItemField: function (item, field) {
  1542. var value = item;
  1543. if (typeof field !== 'string' || item.hasOwnProperty(field)) {
  1544. return item[field];
  1545. }
  1546. var props = field.split('.');
  1547. for (var p in props) {
  1548. value = value && value[props[p]];
  1549. }
  1550. return value;
  1551. },
  1552. // 指定随机数返回
  1553. random: function (min, max) {
  1554. return Math.floor((Math.random() * max) + min);
  1555. },
  1556. // 判断字符串是否是以start开头
  1557. startWith: function(value, start) {
  1558. var reg = new RegExp("^" + start);
  1559. return reg.test(value)
  1560. },
  1561. // 判断字符串是否是以end结尾
  1562. endWith: function(value, end) {
  1563. var reg = new RegExp(end + "$");
  1564. return reg.test(value)
  1565. },
  1566. // 数组去重
  1567. uniqueFn: function(array) {
  1568. var result = [];
  1569. var hashObj = {};
  1570. for (var i = 0; i < array.length; i++) {
  1571. if (!hashObj[array[i]]) {
  1572. hashObj[array[i]] = true;
  1573. result.push(array[i]);
  1574. }
  1575. }
  1576. return result;
  1577. },
  1578. // 数组中的所有元素放入一个字符串
  1579. join: function(array, separator) {
  1580. if ($.common.isEmpty(array)) {
  1581. return null;
  1582. }
  1583. return array.join(separator);
  1584. },
  1585. // 获取form下所有的字段并转换为json对象
  1586. formToJSON: function(formId) {
  1587. var json = {};
  1588. $.each($("#" + formId).serializeArray(), function(i, field) {
  1589. if(json[field.name]) {
  1590. json[field.name] += ("," + field.value);
  1591. } else {
  1592. json[field.name] = field.value;
  1593. }
  1594. });
  1595. return json;
  1596. },
  1597. // 数据字典转下拉框
  1598. dictToSelect: function(datas, value, name) {
  1599. var actions = [];
  1600. actions.push($.common.sprintf("<select class='form-control' name='%s'>", name));
  1601. $.each(datas, function(index, dict) {
  1602. actions.push($.common.sprintf("<option value='%s'", dict.dictValue));
  1603. if (dict.dictValue == ('' + value)) {
  1604. actions.push(' selected');
  1605. }
  1606. actions.push($.common.sprintf(">%s</option>", dict.dictLabel));
  1607. });
  1608. actions.push('</select>');
  1609. return actions.join('');
  1610. },
  1611. // 获取obj对象长度
  1612. getLength: function(obj) {
  1613. var count = 0;  
  1614. for (var i in obj) {
  1615. if (obj.hasOwnProperty(i)) {
  1616. count++;
  1617. }  
  1618. }
  1619. return count;
  1620. },
  1621. // 判断移动端
  1622. isMobile: function () {
  1623. return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i);
  1624. },
  1625. // 数字正则表达式,只能为0-9数字
  1626. numValid : function(text){
  1627. var patten = new RegExp(/^[0-9]+$/);
  1628. return patten.test(text);
  1629. },
  1630. // 英文正则表达式,只能为a-z和A-Z字母
  1631. enValid : function(text){
  1632. var patten = new RegExp(/^[a-zA-Z]+$/);
  1633. return patten.test(text);
  1634. },
  1635. // 英文、数字正则表达式,必须包含(字母,数字)
  1636. enNumValid : function(text){
  1637. var patten = new RegExp(/^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/);
  1638. return patten.test(text);
  1639. },
  1640. // 英文、数字、特殊字符正则表达式,必须包含(字母,数字,特殊字符!@#$%^&*()-=_+)
  1641. charValid : function(text){
  1642. var patten = new RegExp(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[~!@#\$%\^&\*\(\)\-=_\+])[A-Za-z\d~!@#\$%\^&\*\(\)\-=_\+]{6,}$/);
  1643. return patten.test(text);
  1644. },
  1645. }
  1646. });
  1647. })(jQuery);
  1648. /** 表格类型 */
  1649. table_type = {
  1650. bootstrapTable: 0,
  1651. bootstrapTreeTable: 1
  1652. };
  1653. /** 消息状态码 */
  1654. web_status = {
  1655. SUCCESS: 0,
  1656. FAIL: 500,
  1657. WARNING: 301
  1658. };
  1659. /** 弹窗状态码 */
  1660. modal_status = {
  1661. SUCCESS: "success",
  1662. FAIL: "error",
  1663. WARNING: "warning"
  1664. };