ry-ui.js 77 KB

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