easy-admin.js 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. define(["jquery", "tableSelect", "switchSelect", "miniTheme", "xmSelect", "lazyload"], function ($, tableSelect, switchSelect, miniTheme, xmSelect, lazyload) {
  2. //切换日夜模式
  3. window.onInitElemStyle = function () {
  4. try {
  5. miniTheme.renderElemStyle();
  6. $('iframe').each(function (index, iframe) {
  7. if (typeof iframe.contentWindow.onInitElemStyle == "function") {
  8. iframe.contentWindow.onInitElemStyle();
  9. }
  10. });
  11. miniTheme.changeThemeMainColor();
  12. } catch (e) {
  13. }
  14. };
  15. window.onInitElemStyle();
  16. var form = layui.form,
  17. layer = layui.layer,
  18. table = layui.table,
  19. laydate = layui.laydate,
  20. upload = layui.upload,
  21. element = layui.element,
  22. laytpl = layui.laytpl,
  23. tableSelect = layui.tableSelect,
  24. switchSelect = layui.switchSelect,
  25. util = layui.util;
  26. layer.config({
  27. skin: 'layui-layer-easy'
  28. });
  29. var init = {
  30. table_elem: '#currentTable',
  31. table_render_id: 'currentTableRenderId',
  32. upload_url: 'ajax/upload',
  33. upload_exts: 'doc|gif|ico|icon|jpg|mp3|mp4|p12|pem|png|rar',
  34. csrf_token: window.CONFIG.CSRF_TOKEN,
  35. wait_submit: false,
  36. xmSelectList: {},
  37. xmSelectModel: {},
  38. };
  39. var admin = {
  40. config: {
  41. shade: [0.02, '#000'],
  42. },
  43. url: function (url) {
  44. return '/' + CONFIG.ADMIN + '/' + url;
  45. },
  46. headers: function () {
  47. return {'X-CSRF-TOKEN': init.csrf_token};
  48. },
  49. //js版empty,判断变量是否为空
  50. empty: function (r) {
  51. var n, t, e, f = [void 0, null, !1, 0, "", "0"];
  52. for (t = 0, e = f.length; t < e; t++) if (r === f[t]) return !0;
  53. if ("object" == typeof r) {
  54. for (n in r) if (r.hasOwnProperty(n)) return !1;
  55. return !0
  56. }
  57. return !1
  58. },
  59. checkAuth: function (node, elem) {
  60. if (CONFIG.IS_SUPER_ADMIN) {
  61. return true;
  62. }
  63. if ($(elem).attr('data-auth-' + node) === '1') {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. },
  69. parame: function (param, defaultParam) {
  70. return param !== undefined ? param : defaultParam;
  71. },
  72. request: {
  73. post: function (option, ok, no, ex) {
  74. return admin.request.ajax('post', option, ok, no, ex);
  75. },
  76. get: function (option, ok, no, ex) {
  77. return admin.request.ajax('get', option, ok, no, ex);
  78. },
  79. ajax: function (type, option, ok, no, ex) {
  80. type = type || 'get';
  81. option.url = option.url || '';
  82. option.data = option.data || {};
  83. option.prefix = option.prefix || false;
  84. option.statusName = option.statusName || 'code';
  85. option.statusCode = option.statusCode || 1;
  86. ok = ok || function (res) {
  87. };
  88. no = no || function (res) {
  89. var msg = res.msg == undefined ? '返回数据格式有误' : res.msg;
  90. admin.msg.error(msg);
  91. return false;
  92. };
  93. ex = ex || function (res) {
  94. };
  95. if (option.url == '') {
  96. admin.msg.error('请求地址不能为空');
  97. return false;
  98. }
  99. if (option.prefix == true) {
  100. option.url = admin.url(option.url);
  101. }
  102. var index = admin.msg.loading('加载中');
  103. $.ajax({
  104. url: option.url,
  105. type: type,
  106. contentType: "application/x-www-form-urlencoded; charset=UTF-8",
  107. dataType: "json",
  108. headers: admin.headers(),
  109. data: option.data,
  110. timeout: 60000,
  111. success: function (res) {
  112. admin.msg.close(index);
  113. if (eval('res.' + option.statusName) == option.statusCode) {
  114. return ok(res);
  115. } else {
  116. return no(res);
  117. }
  118. },
  119. error: function (xhr, textstatus, thrown) {
  120. admin.msg.error('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!', function () {
  121. ex(this);
  122. });
  123. return false;
  124. },
  125. complete: function (data) {
  126. // @todo 刷新csrf-token
  127. let token = data.responseJSON ? data.responseJSON.__token__ : ''
  128. init.csrf_token = token
  129. init.wait_submit = false
  130. }
  131. });
  132. }
  133. },
  134. common: {
  135. parseNodeStr: function (node) {
  136. var array = node.split('/');
  137. $.each(array, function (key, val) {
  138. if (key === 0) {
  139. val = val.split('.');
  140. $.each(val, function (i, v) {
  141. val[i] = admin.common.humpToLine(v.replace(v[0], v[0].toLowerCase()));
  142. });
  143. val = val.join(".");
  144. array[key] = val;
  145. }
  146. });
  147. node = array.join("/");
  148. return node;
  149. },
  150. lineToHump: function (name) {
  151. return name.replace(/\_(\w)/g, function (all, letter) {
  152. return letter.toUpperCase();
  153. });
  154. },
  155. humpToLine: function (name) {
  156. return name.replace(/([A-Z])/g, "_$1").toLowerCase();
  157. },
  158. },
  159. msg: {
  160. // 成功消息
  161. success: function (msg, callback) {
  162. if (callback === undefined) {
  163. callback = function () {
  164. }
  165. }
  166. var index = layer.msg(msg, {icon: 1, shade: admin.config.shade, scrollbar: false, time: 2000, shadeClose: true}, callback);
  167. return index;
  168. },
  169. // 失败消息
  170. error: function (msg, callback) {
  171. if (callback === undefined) {
  172. callback = function () {
  173. }
  174. }
  175. var index = layer.msg(msg, {icon: 2, shade: admin.config.shade, scrollbar: false, time: 3000, shadeClose: true}, callback);
  176. return index;
  177. },
  178. // 警告消息框
  179. alert: function (msg, callback) {
  180. var index = layer.alert(msg, {end: callback, scrollbar: false});
  181. return index;
  182. },
  183. // 对话框
  184. confirm: function (msg, ok, no) {
  185. var index = layer.confirm(msg, {title: '操作确认', btn: ['确认', '取消']}, function () {
  186. typeof ok === 'function' && ok.call(this);
  187. }, function () {
  188. typeof no === 'function' && no.call(this);
  189. self.close(index);
  190. });
  191. return index;
  192. },
  193. // 消息提示
  194. tips: function (msg, time, callback) {
  195. var index = layer.msg(msg, {time: (time || 3) * 1000, shade: this.shade, end: callback, shadeClose: true});
  196. return index;
  197. },
  198. // 加载中提示
  199. loading: function (msg, callback) {
  200. var index = msg ? layer.msg(msg, {icon: 16, scrollbar: false, shade: this.shade, time: 0, end: callback}) : layer.load(2, {time: 0, scrollbar: false, shade: this.shade, end: callback});
  201. return index;
  202. },
  203. // 关闭消息框
  204. close: function (index) {
  205. return layer.close(index);
  206. }
  207. },
  208. table: {
  209. render: function (options) {
  210. options.init = options.init || init;
  211. options.modifyReload = admin.parame(options.modifyReload, true);
  212. options.elem = options.elem || options.init.table_elem;
  213. options.id = options.id || options.init.table_render_id;
  214. options.layFilter = options.id + '_LayFilter';
  215. options.url = options.url || admin.url(options.init.index_url);
  216. options.headers = admin.headers();
  217. options.page = admin.parame(options.page, true);
  218. options.search = admin.parame(options.search, true);
  219. options.skin = options.skin || 'line';
  220. options.limit = options.limit || 15;
  221. options.limits = options.limits || [10, 15, 20, 25, 50, 100];
  222. options.cols = options.cols || [];
  223. let searchBtn = options.search ? {
  224. title: '搜索',
  225. layEvent: 'TABLE_SEARCH',
  226. icon: 'layui-icon-search',
  227. extend: 'data-table-id="' + options.id + '"'
  228. } : []
  229. options.defaultToolbar = options.defaultToolbar !== false ? (
  230. (options.defaultToolbar === undefined ? ['filter', 'print', 'exports'].concat(searchBtn) : options.defaultToolbar.concat(searchBtn))
  231. ) : false;
  232. // 判断是否为移动端
  233. if (admin.checkMobile()) {
  234. options.defaultToolbar = options.defaultToolbar !== false ? (
  235. !options.search ? ['filter'] : ['filter', {
  236. title: '搜索',
  237. layEvent: 'TABLE_SEARCH',
  238. icon: 'layui-icon-search',
  239. extend: 'data-table-id="' + options.id + '"'
  240. }]) : false;
  241. }
  242. // 判断元素对象是否有嵌套的
  243. options.cols = admin.table.formatCols(options.cols, options.init);
  244. // 初始化表格lay-filter
  245. $(options.elem).attr('lay-filter', options.layFilter);
  246. // 初始化表格搜索
  247. if (options.search === true) {
  248. admin.table.renderSearch(options.cols, options.elem, options.id);
  249. }
  250. // 初始化表格左上方工具栏
  251. options.toolbar = options.toolbar || ['refresh', 'add', 'delete', 'export', 'recycle'];
  252. options.toolbar = admin.table.renderToolbar(options.toolbar, options.elem, options.id, options.init);
  253. // 判断是否有操作列表权限
  254. options.cols = admin.table.renderOperat(options.cols, options.elem);
  255. // 判断是否有初始默认搜索条件
  256. options.where = {}
  257. $.each(options.cols, function (_, colsV) {
  258. let formatFilter = {}
  259. let formatOp = {}
  260. $.each(colsV, function (i, v) {
  261. if (v.field) {
  262. if (v.searchValue) {
  263. formatFilter[v.field] = v.searchValue
  264. formatOp[v.field] = v.searchOp || '='
  265. options.where['filter'] = JSON.stringify(formatFilter);
  266. options.where['op'] = JSON.stringify(formatOp);
  267. }
  268. }
  269. })
  270. })
  271. // 初始化表格
  272. var newTable = table.render(options);
  273. // 监听表格搜索开关显示
  274. admin.table.listenToolbar(options.layFilter, options.id);
  275. // 监听表格开关切换
  276. admin.table.renderSwitch(options.cols, options.init, options.id, options.modifyReload);
  277. // 监听表格开关切换
  278. admin.table.listenEdit(options.init, options.layFilter, options.id, options.modifyReload);
  279. // 监听表格排序
  280. admin.table.listenSort(options);
  281. return newTable;
  282. },
  283. renderToolbar: function (data, elem, tableId, init) {
  284. data = data || [];
  285. if (typeof data == "object") {
  286. var toolbarHtml = '';
  287. $.each(data, function (i, v) {
  288. if (v === 'refresh') {
  289. toolbarHtml += ' <button class="layui-btn layui-btn-sm layuimini-btn-primary" data-table-refresh="' + tableId + '"><i class="fa fa-refresh"></i> </button>\n';
  290. } else if (v === 'add') {
  291. if (admin.checkAuth('add', elem)) {
  292. toolbarHtml += '<button class="layui-btn layui-btn-normal layui-btn-sm" data-open="' + init.add_url + '" data-title="添加"><i class="fa fa-plus"></i> 添加</button>\n';
  293. }
  294. } else if (v === 'delete') {
  295. if (admin.checkAuth('delete', elem)) {
  296. toolbarHtml += '<button class="layui-btn layui-btn-sm layui-btn-danger" data-url="' + init.delete_url + '" data-table-delete="' + tableId + '"><i class="fa fa-trash"></i> 删除</button>\n';
  297. }
  298. } else if (v === 'export') {
  299. if (admin.checkAuth('export', elem)) {
  300. toolbarHtml += '<button class="layui-btn layui-btn-sm layui-btn-success easyadmin-export-btn" data-url="' + init.export_url + '" data-table-export="' + tableId + '"><i class="fa fa-file-excel"></i> 导出</button>\n';
  301. }
  302. } else if (v === 'recycle') {
  303. if (init.recycle_url === undefined) {
  304. console.warn('未定义回收站地址 init.recycle_url')
  305. return false
  306. }
  307. if (admin.checkAuth('recycle', elem)) {
  308. toolbarHtml += '<button class="layui-btn layui-btn-sm layui-bg-orange" data-open="' + init.recycle_url + '" data-title="回收站"><i class="fa fa-recycle"></i> 回收站</button>\n';
  309. }
  310. } else if (typeof v === "object") {
  311. $.each(v, function (ii, vv) {
  312. vv.class = vv.class || '';
  313. vv.icon = vv.icon || '';
  314. vv.auth = vv.auth || '';
  315. vv.url = vv.url || '';
  316. vv.method = vv.method || 'open';
  317. vv.title = vv.title || vv.text;
  318. vv.text = vv.text || vv.title;
  319. vv.extend = vv.extend || '';
  320. vv.checkbox = vv.checkbox || false;
  321. if (admin.checkAuth(vv.auth, elem)) {
  322. toolbarHtml += admin.table.buildToolbarHtml(vv, tableId);
  323. }
  324. });
  325. }
  326. });
  327. return '<div>' + toolbarHtml + '</div>';
  328. }
  329. return data
  330. },
  331. renderSearch: function (cols, elem, tableId) {
  332. // TODO 只初始化第一个table搜索字段,如果存在多个(绝少数需求),得自己去扩展
  333. cols = cols[0] || {};
  334. var newCols = [];
  335. var formHtml = '';
  336. $.each(cols, function (i, d) {
  337. d.field = d.field || false;
  338. d.fieldAlias = admin.parame(d.fieldAlias, d.field);
  339. d.title = d.title || d.field || '';
  340. d.selectList = d.selectList || {};
  341. d.search = admin.parame(d.search, true);
  342. d.searchTip = d.searchTip || '请输入' + d.title || '';
  343. d.searchValue = d.searchValue || '';
  344. d.laySearch = d.laySearch || false;
  345. d.searchOp = d.searchOp || '%*%';
  346. d.timeType = d.timeType || 'datetime';
  347. if (d.field !== false && d.search !== false) {
  348. switch (d.search) {
  349. case true:
  350. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  351. '<label class="layui-form-label">' + d.title + '</label>\n' +
  352. '<div class="layui-input-inline">\n' +
  353. '<input id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" class="layui-input">\n' +
  354. '</div>\n' +
  355. '</div>';
  356. break;
  357. case 'select':
  358. d.searchOp = '=';
  359. var selectHtml = '';
  360. $.each(d.selectList, function (sI, sV) {
  361. var selected = '';
  362. if (sI == d.searchValue) {
  363. selected = 'selected=""';
  364. }
  365. selectHtml += '<option value="' + sI + '" ' + selected + '>' + sV + '</option>/n';
  366. });
  367. var laySearch = ''
  368. if (true === d.laySearch) {
  369. laySearch = 'lay-search'
  370. }
  371. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  372. '<label class="layui-form-label">' + d.title + '</label>\n' +
  373. '<div class="layui-input-inline">\n' +
  374. '<select class="layui-select" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" ' + laySearch + '>\n' +
  375. '<option value="">- 全部 -</option> \n' +
  376. selectHtml +
  377. '</select>\n' +
  378. '</div>\n' +
  379. '</div>';
  380. break;
  381. case 'xmSelect':
  382. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  383. '<label class="layui-form-label">' + d.title + '</label>\n' +
  384. '<div class="layui-input-inline">\n' +
  385. '<div id="c-' + d.fieldAlias + '" class="tableSearch-xmSelect xmSelect-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" data-search-value="' + d.searchValue + '"></div>\n' +
  386. '</div>\n' +
  387. '</div>';
  388. init.xmSelectList[d.fieldAlias] = d.selectList
  389. break;
  390. case 'range':
  391. d.searchOp = 'range';
  392. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  393. '<label class="layui-form-label">' + d.title + '</label>\n' +
  394. '<div class="layui-input-inline">\n' +
  395. '<input style="width: 275px;font-size: 0.82rem" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" class="layui-input">\n' +
  396. '</div>\n' +
  397. '</div>';
  398. break;
  399. case 'time':
  400. d.searchOp = '=';
  401. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  402. '<label class="layui-form-label">' + d.title + '</label>\n' +
  403. '<div class="layui-input-inline">\n' +
  404. '<input id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" class="layui-input">\n' +
  405. '</div>\n' +
  406. '</div>';
  407. break;
  408. case 'date':
  409. d.searchOp = '=';
  410. formHtml += `<div class="layui-form-item layui-inline">
  411. <label class="layui-form-label">${d.title}</label>
  412. <div class="layui-input-inline">
  413. <input data-date data-date-type="date" id="c-${d.fieldAlias}" name="${d.fieldAlias}" data-search-op="${d.searchOp}" value="${d.searchValue}" placeholder="${d.searchTip}" class="layui-input">
  414. </div>
  415. </div>`
  416. break;
  417. case 'datetime':
  418. // 适用于日期格式:yyyy-MM-dd HH:mm:ss
  419. d.searchOp = 'datetime';
  420. formHtml += '<div class="layui-form-item layui-inline">\n' +
  421. '<label class="layui-form-label">' + d.title + '</label>\n' +
  422. '<div class="layui-input-inline">\n' +
  423. '<input style="width: 275px;font-size: 0.82rem" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" class="layui-input">\n' +
  424. '</div>\n' +
  425. '</div>';
  426. break;
  427. }
  428. newCols.push(d);
  429. }
  430. });
  431. if (formHtml !== '') {
  432. // 默认显示搜索表单
  433. let searchTableShow = $(elem).attr('searchTableShow') || 'true'
  434. // 默认关闭搜索表单自动补全功能
  435. let searchTableAutocomplete = $(elem).attr('searchTableAutocomplete') || 'false'
  436. let tableSearchClass = searchTableShow === 'false' ? 'table-search-fieldset layui-hide' : 'table-search-fieldset'
  437. $(elem).before('<fieldset id="searchFieldset_' + tableId + '" class="' + tableSearchClass + '">\n' +
  438. '<legend>条件搜索</legend>\n' +
  439. '<form class="layui-form layui-form-pane form-search">\n' +
  440. formHtml +
  441. '<div class="layui-form-item layui-inline" style="margin-left: 115px">\n' +
  442. '<button type="submit" class="layui-btn layui-btn-normal" data-type="tableSearch" data-table="' + tableId + '" lay-submit lay-filter="' + tableId + '_filter"> 搜 索</button>\n' +
  443. '<button type="reset" class="layui-btn layui-btn-primary" data-table-reset="' + tableId + '"> 重 置 </button>\n' +
  444. ' </div>' +
  445. '</form>' +
  446. '</fieldset>');
  447. admin.table.listenTableSearch(tableId);
  448. // 初始化form表单
  449. form.set({
  450. // 是否阻止 input 框默认的自动输入完成功能
  451. autocomplete: searchTableAutocomplete == 'false' ? 'off' : 'on'
  452. })
  453. form.render();
  454. $.each(newCols, function (ncI, ncV) {
  455. if (ncV.search === 'range' || ncV.search === 'datetime') {
  456. laydate.render({
  457. range: true, type: ncV.timeType, elem: '[name="' + ncV.fieldAlias + '"]', rangeLinked: true,
  458. shortcuts: getRangeShortcuts()
  459. });
  460. }
  461. if (ncV.search === 'time') {
  462. laydate.render({type: ncV.timeType, elem: '[name="' + ncV.fieldAlias + '"]'});
  463. }
  464. });
  465. }
  466. },
  467. renderSwitch: function (cols, tableInit, tableId, modifyReload) {
  468. tableInit.modify_url = tableInit.modify_url || false;
  469. cols = cols[0] || {};
  470. tableId = tableId || init.table_render_id;
  471. if (cols.length > 0) {
  472. $.each(cols, function (i, v) {
  473. v.filter = v.filter || false;
  474. if (v.filter !== false && tableInit.modify_url !== false) {
  475. admin.table.listenSwitch({filter: v.filter, url: tableInit.modify_url, tableId: tableId, modifyReload: modifyReload});
  476. }
  477. });
  478. }
  479. },
  480. renderOperat(data, elem) {
  481. for (dk in data) {
  482. var col = data[dk];
  483. var operat = col[col.length - 1].operat;
  484. if (operat !== undefined) {
  485. var check = false;
  486. for (key in operat) {
  487. var item = operat[key];
  488. if (typeof item === 'string') {
  489. if (admin.checkAuth(item, elem)) {
  490. check = true;
  491. break;
  492. }
  493. } else {
  494. for (k in item) {
  495. var v = item[k];
  496. if (v.auth !== undefined && admin.checkAuth(v.auth, elem)) {
  497. check = true;
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. if (!check) {
  504. data[dk].pop()
  505. }
  506. }
  507. }
  508. return data;
  509. },
  510. buildToolbarHtml: function (toolbar, tableId) {
  511. var html = '';
  512. toolbar.class = toolbar.class || '';
  513. toolbar.icon = toolbar.icon || '';
  514. toolbar.auth = toolbar.auth || '';
  515. toolbar.url = toolbar.url || '';
  516. toolbar.extend = toolbar.extend || '';
  517. toolbar.method = toolbar.method || 'open';
  518. toolbar.field = toolbar.field || 'id';
  519. toolbar.title = toolbar.title || toolbar.text;
  520. toolbar.text = toolbar.text || toolbar.title;
  521. toolbar.checkbox = toolbar.checkbox || false;
  522. var formatToolbar = toolbar;
  523. formatToolbar.icon = formatToolbar.icon !== '' ? '<i class="' + formatToolbar.icon + '"></i> ' : '';
  524. formatToolbar.class = formatToolbar.class !== '' ? 'class="' + formatToolbar.class + '" ' : '';
  525. if (toolbar.method === 'open') {
  526. formatToolbar.method = formatToolbar.method !== '' ? 'data-open="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
  527. } else if (toolbar.method === 'none') { // 常用于与extend配合,自定义监听按钮
  528. formatToolbar.method = '';
  529. } else {
  530. formatToolbar.method = formatToolbar.method !== '' ? 'data-request="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
  531. }
  532. formatToolbar.checkbox = toolbar.checkbox ? ' data-checkbox="true" ' : '';
  533. formatToolbar.tableId = tableId !== undefined ? ' data-table="' + tableId + '" ' : '';
  534. html = '<button ' + formatToolbar.class + formatToolbar.method + formatToolbar.extend + formatToolbar.checkbox + formatToolbar.tableId + '>' + formatToolbar.icon + formatToolbar.text + '</button>';
  535. return html;
  536. },
  537. buildOperatHtml: function (operat, data) {
  538. var html = '';
  539. operat.class = operat.class || '';
  540. operat.icon = operat.icon || '';
  541. operat.auth = operat.auth || '';
  542. operat.url = operat.url || '';
  543. operat.extend = operat.extend || '';
  544. operat.method = operat.method || 'open';
  545. operat.field = operat.field || 'id';
  546. operat.title = operat.title || operat.text;
  547. operat.text = operat.text || operat.title;
  548. operat.visible = typeof operat.visible !== 'undefined' ? operat.visible : true;
  549. var formatOperat = operat;
  550. formatOperat.icon = formatOperat.icon !== '' ? '<i class="' + formatOperat.icon + '"></i> ' : '';
  551. formatOperat.class = formatOperat.class !== '' ? 'class="' + formatOperat.class + '" ' : '';
  552. if (operat.method === 'open') {
  553. formatOperat.method = formatOperat.method !== '' ? 'data-open="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
  554. } else if (operat.method === 'none') { // 常用于与extend配合,自定义监听按钮
  555. formatOperat.method = '';
  556. } else {
  557. formatOperat.method = formatOperat.method !== '' ? 'data-request="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
  558. }
  559. html = '<a ' + formatOperat.class + formatOperat.method + formatOperat.extend + '>' + formatOperat.icon + formatOperat.text + '</a>';
  560. if ('function' === typeof formatOperat.visible) {
  561. let visible = formatOperat.visible(data);
  562. if (typeof visible === 'boolean') {
  563. if (!visible) html = ''
  564. }
  565. } else {
  566. if (typeof formatOperat.visible === 'boolean') {
  567. if (!formatOperat.visible) html = ''
  568. }
  569. }
  570. return html;
  571. },
  572. toolSpliceUrl(url, field, data) {
  573. url = url.indexOf("?") !== -1 ? url + '&' + field + '=' + data[field] : url + '?' + field + '=' + data[field];
  574. return url;
  575. },
  576. formatCols: function (cols, init) {
  577. for (i in cols) {
  578. var col = cols[i];
  579. for (index in col) {
  580. var val = col[index];
  581. // 判断是否包含初始化数据
  582. if (val.init === undefined) {
  583. cols[i][index]['init'] = init;
  584. }
  585. // 格式化列操作栏
  586. if (val.templet === admin.table.tool && val.operat === undefined) {
  587. cols[i][index]['operat'] = ['edit', 'delete'];
  588. }
  589. // 判断是否包含开关组件
  590. if (val.templet === admin.table.switch && val.filter === undefined) {
  591. cols[i][index]['filter'] = val.field;
  592. }
  593. // 判断是否含有搜索下拉列表
  594. if (val.selectList !== undefined && val.search === undefined) {
  595. cols[i][index]['search'] = 'select';
  596. }
  597. // 判断是否初始化对齐方式
  598. if (val.align === undefined) {
  599. cols[i][index]['align'] = 'center';
  600. }
  601. // 部分字段开启排序
  602. var sortDefaultFields = ['id', 'sort'];
  603. if (val.sort === undefined && sortDefaultFields.indexOf(val.field) >= 0) {
  604. cols[i][index]['sort'] = true;
  605. }
  606. // 初始化图片高度
  607. if (val.templet === admin.table.image && val.imageHeight === undefined) {
  608. cols[i][index]['imageHeight'] = 40;
  609. }
  610. // 判断是否多层对象
  611. if (val.field !== undefined && val.field.split(".").length > 1) {
  612. if (val.templet === undefined) {
  613. cols[i][index]['templet'] = admin.table.value;
  614. }
  615. }
  616. // 判断是否列表数据转换
  617. if (val.selectList !== undefined && val.templet === undefined) {
  618. cols[i][index]['templet'] = admin.table.list;
  619. }
  620. }
  621. }
  622. return cols;
  623. },
  624. tool: function (data, option) {
  625. var option = data.LAY_COL || {};
  626. option.operat = option.operat || ['edit', 'delete'];
  627. var elem = option.init.table_elem || init.table_elem;
  628. var html = '';
  629. $.each(option.operat, function (i, item) {
  630. if (typeof item === 'string') {
  631. switch (item) {
  632. case 'edit':
  633. var operat = {
  634. class: 'layui-btn layui-btn-success layui-btn-xs',
  635. method: 'open',
  636. field: 'id',
  637. icon: '',
  638. text: '编辑',
  639. title: '编辑信息',
  640. auth: 'edit',
  641. url: option.init.edit_url,
  642. extend: ""
  643. };
  644. operat.url = admin.table.toolSpliceUrl(operat.url, operat.field, data);
  645. if (admin.checkAuth(operat.auth, elem)) {
  646. html += admin.table.buildOperatHtml(operat, data);
  647. }
  648. break;
  649. case 'delete':
  650. var operat = {
  651. class: 'layui-btn layui-btn-danger layui-btn-xs',
  652. method: 'get',
  653. field: 'id',
  654. icon: '',
  655. text: '删除',
  656. title: '确定删除?',
  657. auth: 'delete',
  658. url: option.init.delete_url,
  659. extend: ""
  660. };
  661. operat.url = admin.table.toolSpliceUrl(operat.url, operat.field, data);
  662. if (admin.checkAuth(operat.auth, elem)) {
  663. html += admin.table.buildOperatHtml(operat, data);
  664. }
  665. break;
  666. }
  667. } else if (typeof item === 'object') {
  668. $.each(item, function (i, operat) {
  669. if (typeof operat !== 'object') return
  670. if ('function' === typeof operat.templet) {
  671. html += operat.templet(data);
  672. return true;
  673. }
  674. operat.class = operat.class || '';
  675. operat.icon = operat.icon || '';
  676. operat.auth = operat.auth || '';
  677. operat.url = operat.url || '';
  678. operat.method = operat.method || 'open';
  679. operat.field = operat.field || 'id';
  680. operat.title = operat.title || operat.text;
  681. operat.text = operat.text || operat.title;
  682. operat.extend = operat.extend || '';
  683. // 自定义表格opreat按钮的弹窗标题风格,extra是表格里的欲加入标题中的字段
  684. operat.extra = operat.extra || '';
  685. if (data[operat.extra] !== undefined) {
  686. operat.title = data[operat.extra] + ' - ' + operat.title;
  687. }
  688. operat.url = admin.table.toolSpliceUrl(operat.url, operat.field, data);
  689. if (admin.checkAuth(operat.auth, elem)) {
  690. html += admin.table.buildOperatHtml(operat, data);
  691. }
  692. });
  693. }
  694. });
  695. return html;
  696. },
  697. list: function (data, option) {
  698. var option = data.LAY_COL || {};
  699. option.selectList = option.selectList || {};
  700. try {
  701. var value = admin.table.defaultValue(data);
  702. } catch (e) {
  703. var value = undefined;
  704. }
  705. if (option.selectList[value] === undefined || option.selectList[value] === '' || option.selectList[value] === null) {
  706. return value;
  707. } else {
  708. return option.selectList[value];
  709. }
  710. },
  711. image: function (data, option) {
  712. var option = data.LAY_COL || {};
  713. option.imageWidth = option.imageWidth || 200;
  714. option.imageHeight = option.imageHeight || 40;
  715. option.imageSplit = option.imageSplit || '|';
  716. option.imageJoin = option.imageJoin || '<br>';
  717. option.title = option.title || option.field;
  718. var field = option.field,
  719. title = data[option.title];
  720. try {
  721. var value = admin.table.defaultValue(data);
  722. } catch (e) {
  723. var value = undefined;
  724. }
  725. if (value === undefined || value === null) {
  726. return '<img style="max-width: ' + option.imageWidth + 'px; max-height: ' + option.imageHeight + 'px;" src="' + value + '" data-image="' + title + '">';
  727. } else {
  728. var values = value.split(option.imageSplit),
  729. valuesHtml = [];
  730. values.forEach((value, index) => {
  731. valuesHtml.push('<img style="max-width: ' + option.imageWidth + 'px; max-height: ' + option.imageHeight + 'px;" class="lazyload" src="/static/common/images/loading.gif" data-src="' + value + '" data-image="' + title + '">');
  732. });
  733. $(function () {
  734. $("img.lazyload").lazyload({threshold: 1});
  735. })
  736. return valuesHtml.join(option.imageJoin);
  737. }
  738. },
  739. url: function (data, option) {
  740. var option = data.LAY_COL || {};
  741. try {
  742. var value = admin.table.defaultValue(data);
  743. } catch (e) {
  744. var value = undefined;
  745. }
  746. return '<a class="layuimini-table-url" href="' + value + '" target="_blank" class="label bg-green">' + value + '</a>';
  747. },
  748. switch: function (data, option) {
  749. var option = data.LAY_COL || {};
  750. option.filter = option.filter || option.field || null;
  751. option.checked = option.checked || 1;
  752. option.tips = option.tips || '开|关';
  753. try {
  754. var value = admin.table.defaultValue(data);
  755. } catch (e) {
  756. var value = undefined;
  757. }
  758. var checked = value === option.checked ? 'checked' : '';
  759. return laytpl('<input type="checkbox" name="' + option.field + '" value="' + data.id + '" lay-skin="switch" lay-text="' + option.tips + '" lay-filter="' + option.filter + '" ' + checked + ' >').render(data);
  760. },
  761. price: function (data, option) {
  762. var option = data.LAY_COL || {};
  763. try {
  764. var value = admin.table.defaultValue(data);
  765. } catch (e) {
  766. var value = undefined;
  767. }
  768. return '<span>¥' + value + '</span>';
  769. },
  770. percent: function (data, option) {
  771. var option = data.LAY_COL || {};
  772. try {
  773. var value = admin.table.defaultValue(data);
  774. } catch (e) {
  775. var value = undefined;
  776. }
  777. return '<span>' + value + '%</span>';
  778. },
  779. icon: function (data, option) {
  780. var option = data.LAY_COL || {};
  781. try {
  782. var value = admin.table.defaultValue(data);
  783. } catch (e) {
  784. var value = undefined;
  785. }
  786. return '<i class="' + value + '"></i>';
  787. },
  788. text: function (data, option) {
  789. var option = data.LAY_COL || {};
  790. try {
  791. var value = admin.table.defaultValue(data);
  792. } catch (e) {
  793. var value = undefined;
  794. }
  795. return '<span class="line-limit-length">' + value + '</span>';
  796. },
  797. value: function (data, option) {
  798. var option = data.LAY_COL || {};
  799. try {
  800. var value = admin.table.defaultValue(data);
  801. } catch (e) {
  802. var value = undefined;
  803. }
  804. return '<span>' + value + '</span>';
  805. },
  806. //时间戳转日期
  807. date: function (data, option) {
  808. var option = data.LAY_COL || {};
  809. var field = option.field, value = '';
  810. try {
  811. value = eval("data." + field);
  812. } catch (e) {
  813. }
  814. if (!admin.empty(value)) {
  815. value = util.toDateString(value * 1000, option.format || 'yyyy-MM-dd HH:mm:ss');
  816. }
  817. return '<span>' + value + '</span>';
  818. },
  819. // 统一列返回数据处理
  820. defaultValue(data, field, _value) {
  821. if (!data.LAY_COL) {
  822. return '';
  823. }
  824. var option = data.LAY_COL || {};
  825. field = field || option.field;
  826. _value = _value || option.defaultValue;
  827. var valueParser = option.valueParser;
  828. var value = _value;
  829. try {
  830. value = eval("data." + field);
  831. } catch (e) {
  832. value = undefined;
  833. }
  834. if (_value != undefined && admin.empty(value)) {
  835. value = defaultValue;
  836. }
  837. if (typeof valueParser == 'function') {
  838. value = valueParser(value, data);
  839. }
  840. return value;
  841. },
  842. listenTableSearch: function (tableId) {
  843. if (Object.keys(init.xmSelectList).length > 0) {
  844. $.each(init.xmSelectList, function (index, value) {
  845. let xmSearchValue = $('#c-' + index).data('search-value') || [];
  846. if (!Array.isArray(xmSearchValue)) xmSearchValue = (xmSearchValue.toString()).split(',')
  847. const keysArray = Object.keys(value).map((key) => {
  848. return {name: value[key], value: key, selected: xmSearchValue.indexOf(key) !== -1}
  849. })
  850. init.xmSelectModel[index] = xmSelect.render({
  851. el: '.xmSelect-' + index, language: 'zn', data: keysArray, name: index,
  852. filterable: true, paging: true, pageSize: 10, toolbar: {show: true},
  853. theme: {color: getComputedStyle(document.documentElement).getPropertyValue('--ea8-theme-main-color') || '#16b777'}
  854. })
  855. })
  856. }
  857. form.on('submit(' + tableId + '_filter)', function (data) {
  858. var dataField = data.field;
  859. var formatFilter = {},
  860. formatOp = {};
  861. $.each(dataField, function (key, val) {
  862. if (val !== '') {
  863. formatFilter[key] = val;
  864. var op = $('#c-' + key).attr('data-search-op');
  865. op = op || '%*%';
  866. formatOp[key] = op;
  867. }
  868. });
  869. table.reload(tableId, {
  870. page: {
  871. curr: 1
  872. }
  873. , where: {
  874. filter: JSON.stringify(formatFilter),
  875. op: JSON.stringify(formatOp)
  876. }
  877. }, 'data');
  878. return false;
  879. });
  880. },
  881. listenSwitch: function (option, ok) {
  882. option.filter = option.filter || '';
  883. option.url = option.url || '';
  884. option.field = option.field || option.filter || '';
  885. option.tableId = option.tableId || init.table_render_id;
  886. option.modifyReload = option.modifyReload || false;
  887. form.on('switch(' + option.filter + ')', function (obj) {
  888. var checked = obj.elem.checked ? 1 : 0;
  889. if (typeof ok === 'function') {
  890. return ok({
  891. id: obj.value,
  892. checked: checked,
  893. });
  894. } else {
  895. var data = {
  896. id: obj.value,
  897. field: option.field,
  898. value: checked,
  899. };
  900. admin.request.post({
  901. url: option.url,
  902. prefix: true,
  903. data: data,
  904. }, function (res) {
  905. if (option.modifyReload) {
  906. table.reload(option.tableId);
  907. }
  908. }, function (res) {
  909. admin.msg.error(res.msg, function () {
  910. table.reload(option.tableId);
  911. });
  912. }, function () {
  913. table.reload(option.tableId);
  914. });
  915. }
  916. });
  917. },
  918. listenToolbar: function (layFilter, tableId) {
  919. table.on('toolbar(' + layFilter + ')', function (obj) {
  920. // 搜索表单的显示
  921. switch (obj.event) {
  922. case 'TABLE_SEARCH':
  923. var searchFieldsetId = 'searchFieldset_' + tableId;
  924. var _that = $("#" + searchFieldsetId);
  925. if (_that.hasClass("layui-hide")) {
  926. _that.removeClass('layui-hide');
  927. } else {
  928. _that.addClass('layui-hide');
  929. }
  930. break;
  931. }
  932. });
  933. },
  934. listenEdit: function (tableInit, layFilter, tableId, modifyReload) {
  935. tableInit.modify_url = tableInit.modify_url || false;
  936. tableId = tableId || init.table_render_id;
  937. if (tableInit.modify_url !== false) {
  938. table.on('edit(' + layFilter + ')', function (obj) {
  939. var value = obj.value,
  940. data = obj.data,
  941. id = data.id,
  942. field = obj.field;
  943. var _data = {
  944. id: id,
  945. field: field,
  946. value: value,
  947. };
  948. admin.request.post({
  949. url: tableInit.modify_url,
  950. prefix: true,
  951. data: _data,
  952. }, function (res) {
  953. if (modifyReload) {
  954. table.reload(tableId);
  955. }
  956. }, function (res) {
  957. admin.msg.error(res.msg, function () {
  958. table.reload(tableId);
  959. });
  960. }, function () {
  961. table.reload(tableId);
  962. });
  963. });
  964. }
  965. },
  966. listenSort: function (options) {
  967. table.on('sort(' + options.layFilter + ')', function (obj) {
  968. let defaultWhere = {}
  969. $.each(options.cols, function (_, colsV) {
  970. let formatFilter = {}
  971. let formatOp = {}
  972. $.each(colsV, function (i, v) {
  973. if (v.field) {
  974. if ($('#c-' + v.field).val()) {
  975. formatFilter[v.field] = $('#c-' + v.field).val()
  976. formatOp[v.field] = v.searchOp || '='
  977. defaultWhere['filter'] = JSON.stringify(formatFilter);
  978. defaultWhere['op'] = JSON.stringify(formatOp);
  979. }
  980. }
  981. })
  982. })
  983. let sortWhere = {tableOrder: obj.field + ' ' + obj.type}
  984. table.reload(options.id, {
  985. where: {...defaultWhere, ...sortWhere}
  986. });
  987. });
  988. }
  989. },
  990. checkMobile: function () {
  991. var userAgentInfo = navigator.userAgent;
  992. var mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  993. var mobile_flag = false;
  994. //根据userAgent判断是否是手机
  995. for (var v = 0; v < mobileAgents.length; v++) {
  996. if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
  997. mobile_flag = true;
  998. break;
  999. }
  1000. }
  1001. var screen_width = window.screen.width;
  1002. var screen_height = window.screen.height;
  1003. //根据屏幕分辨率判断是否是手机
  1004. if (screen_width < 600 && screen_height < 800) {
  1005. mobile_flag = true;
  1006. }
  1007. return mobile_flag;
  1008. },
  1009. open: function (title, url, width, height, isResize, shadeClose = false) {
  1010. isResize = isResize === undefined ? true : isResize;
  1011. var index = layer.open({
  1012. title: title,
  1013. type: 2,
  1014. area: [width, height],
  1015. content: url,
  1016. maxmin: true,
  1017. anim: 0,
  1018. moveOut: true,
  1019. shade: 0.3,
  1020. shadeClose: shadeClose,
  1021. scrollbar: false,
  1022. before: function () {
  1023. },
  1024. success: function (layero, index) {
  1025. if (window.CONFIG.IFRAME_OPEN_TOP == '1') {
  1026. let iframeUrl = ``
  1027. try {
  1028. let iframeId = $('iframe:eq(0)').attr('id')
  1029. let iframe = document.getElementById(iframeId)
  1030. iframeUrl = iframe.contentWindow.location.href
  1031. } catch (e) {
  1032. iframeUrl = location.href
  1033. }
  1034. let _winTopBtn = `
  1035. <span class="_winTopBtn layui-btn layui-btn-primary layui-btn-xs"
  1036. style="position: absolute;top: 14px;right: 120px;color: #fff;border-color: #fff;" onclick="window.open('${iframeUrl}')">
  1037. 新标签页打开
  1038. </span>`
  1039. $('.layui-layer-iframe').append(_winTopBtn)
  1040. }
  1041. },
  1042. end: function () {
  1043. index = null
  1044. }
  1045. });
  1046. if (admin.checkMobile()) {
  1047. layer.full(index);
  1048. }
  1049. if (isResize) {
  1050. $(window).on("resize", function () {
  1051. index && layer.full(index);
  1052. })
  1053. }
  1054. },
  1055. listen: function (preposeCallback, ok, no, ex) {
  1056. // 监听表单是否为必填项
  1057. admin.api.formRequired();
  1058. // 监听表单提交事件
  1059. admin.api.formSubmit(preposeCallback, ok, no, ex);
  1060. // 初始化图片显示以及监听上传事件
  1061. admin.api.upload();
  1062. // 监听富文本初始化
  1063. admin.api.editor();
  1064. // 监听下拉选择生成
  1065. admin.api.select();
  1066. // 监听时间控件生成
  1067. admin.api.date();
  1068. form.render();
  1069. // 表格修改
  1070. $("body").on("mouseenter", ".table-edit-tips", function () {
  1071. var openTips = layer.tips('点击行内容可以进行修改', $(this), {tips: [2, '#e74c3c'], time: 4000});
  1072. });
  1073. // 监听弹出层的打开
  1074. $('body').on('click', '[data-open]', function () {
  1075. var clienWidth = $(this).attr('data-width'),
  1076. clientHeight = $(this).attr('data-height'),
  1077. dataFull = $(this).attr('data-full'),
  1078. checkbox = $(this).attr('data-checkbox'),
  1079. url = $(this).attr('data-open'),
  1080. external = $(this).attr('data-external') || false,
  1081. tableId = $(this).attr('data-table');
  1082. if (checkbox === 'true') {
  1083. tableId = tableId || init.table_render_id;
  1084. var checkStatus = table.checkStatus(tableId),
  1085. data = checkStatus.data;
  1086. if (data.length <= 0) {
  1087. admin.msg.error('请勾选需要操作的数据');
  1088. return false;
  1089. }
  1090. var ids = [];
  1091. $.each(data, function (i, v) {
  1092. ids.push(v.id);
  1093. });
  1094. if (url.indexOf("?") === -1) {
  1095. url += '?id=' + ids.join(',');
  1096. } else {
  1097. url += '&id=' + ids.join(',');
  1098. }
  1099. }
  1100. if (clienWidth === undefined || clientHeight === undefined) {
  1101. clienWidth = '65%';
  1102. clientHeight = '85%';
  1103. }
  1104. if (dataFull === 'true') {
  1105. clienWidth = '100%';
  1106. clientHeight = '100%';
  1107. }
  1108. admin.open(
  1109. $(this).attr('data-title'),
  1110. external ? url : admin.url(url),
  1111. clienWidth,
  1112. clientHeight,
  1113. );
  1114. });
  1115. // 放大图片
  1116. $('body').on('click', '[data-image]', function () {
  1117. var title = $(this).attr('data-image'),
  1118. src = $(this).attr('src'),
  1119. alt = $(this).attr('alt');
  1120. var photos = {
  1121. "title": title,
  1122. "id": Math.random(),
  1123. "data": [
  1124. {
  1125. "alt": alt,
  1126. "pid": Math.random(),
  1127. "src": src,
  1128. "thumb": src
  1129. }
  1130. ]
  1131. };
  1132. layer.photos({
  1133. photos: photos,
  1134. anim: 5
  1135. });
  1136. return false;
  1137. });
  1138. // 放大一组图片
  1139. $('body').on('click', '[data-images]', function () {
  1140. var title = $(this).attr('data-images'),
  1141. // 从当前元素向上找layuimini-upload-show找到第一个后停止, 再找其所有子元素li
  1142. doms = $(this).closest(".layuimini-upload-show").children("li"),
  1143. // 被点击的图片地址
  1144. now_src = $(this).attr('src'),
  1145. alt = $(this).attr('alt'),
  1146. data = [];
  1147. $.each(doms, function (key, value) {
  1148. var src = $(value).find('img').attr('src');
  1149. if (src != now_src) {
  1150. // 压入其他图片地址
  1151. data.push({
  1152. "alt": alt,
  1153. "pid": Math.random(),
  1154. "src": src,
  1155. "thumb": src
  1156. });
  1157. } else {
  1158. // 把当前图片插入到头部
  1159. data.unshift({
  1160. "alt": alt,
  1161. "pid": Math.random(),
  1162. "src": now_src,
  1163. "thumb": now_src
  1164. });
  1165. }
  1166. });
  1167. var photos = {
  1168. "title": title,
  1169. "id": Math.random(),
  1170. "data": data,
  1171. };
  1172. layer.photos({
  1173. photos: photos,
  1174. anim: 5
  1175. });
  1176. return false;
  1177. });
  1178. // 监听动态表格刷新
  1179. $('body').on('click', '[data-table-refresh]', function () {
  1180. var tableId = $(this).attr('data-table-refresh');
  1181. if (tableId === undefined || tableId === '' || tableId == null) {
  1182. tableId = init.table_render_id;
  1183. }
  1184. table.reload(tableId);
  1185. });
  1186. // 监听搜索表格重置
  1187. $('body').on('click', '[data-table-reset]', function () {
  1188. let tableId = $(this).attr('data-table-reset');
  1189. if (tableId === undefined || tableId === '' || tableId == null) {
  1190. tableId = init.table_render_id;
  1191. }
  1192. let cols = table.getOptions(tableId)?.cols || {}
  1193. let defaultWhere = {}
  1194. $.each(cols, function (_, colsV) {
  1195. let formatFilter = {}
  1196. let formatOp = {}
  1197. $.each(colsV, function (i, v) {
  1198. if (v.field) {
  1199. if (v.searchValue) {
  1200. formatFilter[v.field] = v.searchValue
  1201. formatOp[v.field] = v.searchOp || '='
  1202. defaultWhere['filter'] = JSON.stringify(formatFilter);
  1203. defaultWhere['op'] = JSON.stringify(formatOp);
  1204. }
  1205. }
  1206. })
  1207. })
  1208. if (Object.keys(init.xmSelectModel).length > 0) {
  1209. $.each(init.xmSelectModel, function (index, value) {
  1210. init.xmSelectModel[index].setValue([])
  1211. })
  1212. }
  1213. table.reload(tableId, {
  1214. page: {curr: 1}, where: {...defaultWhere}
  1215. }, 'data');
  1216. });
  1217. // 监听请求
  1218. $('body').on('click', '[data-request]', function () {
  1219. var title = $(this).attr('data-title'),
  1220. url = $(this).attr('data-request'),
  1221. tableId = $(this).attr('data-table'),
  1222. addons = $(this).attr('data-addons'),
  1223. checkbox = $(this).attr('data-checkbox'),
  1224. direct = $(this).attr('data-direct'),
  1225. field = $(this).attr('data-field') || 'id';
  1226. title = title || '确定进行该操作?';
  1227. if (direct === 'true') {
  1228. admin.msg.confirm(title, function () {
  1229. window.location.href = url;
  1230. });
  1231. return false;
  1232. }
  1233. var postData = {};
  1234. if (checkbox === 'true') {
  1235. tableId = tableId || init.table_render_id;
  1236. var checkStatus = table.checkStatus(tableId),
  1237. data = checkStatus.data;
  1238. if (data.length <= 0) {
  1239. admin.msg.error('请勾选需要操作的数据');
  1240. return false;
  1241. }
  1242. var ids = [];
  1243. $.each(data, function (i, v) {
  1244. ids.push(v[field]);
  1245. });
  1246. postData[field] = ids;
  1247. }
  1248. if (addons !== true && addons !== 'true') {
  1249. url = admin.url(url);
  1250. }
  1251. tableId = tableId || init.table_render_id;
  1252. admin.msg.confirm(title, function () {
  1253. admin.request.post({
  1254. url: url,
  1255. data: postData,
  1256. }, function (res) {
  1257. admin.msg.success(res.msg, function () {
  1258. table.reload(tableId);
  1259. $('[data-treetable-refresh]').trigger("click");
  1260. });
  1261. })
  1262. });
  1263. return false;
  1264. });
  1265. // excel导出
  1266. $('body').on('click', '[data-table-export]', function () {
  1267. var tableId = $(this).attr('data-table-export'),
  1268. url = $(this).attr('data-url');
  1269. let par = $("#searchFieldset_" + tableId).find('form').serialize();
  1270. let parArr = par.split('&')
  1271. var formatFilter = {}, formatOp = {};
  1272. [formatData] = parArr.map((arr) => {
  1273. [key, val] = arr.split('=');
  1274. if (val !== '') {
  1275. formatFilter[key] = val;
  1276. var op = $('#c-' + key).attr('data-search-op');
  1277. op = op || '%*%';
  1278. formatOp[key] = op;
  1279. }
  1280. return {formatFilter, formatOp};
  1281. })
  1282. let schPar = 'filter=' + JSON.stringify(formatData.formatFilter) + '&' + 'op=' + JSON.stringify(formatData.formatOp);
  1283. url = (url.includes('?')) ? url + '&' + schPar : url + '?' + schPar;
  1284. var index = admin.msg.confirm('根据查询进行导出,确定导出?', function () {
  1285. window.location = admin.url(url);
  1286. layer.close(index);
  1287. });
  1288. });
  1289. // 数据表格多删除
  1290. $('body').on('click', '[data-table-delete]', function () {
  1291. var tableId = $(this).attr('data-table-delete'),
  1292. url = $(this).attr('data-url');
  1293. tableId = tableId || init.table_render_id;
  1294. url = url !== undefined ? admin.url(url) : window.location.href;
  1295. var checkStatus = table.checkStatus(tableId),
  1296. data = checkStatus.data;
  1297. if (data.length <= 0) {
  1298. admin.msg.error('请勾选需要删除的数据');
  1299. return false;
  1300. }
  1301. var ids = [];
  1302. $.each(data, function (i, v) {
  1303. ids.push(v.id);
  1304. });
  1305. admin.msg.confirm('确定删除?', function () {
  1306. admin.request.post({
  1307. url: url,
  1308. data: {
  1309. id: ids
  1310. },
  1311. }, function (res) {
  1312. admin.msg.success(res.msg, function () {
  1313. table.reload(tableId);
  1314. });
  1315. });
  1316. });
  1317. return false;
  1318. });
  1319. },
  1320. api: {
  1321. form: function (url, data, ok, no, ex, refreshTable) {
  1322. if (refreshTable === undefined) {
  1323. refreshTable = true;
  1324. }
  1325. ok = ok || function (res) {
  1326. res.msg = res.msg || '';
  1327. admin.msg.success(res.msg, function () {
  1328. admin.api.closeCurrentOpen({
  1329. refreshTable: refreshTable
  1330. });
  1331. });
  1332. return false;
  1333. };
  1334. admin.request.post({
  1335. url: url,
  1336. data: data,
  1337. }, ok, no, ex);
  1338. return false;
  1339. },
  1340. closeCurrentOpen: function (option) {
  1341. option = option || {};
  1342. option.refreshTable = option.refreshTable || false;
  1343. option.refreshFrame = option.refreshFrame || false;
  1344. if (option.refreshTable === true) {
  1345. option.refreshTable = init.table_render_id;
  1346. }
  1347. var index = parent.layer.getFrameIndex(window.name);
  1348. parent.layer.close(index);
  1349. if (option.refreshTable !== false) {
  1350. parent.layui.table.reload(option.refreshTable);
  1351. }
  1352. if (option.refreshFrame) {
  1353. parent.location.reload();
  1354. }
  1355. return false;
  1356. },
  1357. refreshFrame: function () {
  1358. parent.location.reload();
  1359. return false;
  1360. },
  1361. refreshTable: function (tableName) {
  1362. tableName = tableName || 'currentTable';
  1363. table.reload(tableName);
  1364. },
  1365. formRequired: function () {
  1366. var verifyList = document.querySelectorAll("[lay-verify]");
  1367. if (verifyList.length > 0) {
  1368. $.each(verifyList, function (i, v) {
  1369. var verify = $(this).attr('lay-verify');
  1370. // todo 必填项处理
  1371. if (verify.includes('required')) {
  1372. var label = $(this).parent().prev();
  1373. if (label.is('label') && !label.hasClass('required')) {
  1374. label.addClass('required');
  1375. }
  1376. if ($(this).attr('lay-reqtext') === undefined && $(this).attr('placeholder') !== undefined) {
  1377. $(this).attr('lay-reqtext', $(this).attr('placeholder'));
  1378. }
  1379. if ($(this).attr('placeholder') === undefined && $(this).attr('lay-reqtext') !== undefined) {
  1380. $(this).attr('placeholder', $(this).attr('lay-reqtext'));
  1381. }
  1382. }
  1383. });
  1384. }
  1385. },
  1386. formSubmit: function (preposeCallback, ok, no, ex) {
  1387. var formList = document.querySelectorAll("[lay-submit]");
  1388. // 表单提交自动处理
  1389. if (formList.length > 0) {
  1390. $.each(formList, function (i, v) {
  1391. var filter = $(this).attr('lay-filter'),
  1392. type = $(this).attr('data-type'),
  1393. refresh = $(this).attr('data-refresh'),
  1394. url = $(this).attr('lay-submit');
  1395. // 表格搜索不做自动提交
  1396. if (type === 'tableSearch') {
  1397. return false;
  1398. }
  1399. // 判断是否需要刷新表格
  1400. if (refresh === 'false') {
  1401. refresh = false;
  1402. } else {
  1403. refresh = true;
  1404. }
  1405. // 自动添加layui事件过滤器
  1406. if (filter === undefined || filter === '') {
  1407. filter = 'save_form_' + (i + 1);
  1408. $(this).attr('lay-filter', filter)
  1409. }
  1410. if (url === undefined || url === '' || url === null) {
  1411. url = window.location.href;
  1412. } else {
  1413. url = admin.url(url);
  1414. }
  1415. form.on('submit(' + filter + ')', function (data) {
  1416. if (init.wait_submit) {
  1417. layer.msg('你点击太快了', {icon: 16, shade: 0.3, shadeClose: false, time: 1000})
  1418. return false
  1419. }
  1420. var dataField = data.field;
  1421. var editorList = document.querySelectorAll(".editor");
  1422. // 富文本数据处理
  1423. if (editorList.length > 0) {
  1424. $.each(editorList, function (i, v) {
  1425. switch (window.CONFIG.EDITOR_TYPE) {
  1426. case 'ckeditor':
  1427. var name = $(this).attr("name");
  1428. dataField[name] = CKEDITOR.instances[name].getData();
  1429. break;
  1430. case 'wangEditor':
  1431. var name = $(this).attr("name");
  1432. dataField[name] = (window["wangEditor_" + i]).getHtml()
  1433. break;
  1434. case 'EasyMDE':
  1435. var name = $(this).attr("name");
  1436. dataField[name] = (window["easyMDE" + i]).value()
  1437. break;
  1438. default:
  1439. var name = $(this).attr("id");
  1440. dataField[name] = UE.getEditor(name).getContent();
  1441. }
  1442. });
  1443. }
  1444. if (typeof preposeCallback === 'function') {
  1445. dataField = preposeCallback(dataField);
  1446. }
  1447. init.wait_submit = true
  1448. admin.api.form(url, dataField, ok, no, ex, refresh);
  1449. return false;
  1450. });
  1451. });
  1452. }
  1453. },
  1454. upload: function () {
  1455. var uploadList = document.querySelectorAll("[data-upload]");
  1456. var uploadSelectList = document.querySelectorAll("[data-upload-select]");
  1457. if (uploadList.length > 0) {
  1458. $.each(uploadList, function (i, v) {
  1459. var uploadExts = $(this).attr('data-upload-exts') || init.upload_exts,
  1460. uploadName = $(this).attr('data-upload'),
  1461. uploadNumber = $(this).attr('data-upload-number') || 'one',
  1462. uploadSign = $(this).attr('data-upload-sign') || '|',
  1463. uploadAccept = $(this).attr('data-upload-accept') || 'file',
  1464. uploadAcceptMime = $(this).attr('data-upload-mimetype') || '',
  1465. elem = "input[name='" + uploadName + "']",
  1466. uploadElem = this;
  1467. // 监听上传事件
  1468. upload.render({
  1469. elem: this,
  1470. url: admin.url(init.upload_url),
  1471. exts: uploadExts,
  1472. accept: uploadAccept,//指定允许上传时校验的文件类型
  1473. acceptMime: uploadAcceptMime,//规定打开文件选择框时,筛选出的文件类型
  1474. multiple: uploadNumber !== 'one',//是否多文件上传
  1475. headers: admin.headers(),
  1476. before: function () {
  1477. this.headers['X-CSRF-TOKEN'] = init.csrf_token
  1478. },
  1479. done: function (res) {
  1480. if (res.code === 1) {
  1481. var url = res.data.url;
  1482. if (uploadNumber !== 'one') {
  1483. var oldUrl = $(elem).val();
  1484. if (oldUrl !== '') {
  1485. url = oldUrl + uploadSign + url;
  1486. }
  1487. }
  1488. $(elem).val(url);
  1489. $(elem).trigger("input");
  1490. admin.msg.success(res.msg);
  1491. } else {
  1492. admin.msg.error(res.msg);
  1493. }
  1494. let token = res ? res.__token__ : ''
  1495. init.csrf_token = token
  1496. return false;
  1497. }
  1498. });
  1499. // 监听上传input值变化
  1500. $(elem).bind("input propertychange", function (event) {
  1501. var urlString = $(this).val(),
  1502. urlArray = urlString.split(uploadSign),
  1503. uploadIcon = $(uploadElem).attr('data-upload-icon') || "file";
  1504. $('#bing-' + uploadName).remove();
  1505. if (urlString.length > 0) {
  1506. var parant = $(this).parent('div');
  1507. var liHtml = '';
  1508. $.each(urlArray, function (i, v) {
  1509. liHtml += '<li><a><img src="' + v + '" data-image onerror="this.src=\'' + BASE_URL + 'admin/images/upload-icons/' + uploadIcon + '.png\';this.onerror=null"></a><small class="uploads-delete-tip bg-red badge" data-upload-delete="' + uploadName + '" data-upload-url="' + v + '" data-upload-sign="' + uploadSign + '">×</small></li>\n';
  1510. });
  1511. parant.after('<ul id="bing-' + uploadName + '" class="layui-input-block layuimini-upload-show">\n' + liHtml + '</ul>');
  1512. }
  1513. });
  1514. // 非空初始化图片显示
  1515. if ($(elem).val() !== '') {
  1516. $(elem).trigger("input");
  1517. }
  1518. });
  1519. // 监听上传文件的删除事件
  1520. $('body').on('click', '[data-upload-delete]', function () {
  1521. var uploadName = $(this).attr('data-upload-delete'),
  1522. deleteUrl = $(this).attr('data-upload-url'),
  1523. sign = $(this).attr('data-upload-sign');
  1524. var confirm = admin.msg.confirm('确定删除?', function () {
  1525. var elem = "input[name='" + uploadName + "']";
  1526. var currentUrl = $(elem).val();
  1527. var url = '';
  1528. if (currentUrl !== deleteUrl) {
  1529. url = currentUrl.search(deleteUrl) === 0 ? currentUrl.replace(deleteUrl + sign, '') : currentUrl.replace(sign + deleteUrl, '');
  1530. $(elem).val(url);
  1531. $(elem).trigger("input");
  1532. } else {
  1533. $(elem).val(url);
  1534. $('#bing-' + uploadName).remove();
  1535. }
  1536. admin.msg.close(confirm);
  1537. });
  1538. return false;
  1539. });
  1540. }
  1541. if (uploadSelectList.length > 0) {
  1542. $.each(uploadSelectList, function (i, v) {
  1543. var uploadName = $(this).attr('data-upload-select'),
  1544. uploadNumber = $(this).attr('data-upload-number') || 'one',
  1545. uploadSign = $(this).attr('data-upload-sign') || '|';
  1546. var selectCheck = uploadNumber === 'one' ? 'radio' : 'checkbox';
  1547. var elem = "input[name='" + uploadName + "']",
  1548. uploadElem = $(this).attr('id');
  1549. tableSelect.render({
  1550. elem: "#" + uploadElem,
  1551. checkedKey: 'id',
  1552. searchType: 'more',
  1553. searchList: [
  1554. {searchKey: 'title', searchPlaceholder: '请输入文件名'},
  1555. ],
  1556. table: {
  1557. url: admin.url('ajax/getUploadFiles'),
  1558. cols: [[
  1559. {type: selectCheck},
  1560. {field: 'id', title: 'ID'},
  1561. {field: 'url', minWidth: 80, search: false, title: '图片信息', imageHeight: 30, align: "center", templet: admin.table.image},
  1562. {field: 'original_name', width: 150, title: '文件原名', align: "center"},
  1563. {field: 'mime_type', width: 120, title: 'mime类型', align: "center"},
  1564. {field: 'create_time', width: 200, title: '创建时间', align: "center", search: 'range'},
  1565. ]]
  1566. },
  1567. done: function (e, data) {
  1568. var urlArray = [];
  1569. $.each(data.data, function (index, val) {
  1570. urlArray.push(val.url)
  1571. });
  1572. var url = urlArray.join(uploadSign);
  1573. admin.msg.success('选择成功', function () {
  1574. $(elem).val(url);
  1575. $(elem).trigger("input");
  1576. });
  1577. }
  1578. })
  1579. });
  1580. }
  1581. },
  1582. editor: function () {
  1583. let editorList = document.querySelectorAll(".editor");
  1584. if (editorList.length > 0) {
  1585. let wangEditors = {}
  1586. $.each(editorList, function (i, v) {
  1587. switch (window.CONFIG.EDITOR_TYPE) {
  1588. case 'ckeditor':
  1589. CKEDITOR.tools.setCookie('ckCsrfToken', init.csrf_token);
  1590. CKEDITOR.replace($(this).attr("name"), {
  1591. height: $(this).height(),
  1592. filebrowserImageUploadUrl: admin.url('ajax/upload?type=editor'),
  1593. });
  1594. break;
  1595. case 'wangEditor':
  1596. var wangEditor = window.wangEditor;
  1597. var wangEditorName = "wangEditor_" + i
  1598. window[wangEditorName] = wangEditor.createEditor({
  1599. selector: '#editor_' + $(this).attr('name'),
  1600. html: $(this).text(),
  1601. config: {
  1602. MENU_CONF: {
  1603. // 上传图片
  1604. uploadImage: {
  1605. server: window.CONFIG.ADMIN_UPLOAD_URL,
  1606. fieldName: 'file',
  1607. maxFileSize: window.CONFIG.MAX_FILE_SIZE,
  1608. meta: {
  1609. editor: 'editor',
  1610. },
  1611. async customInsert(res, insertFn) {
  1612. let code = res.code || 0
  1613. if (code != '1') {
  1614. layer.msg(res.msg || '上传失败', {icon: 2});
  1615. return
  1616. }
  1617. let url = res.data?.url || ''
  1618. let alt = ''
  1619. let href = ''
  1620. insertFn(url, alt, href)
  1621. }
  1622. },
  1623. // 上传视频
  1624. uploadVideo: {
  1625. server: window.CONFIG.ADMIN_UPLOAD_URL,
  1626. fieldName: 'file',
  1627. meta: {editor: 'editor',},
  1628. async customInsert(res, insertFn) {
  1629. let code = res.code || 0
  1630. if (code != '1') {
  1631. layer.msg(res.msg || '上传失败', {icon: 2});
  1632. return
  1633. }
  1634. let url = res.data?.url || ''
  1635. let alt = ''
  1636. let href = ''
  1637. insertFn(url, alt, href)
  1638. }
  1639. }
  1640. },
  1641. }
  1642. })
  1643. let editor = window[wangEditorName]
  1644. wangEditor.createToolbar({
  1645. editor,
  1646. selector: '#editor_toolbar_' + $(this).attr("name"),
  1647. config: {}
  1648. })
  1649. break;
  1650. case 'EasyMDE':
  1651. const easyMDEName = "easyMDE" + i
  1652. window[easyMDEName] = new EasyMDE({
  1653. element: document.getElementById($(this).attr("name")),
  1654. initialValue: $(this).text(),
  1655. });
  1656. break;
  1657. default:
  1658. let name = $(this).attr("name");
  1659. let content = $(this).data('content')
  1660. let editorOption = {
  1661. initialFrameWidth: '100%',
  1662. initialFrameHeight: 420,
  1663. initialContent: content,
  1664. toolbars: [['fullscreen', 'source', '|', 'undo', 'redo', '|',
  1665. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
  1666. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  1667. 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
  1668. 'directionalityltr', 'directionalityrtl', 'indent', '|',
  1669. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
  1670. 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
  1671. 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
  1672. 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
  1673. 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
  1674. 'print', 'preview', 'searchreplace', 'help', 'drafts']
  1675. ],
  1676. }
  1677. setTimeout(function () {
  1678. let _UEditor = new baidu.editor.ui.Editor(editorOption);
  1679. _UEditor.render(name);
  1680. }, 100)
  1681. break;
  1682. }
  1683. });
  1684. }
  1685. },
  1686. select: function () {
  1687. var selectList = document.querySelectorAll("[data-select]");
  1688. $.each(selectList, function (i, v) {
  1689. var url = $(this).attr('data-select'),
  1690. selectFields = $(this).attr('data-fields'),
  1691. value = $(this).attr('data-value'),
  1692. that = this,
  1693. html = '<option value=""></option>';
  1694. var fields = selectFields.replace(/\s/g, "").split(',');
  1695. if (fields.length !== 2) {
  1696. return admin.msg.error('下拉选择字段有误');
  1697. }
  1698. admin.request.get(
  1699. {
  1700. url: url,
  1701. data: {
  1702. selectFields: selectFields
  1703. },
  1704. }, function (res) {
  1705. var list = res.data;
  1706. list.forEach(val => {
  1707. var key = val[fields[0]];
  1708. if (value !== undefined && key.toString() === value) {
  1709. html += '<option value="' + key + '" selected="">' + val[fields[1]] + '</option>';
  1710. } else {
  1711. html += '<option value="' + key + '">' + val[fields[1]] + '</option>';
  1712. }
  1713. });
  1714. $(that).html(html);
  1715. form.render();
  1716. }
  1717. );
  1718. });
  1719. let switchSelectList = document.querySelectorAll("[data-show]");
  1720. $.each(switchSelectList, function (i, v) {
  1721. let _show = $(this).attr('data-show');
  1722. if (_show === 'switchSelect') {
  1723. let _data = $(this).attr('data-list');
  1724. let _value = $(this).attr('data-value') || ''
  1725. let _target = $(this).attr('data-target') || ''
  1726. let _name = $(this).attr('data-name') || ''
  1727. try {
  1728. new switchSelect({
  1729. elem: $(this), data: JSON.parse(_data), default: _value, target: _target, name: _name, disabled: $(this).attr('disabled') === 'disabled'
  1730. });
  1731. } catch (e) {
  1732. console.error(e)
  1733. }
  1734. }
  1735. });
  1736. },
  1737. date: function () {
  1738. var dateList = document.querySelectorAll("[data-date]");
  1739. if (dateList.length > 0) {
  1740. $.each(dateList, function (i, v) {
  1741. var format = $(this).attr('data-date'),
  1742. type = $(this).attr('data-date-type'),
  1743. range = $(this).attr('data-date-range');
  1744. if (type === undefined || type === '' || type === null) {
  1745. type = 'datetime';
  1746. }
  1747. var options = {
  1748. elem: this,
  1749. type: type,
  1750. };
  1751. if (format !== undefined && format !== '' && format !== null) {
  1752. options['format'] = format;
  1753. }
  1754. if (range !== undefined) {
  1755. if (range === null || range === '') {
  1756. range = '-';
  1757. }
  1758. options['range'] = range;
  1759. }
  1760. laydate.render(options);
  1761. });
  1762. }
  1763. },
  1764. },
  1765. ai: {
  1766. chat: function (content, options, cancel) {
  1767. let id = 'chat_' + (new Date()).getTime()
  1768. layer.open({
  1769. 'title': options?.title || 'AI建议',
  1770. type: 1,
  1771. area: options?.area || (admin.checkMobile() ? ['95%', '60%'] : ['50%', '60%']),
  1772. shade: options?.shade || 0,
  1773. shadeClose: options?.shadeClose || false,
  1774. scrollbar: options?.scrollbar || false,
  1775. maxmin: options?.maxmin || true,
  1776. anim: options?.anim || 0,
  1777. content: `<div style="padding: 20px;white-space: pre-wrap;" id="${id}"></div>`,
  1778. success: function (layero, index) {
  1779. let elem = document.getElementById(id)
  1780. if (options?.stream) {
  1781. let index = 0;
  1782. let lastTime = performance.now();
  1783. const interval = options.interval || 100;
  1784. function typeCharacter(currentTime) {
  1785. if (index < content.length) {
  1786. if (currentTime - lastTime >= interval) {
  1787. elem.innerHTML += content.charAt(index);
  1788. index++;
  1789. lastTime = currentTime;
  1790. elem.scrollIntoView({behavior: "smooth", block: "end"});
  1791. }
  1792. requestAnimationFrame(typeCharacter);
  1793. }
  1794. }
  1795. requestAnimationFrame(typeCharacter);
  1796. } else {
  1797. content = content.replace(/\r\n/g, '<br>').replace(/\n/g, '<br>')
  1798. setTimeout(() => {
  1799. elem.innerHTML = content
  1800. }, 100)
  1801. }
  1802. },
  1803. cancel: function (index, layero) {
  1804. cancel()
  1805. }
  1806. })
  1807. },
  1808. },
  1809. };
  1810. return admin;
  1811. });