ry-ui.js 78 KB

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