ry-ui.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. 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 == web_status.SUCCESS) {
  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. if ($.common.isEmpty(datas) || $.common.isEmpty(value)) {
  494. return '';
  495. }
  496. var actions = [];
  497. $.each(datas, function(index, dict) {
  498. if (dict.dictValue == ('' + value)) {
  499. var listClass = $.common.equals("default", dict.listClass) || $.common.isEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
  500. actions.push($.common.sprintf("<span class='%s'>%s</span>", listClass, dict.dictLabel));
  501. return false;
  502. }
  503. });
  504. return actions.join('');
  505. },
  506. // 回显数据字典(字符串数组)
  507. selectDictLabels: function(datas, value, separator) {
  508. if ($.common.isEmpty(datas) || $.common.isEmpty(value)) {
  509. return '';
  510. }
  511. var currentSeparator = $.common.isEmpty(separator) ? "," : separator;
  512. var actions = [];
  513. $.each(value.split(currentSeparator), function(i, val) {
  514. $.each(datas, function(index, dict) {
  515. if (dict.dictValue == ('' + val)) {
  516. var listClass = $.common.equals("default", dict.listClass) || $.common.isEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
  517. actions.push($.common.sprintf("<span class='%s'>%s </span>", listClass, dict.dictLabel));
  518. return false;
  519. }
  520. });
  521. });
  522. return actions.join('');
  523. },
  524. // 显示表格指定列
  525. showColumn: function(column, tableId) {
  526. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  527. $("#" + currentId).bootstrapTable('showColumn', column);
  528. },
  529. // 隐藏表格指定列
  530. hideColumn: function(column, tableId) {
  531. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  532. $("#" + currentId).bootstrapTable('hideColumn', column);
  533. },
  534. // 显示所有表格列
  535. showAllColumns: function(tableId) {
  536. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  537. $("#" + currentId).bootstrapTable('showAllColumns');
  538. },
  539. // 隐藏所有表格列
  540. hideAllColumns: function(tableId) {
  541. var currentId = $.common.isEmpty(tableId) ? table.options.id : tableId;
  542. $("#" + currentId).bootstrapTable('hideAllColumns');
  543. }
  544. },
  545. // 表格树封装处理
  546. treeTable: {
  547. // 初始化表格
  548. init: function(options) {
  549. var defaults = {
  550. id: "bootstrap-tree-table",
  551. type: 1, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  552. height: 0,
  553. rootIdValue: null,
  554. ajaxParams: {},
  555. toolbar: "toolbar",
  556. striped: false,
  557. expandColumn: 1,
  558. showSearch: true,
  559. showRefresh: true,
  560. showColumns: true,
  561. expandAll: true,
  562. expandFirst: true
  563. };
  564. var options = $.extend(defaults, options);
  565. table.options = options;
  566. table.config[options.id] = options;
  567. $.table.initEvent();
  568. $.bttTable = $('#' + options.id).bootstrapTreeTable({
  569. code: options.code, // 用于设置父子关系
  570. parentCode: options.parentCode, // 用于设置父子关系
  571. type: 'post', // 请求方式(*)
  572. url: options.url, // 请求后台的URL(*)
  573. data: options.data, // 无url时用于渲染的数据
  574. ajaxParams: options.ajaxParams, // 请求数据的ajax的data属性
  575. rootIdValue: options.rootIdValue, // 设置指定根节点id值
  576. height: options.height, // 表格树的高度
  577. expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
  578. striped: options.striped, // 是否显示行间隔色
  579. bordered: false, // 是否显示边框
  580. toolbar: '#' + options.toolbar, // 指定工作栏
  581. showSearch: options.showSearch, // 是否显示检索信息
  582. showRefresh: options.showRefresh, // 是否显示刷新按钮
  583. showColumns: options.showColumns, // 是否显示隐藏某列下拉框
  584. expandAll: options.expandAll, // 是否全部展开
  585. expandFirst: options.expandFirst, // 是否默认第一级展开--expandAll为false时生效
  586. columns: options.columns, // 显示列信息(*)
  587. responseHandler: $.treeTable.responseHandler, // 在加载服务器发送来的数据之前处理函数
  588. onLoadSuccess: $.treeTable.onLoadSuccess // 当所有数据被加载时触发处理函数
  589. });
  590. },
  591. // 条件查询
  592. search: function(formId) {
  593. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  594. var params = $.common.formToJSON(currentId);
  595. $.bttTable.bootstrapTreeTable('refresh', params);
  596. },
  597. // 刷新
  598. refresh: function() {
  599. $.bttTable.bootstrapTreeTable('refresh');
  600. },
  601. // 查询表格树指定列值
  602. selectColumns: function(column) {
  603. var rows = $.map($.bttTable.bootstrapTreeTable('getSelections'), function (row) {
  604. return $.common.getItemField(row, column);
  605. });
  606. return $.common.uniqueFn(rows);
  607. },
  608. // 请求获取数据后处理回调函数,校验异常状态提醒
  609. responseHandler: function(res) {
  610. if (typeof table.options.responseHandler == "function") {
  611. table.options.responseHandler(res);
  612. }
  613. if (res.code != undefined && res.code != web_status.SUCCESS) {
  614. $.modal.alertWarning(res.msg);
  615. return [];
  616. } else {
  617. return res;
  618. }
  619. },
  620. // 当所有数据被加载时触发
  621. onLoadSuccess: function(data) {
  622. if (typeof table.options.onLoadSuccess == "function") {
  623. table.options.onLoadSuccess(data);
  624. }
  625. $(".table [data-toggle='tooltip']").tooltip();
  626. },
  627. },
  628. // 表单封装处理
  629. form: {
  630. // 表单重置
  631. reset: function(formId, tableId) {
  632. table.set(tableId);
  633. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  634. $("#" + currentId)[0].reset();
  635. if (table.options.type == table_type.bootstrapTable) {
  636. if($.common.isEmpty(tableId)){
  637. $("#" + table.options.id).bootstrapTable('refresh');
  638. } else{
  639. $("#" + tableId).bootstrapTable('refresh');
  640. }
  641. } else if (table.options.type == table_type.bootstrapTreeTable) {
  642. if($.common.isEmpty(tableId)){
  643. $("#" + table.options.id).bootstrapTreeTable('refresh', []);
  644. } else{
  645. $("#" + tableId).bootstrapTreeTable('refresh', []);
  646. }
  647. }
  648. },
  649. // 获取选中复选框项
  650. selectCheckeds: function(name) {
  651. var checkeds = "";
  652. $('input:checkbox[name="' + name + '"]:checked').each(function(i) {
  653. if (0 == i) {
  654. checkeds = $(this).val();
  655. } else {
  656. checkeds += ("," + $(this).val());
  657. }
  658. });
  659. return checkeds;
  660. },
  661. // 获取选中下拉框项
  662. selectSelects: function(name) {
  663. var selects = "";
  664. $('#' + name + ' option:selected').each(function (i) {
  665. if (0 == i) {
  666. selects = $(this).val();
  667. } else {
  668. selects += ("," + $(this).val());
  669. }
  670. });
  671. return selects;
  672. }
  673. },
  674. // 弹出层封装处理
  675. modal: {
  676. // 显示图标
  677. icon: function(type) {
  678. var icon = "";
  679. if (type == modal_status.WARNING) {
  680. icon = 0;
  681. } else if (type == modal_status.SUCCESS) {
  682. icon = 1;
  683. } else if (type == modal_status.FAIL) {
  684. icon = 2;
  685. } else {
  686. icon = 3;
  687. }
  688. return icon;
  689. },
  690. // 消息提示
  691. msg: function(content, type) {
  692. if (type != undefined) {
  693. layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 });
  694. } else {
  695. layer.msg(content);
  696. }
  697. },
  698. // 错误消息
  699. msgError: function(content) {
  700. $.modal.msg(content, modal_status.FAIL);
  701. },
  702. // 成功消息
  703. msgSuccess: function(content) {
  704. $.modal.msg(content, modal_status.SUCCESS);
  705. },
  706. // 警告消息
  707. msgWarning: function(content) {
  708. $.modal.msg(content, modal_status.WARNING);
  709. },
  710. // 弹出提示
  711. alert: function(content, type) {
  712. layer.alert(content, {
  713. icon: $.modal.icon(type),
  714. title: "系统提示",
  715. btn: ['确认'],
  716. btnclass: ['btn btn-primary'],
  717. });
  718. },
  719. // 消息提示并刷新父窗体
  720. msgReload: function(msg, type) {
  721. layer.msg(msg, {
  722. icon: $.modal.icon(type),
  723. time: 500,
  724. shade: [0.1, '#8F8F8F']
  725. },
  726. function() {
  727. $.modal.reload();
  728. });
  729. },
  730. // 错误提示
  731. alertError: function(content) {
  732. $.modal.alert(content, modal_status.FAIL);
  733. },
  734. // 成功提示
  735. alertSuccess: function(content) {
  736. $.modal.alert(content, modal_status.SUCCESS);
  737. },
  738. // 警告提示
  739. alertWarning: function(content) {
  740. $.modal.alert(content, modal_status.WARNING);
  741. },
  742. // 关闭窗体
  743. close: function (index) {
  744. if($.common.isEmpty(index)){
  745. var index = parent.layer.getFrameIndex(window.name);
  746. parent.layer.close(index);
  747. } else {
  748. layer.close(index);
  749. }
  750. },
  751. // 关闭全部窗体
  752. closeAll: function () {
  753. layer.closeAll();
  754. },
  755. // 确认窗体
  756. confirm: function (content, callBack) {
  757. layer.confirm(content, {
  758. icon: 3,
  759. title: "系统提示",
  760. btn: ['确认', '取消']
  761. }, function (index) {
  762. layer.close(index);
  763. callBack(true);
  764. });
  765. },
  766. // 弹出层指定宽度
  767. open: function (title, url, width, height, callback) {
  768. //如果是移动端,就使用自适应大小弹窗
  769. if ($.common.isMobile()) {
  770. width = 'auto';
  771. height = 'auto';
  772. }
  773. if ($.common.isEmpty(title)) {
  774. title = false;
  775. }
  776. if ($.common.isEmpty(url)) {
  777. url = "/404.html";
  778. }
  779. if ($.common.isEmpty(width)) {
  780. width = 800;
  781. }
  782. if ($.common.isEmpty(height)) {
  783. height = ($(window).height() - 50);
  784. }
  785. if ($.common.isEmpty(callback)) {
  786. callback = function(index, layero) {
  787. var iframeWin = layero.find('iframe')[0];
  788. iframeWin.contentWindow.submitHandler(index, layero);
  789. }
  790. }
  791. layer.open({
  792. type: 2,
  793. area: [width + 'px', height + 'px'],
  794. fix: false,
  795. //不固定
  796. maxmin: true,
  797. shade: 0.3,
  798. title: title,
  799. content: url,
  800. btn: ['确定', '关闭'],
  801. // 弹层外区域关闭
  802. shadeClose: true,
  803. yes: callback,
  804. cancel: function(index) {
  805. return true;
  806. }
  807. });
  808. },
  809. // 弹出层指定参数选项
  810. openOptions: function (options) {
  811. var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
  812. var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
  813. var _width = $.common.isEmpty(options.width) ? "800" : options.width;
  814. var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
  815. var _btn = ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'];
  816. if ($.common.isEmpty(options.yes)) {
  817. options.yes = function(index, layero) {
  818. options.callBack(index, layero);
  819. }
  820. }
  821. var btnCallback = {};
  822. if(options.btn instanceof Array){
  823. for (var i = 1, len = options.btn.length; i < len; i++) {
  824. var btn = options["btn" + (i + 1)];
  825. if (btn) {
  826. btnCallback["btn" + (i + 1)] = btn;
  827. }
  828. }
  829. }
  830. var index = layer.open($.extend({
  831. type: 2,
  832. maxmin: $.common.isEmpty(options.maxmin) ? true : options.maxmin,
  833. shade: 0.3,
  834. title: _title,
  835. fix: false,
  836. area: [_width + 'px', _height + 'px'],
  837. content: _url,
  838. shadeClose: $.common.isEmpty(options.shadeClose) ? true : options.shadeClose,
  839. skin: options.skin,
  840. btn: $.common.isEmpty(options.btn) ? _btn : options.btn,
  841. yes: options.yes,
  842. cancel: function () {
  843. return true;
  844. }
  845. }, btnCallback));
  846. if ($.common.isNotEmpty(options.full) && options.full === true) {
  847. layer.full(index);
  848. }
  849. },
  850. // 弹出层全屏
  851. openFull: function (title, url, width, height) {
  852. //如果是移动端,就使用自适应大小弹窗
  853. if ($.common.isMobile()) {
  854. width = 'auto';
  855. height = 'auto';
  856. }
  857. if ($.common.isEmpty(title)) {
  858. title = false;
  859. }
  860. if ($.common.isEmpty(url)) {
  861. url = "/404.html";
  862. }
  863. if ($.common.isEmpty(width)) {
  864. width = 800;
  865. }
  866. if ($.common.isEmpty(height)) {
  867. height = ($(window).height() - 50);
  868. }
  869. var index = layer.open({
  870. type: 2,
  871. area: [width + 'px', height + 'px'],
  872. fix: false,
  873. //不固定
  874. maxmin: true,
  875. shade: 0.3,
  876. title: title,
  877. content: url,
  878. btn: ['确定', '关闭'],
  879. // 弹层外区域关闭
  880. shadeClose: true,
  881. yes: function(index, layero) {
  882. var iframeWin = layero.find('iframe')[0];
  883. iframeWin.contentWindow.submitHandler(index, layero);
  884. },
  885. cancel: function(index) {
  886. return true;
  887. }
  888. });
  889. layer.full(index);
  890. },
  891. // 选卡页方式打开
  892. openTab: function (title, url, isRefresh) {
  893. createMenuItem(url, title, isRefresh);
  894. },
  895. // 选卡页同一页签打开
  896. parentTab: function (title, url) {
  897. var dataId = window.frameElement.getAttribute('data-id');
  898. createMenuItem(url, title);
  899. closeItem(dataId);
  900. },
  901. // 关闭选项卡
  902. closeTab: function (dataId) {
  903. closeItem(dataId);
  904. },
  905. // 禁用按钮
  906. disable: function() {
  907. var doc = window.top == window.parent ? window.document : window.parent.document;
  908. $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
  909. },
  910. // 启用按钮
  911. enable: function() {
  912. var doc = window.top == window.parent ? window.document : window.parent.document;
  913. $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
  914. },
  915. // 打开遮罩层
  916. loading: function (message) {
  917. $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
  918. },
  919. // 关闭遮罩层
  920. closeLoading: function () {
  921. setTimeout(function(){
  922. $.unblockUI();
  923. }, 50);
  924. },
  925. // 重新加载
  926. reload: function () {
  927. parent.location.reload();
  928. }
  929. },
  930. // 操作封装处理
  931. operate: {
  932. // 提交数据
  933. submit: function(url, type, dataType, data, callback) {
  934. var config = {
  935. url: url,
  936. type: type,
  937. dataType: dataType,
  938. data: data,
  939. beforeSend: function () {
  940. $.modal.loading("正在处理中,请稍后...");
  941. },
  942. success: function(result) {
  943. if (typeof callback == "function") {
  944. callback(result);
  945. }
  946. $.operate.ajaxSuccess(result);
  947. }
  948. };
  949. $.ajax(config)
  950. },
  951. // post请求传输
  952. post: function(url, data, callback) {
  953. $.operate.submit(url, "post", "json", data, callback);
  954. },
  955. // get请求传输
  956. get: function(url, callback) {
  957. $.operate.submit(url, "get", "json", "", callback);
  958. },
  959. // 详细信息
  960. detail: function(id, width, height) {
  961. table.set();
  962. var _url = $.operate.detailUrl(id);
  963. var _width = $.common.isEmpty(width) ? "800" : width;
  964. var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
  965. //如果是移动端,就使用自适应大小弹窗
  966. if ($.common.isMobile()) {
  967. _width = 'auto';
  968. _height = 'auto';
  969. }
  970. var options = {
  971. title: table.options.modalName + "详细",
  972. width: _width,
  973. height: _height,
  974. url: _url,
  975. skin: 'layui-layer-gray',
  976. btn: ['关闭'],
  977. yes: function (index, layero) {
  978. layer.close(index);
  979. }
  980. };
  981. $.modal.openOptions(options);
  982. },
  983. // 详细访问地址
  984. detailUrl: function(id) {
  985. var url = "/404.html";
  986. if ($.common.isNotEmpty(id)) {
  987. url = table.options.detailUrl.replace("{id}", id);
  988. } else {
  989. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  990. if (id.length == 0) {
  991. $.modal.alertWarning("请至少选择一条记录");
  992. return;
  993. }
  994. url = table.options.detailUrl.replace("{id}", id);
  995. }
  996. return url;
  997. },
  998. // 删除信息
  999. remove: function(id) {
  1000. table.set();
  1001. $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗?", function() {
  1002. var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
  1003. if(table.options.type == table_type.bootstrapTreeTable) {
  1004. $.operate.get(url);
  1005. } else {
  1006. var data = { "ids": id };
  1007. $.operate.submit(url, "post", "json", data);
  1008. }
  1009. });
  1010. },
  1011. // 批量删除信息
  1012. removeAll: function() {
  1013. table.set();
  1014. var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1015. if (rows.length == 0) {
  1016. $.modal.alertWarning("请至少选择一条记录");
  1017. return;
  1018. }
  1019. $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
  1020. var url = table.options.removeUrl;
  1021. var data = { "ids": rows.join() };
  1022. $.operate.submit(url, "post", "json", data);
  1023. });
  1024. },
  1025. // 清空信息
  1026. clean: function() {
  1027. table.set();
  1028. $.modal.confirm("确定清空所有" + table.options.modalName + "吗?", function() {
  1029. var url = table.options.cleanUrl;
  1030. $.operate.submit(url, "post", "json", "");
  1031. });
  1032. },
  1033. // 添加信息
  1034. add: function(id) {
  1035. table.set();
  1036. $.modal.open("添加" + table.options.modalName, $.operate.addUrl(id));
  1037. },
  1038. // 添加信息,以tab页展现
  1039. addTab: function (id) {
  1040. table.set();
  1041. $.modal.openTab("添加" + table.options.modalName, $.operate.addUrl(id));
  1042. },
  1043. // 添加信息 全屏
  1044. addFull: function(id) {
  1045. table.set();
  1046. var url = $.common.isEmpty(id) ? table.options.createUrl : table.options.createUrl.replace("{id}", id);
  1047. $.modal.openFull("添加" + table.options.modalName, url);
  1048. },
  1049. // 添加访问地址
  1050. addUrl: function(id) {
  1051. var url = $.common.isEmpty(id) ? table.options.createUrl.replace("{id}", "") : table.options.createUrl.replace("{id}", id);
  1052. return url;
  1053. },
  1054. // 修改信息
  1055. edit: function(id) {
  1056. table.set();
  1057. if($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
  1058. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1059. if ($.common.isEmpty(row)) {
  1060. $.modal.alertWarning("请至少选择一条记录");
  1061. return;
  1062. }
  1063. var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1064. $.modal.open("修改" + table.options.modalName, url);
  1065. } else {
  1066. $.modal.open("修改" + table.options.modalName, $.operate.editUrl(id));
  1067. }
  1068. },
  1069. // 修改信息,以tab页展现
  1070. editTab: function(id) {
  1071. table.set();
  1072. $.modal.openTab("修改" + table.options.modalName, $.operate.editUrl(id));
  1073. },
  1074. // 修改信息 全屏
  1075. editFull: function(id) {
  1076. table.set();
  1077. var url = "/404.html";
  1078. if ($.common.isNotEmpty(id)) {
  1079. url = table.options.updateUrl.replace("{id}", id);
  1080. } else {
  1081. if(table.options.type == table_type.bootstrapTreeTable) {
  1082. var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
  1083. if ($.common.isEmpty(row)) {
  1084. $.modal.alertWarning("请至少选择一条记录");
  1085. return;
  1086. }
  1087. url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
  1088. } else {
  1089. var row = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1090. url = table.options.updateUrl.replace("{id}", row);
  1091. }
  1092. }
  1093. $.modal.openFull("修改" + table.options.modalName, url);
  1094. },
  1095. // 修改访问地址
  1096. editUrl: function(id) {
  1097. var url = "/404.html";
  1098. if ($.common.isNotEmpty(id)) {
  1099. url = table.options.updateUrl.replace("{id}", id);
  1100. } else {
  1101. var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
  1102. if (id.length == 0) {
  1103. $.modal.alertWarning("请至少选择一条记录");
  1104. return;
  1105. }
  1106. url = table.options.updateUrl.replace("{id}", id);
  1107. }
  1108. return url;
  1109. },
  1110. // 保存信息 刷新表格
  1111. save: function(url, data, callback) {
  1112. var config = {
  1113. url: url,
  1114. type: "post",
  1115. dataType: "json",
  1116. data: data,
  1117. beforeSend: function () {
  1118. $.modal.loading("正在处理中,请稍后...");
  1119. $.modal.disable();
  1120. },
  1121. success: function(result) {
  1122. if (typeof callback == "function") {
  1123. callback(result);
  1124. }
  1125. $.operate.successCallback(result);
  1126. }
  1127. };
  1128. $.ajax(config)
  1129. },
  1130. // 保存信息 弹出提示框
  1131. saveModal: function(url, data, callback) {
  1132. var config = {
  1133. url: url,
  1134. type: "post",
  1135. dataType: "json",
  1136. data: data,
  1137. beforeSend: function () {
  1138. $.modal.loading("正在处理中,请稍后...");
  1139. },
  1140. success: function(result) {
  1141. if (typeof callback == "function") {
  1142. callback(result);
  1143. }
  1144. if (result.code == web_status.SUCCESS) {
  1145. $.modal.alertSuccess(result.msg)
  1146. } else if (result.code == web_status.WARNING) {
  1147. $.modal.alertWarning(result.msg)
  1148. } else {
  1149. $.modal.alertError(result.msg);
  1150. }
  1151. $.modal.closeLoading();
  1152. }
  1153. };
  1154. $.ajax(config)
  1155. },
  1156. // 保存选项卡信息
  1157. saveTab: function(url, data, callback) {
  1158. var config = {
  1159. url: url,
  1160. type: "post",
  1161. dataType: "json",
  1162. data: data,
  1163. beforeSend: function () {
  1164. $.modal.loading("正在处理中,请稍后...");
  1165. },
  1166. success: function(result) {
  1167. if (typeof callback == "function") {
  1168. callback(result);
  1169. }
  1170. $.operate.successTabCallback(result);
  1171. }
  1172. };
  1173. $.ajax(config)
  1174. },
  1175. // 保存结果弹出msg刷新table表格
  1176. ajaxSuccess: function (result) {
  1177. if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTable) {
  1178. $.modal.msgSuccess(result.msg);
  1179. $.table.refresh();
  1180. } else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) {
  1181. $.modal.msgSuccess(result.msg);
  1182. $.treeTable.refresh();
  1183. } else if (result.code == web_status.SUCCESS && $.common.isEmpty(table.options.type)) {
  1184. $.modal.msgSuccess(result.msg)
  1185. } else if (result.code == web_status.WARNING) {
  1186. $.modal.alertWarning(result.msg)
  1187. } else {
  1188. $.modal.alertError(result.msg);
  1189. }
  1190. $.modal.closeLoading();
  1191. },
  1192. // 成功结果提示msg(父窗体全局更新)
  1193. saveSuccess: function (result) {
  1194. if (result.code == web_status.SUCCESS) {
  1195. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1196. } else if (result.code == web_status.WARNING) {
  1197. $.modal.alertWarning(result.msg)
  1198. } else {
  1199. $.modal.alertError(result.msg);
  1200. }
  1201. $.modal.closeLoading();
  1202. },
  1203. // 成功回调执行事件(父窗体静默更新)
  1204. successCallback: function(result) {
  1205. if (result.code == web_status.SUCCESS) {
  1206. var parent = window.parent;
  1207. if (parent.table.options.type == table_type.bootstrapTable) {
  1208. $.modal.close();
  1209. parent.$.modal.msgSuccess(result.msg);
  1210. parent.$.table.refresh();
  1211. } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
  1212. $.modal.close();
  1213. parent.$.modal.msgSuccess(result.msg);
  1214. parent.$.treeTable.refresh();
  1215. } else {
  1216. $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
  1217. }
  1218. } else if (result.code == web_status.WARNING) {
  1219. $.modal.alertWarning(result.msg)
  1220. } else {
  1221. $.modal.alertError(result.msg);
  1222. }
  1223. $.modal.closeLoading();
  1224. $.modal.enable();
  1225. },
  1226. // 选项卡成功回调执行事件(父窗体静默更新)
  1227. successTabCallback: function(result) {
  1228. if (result.code == web_status.SUCCESS) {
  1229. var topWindow = $(window.parent.document);
  1230. var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
  1231. var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
  1232. $.modal.close();
  1233. $contentWindow.$.modal.msgSuccess(result.msg);
  1234. $contentWindow.$(".layui-layer-padding").removeAttr("style");
  1235. if ($contentWindow.table.options.type == table_type.bootstrapTable) {
  1236. $contentWindow.$.table.refresh();
  1237. } else if ($contentWindow.table.options.type == table_type.bootstrapTreeTable) {
  1238. $contentWindow.$.treeTable.refresh();
  1239. }
  1240. $.modal.closeTab();
  1241. } else if (result.code == web_status.WARNING) {
  1242. $.modal.alertWarning(result.msg)
  1243. } else {
  1244. $.modal.alertError(result.msg);
  1245. }
  1246. $.modal.closeLoading();
  1247. }
  1248. },
  1249. // 校验封装处理
  1250. validate: {
  1251. // 判断返回标识是否唯一 false 不存在 true 存在
  1252. unique: function (value) {
  1253. if (value == "0") {
  1254. return true;
  1255. }
  1256. return false;
  1257. },
  1258. // 表单验证
  1259. form: function (formId) {
  1260. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1261. return $("#" + currentId).validate().form();
  1262. },
  1263. // 重置表单验证(清除提示信息)
  1264. reset: function (formId) {
  1265. var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
  1266. return $("#" + currentId).validate().resetForm();
  1267. }
  1268. },
  1269. // 树插件封装处理
  1270. tree: {
  1271. _option: {},
  1272. _lastValue: {},
  1273. // 初始化树结构
  1274. init: function(options) {
  1275. var defaults = {
  1276. id: "tree", // 属性ID
  1277. expandLevel: 0, // 展开等级节点
  1278. view: {
  1279. selectedMulti: false, // 设置是否允许同时选中多个节点
  1280. nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
  1281. },
  1282. check: {
  1283. enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
  1284. nocheckInherit: true, // 设置子节点是否自动继承
  1285. chkboxType: { "Y": "ps", "N": "ps" } // 父子节点的关联关系
  1286. },
  1287. data: {
  1288. key: {
  1289. title: "title" // 节点数据保存节点提示信息的属性名称
  1290. },
  1291. simpleData: {
  1292. enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
  1293. }
  1294. },
  1295. };
  1296. var options = $.extend(defaults, options);
  1297. $.tree._option = options;
  1298. // 树结构初始化加载
  1299. var setting = {
  1300. callback: {
  1301. onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
  1302. onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
  1303. onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
  1304. },
  1305. check: options.check,
  1306. view: options.view,
  1307. data: options.data
  1308. };
  1309. $.get(options.url, function(data) {
  1310. var treeId = $("#treeId").val();
  1311. tree = $.fn.zTree.init($("#" + options.id), setting, data);
  1312. $._tree = tree;
  1313. for (var i = 0; i < options.expandLevel; i++) {
  1314. var nodes = tree.getNodesByParam("level", i);
  1315. for (var j = 0; j < nodes.length; j++) {
  1316. tree.expandNode(nodes[j], true, false, false);
  1317. }
  1318. }
  1319. var node = tree.getNodesByParam("id", treeId, null)[0];
  1320. $.tree.selectByIdName(treeId, node);
  1321. // 回调tree方法
  1322. if(typeof(options.callBack) === "function"){
  1323. options.callBack(tree);
  1324. }
  1325. });
  1326. },
  1327. // 搜索节点
  1328. searchNode: function() {
  1329. // 取得输入的关键字的值
  1330. var value = $.common.trim($("#keyword").val());
  1331. if ($.tree._lastValue == value) {
  1332. return;
  1333. }
  1334. // 保存最后一次搜索名称
  1335. $.tree._lastValue = value;
  1336. var nodes = $._tree.getNodes();
  1337. // 如果要查空字串,就退出不查了。
  1338. if (value == "") {
  1339. $.tree.showAllNode(nodes);
  1340. return;
  1341. }
  1342. $.tree.hideAllNode(nodes);
  1343. // 根据搜索值模糊匹配
  1344. $.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
  1345. },
  1346. // 根据Id和Name选中指定节点
  1347. selectByIdName: function(treeId, node) {
  1348. if ($.common.isNotEmpty(treeId) && treeId == node.id) {
  1349. $._tree.selectNode(node, true);
  1350. }
  1351. },
  1352. // 显示所有节点
  1353. showAllNode: function(nodes) {
  1354. nodes = $._tree.transformToArray(nodes);
  1355. for (var i = nodes.length - 1; i >= 0; i--) {
  1356. if (nodes[i].getParentNode() != null) {
  1357. $._tree.expandNode(nodes[i], true, false, false, false);
  1358. } else {
  1359. $._tree.expandNode(nodes[i], true, true, false, false);
  1360. }
  1361. $._tree.showNode(nodes[i]);
  1362. $.tree.showAllNode(nodes[i].children);
  1363. }
  1364. },
  1365. // 隐藏所有节点
  1366. hideAllNode: function(nodes) {
  1367. var tree = $.fn.zTree.getZTreeObj("tree");
  1368. var nodes = $._tree.transformToArray(nodes);
  1369. for (var i = nodes.length - 1; i >= 0; i--) {
  1370. $._tree.hideNode(nodes[i]);
  1371. }
  1372. },
  1373. // 显示所有父节点
  1374. showParent: function(treeNode) {
  1375. var parentNode;
  1376. while ((parentNode = treeNode.getParentNode()) != null) {
  1377. $._tree.showNode(parentNode);
  1378. $._tree.expandNode(parentNode, true, false, false);
  1379. treeNode = parentNode;
  1380. }
  1381. },
  1382. // 显示所有孩子节点
  1383. showChildren: function(treeNode) {
  1384. if (treeNode.isParent) {
  1385. for (var idx in treeNode.children) {
  1386. var node = treeNode.children[idx];
  1387. $._tree.showNode(node);
  1388. $.tree.showChildren(node);
  1389. }
  1390. }
  1391. },
  1392. // 更新节点状态
  1393. updateNodes: function(nodeList) {
  1394. $._tree.showNodes(nodeList);
  1395. for (var i = 0, l = nodeList.length; i < l; i++) {
  1396. var treeNode = nodeList[i];
  1397. $.tree.showChildren(treeNode);
  1398. $.tree.showParent(treeNode)
  1399. }
  1400. },
  1401. // 获取当前被勾选集合
  1402. getCheckedNodes: function(column) {
  1403. var _column = $.common.isEmpty(column) ? "id" : column;
  1404. var nodes = $._tree.getCheckedNodes(true);
  1405. return $.map(nodes, function (row) {
  1406. return row[_column];
  1407. }).join();
  1408. },
  1409. // 不允许根父节点选择
  1410. notAllowParents: function(_tree) {
  1411. var nodes = _tree.getSelectedNodes();
  1412. if(nodes.length == 0){
  1413. $.modal.msgError("请选择节点后提交");
  1414. return false;
  1415. }
  1416. for (var i = 0; i < nodes.length; i++) {
  1417. if (nodes[i].level == 0) {
  1418. $.modal.msgError("不能选择根节点(" + nodes[i].name + ")");
  1419. return false;
  1420. }
  1421. if (nodes[i].isParent) {
  1422. $.modal.msgError("不能选择父节点(" + nodes[i].name + ")");
  1423. return false;
  1424. }
  1425. }
  1426. return true;
  1427. },
  1428. // 不允许最后层级节点选择
  1429. notAllowLastLevel: function(_tree) {
  1430. var nodes = _tree.getSelectedNodes();
  1431. for (var i = 0; i < nodes.length; i++) {
  1432. if (!nodes[i].isParent) {
  1433. $.modal.msgError("不能选择最后层级节点(" + nodes[i].name + ")");
  1434. return false;
  1435. }
  1436. }
  1437. return true;
  1438. },
  1439. // 隐藏/显示搜索栏
  1440. toggleSearch: function() {
  1441. $('#search').slideToggle(200);
  1442. $('#btnShow').toggle();
  1443. $('#btnHide').toggle();
  1444. $('#keyword').focus();
  1445. },
  1446. // 折叠
  1447. collapse: function() {
  1448. $._tree.expandAll(false);
  1449. },
  1450. // 展开
  1451. expand: function() {
  1452. $._tree.expandAll(true);
  1453. }
  1454. },
  1455. // 通用方法封装处理
  1456. common: {
  1457. // 判断字符串是否为空
  1458. isEmpty: function (value) {
  1459. if (value == null || this.trim(value) == "") {
  1460. return true;
  1461. }
  1462. return false;
  1463. },
  1464. // 判断一个字符串是否为非空串
  1465. isNotEmpty: function (value) {
  1466. return !$.common.isEmpty(value);
  1467. },
  1468. // 空对象转字符串
  1469. nullToStr: function(value) {
  1470. if ($.common.isEmpty(value)) {
  1471. return "-";
  1472. }
  1473. return value;
  1474. },
  1475. // 是否显示数据 为空默认为显示
  1476. visible: function (value) {
  1477. if ($.common.isEmpty(value) || value == true) {
  1478. return true;
  1479. }
  1480. return false;
  1481. },
  1482. // 空格截取
  1483. trim: function (value) {
  1484. if (value == null) {
  1485. return "";
  1486. }
  1487. return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
  1488. },
  1489. // 比较两个字符串(大小写敏感)
  1490. equals: function (str, that) {
  1491. return str == that;
  1492. },
  1493. // 比较两个字符串(大小写不敏感)
  1494. equalsIgnoreCase: function (str, that) {
  1495. return String(str).toUpperCase() === String(that).toUpperCase();
  1496. },
  1497. // 将字符串按指定字符分割
  1498. split: function (str, sep, maxLen) {
  1499. if ($.common.isEmpty(str)) {
  1500. return null;
  1501. }
  1502. var value = String(str).split(sep);
  1503. return maxLen ? value.slice(0, maxLen - 1) : value;
  1504. },
  1505. // 字符串格式化(%s )
  1506. sprintf: function (str) {
  1507. var args = arguments, flag = true, i = 1;
  1508. str = str.replace(/%s/g, function () {
  1509. var arg = args[i++];
  1510. if (typeof arg === 'undefined') {
  1511. flag = false;
  1512. return '';
  1513. }
  1514. return arg == null ? '' : arg;
  1515. });
  1516. return flag ? str : '';
  1517. },
  1518. // 日期格式化 时间戳 -> yyyy-MM-dd HH-mm-ss
  1519. dateFormat: function(date, format) {
  1520. var that = this;
  1521. if (that.isEmpty(date)) return "";
  1522. if (!date) return;
  1523. if (!format) format = "yyyy-MM-dd";
  1524. switch (typeof date) {
  1525. case "string":
  1526. date = new Date(date.replace(/-/, "/"));
  1527. break;
  1528. case "number":
  1529. date = new Date(date);
  1530. break;
  1531. }
  1532. if (!date instanceof Date) return;
  1533. var dict = {
  1534. "yyyy": date.getFullYear(),
  1535. "M": date.getMonth() + 1,
  1536. "d": date.getDate(),
  1537. "H": date.getHours(),
  1538. "m": date.getMinutes(),
  1539. "s": date.getSeconds(),
  1540. "MM": ("" + (date.getMonth() + 101)).substr(1),
  1541. "dd": ("" + (date.getDate() + 100)).substr(1),
  1542. "HH": ("" + (date.getHours() + 100)).substr(1),
  1543. "mm": ("" + (date.getMinutes() + 100)).substr(1),
  1544. "ss": ("" + (date.getSeconds() + 100)).substr(1)
  1545. };
  1546. return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g,
  1547. function() {
  1548. return dict[arguments[0]];
  1549. });
  1550. },
  1551. // 获取节点数据,支持多层级访问
  1552. getItemField: function (item, field) {
  1553. var value = item;
  1554. if (typeof field !== 'string' || item.hasOwnProperty(field)) {
  1555. return item[field];
  1556. }
  1557. var props = field.split('.');
  1558. for (var p in props) {
  1559. value = value && value[props[p]];
  1560. }
  1561. return value;
  1562. },
  1563. // 指定随机数返回
  1564. random: function (min, max) {
  1565. return Math.floor((Math.random() * max) + min);
  1566. },
  1567. // 判断字符串是否是以start开头
  1568. startWith: function(value, start) {
  1569. var reg = new RegExp("^" + start);
  1570. return reg.test(value)
  1571. },
  1572. // 判断字符串是否是以end结尾
  1573. endWith: function(value, end) {
  1574. var reg = new RegExp(end + "$");
  1575. return reg.test(value)
  1576. },
  1577. // 数组去重
  1578. uniqueFn: function(array) {
  1579. var result = [];
  1580. var hashObj = {};
  1581. for (var i = 0; i < array.length; i++) {
  1582. if (!hashObj[array[i]]) {
  1583. hashObj[array[i]] = true;
  1584. result.push(array[i]);
  1585. }
  1586. }
  1587. return result;
  1588. },
  1589. // 数组中的所有元素放入一个字符串
  1590. join: function(array, separator) {
  1591. if ($.common.isEmpty(array)) {
  1592. return null;
  1593. }
  1594. return array.join(separator);
  1595. },
  1596. // 获取form下所有的字段并转换为json对象
  1597. formToJSON: function(formId) {
  1598. var json = {};
  1599. $.each($("#" + formId).serializeArray(), function(i, field) {
  1600. if(json[field.name]) {
  1601. json[field.name] += ("," + field.value);
  1602. } else {
  1603. json[field.name] = field.value;
  1604. }
  1605. });
  1606. return json;
  1607. },
  1608. // 数据字典转下拉框
  1609. dictToSelect: function(datas, value, name) {
  1610. var actions = [];
  1611. actions.push($.common.sprintf("<select class='form-control' name='%s'>", name));
  1612. $.each(datas, function(index, dict) {
  1613. actions.push($.common.sprintf("<option value='%s'", dict.dictValue));
  1614. if (dict.dictValue == ('' + value)) {
  1615. actions.push(' selected');
  1616. }
  1617. actions.push($.common.sprintf(">%s</option>", dict.dictLabel));
  1618. });
  1619. actions.push('</select>');
  1620. return actions.join('');
  1621. },
  1622. // 获取obj对象长度
  1623. getLength: function(obj) {
  1624. var count = 0;  
  1625. for (var i in obj) {
  1626. if (obj.hasOwnProperty(i)) {
  1627. count++;
  1628. }  
  1629. }
  1630. return count;
  1631. },
  1632. // 判断移动端
  1633. isMobile: function () {
  1634. return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i);
  1635. },
  1636. // 数字正则表达式,只能为0-9数字
  1637. numValid : function(text){
  1638. var patten = new RegExp(/^[0-9]+$/);
  1639. return patten.test(text);
  1640. },
  1641. // 英文正则表达式,只能为a-z和A-Z字母
  1642. enValid : function(text){
  1643. var patten = new RegExp(/^[a-zA-Z]+$/);
  1644. return patten.test(text);
  1645. },
  1646. // 英文、数字正则表达式,必须包含(字母,数字)
  1647. enNumValid : function(text){
  1648. var patten = new RegExp(/^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/);
  1649. return patten.test(text);
  1650. },
  1651. // 英文、数字、特殊字符正则表达式,必须包含(字母,数字,特殊字符!@#$%^&*()-=_+)
  1652. charValid : function(text){
  1653. var patten = new RegExp(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[~!@#\$%\^&\*\(\)\-=_\+])[A-Za-z\d~!@#\$%\^&\*\(\)\-=_\+]{6,}$/);
  1654. return patten.test(text);
  1655. },
  1656. }
  1657. });
  1658. })(jQuery);
  1659. /** 表格类型 */
  1660. table_type = {
  1661. bootstrapTable: 0,
  1662. bootstrapTreeTable: 1
  1663. };
  1664. /** 消息状态码 */
  1665. web_status = {
  1666. SUCCESS: 0,
  1667. FAIL: 500,
  1668. WARNING: 301
  1669. };
  1670. /** 弹窗状态码 */
  1671. modal_status = {
  1672. SUCCESS: "success",
  1673. FAIL: "error",
  1674. WARNING: "warning"
  1675. };