prologutil.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /* eslint-disable no-console */
  2. /* eslint-disable brace-style */
  3. /* eslint-disable block-scoped-var */
  4. /* eslint-disable no-unused-vars */
  5. /* eslint-disable quotes */
  6. /**
  7. * 2018.10.27
  8. * guoj
  9. * 公共方法
  10. */
  11. //获取用户信息
  12. var userGJ = JSON.parse(window.localStorage.getItem('userinfo'));
  13. //异步调后台
  14. function utilAjaxGJ(url, type, data, Callback, contentType) {
  15. let pload = PlgDialog.load(2, { shade: [0.6, '#fff'] });
  16. Prolog.ajax({
  17. url: url,
  18. type: type,
  19. data: data,
  20. contentType: contentType,
  21. success: function (da) {
  22. PlgDialog.close(pload);
  23. if (typeof da != 'object') {
  24. da = JSON.parse(da);
  25. }
  26. if (da.success != null && da.success == false) {
  27. if (
  28. url == '/outbound/inv-move-task-h/confirm-sernum' ||
  29. url == '/inbound/rcvh/get-by-loc' ||
  30. url == '/inbound/rcvh/get-by-containerno' ||
  31. url == '/basic/inv-ser-no/collect-serno' ||
  32. url == '/outbound/inv-move-task-h/confirm-task'
  33. ) {
  34. layer.msg(da.message);
  35. Callback(da);
  36. return false;
  37. }
  38. setTimeout(() => {
  39. layer.alert(da.message || '接口调用失败');
  40. }, 300);
  41. // Callback(da);
  42. return false;
  43. }
  44. Callback(da);
  45. },
  46. error: function () { }
  47. });
  48. }
  49. //同步调后台
  50. function utilSyncAjaxGJ(url, type, data, Callback, contentType) {
  51. PlgDialog.load(2, { shade: [0.6, '#fff'] });
  52. Prolog.syncAjax({
  53. url: url,
  54. type: type,
  55. data: data,
  56. contentType: contentType,
  57. success: function (da) {
  58. layer.closeAll();
  59. if (typeof da != 'object') da = JSON.parse(da);
  60. if (da.success != null && da.success == false) {
  61. setTimeout(() => {
  62. layer.alert(da.message);
  63. }, 300);
  64. Callback(da);
  65. return;
  66. }
  67. Callback(da);
  68. },
  69. error: function () { }
  70. });
  71. }
  72. //弹出导入导出
  73. function utilImportGJ(Template, url) {
  74. layer.open({
  75. title: '导入导出',
  76. shadeClose: true,
  77. area: ['800px', '480px'],
  78. btn: [],
  79. content: ' <div id="app2"></div>', //iframe的url
  80. yes: function (index, layero) {
  81. //如果设定了yes回调,需进行手工关闭
  82. layer.close(index);
  83. }
  84. });
  85. var config2 = {
  86. indexActive: 0, //初始化默认选中项
  87. skin: 'card',
  88. content: [
  89. {
  90. title: '批量导入',
  91. template:
  92. '<div id="pidao">1、上传文件格式支持xls、xlsx或者zone,大小不超过2MB。<br />2、云解析DNS会按照预定模板扫描您的文件,并导入数据。下载模板<br />3、每次最多可以导入500条解析记录,超出的部分将不会导入。<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><button class="layui-btn layui-btn-sm layui-btn-primary" id="GJXIAZ"><i class="layui-icon" >&#xe605;</i> 下载模板</button><button class="layui-btn layui-btn-sm layui-btn-primary" id="GJDAOR"><i class="layui-icon" >&#xe7ea;</i> 导入</button>'
  93. },
  94. {
  95. title: '批量导出',
  96. template:
  97. '<div id="pichu">批量导出<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><button class="layui-btn layui-btn-sm layui-btn-primary" id="GJexports3"><i class="layui-icon" >&#xe7ea;</i> 导出</button>'
  98. }
  99. ]
  100. };
  101. var tabs2 = new PlgTabs(config2).renderTo('app2').on(function (ele) {
  102. // eslint-disable-next-line no-console
  103. console.log(this);
  104. });
  105. //下载工作台模板
  106. document.querySelector('#GJXIAZ').onclick = function () {
  107. window.open(Template);
  108. };
  109. //导入
  110. document.querySelector('#GJDAOR').onclick = function () { };
  111. //导出
  112. document.querySelector('#GJexports3').onclick = function () {
  113. PlgDialog.msg('导出成功');
  114. };
  115. //文件上传
  116. layui.use('upload', function () {
  117. var $ = layui.jquery,
  118. upload = layui.upload;
  119. upload.render({
  120. elem: '#GJDAOR',
  121. url: url,
  122. accept: 'file',
  123. before: function (obj) {
  124. console.log(this.item);
  125. },
  126. done: function (res) {
  127. console.log(res);
  128. }
  129. });
  130. });
  131. }
  132. //弹出导入导出2
  133. function utilImportGJ2(id, ImportId, exportId, url, Callback) {
  134. var config2 = {
  135. indexActive: 0, //初始化默认选中项
  136. skin: 'card',
  137. content: [
  138. {
  139. title: '批量导入',
  140. template:
  141. '<div id="pidao">1、上传文件格式支持xls、xlsx或者zone,大小不超过2MB。<br />2、云解析DNS会按照预定模板扫描您的文件,并导入数据。下载模板<br />3、每次最多可以导入500条解析记录,超出的部分将不会导入。<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><button class="layui-btn layui-btn-sm layui-btn-primary" id="' +
  142. ImportId +
  143. '"><i class="layui-icon" >&#xe7ea;</i> 导入</button>'
  144. },
  145. {
  146. title: '批量导出',
  147. template:
  148. '<div id="pichu">批量导出<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></div><button class="layui-btn layui-btn-sm layui-btn-primary" id="' +
  149. exportId +
  150. '"><i class="layui-icon" >&#xe7ea;</i> 导出</button>'
  151. }
  152. ]
  153. };
  154. var tabs2 = new PlgTabs(config2).renderTo(id).on(function (ele) {
  155. console.log(this);
  156. });
  157. //导出
  158. document.querySelector('#' + exportId + '').onclick = function () {
  159. PlgDialog.msg('导出成功');
  160. Callback();
  161. };
  162. //文件上传
  163. layui.use('upload', function () {
  164. var token = window.localStorage.getItem('authorization');
  165. if (token == null || token == '') {
  166. PlgDialog.msg('登录状态异常,请重新登录!');
  167. window.location.href = '/menu?path=login';
  168. return;
  169. }
  170. var $ = layui.jquery,
  171. upload = layui.upload;
  172. upload.render({
  173. elem: '#' + ImportId + '',
  174. url: url,
  175. accept: 'file',
  176. headers: { Authorization: token },
  177. contentType: 'multipart/form-data',
  178. before: function (obj) {
  179. console.log(this.item);
  180. },
  181. done: function (res) {
  182. console.log(res);
  183. }
  184. });
  185. });
  186. }
  187. function getDeleteAll(grid) {
  188. var data = grid.getCheckedIds();
  189. if (data == null || data == '') {
  190. data = grid.getSelectedRowId();
  191. }
  192. if (data == null) {
  193. PlgDialog.msg('请选择行!');
  194. return;
  195. }
  196. data = data.split(',');
  197. return data;
  198. }
  199. //公共批量删除
  200. function utilDelete(url, type, datas, Callback, contentType) {
  201. layer.open({
  202. title: '删除操作',
  203. content: '您确认要删除吗?',
  204. btn: ['确认', '取消'],
  205. yes: function (index, layero) {
  206. utilAjaxGJ(url, type, datas, Callback, contentType);
  207. layer.closeAll();
  208. },
  209. btn2: function (index, layero) {
  210. layer.closeAll();
  211. }
  212. });
  213. }
  214. //公共单个删除
  215. function utilDeleteOne(grid, name, url, type, Callback, contentType) {
  216. var datas;
  217. var data = grid.getCheckedIds();
  218. if (data == null || data == '') {
  219. data = grid.getSelectedRowId();
  220. }
  221. if (data == null) {
  222. PlgDialog.msg('请选择行!');
  223. return;
  224. }
  225. datas = { id: data };
  226. layer.open({
  227. title: '删除操作',
  228. content: '您确认要删除吗?',
  229. btn: ['确认', '取消'],
  230. yes: function (index, layero) {
  231. utilAjaxGJ(url, type, datas, Callback, contentType);
  232. layer.closeAll();
  233. },
  234. btn2: function (index, layero) {
  235. layer.closeAll();
  236. }
  237. });
  238. }
  239. //公共查询下拉框
  240. function getSelectGJ(data, Callback) {
  241. //查询下拉框
  242. utilSyncAjaxGJ(
  243. '/maindata/refdata/dtl/dd-list',
  244. 'post',
  245. JSON.stringify(data),
  246. function (datas) {
  247. Callback(datas);
  248. },
  249. 'application/json'
  250. );
  251. }
  252. //获取库别
  253. function getDropDownWharehouseGJ() {
  254. var datas;
  255. utilSyncAjaxGJ('/basic/storeroom/list-all', 'post', { branchid: userGJ.p_companyid }, function (data) {
  256. datas = data.data;
  257. datas.unshift({ text: '', value: '' });
  258. });
  259. return datas;
  260. }
  261. //获取弹出框
  262. function getEject(opts, top, left, Callback) {
  263. PlgDialog.open({
  264. title: '通用弹出框',
  265. offset: [top + 'px', left + 'px'],
  266. shadeClose: true, //点击遮罩关闭
  267. content: '<div id="EjectGJ" class="grid-container-full" ></div>',
  268. area: ['550px', '400px']
  269. });
  270. $('.layui-layer-btn0').hide();
  271. //分页参数 pageSize pageNum
  272. var gridGJ = new PlgGrid(opts);
  273. gridGJ.renderTo('EjectGJ');
  274. gridGJ.loadData();
  275. //按钮添加方法
  276. gridGJ.attachToolBarEvent('onButtonClick', function (name) {
  277. if (name == 'querycarr') {
  278. var fdata = gridGJ.getToolBarForm().getFormData();
  279. for (var key in fdata) {
  280. // eslint-disable-next-line no-console
  281. console.log(fdata.key);
  282. if (fdata[key] == '' || fdata[key] == null || fdata[key] == undefined) {
  283. fdata[key] = '%';
  284. }
  285. }
  286. if (opts.contentType == 'application/json') {
  287. var from_par = JSON.parse(opts.params);
  288. var jsonData = Object.assign(from_par, fdata);
  289. gridGJ.setParams(JSON.stringify(jsonData));
  290. } else {
  291. // eslint-disable-next-line no-redeclare
  292. var jsonData = Object.assign(opts.params, fdata);
  293. gridGJ.setParams(jsonData);
  294. }
  295. gridGJ.reload();
  296. }
  297. if (name == 'car_Reset') {
  298. gridGJ.getToolBarForm().clear();
  299. }
  300. });
  301. //双击事件
  302. gridGJ.on('onRowDblClicked', function (id, ids) {
  303. var data = gridGJ.getSelectedRowData();
  304. Callback(data);
  305. });
  306. }
  307. //打印服务
  308. function PrintService(url, data, callback) {
  309. const moUrl = localStorage.getItem('printserviceip');
  310. var xhr = $.ajax({
  311. url: url || moUrl,
  312. timeout: 30000,
  313. type: 'POST',
  314. contentType: 'application/json',
  315. async: false,
  316. data: JSON.stringify(data), //报表数据
  317. dataType: 'json',
  318. success: function (msg) {
  319. callback(msg);
  320. },
  321. error: function (XMLHttpRequest, textStatus, errorThrown) {
  322. console.error(XMLHttpRequest.status);
  323. callback(null);
  324. },
  325. complete: function (XMLHttpRequest, status) {
  326. //请求完成后最终执行参数
  327. if (status === 'timeout') {
  328. xhr.abort();
  329. callback(null);
  330. }
  331. }
  332. });
  333. }
  334. //根据value获取中文名称,datas数据集合,text数据值
  335. function getSelectValue(datas, text) {
  336. var str = text;
  337. if (text == null) {
  338. str = '';
  339. } else if (datas == null) {
  340. str = text;
  341. } else {
  342. for (var i = 0; i < datas.length; i++) {
  343. if (datas[i].value == text) str = datas[i].text;
  344. }
  345. }
  346. return str;
  347. }
  348. /*
  349. * @method 删除 PlgGrid 行数据
  350. * @param grid - grid控件
  351. * @param url {string} - 数据接口地址
  352. * @param type {string} - 数据接口请求类型,为空时默认post
  353. * @param contenttype {string} - 数据接口请求 contentType 类型,为空时默认application/x-www-form-urlencoded
  354. * @param param {object} - 请求参数名 {"id":0}
  355. * @author jiw
  356. * @deprecated 删除PlgGrid选中行数据,删除成功后reload
  357. */
  358. Prolog.delGridRowData = function (grid, url, type, contenttype, param, multiselect) {
  359. if (multiselect === false) {
  360. if (grid.getSelectedRowId() == null && param.length < 1) {
  361. PlgDialog.msg('请选择行!');
  362. return;
  363. }
  364. } else {
  365. if (grid.getCheckedIds() == null) {
  366. PlgDialog.msg('请选择行!');
  367. return;
  368. }
  369. }
  370. PlgDialog.confirm(
  371. '是否确定删除?',
  372. {
  373. title: '删除提示',
  374. btn: ['确定', '取消'],
  375. zIndex: layer.zIndex
  376. },
  377. function (index) {
  378. PlgDialog.close(index);
  379. if (type === '') type = 'post';
  380. if (contenttype === '') contenttype = 'application/x-www-form-urlencoded';
  381. Prolog.ajax({
  382. url: url,
  383. type: type,
  384. contentType: contenttype,
  385. data: param,
  386. success: function (data) {
  387. if (typeof data != 'object') data = JSON.parse(data);
  388. if (data.success) {
  389. setTimeout(() => {
  390. PlgDialog.msg(data.message);
  391. }, 300);
  392. setTimeout(() => {
  393. grid.reload();
  394. }, 10);
  395. } else {
  396. setTimeout(() => {
  397. PlgDialog.alert(data.message == null ? '接口调用失败' : data.message);
  398. }, 300);
  399. }
  400. },
  401. error: function () {
  402. PlgDialog.msg('接口调用失败');
  403. }
  404. });
  405. }
  406. );
  407. };
  408. /*
  409. * @method 根据code获取中文名称
  410. * @param rsp {object}- rsp数据集合,[{"value":"0","text":"test"}]
  411. * @param code {string} - code数据值
  412. * @author jiw
  413. * @deprecated 遍历集合,匹配code,返回 text
  414. */
  415. Prolog.getNameByID = function (rsp, code) {
  416. let str = code;
  417. if (code == null) {
  418. str = '';
  419. } else if (rsp == null) {
  420. str = code;
  421. } else {
  422. rsp.forEach(item => {
  423. if (item.value == code) str = item.text;
  424. });
  425. }
  426. return str;
  427. };
  428. /*
  429. * @method 将通用参数数据返回数据,生成下拉框所需格式
  430. * @param data {object}- data数据集合,[{"value":"0","text":"test"}]
  431. * @param addNsN {bool} - 是否首行为空
  432. * @author jiw
  433. * @deprecated 将通用参数数据返回数据,生成下拉框所需格式
  434. */
  435. Prolog.bindRefdata = function (data, addNsN) {
  436. let res = [];
  437. if (addNsN) {
  438. res.push({ text: '', value: '' });
  439. }
  440. data.forEach(item => {
  441. res.push(item);
  442. });
  443. return res;
  444. };
  445. /*
  446. * @method 日期格式化
  447. * @param time {Date}- 要格式化的日期
  448. * @param format {string} - 返回格式;年%YY;月%MM;日%DD,时%hh,分%mm,秒%ss,毫秒%ff
  449. * @author jiw
  450. * @deprecated 返回指定格式日期,默认返回yyyy-mm-dd;例:fromatDate(new Date("2019-01-01"), "%YY-%MM-%DD"),执行后返回 2019-01-01
  451. */
  452. Prolog.formatDate = function (time, format) {
  453. if (time == '' || time == null) return '';
  454. let dt = new Date(time);
  455. let y = dt.getFullYear();
  456. let m = dt.getMonth() + 1 < 10 ? '0' + (dt.getMonth() + 1) : dt.getMonth() + 1;
  457. let d = dt.getDate() < 10 ? '0' + dt.getDate() : dt.getDate();
  458. let hh = dt.getHours() < 10 ? '0' + dt.getHours() : dt.getHours();
  459. let mm = dt.getMinutes() < 10 ? '0' + dt.getMinutes() : dt.getMinutes();
  460. let ss = dt.getSeconds() < 10 ? '0' + dt.getSeconds() : dt.getSeconds();
  461. let ff = dt.getMilliseconds();
  462. if (format == null || format == '' || format == undefined) {
  463. return y + '-' + m + '-' + d;
  464. }
  465. let arr = format.split('%');
  466. if (arr.length > 0) {
  467. let res = '';
  468. for (let i = 0; i < arr.length; i++) {
  469. let t = arr[i];
  470. if (t.substring(0, 2) == 'YY') {
  471. res += y;
  472. } else if (t.substring(0, 2) == 'MM') {
  473. res += m;
  474. } else if (t.substring(0, 2) == 'DD') {
  475. res += d;
  476. } else if (t.substring(0, 2) == 'hh') {
  477. res += hh;
  478. } else if (t.substring(0, 2) == 'mm') {
  479. res += mm;
  480. } else if (t.substring(0, 2) == 'ss') {
  481. res += ss;
  482. } else if (t.substring(0, 2) == 'ff') {
  483. res += ff;
  484. }
  485. if (i < arr.length - 1) res += t.substring(2);
  486. }
  487. return res;
  488. }
  489. };
  490. /*
  491. * @method 导出xls并下载文件
  492. * @param url {string}- 接口地址
  493. * @param filename {string} - 文件全名,可为空,默认xls
  494. * @param mtype {string} - 请求类型;可为空,默认get
  495. * @param ctype {string} - Content-Type类型;可为空,默认 application/x-www-form-urlencoded
  496. * @param data {string} - 请求参数;传JSON格式
  497. * @author jiw
  498. * @deprecated 调用接口并下载文件
  499. if (grid.getRowsNum() <= 0) {
  500. PlgDialog.msg("没有要导出的数据");
  501. return;
  502. }
  503. let url = "/export/v1.0";
  504. let name = "报表" + (Prolog.formatDate(new Date(), "%YY%MM%DD")) + ".xls"
  505. let par = grid.getSideBarForm().getFormData();
  506. Prolog.exportFile(url, name, "application/json", "post", par);
  507. Prolog.exportFile(url, name, "", "get", par);
  508. Prolog.exportFile(url, name, "", "post", par);
  509. */
  510. Prolog.exportFile = function (url, filename, ctype, mtype, data, type) {
  511. let pload = PlgDialog.load(2, { shade: [0.6, '#fff'] });
  512. mtype = mtype.toLowerCase();
  513. ctype = ctype.toLowerCase();
  514. if (!ctype) ctype = 'application/x-www-form-urlencoded';
  515. if (!mtype) mtype = 'get';
  516. if (!filename) filename = '导出文件' + Prolog.formatDate(new Date(), '%YY%MM%DD') + '.xls';
  517. if (data == null || data == '') data = {};
  518. if (mtype == 'post' && ctype == 'application/json') {
  519. data = JSON.stringify(data);
  520. } else {
  521. ctype = 'application/x-www-form-urlencoded';
  522. let d = '';
  523. for (var key in data) {
  524. d += '&' + key + '=' + data[key];
  525. }
  526. data = d.substring(1);
  527. if (mtype == 'get') {
  528. url = url + '?' + data;
  529. }
  530. }
  531. if (!type) {
  532. type = 'text/xls';
  533. }
  534. // type = 'application/zip';
  535. var token = localStorage.getItem('authorization');
  536. var xhr = new XMLHttpRequest();
  537. xhr.open(mtype, url);
  538. xhr.setRequestHeader('Authorization', token);
  539. xhr.setRequestHeader('Content-Type', ctype);
  540. xhr.responseType = 'blob';
  541. xhr.onreadystatechange = function () {
  542. PlgDialog.close(pload);
  543. if (xhr.readyState == 4 && xhr.status == 200) {
  544. var blob = new Blob([xhr.response], { type: type });
  545. var csvUrl = URL.createObjectURL(blob);
  546. var link = document.createElement('a');
  547. link.href = csvUrl;
  548. link.download = filename;
  549. link.click();
  550. }
  551. };
  552. xhr.send(data);
  553. };
  554. /*
  555. * @method 生成圆形
  556. * @param color {string} - 颜色
  557. * @author jiw
  558. * @deprecated 生成不同颜色的圆形
  559. */
  560. Prolog.getCircle = function (color) {
  561. let arr = ['yellow', 'green', 'black', 'red', 'blue'];
  562. if (arr.indexOf(color) == -1) color = 'black';
  563. let str = '<span class="plg-span-' + color + '"></span>';
  564. return str;
  565. };
  566. /*
  567. * @method 验证是否IPV4
  568. * @param ip {string} - ip地址
  569. * @author jiw
  570. * @deprecated
  571. */
  572. Prolog.isIP = function (ip) {
  573. var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
  574. return reg.test(ip);
  575. };
  576. /*
  577. * @method 绑定下拉数据JSON
  578. * @param rsp {object} - 绑定的变量
  579. * @param data {object} - 源数据
  580. * @param addNsN {object} - 是否增加空行
  581. * @author jiw
  582. * @deprecated
  583. */
  584. Prolog.bindRefdata = function (rsp, data, addNsN) {
  585. if (addNsN) {
  586. rsp.push({ text: '', value: '' });
  587. }
  588. for (var i = 0; i < data.length; i++) {
  589. rsp.push({ text: data[i].text, value: data[i].value });
  590. }
  591. };
  592. //设置下拉表格方法
  593. function setMethod(grid, table, Callback) {
  594. //工具栏按钮事件
  595. grid.attachToolBarEvent('onButtonClick', function (name) {
  596. if (name == 'querycarr') {
  597. var fdata = grid.getToolBarForm().getFormData();
  598. for (var key in fdata) {
  599. if (fdata[key] == '' || fdata[key] == null || fdata[key] == undefined) {
  600. if (key == 'likeItemcode' || key == 'itemid') {
  601. fdata[key] = '%';
  602. }
  603. }
  604. }
  605. if (table.contentType == 'application/json') {
  606. var from_par = JSON.parse(table.params);
  607. var jsonData = Object.assign(from_par, fdata);
  608. grid.setParams(JSON.stringify(jsonData));
  609. } else {
  610. // eslint-disable-next-line no-redeclare
  611. var jsonData = Object.assign(table.params, fdata);
  612. grid.setParams(jsonData);
  613. }
  614. grid.reload();
  615. }
  616. if (name == 'car_Reset') {
  617. grid.getToolBarForm().clear();
  618. }
  619. });
  620. //单击事件
  621. grid.on('onRowSelect', function (id, ind) {
  622. var data = grid.getSelectedRowData();
  623. Callback(data, id, ind);
  624. });
  625. }
  626. //返回 2015-03-19 格式
  627. function FormatDate(datetime) {
  628. if (datetime == null) {
  629. return null;
  630. }
  631. var y = datetime.getFullYear();
  632. var m = datetime.getMonth() + 1;
  633. m = m < 10 ? '0' + m : m;
  634. var d = datetime.getDate();
  635. d = d < 10 ? '0' + d : d;
  636. return y + '-' + m + '-' + d;
  637. }
  638. //返回 2009-06-12 17:18:05 格式
  639. function FormatDateTime(datetime) {
  640. if (datetime == null) {
  641. return '';
  642. }
  643. var year = datetime.getFullYear();
  644. var month = datetime.getMonth() + 1; //js从0开始取
  645. var date = datetime.getDate();
  646. var hour = datetime.getHours();
  647. var minutes = datetime.getMinutes();
  648. var second = datetime.getSeconds();
  649. if (month < 10) {
  650. month = '0' + month;
  651. }
  652. if (date < 10) {
  653. date = '0' + date;
  654. }
  655. if (hour < 10) {
  656. hour = '0' + hour;
  657. }
  658. if (minutes < 10) {
  659. minutes = '0' + minutes;
  660. }
  661. if (second < 10) {
  662. second = '0' + second;
  663. }
  664. var time = year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + second; //2009-06-12 17:18:05
  665. return time;
  666. }
  667. //返回 17:18 格式
  668. function FormatTime(datetime) {
  669. if (datetime == null) {
  670. return null;
  671. }
  672. var hour = datetime.getHours();
  673. var minutes = datetime.getMinutes();
  674. if (hour < 10) {
  675. hour = '0' + hour;
  676. }
  677. if (minutes < 10) {
  678. minutes = '0' + minutes;
  679. }
  680. var time = hour + ':' + minutes; //17:18
  681. return time;
  682. }
  683. /*
  684. * @method 返回 17:18:50 格式
  685. * @param datetime {Date} - 时间对象
  686. * @author zzx
  687. * @deprecated
  688. */
  689. function FormatSecond(datetime) {
  690. if (datetime == null) {
  691. return null;
  692. }
  693. var hour = datetime.getHours();
  694. var minutes = datetime.getMinutes();
  695. var seconds = datetime.getSeconds();
  696. if (hour < 10) {
  697. hour = '0' + hour;
  698. }
  699. if (minutes < 10) {
  700. minutes = '0' + minutes;
  701. }
  702. if (seconds < 10) {
  703. seconds = '0' + seconds;
  704. }
  705. var time = hour + ':' + minutes + ':' + seconds; //17:18
  706. return time;
  707. }
  708. //from去重
  709. function setFormDataGJ(plgform, data) {
  710. var fromData = plgform.getFormData();
  711. for (var key in fromData) {
  712. fromData[key] = data[key];
  713. }
  714. plgform.setFormData(fromData);
  715. }
  716. //导入事件公共方法
  717. function uploadImportGJ(url, classname, Callback) {
  718. layui.use('upload', function () {
  719. var token = window.localStorage.getItem('authorization');
  720. if (token == null || token == '') {
  721. PlgDialog.msg('登录状态异常,请重新登录!');
  722. window.location.href = '/menu?path=login';
  723. return;
  724. }
  725. var upload = layui.upload;
  726. upload.render({
  727. elem: classname,
  728. url: url,
  729. headers: { Authorization: token },
  730. accept: 'file', //普通文件
  731. contentType: 'multipart/form-data',
  732. done: function (res) {
  733. if (res.success) {
  734. Callback(res);
  735. } else {
  736. PlgDialog.alert(res.message);
  737. }
  738. },
  739. error: function (index) {
  740. console.log(index);
  741. }
  742. });
  743. });
  744. }
  745. /**
  746. * 获取指定日期:
  747. * @param {number} days 指定日期的前后N天
  748. * @param {string} date 指定日期
  749. * @param {string} format 指定格式
  750. * @returns {string} 返回时间
  751. */
  752. function getSpecDate(days, date, format) {
  753. days = days || 0;
  754. let currDate = date ? new Date(date) : new Date();
  755. let specTime = currDate.setDate(currDate.getDate() + days);
  756. const formatDate = (date, format) => {
  757. const padStart = (value, length, char) => {
  758. value = value + '';
  759. return value.padStart(length, char);
  760. };
  761. if (!date) return '--';
  762. let _date = date;
  763. format = format || 'YYYY-MM-DD';
  764. let splitArr = format.split(/(YYYY|MM|DD|hh|mm|ss)+/);
  765. if (typeof date !== 'object') {
  766. _date = new Date(date);
  767. }
  768. return splitArr
  769. .map(item => {
  770. if (item === 'YYYY') {
  771. return _date.getFullYear();
  772. }
  773. if (item === 'MM') {
  774. return padStart(_date.getMonth() + 1, 2, 0);
  775. }
  776. if (item === 'DD') {
  777. return padStart(_date.getDate(), 2, 0);
  778. }
  779. if (item === 'hh') {
  780. return padStart(_date.getHours(), 2, 0);
  781. }
  782. if (item === 'mm') {
  783. return padStart(_date.getMinutes(), 2, 0);
  784. }
  785. if (item === 'ss') {
  786. return padStart(_date.getSeconds(), 2, 0);
  787. }
  788. return item;
  789. })
  790. .join('');
  791. };
  792. return formatDate(specTime, format);
  793. }
  794. /*
  795. * @method 删除请求条件中属性值为空的属性
  796. * @param obj {} - 请求数据
  797. * @author zzx
  798. * @deprecated
  799. */
  800. function dealNullQueryCondition(obj) {
  801. for (var p in obj) {
  802. if (obj[p] === '' || obj[p] !== 0 && obj[p] !== false && !obj[p] && (typeof obj[p] !== 'boolean' || typeof obj[p] !== 'number')) {
  803. delete obj[p];
  804. }
  805. }
  806. }
  807. /*
  808. * @method 获取系统信息
  809. * @author jiw
  810. * @deprecated 返回系统信息,系统名,版本号
  811. */
  812. Prolog.getSysInfo = function () {
  813. let info = {
  814. sysid: 'plg-std-tms',
  815. sysname: 'UPCloud Solution For TMS',
  816. version: '1.0.2'
  817. };
  818. return info;
  819. };
  820. //返回 当前天前七天2015-03-19 格式
  821. function ship_FormatDate(datetime) {
  822. datetime.setTime(datetime.getTime() - 7 * 24 * 60 * 60 * 1000);
  823. return FormatDate(datetime);
  824. }
  825. /*
  826. * @method 验证车牌是否正确
  827. * @param no {string} - 车牌号
  828. * @author jiw
  829. * @deprecated
  830. */
  831. Prolog.isCarID = function (no) {
  832. var reg = /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/;
  833. return reg.test(no);
  834. };
  835. /*
  836. * @method 验证手机号是否正确
  837. * @param no {string} - 手机号
  838. * @author jiw
  839. * @deprecated
  840. */
  841. Prolog.isMobileNo = function (no) {
  842. var reg = /^1[3456789]\d{9}$/;
  843. return reg.test(no);
  844. };
  845. /*
  846. * @method 验证座机号是否正确
  847. * @param no {string} - 座机号
  848. * @author jiw
  849. * @deprecated
  850. */
  851. Prolog.isLandlineNo = function (no) {
  852. var reg = /0\d{2,3}-\d{7,8}/;
  853. return reg.test(no);
  854. };
  855. /*
  856. * @method 验证邮箱是否正确
  857. * @param no {string} - 邮箱
  858. * @author jiw
  859. * @deprecated
  860. */
  861. Prolog.isEmail = function (no) {
  862. var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  863. return reg.test(no);
  864. };
  865. /*
  866. * @method 获取图片url
  867. * @author zzx
  868. * @param request 接口请求参数
  869. */
  870. function querySysPicture(request) {
  871. let pload = PlgDialog.load(2, { shade: [0.6, '#fff'] });
  872. return new Promise((resolve, reject) => {
  873. Prolog.ajax({
  874. url: '/business/sysFile/querySysPicture',
  875. type: 'post',
  876. contentType: 'application/json',
  877. data: JSON.stringify(request),
  878. success: function (res) {
  879. PlgDialog.close(pload);
  880. if (res.success) {
  881. resolve(res.data);
  882. } else {
  883. reject(res);
  884. }
  885. },
  886. error: function () {
  887. PlgDialog.close(pload);
  888. return false;
  889. }
  890. });
  891. });
  892. }
  893. /*
  894. * @method 弹窗查看图片
  895. * @author zzx
  896. * @param res 图片list
  897. * @param index 默认打开图片索引
  898. */
  899. function imageView(res, index) {
  900. if (res.length == 0) {
  901. PlgDialog.msg('没有要查看的图片数据');
  902. return;
  903. }
  904. let ul = document.createElement('ul');
  905. for (let i = 0; i < res.length; i++) {
  906. let li = document.createElement('li');
  907. let img = new Image();
  908. img.src = res[i].fileUrl;
  909. li.appendChild(img);
  910. ul.appendChild(li);
  911. }
  912. let viewer = new Viewer(ul, {
  913. hidden: function () {
  914. //关闭后清理
  915. ul.remove();
  916. $('#viewer' + viewer.id).remove();
  917. },
  918. url: 'src'
  919. });
  920. viewer.view(index || 0);
  921. }
  922. /*
  923. * @method 验证是否为整数
  924. * @param val {string} - 数字
  925. * @author jiw
  926. * @deprecated
  927. */
  928. Prolog.isInt = function (val) {
  929. let reg = /^-?[1-9]\d*$/;
  930. return reg.test(val);
  931. };
  932. /*
  933. * @method 将一个浮点数转成整数,返回整数和倍数。如 3.14 >> 314,倍数是 100
  934. * @param floatNum {number} 小数
  935. * @return {object}
  936. * {times:100, num: 314}
  937. * @author jiw
  938. */
  939. function toInteger(floatNum) {
  940. var ret = { times: 1, num: 0 };
  941. if (Prolog.isInt(floatNum) || floatNum === 0) {
  942. ret.num = floatNum;
  943. return ret;
  944. }
  945. let strfi = floatNum + '';
  946. let list = strfi.split('.');
  947. ret.times = Number('1'.padEnd(list[1].length + 1, 0));
  948. ret.num = Number(list[0] + list[1]);
  949. // var dotPos = strfi.indexOf(".");
  950. // var len = strfi.substr(dotPos + 1).length;
  951. // var times = Math.pow(10, len);
  952. // var intNum = parseInt(floatNum * times + 0.5, 10);
  953. // ret.times = times;
  954. // ret.num = intNum;
  955. return ret;
  956. }
  957. /*
  958. * @method 浮点数实现加减乘除运算,确保不丢失精度
  959. * 思路:把小数放大为整数(乘),进行算术运算,再缩小为小数(除)
  960. * @param a {number} 运算数1
  961. * @param b {number} 运算数2
  962. * @param op {string} 运算类型,有加减乘除(+|-|*|/)
  963. * @author jiw
  964. * @deprecated 示例: Prolog.floatOper(10.908, 10, "-") //0.908
  965. */
  966. Prolog.floatOper = function (a, b, op) {
  967. var o1 = toInteger(Number(a));
  968. var o2 = toInteger(Number(b));
  969. var n1 = o1.num;
  970. var n2 = o2.num;
  971. var t1 = o1.times;
  972. var t2 = o2.times;
  973. var max = t1 > t2 ? t1 : t2;
  974. var result = null;
  975. switch (op) {
  976. //加
  977. case '+':
  978. // 两个小数位数相同
  979. if (t1 === t2) {
  980. result = n1 + n2;
  981. }
  982. // o1 小数位 大于 o2
  983. else if (t1 > t2) {
  984. result = n1 + n2 * (t1 / t2);
  985. }
  986. // o1 小数位 小于 o2
  987. else {
  988. result = n1 * (t2 / t1) + n2;
  989. }
  990. return result / max;
  991. //减
  992. case '-':
  993. // 两个小数位数相同
  994. if (t1 === t2) {
  995. result = n1 - n2;
  996. }
  997. // t1 小数位 大于 t2
  998. else if (t1 > t2) {
  999. result = n1 - n2 * (t1 / t2);
  1000. }
  1001. // t1 小数位 小于 t2
  1002. else {
  1003. result = n1 * (t2 / t1) - n2;
  1004. }
  1005. return result / max;
  1006. //乘
  1007. case '*':
  1008. result = n1 * n2 / (t1 * t2);
  1009. return result;
  1010. //除
  1011. case '/':
  1012. result = n1 / n2 * (t2 / t1);
  1013. return result;
  1014. }
  1015. };
  1016. // 获取右上角消息弹窗数据
  1017. function alertMessage_count() {
  1018. Prolog.ajax({
  1019. url: '/maindata/alertMessage/count',
  1020. type: 'post',
  1021. contentType: 'application/json',
  1022. data: '{}',
  1023. success: function (res) {
  1024. if (res.success) {
  1025. $('#remind-box-badge').html(res.data.alertMessageQuantity);
  1026. }
  1027. },
  1028. error: function () { }
  1029. });
  1030. }
  1031. // 自定义事件监听
  1032. class EventEmitter {
  1033. constructor() {
  1034. this._events = {};
  1035. }
  1036. on(event, callback) {
  1037. //监听 event 事件,触发时调用 callback 函数
  1038. let callbacks = this._events[event] || [];
  1039. callbacks.push(callback);
  1040. this._events[event] = callbacks;
  1041. return this;
  1042. }
  1043. off(event, callback) {
  1044. //停止监听 event 事件
  1045. let callbacks = this._events[event];
  1046. this._events[event] = callbacks && callbacks.filter(fn => fn !== callback);
  1047. return this;
  1048. }
  1049. offByName(event) {
  1050. //停止监听 event 事件
  1051. for (const key in this._events) {
  1052. if (key.includes(event)) {
  1053. delete this._events[key];
  1054. }
  1055. }
  1056. return this;
  1057. }
  1058. emit(event, ...args) {
  1059. //触发事件,并把参数传给事件的处理函数
  1060. const callbacks = this._events[event];
  1061. callbacks.forEach(fn => fn.apply(null, args));
  1062. return this;
  1063. }
  1064. }
  1065. //实例化
  1066. const eve = new EventEmitter();
  1067. // 页面扩展字段方法
  1068. function extendedFieldList({ list, className = 'layui-col-md3' }, callback) {
  1069. let gird_opts_list = [],
  1070. form_opts_list = [],
  1071. date_list = [];
  1072. list.forEach(item => {
  1073. gird_opts_list.push({ id: item.fieldName, name: item.displayName, type: 'ro', width: 120 });
  1074. // 文本输入框
  1075. if (item.fieldType === '1') {
  1076. form_opts_list.push({ type: 'input', name: item.fieldName, label: item.displayName + ':', className });
  1077. }
  1078. // 数字输入框
  1079. else if (item.fieldType === '2') {
  1080. form_opts_list.push({ type: 'input', name: item.fieldName, label: item.displayName + ':', className, validate: 'Money_11bit' });
  1081. }
  1082. // 日期输入框
  1083. else if (item.fieldType === '3') {
  1084. form_opts_list.push({ type: 'calendar', name: item.fieldName, label: item.displayName + ':', className, readonly: true, enableTime: true, minutesInterval: 1, dateFormat: '%Y-%m-%d %H:%i:%s' });
  1085. date_list.push(item.fieldName);
  1086. }
  1087. // 单选框
  1088. else if (item.fieldType === '4') {
  1089. let options = item.fieldChoiceValue.split(',').map(el => {
  1090. return { value: el, text: el };
  1091. });
  1092. options.unshift({ value: '', text: '' });
  1093. form_opts_list.push({ type: 'combo', name: item.fieldName, label: item.displayName + ':', className, options, readonly: true });
  1094. }
  1095. });
  1096. callback && callback({ gird_opts_list, form_opts_list, date_list });
  1097. }
  1098. // 导入弹窗
  1099. function importDialog({ id, importUrl, downloadUrl, downloadName }) {
  1100. const dialog_id = id + '_dialog';
  1101. PlgDialog.open({
  1102. title: '导入',
  1103. shadeClose: true,
  1104. type: 1, // 设置为1,手动控制dialog的开关
  1105. area: ['500px', '300px'],
  1106. content: '<div id =' + dialog_id + '></div>' //iframe的url
  1107. });
  1108. let dialogForm_opts = {
  1109. renderer: dialog_id,
  1110. items: [
  1111. { type: 'settings', labelAlign: 'right', labelWidth: 75 },
  1112. {
  1113. type: 'row',
  1114. list: [{ type: 'input', className: 'layui-col-md12 importName', name: 'importName', label: '导入文件:', readonly: true, placeholder: '点击选择导入文件' }]
  1115. },
  1116. {
  1117. type: 'row',
  1118. list: [
  1119. {
  1120. type: 'block',
  1121. className: 'layui-col-md12 right',
  1122. list: [
  1123. { type: 'button', name: 'dialog_import', className: 'dialog_import btn-normal', value: '导入' },
  1124. { type: 'newcolumn' },
  1125. { type: 'button', name: 'dialog_download', className: 'btn-normal', value: '下载模板' },
  1126. { type: 'newcolumn' },
  1127. { type: 'button', name: 'dialog_cancel', value: '取消' }
  1128. ]
  1129. }
  1130. ]
  1131. }
  1132. ]
  1133. };
  1134. dialogForm = new PlgForm(dialogForm_opts);
  1135. // 初始化上传
  1136. var uploadInst = layui.upload.render({
  1137. headers: {
  1138. Authorization: localStorage.getItem('authorization')
  1139. },
  1140. elem: '.importName',
  1141. url: importUrl,
  1142. size: 10 * 1024,
  1143. accept: 'file',
  1144. exts: 'xlsx|xls',
  1145. field: 'file',
  1146. auto: false,
  1147. bindAction: '.dialog_import',
  1148. choose: function (obj) {
  1149. obj.preview(function (index, file) {
  1150. dialogForm.setItemValue('importName', file.name);
  1151. });
  1152. },
  1153. before: function () {
  1154. pload = PlgDialog.load(2, { shade: [0.6, '#fff'] });
  1155. },
  1156. done: function (res) {
  1157. PlgDialog.close(pload);
  1158. if (res.success) {
  1159. layer.closeAll();
  1160. PlgDialog.msg(res.message);
  1161. master_grid.reload();
  1162. } else {
  1163. setTimeout(() => {
  1164. PlgDialog.alert(res.message || '接口调用失败');
  1165. }, 300);
  1166. }
  1167. },
  1168. error: function () {
  1169. PlgDialog.close(pload);
  1170. PlgDialog.msg('接口调用失败');
  1171. }
  1172. });
  1173. // 表单按钮点击事件
  1174. dialogForm.attachEvent('onButtonClick', function (name) {
  1175. if (name === 'dialog_download') {
  1176. let name = downloadName || '模板文件';
  1177. Prolog.exportFile(downloadUrl, name + '.xlsx', '', 'post');
  1178. } else if (name === 'dialog_import') {
  1179. if (!dialogForm.getItemValue('importName')) {
  1180. layer.msg('请选择导入文件');
  1181. return false;
  1182. }
  1183. } else if (name === 'dialog_cancel') {
  1184. layer.closeAll();
  1185. }
  1186. });
  1187. }