prologutil.js 31 KB

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