tool.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. import moment from 'moment'
  2. import { getToken } from './request'
  3. import { GlobalConstant } from '@/constant'
  4. import DictCache from '@/utils/dict'
  5. import {
  6. message
  7. } from 'ant-design-vue'
  8. const BaseTool = {}
  9. BaseTool.Constant = {
  10. labelCol: {
  11. xs: { span: 24 },
  12. sm: { span: 8 } // 2的倍数
  13. },
  14. labelCol2: {
  15. xs: { span: 4 },
  16. sm: { span: 4 } // 2的倍数
  17. },
  18. labelCol3: {
  19. xs: { span: 6 },
  20. sm: { span: 6 } // 2的倍数
  21. },
  22. wrapperCol: {
  23. xs: { span: 24 },
  24. sm: { span: 13 }
  25. },
  26. wrapperCol2: {
  27. xs: { span: 18 },
  28. sm: { span: 18 }
  29. },
  30. wrapperCol3: {
  31. xs: { span: 24 },
  32. sm: { span: 15 }
  33. },
  34. row: {
  35. gutter: 16
  36. },
  37. INVEST: 'INVEST',
  38. FINANCE: 'FINANCE',
  39. FINANCING: 'FINANCING',
  40. FILE_URL: 'http://sb.pc.litian2017.com',
  41. HOST: window.location.protocol + '//' + window.location.host,
  42. scrollY: document.documentElement.clientHeight < 680 ? document.documentElement.clientHeight * 0.4 : document.documentElement.clientHeight * 0.5
  43. }
  44. // 时间对象
  45. BaseTool.Moment = moment
  46. BaseTool.Date = {
  47. PICKER_NORM_YEAR: 'YYYY',
  48. PICKER_NORM_MONTH: 'MM',
  49. PICKER_NORM_YEAR_MONTH: 'YYYY-MM',
  50. PICKER_NORM_DATE_PATTERN: 'YYYY-MM-DD',
  51. PICKER_NORM_TIME_PATTERN: 'HH:mm:ss',
  52. PICKER_NORM_DATETIME_PATTERN: 'YYYY-MM-DD HH:mm:ss',
  53. /**
  54. * 将字符串日期格式化
  55. * @param date
  56. * @param pattern
  57. * @returns {string}
  58. */
  59. formatter (date, pattern) {
  60. if (BaseTool.Object.isBlank(date)) {
  61. return ''
  62. } else {
  63. return BaseTool.Moment(date, BaseTool.Date.PICKER_NORM_DATETIME_PATTERN).format(pattern)
  64. }
  65. },
  66. getCountBetween (date1, date2, type = 1) {
  67. const startTime = new Date(date1) // 开始时间
  68. const endTime = new Date(date2) // 结束时间
  69. const usedTime = endTime - startTime // 相差的毫秒数
  70. const days = Math.floor(usedTime / (24 * 3600 * 1000)) // 计算出天数
  71. const leavel = usedTime % (24 * 3600 * 1000) // 计算天数后剩余的时间
  72. const hours = Math.floor(leavel / (3600 * 1000)) // 计算剩余的小时数
  73. const leavel2 = leavel % (3600 * 1000) // 计算剩余小时后剩余的毫秒数
  74. const minutes = Math.floor(leavel2 / (60 * 1000)) // 计算剩余的分钟数
  75. if (type === 1) {
  76. return days
  77. }
  78. if (type === 2) {
  79. return hours
  80. }
  81. if (type === 3) {
  82. return minutes
  83. }
  84. }
  85. }
  86. BaseTool.String = {
  87. isBlank (text) {
  88. return text === null || text === undefined || text === 'undefined' || text === ''
  89. },
  90. isNotBlank (text) {
  91. return !this.isBlank(text)
  92. },
  93. uuid () {
  94. const s = []
  95. const hexDigits = '0123456789abcdef'
  96. for (let i = 0; i < 36; i++) {
  97. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  98. }
  99. s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  100. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  101. // s[8] = s[13] = s[18] = s[23] = '-'
  102. s[8] = s[13] = s[18] = s[23] = ''
  103. const uuid = s.join('')
  104. return uuid
  105. }
  106. }
  107. BaseTool.Object = {
  108. isBlank (text) {
  109. if (typeof text === 'string') {
  110. return BaseTool.String.isBlank(text)
  111. }
  112. return text === null || text === undefined
  113. },
  114. isNotBlank (text) {
  115. return !this.isBlank(text)
  116. },
  117. /**
  118. * 获取对象属性
  119. * @param obj
  120. * @param field
  121. * @returns {*}
  122. */
  123. getField (obj, field) {
  124. if (BaseTool.String.isBlank(obj) || BaseTool.String.isBlank(field)) {
  125. return ''
  126. }
  127. return obj[field]
  128. },
  129. /**
  130. * 深克隆
  131. * @param data
  132. * @returns {{}|any}
  133. */
  134. copy: function (data) {
  135. if (BaseTool.Object.isBlank(data)) {
  136. return {}
  137. }
  138. return JSON.parse(JSON.stringify(data))
  139. }
  140. }
  141. BaseTool.Table = {
  142. defaultOptions (that) {
  143. return {
  144. alert: { show: true, clear: () => { that.selectedRowKeys = [] } },
  145. rowSelection: {
  146. selectedRowKeys: that.selectedRowKeys,
  147. onChange: that.onSelectChange
  148. }
  149. }
  150. },
  151. tableOption (that) {
  152. if (!that.optionAlertShow) {
  153. that.options = {
  154. alert: { show: true, clear: () => { that.selectedRowKeys = [] } },
  155. rowSelection: {
  156. selectedRowKeys: that.selectedRowKeys,
  157. onChange: that.onSelectChange,
  158. getCheckboxProps: record => ({
  159. props: {
  160. disabled: false,
  161. name: record.id
  162. }
  163. })
  164. }
  165. }
  166. that.optionAlertShow = true
  167. } else {
  168. that.options = {
  169. alert: false,
  170. rowSelection: null
  171. }
  172. that.optionAlertShow = false
  173. }
  174. },
  175. getMapText (obj, field) {
  176. if (BaseTool.String.isBlank(obj) || BaseTool.String.isBlank(field)) {
  177. return ''
  178. }
  179. return obj[field]
  180. },
  181. getMapTextForMoneyType (obj, field) {
  182. if (BaseTool.String.isBlank(obj) || BaseTool.String.isBlank(field)) {
  183. return ''
  184. }
  185. return '(币种:' + obj[field] + ')'
  186. },
  187. colSpan (value, col) {
  188. return {
  189. children: value,
  190. attrs: {
  191. colSpan: col
  192. }
  193. }
  194. },
  195. rowSpan (value, row) {
  196. return {
  197. children: value,
  198. attrs: {
  199. rowSpan: row
  200. }
  201. }
  202. },
  203. rowSpanDiv (value, row) {
  204. return {
  205. children: BaseTool.Amount.formatter(BaseTool.Number.div(value, 10000, 4)),
  206. attrs: {
  207. rowSpan: row
  208. }
  209. }
  210. },
  211. allSpan (value, col, row) {
  212. return {
  213. children: value,
  214. attrs: {
  215. colSpan: col,
  216. rowSpan: row
  217. }
  218. }
  219. },
  220. statusCustomRender (vm, text, record, type) {
  221. if (BaseTool.Object.isBlank(text)) {
  222. return ''
  223. }
  224. const label = DictCache.getLabelByValue(DictCache.TYPE[type], text)
  225. const status1 = DictCache.COLOR[type][text]
  226. const myProps = { status: status1, text: label }
  227. return vm.$createElement('Badge', { ...{ props: { ...myProps } } })
  228. },
  229. statusCustomRenderWhether (vm, text) {
  230. if (BaseTool.Object.isBlank(text)) {
  231. return vm.$createElement('Badge', { ...{ props: { status: 'error', text: '否' } } })
  232. }
  233. const label = text ? '是' : '否'
  234. const status1 = text ? 'success' : 'error'
  235. const myProps = { status: status1, text: label }
  236. return vm.$createElement('Badge', { ...{ props: { ...myProps } } })
  237. },
  238. statusCustomRenderDict (vm, text, record, colorMap, dictMap) {
  239. if (BaseTool.Object.isBlank(text)) {
  240. return ''
  241. }
  242. const label = dictMap[text]
  243. const status1 = colorMap[text]
  244. const myProps = { status: status1, text: label }
  245. return vm.$createElement('Badge', { ...{ props: { ...myProps } } })
  246. },
  247. statusCustomRenderTypeDict (vm, text, record, type, dictMap) {
  248. if (BaseTool.Object.isBlank(text)) {
  249. return ''
  250. }
  251. const label = dictMap[text]
  252. const status1 = DictCache.COLOR[type][text]
  253. const myProps = { status: status1, text: label }
  254. return vm.$createElement('Badge', { ...{ props: { ...myProps } } })
  255. },
  256. statusCustomRenderFun (vm, fun) {
  257. const myProps = fun()
  258. return vm.$createElement('Badge', { ...{ props: { ...myProps } } })
  259. },
  260. customRenderWidth: (vm, text, width) => {
  261. const content = {
  262. content: props => vm.$createElement('div', text)
  263. }
  264. const textDom = vm.$createElement('div', {
  265. style: {
  266. maxWidth: width + ' !important',
  267. overflow: 'hidden',
  268. whiteSpace: 'nowrap',
  269. textOverflow: 'ellipsis',
  270. cursor: 'pointer'
  271. }
  272. }, text)
  273. return vm.$createElement('a-popover', { scopedSlots: content, attrs: { placement: 'top' } }, [textDom])
  274. },
  275. customRenderWidthDefault: (vm, text) => {
  276. return BaseTool.Table.customRenderWidth(vm, text, '150px')
  277. },
  278. customCellWidth: (text, width) => {
  279. return {
  280. attrs: {
  281. title: text
  282. },
  283. style: {
  284. maxWidth: width + ' !important',
  285. overflow: 'hidden',
  286. whiteSpace: 'nowrap',
  287. textOverflow: 'ellipsis',
  288. cursor: 'pointer'
  289. }
  290. }
  291. },
  292. customCellDefault: (text) => {
  293. return BaseTool.Table.customCellWidth(text, '150px')
  294. },
  295. customRenderDictValue (vm, text, record, dictMap) {
  296. if (BaseTool.Object.isBlank(text)) {
  297. return ''
  298. }
  299. return dictMap[text]
  300. },
  301. customRenderMultiDictValue (vm, text, record, dictMap) {
  302. if (BaseTool.Object.isBlank(text)) {
  303. return ''
  304. }
  305. const values = text.split(',')
  306. const texts = new Array()
  307. values.forEach((item, index, array) => {
  308. texts.push(dictMap[item])
  309. })
  310. return texts.join(',')
  311. }
  312. }
  313. BaseTool.ListForm = {
  314. clearOneList: (vm) => {
  315. vm.$store.dispatch('setOneListAdd', [])
  316. vm.$store.dispatch('setOneListUpdate', [])
  317. vm.$store.dispatch('setOneListDelete', [])
  318. },
  319. setOneListValues: (vm, values) => {
  320. values.updateList = vm.$store.getters.oneListUpdate
  321. values.deleteList = vm.$store.getters.oneListDelete
  322. values.addList = vm.$store.getters.oneListAdd
  323. console.log(values.addList)
  324. },
  325. setTwoListValues: (vm, values, id) => {
  326. values.updateList = vm.$store.getters.twoListUpdate[id]
  327. values.deleteList = vm.$store.getters.twoListDelete[id]
  328. values.addList = vm.$store.getters.twoListAdd[id]
  329. // values.addList = addList
  330. // // 将删除表里面排除 删除后又添加进来的 适用于同一个明细的 比如采购申请 采购单 合同登
  331. // const addListMap = {}
  332. // if (addList) {
  333. // addList.forEach((item) => {
  334. // addListMap[item.id] = item
  335. // })
  336. // }
  337. // const result = []
  338. // if (deleteList) {
  339. // deleteList.forEach((item) => {
  340. // const elem = addListMap[item.id]
  341. // if (!elem) {
  342. // result.push(item)
  343. // }
  344. // })
  345. // }
  346. // values.deleteList = result
  347. },
  348. pushOneListAdd: (vm, item) => {
  349. if (!item.exist) {
  350. const oneListAdd = vm.$store.getters.oneListAdd
  351. oneListAdd.push(item)
  352. vm.$store.dispatch('setOneListAdd', oneListAdd)
  353. }
  354. },
  355. pushOneListAddMore: (vm, items) => {
  356. const oneListAdd = vm.$store.getters.oneListAdd
  357. const addListMap = {}
  358. oneListAdd.forEach((item) => {
  359. addListMap[item.id] = item
  360. })
  361. items.forEach((item) => {
  362. if (!item.exist) {
  363. const elem = addListMap[item.id]
  364. if (!elem) {
  365. oneListAdd.push(item)
  366. }
  367. }
  368. })
  369. vm.$store.dispatch('setOneListAdd', oneListAdd)
  370. },
  371. pushOneListAddMoreForEdit: (vm, items, id) => {
  372. const oneListAdd = vm.$store.getters.oneListAdd
  373. const addListMap = {}
  374. oneListAdd.forEach((item) => {
  375. addListMap[item[id]] = item
  376. })
  377. items.forEach((item) => {
  378. if (!item.exist) {
  379. const elem = addListMap[item[id]]
  380. if (!elem) {
  381. oneListAdd.push(item)
  382. }
  383. }
  384. })
  385. console.log(oneListAdd)
  386. vm.$store.dispatch('setOneListAdd', oneListAdd)
  387. },
  388. pushTwoListAddMore: (vm, items, id) => {
  389. const twoListAddMap = vm.$store.getters.twoListAdd
  390. let twoListAdd = twoListAddMap[id]
  391. const addListMap = {}
  392. if (twoListAdd) {
  393. twoListAdd.forEach((item) => {
  394. addListMap[item.id] = item
  395. })
  396. } else {
  397. twoListAdd = []
  398. }
  399. items.forEach((item) => {
  400. if (!item.exist) {
  401. const elem = addListMap[item.id]
  402. if (!elem) {
  403. twoListAdd.push(item)
  404. }
  405. }
  406. })
  407. twoListAddMap[id] = twoListAdd
  408. vm.$store.dispatch('setTwoListAdd', twoListAddMap)
  409. },
  410. pushOneListUpdate: (vm, item) => {
  411. const oneListAdd = vm.$store.getters.oneListAdd
  412. let flag = true
  413. for (let i = 0; i < oneListAdd.length; i++) {
  414. const value = oneListAdd[i]
  415. if (item.id === value.id) {
  416. oneListAdd[i] = item
  417. flag = false
  418. }
  419. }
  420. if (!flag) {
  421. vm.$store.dispatch('setOneListAdd', oneListAdd)
  422. } else {
  423. const oneListUpdate = vm.$store.getters.oneListUpdate
  424. const updateListMap = {}
  425. oneListUpdate.forEach((item) => {
  426. updateListMap[item.id] = item
  427. })
  428. const elem = updateListMap[item.id]
  429. if (!elem) {
  430. oneListUpdate.push(item)
  431. } else {
  432. for (let i = 0; i < oneListUpdate.length; i++) {
  433. const one = oneListUpdate[i]
  434. if (one.id === item.id) {
  435. oneListUpdate[i] = item
  436. break
  437. }
  438. }
  439. }
  440. vm.$store.dispatch('setOneListUpdate', oneListUpdate)
  441. }
  442. },
  443. pushOneListDelete: (vm, items) => {
  444. const oneListDelete = vm.$store.getters.oneListDelete
  445. const oneListDeleteMap = {}
  446. items.forEach((item) => {
  447. oneListDelete.push(item)
  448. oneListDeleteMap[item.id] = item
  449. })
  450. vm.$store.dispatch('setOneListDelete', oneListDelete)
  451. const oneListAdd = vm.$store.getters.oneListAdd
  452. const result = []
  453. oneListAdd.forEach((item) => {
  454. const elem = oneListDeleteMap[item.id]
  455. if (!elem) {
  456. result.push(item)
  457. }
  458. })
  459. vm.$store.dispatch('setOneListAdd', result)
  460. },
  461. clearTwoList: (vm) => {
  462. vm.$store.dispatch('setTwoListAdd', {})
  463. vm.$store.dispatch('setTwoListUpdate', {})
  464. vm.$store.dispatch('setTwoListDelete', {})
  465. },
  466. initTwoList: (vm, id) => {
  467. console.log(id)
  468. const twoListAddMap = vm.$store.getters.twoListAdd
  469. const twoListAdd = twoListAddMap[id]
  470. if (!twoListAdd) {
  471. twoListAddMap[id] = []
  472. }
  473. vm.$store.dispatch('setTwoListAdd', twoListAddMap)
  474. const twoListUpdateMap = vm.$store.getters.twoListUpdate
  475. const twoListUpdate = twoListUpdateMap[id]
  476. if (!twoListUpdate) {
  477. twoListUpdateMap[id] = []
  478. }
  479. vm.$store.dispatch('setTwoListUpdate', twoListUpdateMap)
  480. const twoListDeleteMap = vm.$store.getters.twoListDelete
  481. const twoListDelete = twoListDeleteMap[id]
  482. if (!twoListDelete) {
  483. twoListDeleteMap[id] = []
  484. }
  485. vm.$store.dispatch('setTwoListDelete', twoListDeleteMap)
  486. },
  487. pushTwoListAdd: (vm, item, id) => {
  488. const twoListAddMap = vm.$store.getters.twoListAdd
  489. const twoListAdd = twoListAddMap[id]
  490. twoListAdd.push(item)
  491. twoListAddMap[id] = twoListAdd
  492. vm.$store.dispatch('setTwoListAdd', twoListAddMap)
  493. },
  494. pushTwoListUpdate: (vm, item, id) => {
  495. const twoListAddMap = vm.$store.getters.twoListAdd
  496. const twoListAdd = twoListAddMap[id]
  497. let flag = true
  498. for (let i = 0; i < twoListAdd.length; i++) {
  499. const value = twoListAdd[i]
  500. if (item.id === value.id) {
  501. twoListAdd[i] = item
  502. flag = false
  503. }
  504. }
  505. if (!flag) {
  506. twoListAddMap[id] = twoListAdd
  507. vm.$store.dispatch('setTwoListAdd', twoListAddMap)
  508. } else {
  509. const twoListUpdateMap = vm.$store.getters.twoListUpdate
  510. const twoListUpdate = twoListUpdateMap[id]
  511. const updateListMap = {}
  512. twoListUpdate.forEach((item) => {
  513. updateListMap[item.id] = item
  514. })
  515. const elem = updateListMap[item.id]
  516. if (!elem) {
  517. twoListUpdate.push(item)
  518. twoListUpdateMap[id] = twoListUpdate
  519. vm.$store.dispatch('setTwoListUpdate', twoListUpdateMap)
  520. }
  521. }
  522. },
  523. pushTwoListDelete: (vm, items, id) => {
  524. const twoListDeleteMap = vm.$store.getters.twoListDelete
  525. let twoListDelete = twoListDeleteMap[id]
  526. if (!twoListDelete) {
  527. twoListDelete = []
  528. }
  529. const listDeleteMap = {}
  530. items.forEach((item) => {
  531. twoListDelete.push(item)
  532. listDeleteMap[item.id] = item
  533. })
  534. twoListDeleteMap[id] = twoListDelete
  535. vm.$store.dispatch('setTwoListDelete', twoListDeleteMap)
  536. const twoListAddMap = vm.$store.getters.twoListAdd
  537. const twoListAdd = twoListAddMap[id]
  538. const result = []
  539. if (twoListAdd) {
  540. twoListAdd.forEach((item) => {
  541. const elem = listDeleteMap[item.id]
  542. if (!elem) {
  543. result.push(item)
  544. }
  545. })
  546. }
  547. twoListAddMap[id] = result
  548. vm.$store.dispatch('setTwoListAdd', twoListAddMap)
  549. },
  550. getOneListHandledRows: (vm, rows) => {
  551. const oneListAdd = vm.$store.getters.oneListAdd
  552. const oneListUpdate = vm.$store.getters.oneListUpdate
  553. const oneListUpdateMap = {}
  554. oneListUpdate.forEach((item) => {
  555. oneListUpdateMap[item.id] = item
  556. })
  557. const oneListDelete = vm.$store.getters.oneListDelete
  558. const oneListDeleteMap = {}
  559. oneListDelete.forEach((item) => {
  560. oneListDeleteMap[item.id] = item
  561. })
  562. const result = []
  563. rows.forEach((item) => {
  564. item.exist = 1
  565. const id = item.id
  566. let elem = oneListDeleteMap[id]
  567. if (!elem) {
  568. elem = oneListUpdateMap[id]
  569. if (elem) {
  570. result.push(elem)
  571. } else {
  572. result.push(item)
  573. }
  574. }
  575. })
  576. const resultMap = {}
  577. result.forEach((item) => {
  578. resultMap[item.id] = item
  579. })
  580. oneListAdd.forEach((item) => {
  581. const elem = resultMap[item.id]
  582. if (!elem) {
  583. result.splice(0, 0, item)
  584. }
  585. })
  586. return result
  587. },
  588. getOneListHandledRowsForEdit: (vm, rows, id) => {
  589. const oneListDelete = vm.$store.getters.oneListDelete
  590. const oneListDeleteMap = {}
  591. oneListDelete.forEach((item) => {
  592. oneListDeleteMap[item[id]] = item
  593. })
  594. const result = []
  595. const resultMap = {}
  596. rows.forEach((item) => {
  597. const i = item[id]
  598. const elem = oneListDeleteMap[i]
  599. if (!elem) {
  600. resultMap[i] = item
  601. result.push(item)
  602. }
  603. })
  604. const oneListAdd = vm.$store.getters.oneListAdd
  605. oneListAdd.forEach((item) => {
  606. const elem = resultMap[item[id]]
  607. if (!elem) {
  608. result.splice(0, 0, item)
  609. resultMap[item[id]] = item
  610. }
  611. })
  612. // rows.forEach((item) => {
  613. // const elem = resultMap[item[id]]
  614. // if (!elem) {
  615. // result.push(item)
  616. // }
  617. // })
  618. return result
  619. },
  620. getTwoListHandledRows: (vm, rows, id) => {
  621. const twoListAdd = vm.$store.getters.twoListAdd[id]
  622. const twoListUpdate = vm.$store.getters.twoListUpdate[id]
  623. const twoListUpdateMap = {}
  624. if (twoListUpdate) {
  625. twoListUpdate.forEach((item) => {
  626. twoListUpdateMap[item.id] = item
  627. })
  628. }
  629. const twoListDelete = vm.$store.getters.twoListDelete[id]
  630. const twoListDeleteMap = {}
  631. if (twoListDelete) {
  632. twoListDelete.forEach((item) => {
  633. twoListDeleteMap[item.id] = item
  634. })
  635. }
  636. const result = []
  637. rows.forEach((item) => {
  638. item.exist = 1
  639. const id = item.id
  640. let elem = twoListDeleteMap[id]
  641. if (!elem) {
  642. elem = twoListUpdateMap[id]
  643. if (elem) {
  644. result.push(elem)
  645. } else {
  646. result.push(item)
  647. }
  648. }
  649. })
  650. const resultMap = {}
  651. result.forEach((item) => {
  652. resultMap[item.id] = item
  653. })
  654. if (twoListAdd) {
  655. twoListAdd.forEach((item) => {
  656. const elem = resultMap[item.id]
  657. if (!elem) {
  658. result.splice(0, 0, item)
  659. }
  660. })
  661. }
  662. return result
  663. }
  664. }
  665. BaseTool.Amount = {
  666. /**
  667. * 元换算为万元
  668. * @param num :
  669. * @returns {number} :
  670. */
  671. divTenThousand (num) {
  672. if (num == null) {
  673. return 0
  674. }
  675. return BaseTool.Number.div(num, 10000, 3)
  676. // return num
  677. },
  678. /**
  679. * 元换算为万元,并格式化
  680. * @param num :
  681. * @returns {string} :
  682. */
  683. divTenThousandFormatter (num) {
  684. if (BaseTool.String.isBlank(num)) {
  685. return num
  686. }
  687. return this.formatter(this.divTenThousand(num))
  688. },
  689. /**
  690. * 格式化金额
  691. * @param value
  692. * @returns {string}
  693. */
  694. formatter: value => {
  695. if (BaseTool.String.isBlank(value)) {
  696. return value
  697. }
  698. return `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  699. },
  700. /**
  701. * 格式化利率
  702. * @param value
  703. * @returns {string|*}
  704. */
  705. formatterRate: value => {
  706. if (BaseTool.String.isBlank(value)) {
  707. return value
  708. }
  709. return value + '%'
  710. },
  711. /**
  712. * 去掉格式化金额
  713. * @param value
  714. * @returns {*}
  715. */
  716. parser: value => value.replace(/\$\s?|(,*)/g, ''),
  717. /**
  718. * 万元换算为元
  719. * @param num
  720. * @returns {*}
  721. */
  722. mulTenThousand (num) {
  723. if (BaseTool.String.isBlank(num)) {
  724. return 0
  725. }
  726. return BaseTool.Number.mul(num, 10000, 6)
  727. // return num
  728. },
  729. /**
  730. * 元换算为万元,并格式化
  731. * @param num :
  732. * @returns {string} :
  733. */
  734. default (value) {
  735. if (BaseTool.String.isBlank(value) || value === 0) {
  736. return '0.00'
  737. }
  738. return value
  739. },
  740. /**
  741. * 元换算为万元,并格式化
  742. * @param num :
  743. * @returns {string} :
  744. */
  745. defaultDivTenThousandFormatter (num) {
  746. if (BaseTool.String.isBlank(num) || num === 0) {
  747. return '0.00'
  748. }
  749. return this.formatter(this.divTenThousand(num))
  750. }
  751. }
  752. BaseTool.Number = {
  753. /**
  754. * 校验费率最大值是否大于100 与 小于0
  755. * 由于校验框架对值校验需要用a-input-number组件,否则是按照字符串校验
  756. * @param rule
  757. * @param value
  758. * @param callback
  759. * @param max
  760. * @param min
  761. */
  762. rateValidatorMax: (rule, value, callback) => {
  763. if (BaseTool.Object.isBlank(rule.min)) {
  764. rule.min = 0
  765. }
  766. if (BaseTool.Object.isBlank(rule.max)) {
  767. rule.max = 100
  768. }
  769. const myValue = parseFloat(value)
  770. if (myValue > rule.max) {
  771. callback(new Error(rule.messageBefore + '不能大于' + rule.max))
  772. } else if (myValue < rule.min) {
  773. callback(new Error(rule.messageBefore + '不能小于' + rule.min))
  774. } else {
  775. callback()
  776. }
  777. },
  778. numberValidatorMax: (rule, value, callback) => {
  779. const myValue = parseFloat(value)
  780. if (!BaseTool.Object.isBlank(rule.max) && myValue > rule.max) {
  781. callback(new Error(rule.messageBefore + '不能大于' + rule.max))
  782. } else if (!BaseTool.Object.isBlank(rule.min) && myValue < rule.min) {
  783. callback(new Error(rule.messageBefore + '不能小于' + rule.min))
  784. } else {
  785. callback()
  786. }
  787. },
  788. add (arg1, arg2) {
  789. if (BaseTool.String.isBlank(arg1)) {
  790. arg1 = 0
  791. }
  792. if (BaseTool.String.isBlank(arg2)) {
  793. arg2 = 0
  794. }
  795. let r1, r2
  796. try {
  797. r1 = arg1.toString().split('.')[1].length
  798. } catch (e) {
  799. r1 = 0
  800. }
  801. try {
  802. r2 = arg2.toString().split('.')[1].length
  803. } catch (e) {
  804. r2 = 0
  805. }
  806. const m = Math.pow(10, Math.max(r1, r2))
  807. const n = (r1 >= r2) ? r1 : r2
  808. return ((arg1 * m + arg2 * m) / m).toFixed(n)
  809. },
  810. sub (arg1, arg2) {
  811. if (BaseTool.String.isBlank(arg1)) {
  812. arg1 = 0
  813. }
  814. if (BaseTool.String.isBlank(arg2)) {
  815. arg2 = 0
  816. }
  817. let re1, re2
  818. try {
  819. re1 = arg1.toString().split('.')[1].length
  820. } catch (e) {
  821. re1 = 0
  822. }
  823. try {
  824. re2 = arg2.toString().split('.')[1].length
  825. } catch (e) {
  826. re2 = 0
  827. }
  828. const m = Math.pow(10, Math.max(re1, re2))
  829. const n = (re1 >= re2) ? re1 : re2
  830. return ((arg1 * m - arg2 * m) / m).toFixed(n)
  831. },
  832. mul (arg1, arg2) {
  833. if (BaseTool.String.isBlank(arg1)) {
  834. arg1 = 0
  835. }
  836. if (BaseTool.String.isBlank(arg2)) {
  837. arg2 = 0
  838. }
  839. let m = 0
  840. const s1 = arg1.toString()
  841. const s2 = arg2.toString()
  842. try {
  843. m += s1.split('.')[1].length
  844. } catch (e) {}
  845. try {
  846. m += s2.split('.')[1].length
  847. } catch (e) {}
  848. return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
  849. },
  850. /**
  851. * 除法 注意如果整除了 小数点将无效
  852. * @param arg1 : 被除数
  853. * @param arg2 : 除数
  854. * @param digit :精确位数
  855. * @returns {number}
  856. */
  857. div (arg1, arg2, digit) {
  858. if (BaseTool.String.isBlank(arg1)) {
  859. arg1 = 0
  860. }
  861. if (BaseTool.String.isBlank(arg2)) {
  862. arg2 = 1
  863. }
  864. let t1 = 0
  865. let t2 = 0
  866. try { t1 = arg1.toString().split('.')[1].length } catch (e) {}
  867. try { t2 = arg2.toString().split('.')[1].length } catch (e) {}
  868. const r1 = Number(arg1.toString().replace('.', ''))
  869. const r2 = Number(arg2.toString().replace('.', ''))
  870. // 获取小数点后的计算值
  871. const result = ((r1 / r2) * Math.pow(10, t2 - t1)).toString()
  872. let result2 = result.split('.')[1]
  873. if (result2 !== undefined) {
  874. result2 = result2.substring(0, digit > result2.length ? result2.length : digit)
  875. } else {
  876. result2 = ''
  877. }
  878. while (result2.length < digit) {
  879. result2 = result2 + '0'
  880. }
  881. if (digit === 0) {
  882. return Number(result.split('.')[0])
  883. }
  884. return Number(result.split('.')[0] + '.' + result2)
  885. },
  886. /**
  887. * 除法 注意如果整除了 小数点将无效
  888. * @param arg1 : 被除数
  889. * @param arg2 : 除数
  890. * @param digit :精确位数
  891. * @returns {number}
  892. */
  893. divForDollar (arg1, arg2, digit) {
  894. return '(' + BaseTool.Amount.formatter(BaseTool.Number.div(arg1, arg2, digit)) + '美元)'
  895. }
  896. }
  897. BaseTool.UPLOAD = {
  898. /**
  899. * 上传文件校验是否为文件
  900. * @param file
  901. * @param successBack
  902. * @param errorFun
  903. * @returns {Promise<boolean>|boolean}
  904. */
  905. isFileUpload (file, successBack, errorFun) {
  906. const reg = /\.(doc|docx|pdf|zip|xls|xlsx)(\?.*)?$/
  907. if (reg.test(file.name)) {
  908. if (!BaseTool.Object.isBlank(successBack)) {
  909. successBack(file)
  910. }
  911. return true
  912. } else {
  913. if (!BaseTool.Object.isBlank(errorFun)) {
  914. errorFun()
  915. } else {
  916. message.error(`请上传正确的word、pdf、docx、zip、xls、xlsx文件`)
  917. }
  918. // reject(new Error('请上传正确的word文件或pdf文件'))
  919. return new Promise((resolve, reject) => {
  920. reject(new Error('请上传正确的word、pdf、docx、zip、xls、xlsx文件'))
  921. return false
  922. })
  923. }
  924. },
  925. /**
  926. * 上传文件校验是否为图片
  927. * @param file
  928. * @param successBack
  929. * @param errorFun
  930. * @returns {Promise<boolean>|boolean}
  931. */
  932. isImageUpload (file, successBack, errorFun) {
  933. const reg = /\.(jpeg|jpg|png|gif)(\?.*)?$/
  934. if (reg.test(file.name)) {
  935. if (!BaseTool.Object.isBlank(successBack)) {
  936. successBack(file)
  937. }
  938. return true
  939. } else {
  940. if (!BaseTool.Object.isBlank(errorFun)) {
  941. errorFun()
  942. } else {
  943. message.error(`请上传正确的图片`)
  944. }
  945. // reject(new Error('请上传正确的word文件或pdf文件'))
  946. return new Promise((resolve, reject) => {
  947. reject(new Error('请上传正确的图片'))
  948. return false
  949. })
  950. }
  951. },
  952. downLoadExportExcel (data) {
  953. const contentDistribution = data.headers['content-disposition']
  954. // attachment;filename=%E6%8A%95%E8%B5%84%E9%A1%B9%E7%9B%AE%E4%BF%A1%E6%81%AF20200202162009965.xls
  955. const fileName = decodeURI(contentDistribution.replace('attachment;filename=', ''))
  956. if (window.navigator.msSaveOrOpenBlob) {
  957. navigator.msSaveBlob(data, fileName)
  958. }
  959. const blob = new Blob([data.data])
  960. const elink = document.createElement('a')
  961. elink.download = fileName
  962. elink.style.display = 'none'
  963. elink.href = URL.createObjectURL(blob)
  964. document.body.appendChild(elink)
  965. elink.click()
  966. URL.revokeObjectURL(elink.href)// 释放 URL 对象
  967. document.body.removeChild(elink)
  968. },
  969. transImg (imgArr = []) {
  970. const temp = []
  971. if (BaseTool.Object.isBlank(imgArr)) {
  972. return temp
  973. }
  974. imgArr.forEach((item) => {
  975. if (BaseTool.String.isNotBlank(item.url)) {
  976. temp.push({
  977. url: GlobalConstant.BaseImageUrl + item.url,
  978. uid: item.id,
  979. name: item.name,
  980. value: item
  981. })
  982. }
  983. })
  984. return temp
  985. },
  986. filePreview (file) {
  987. if (file.url == null) {
  988. return
  989. }
  990. window.open(process.env.VUE_APP_PREVIEW_URL + BaseTool.Constant.HOST + BaseTool.Constant.FILE_URL + file.url)
  991. },
  992. getUploadFileDTO (fileList = [], type = Number) {
  993. // 添加图片
  994. const length = fileList.length
  995. const temp = []
  996. if (length >= 1) {
  997. for (let i = 0; i < length; i++) {
  998. let data = {}
  999. if (fileList[i].response) {
  1000. data = fileList[i].response.data
  1001. } else {
  1002. data = fileList[i].value
  1003. }
  1004. const fileDTO = {}
  1005. fileDTO.type = type
  1006. fileDTO.name = data.name
  1007. fileDTO.fileName = data.fileName
  1008. fileDTO.fileFormat = data.fileFormat
  1009. fileDTO.url = data.url
  1010. fileDTO.id = data.id || fileList[i].uid
  1011. temp.push(fileDTO)
  1012. }
  1013. }
  1014. return temp
  1015. },
  1016. getUploadOneFileDTO (file, type = Number) {
  1017. // 添加图片
  1018. const temp = []
  1019. if (file != null) {
  1020. let data = {}
  1021. if (file.response) {
  1022. data = file.response.data
  1023. } else {
  1024. data = file
  1025. }
  1026. const fileDTO = {}
  1027. fileDTO.type = type
  1028. fileDTO.name = data.name
  1029. fileDTO.fileName = data.fileName
  1030. fileDTO.fileFormat = data.fileFormat
  1031. fileDTO.url = data.url
  1032. temp.push(fileDTO)
  1033. }
  1034. return temp
  1035. },
  1036. handleFileChange ({ file, fileList }) {
  1037. const result = {}
  1038. result.defaultFileList = fileList
  1039. result.success = false
  1040. if (file.status === 'done') {
  1041. result.fileList = BaseTool.UPLOAD.getUploadFileDTO(fileList, GlobalConstant.FileTypeFile)
  1042. result.success = true
  1043. } else if (file.status === 'removed') {
  1044. result.fileList = BaseTool.UPLOAD.getUploadFileDTO(fileList, GlobalConstant.FileTypeFile)
  1045. result.success = true
  1046. } else if (file.status === 'error') {
  1047. message.error('上传失败')
  1048. }
  1049. return result
  1050. },
  1051. handleImageChange ({ file, fileList }) {
  1052. const result = {}
  1053. result.defaultImageList = fileList
  1054. result.success = false
  1055. if (file.status === 'done') {
  1056. result.imageList = BaseTool.UPLOAD.getUploadFileDTO(fileList, GlobalConstant.FileTypeImg)
  1057. result.success = true
  1058. } else if (file.status === 'removed') {
  1059. result.imageList = BaseTool.UPLOAD.getUploadFileDTO(fileList, GlobalConstant.FileTypeImg)
  1060. result.success = true
  1061. } else if (file.status === 'error') {
  1062. message.error('上传失败')
  1063. } else {
  1064. }
  1065. return result
  1066. }
  1067. }
  1068. BaseTool.Util = {
  1069. _isMobile () {
  1070. const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  1071. return flag
  1072. },
  1073. getToken (url) {
  1074. return getToken(url)
  1075. },
  1076. /**
  1077. * 页面合并单元格
  1078. * @param mergeMap :合并信息对象
  1079. * @param keys :获取的key
  1080. * @returns {{}|number} :
  1081. */
  1082. getSpan (mergeMap = {}, ...keys) {
  1083. if (mergeMap == null) {
  1084. return 1
  1085. }
  1086. let map = mergeMap
  1087. for (let i = 0; i < keys.length; i++) {
  1088. if (map[keys[i]] == null) {
  1089. return 1
  1090. }
  1091. map = map[keys[i]]
  1092. }
  1093. return map
  1094. },
  1095. /**
  1096. * 根据不同类型获取行数据
  1097. * @param record :
  1098. * @param key :
  1099. * @returns {string|*}
  1100. */
  1101. getRecordText (record, key) {
  1102. let result = ''
  1103. if (record.data == null) {
  1104. return ''
  1105. }
  1106. if (record.type === 1) {
  1107. result = record.data[key]
  1108. } else {
  1109. if (record.data[key] == null) {
  1110. return ''
  1111. }
  1112. result = record.data[key]['data']
  1113. }
  1114. return result
  1115. },
  1116. /**
  1117. * 获取携带合并单元格的行数据
  1118. * @param record :
  1119. * @param key :
  1120. * @returns {{children: null, attrs: {rowSpan: ({}|number), colSpan: ({}|number)}}}
  1121. */
  1122. getCustomRender (record, key) {
  1123. const obj = {
  1124. children: null,
  1125. attrs: {
  1126. rowSpan: this.getSpan(record['mergeMap'], key, 'rowSpan'),
  1127. colSpan: this.getSpan(record['mergeMap'], key, 'cellSpan')
  1128. }
  1129. }
  1130. obj.children = this.getRecordText(record, key)
  1131. return obj
  1132. },
  1133. /**
  1134. * 获取携带合并单元格的行数据 ,并对类型进行格式化
  1135. * @param record :
  1136. * @param key :
  1137. * @param type: 1表示string 2表示格式化金额 3表示万元之后格式金额
  1138. * @returns {{children: null, attrs: {rowSpan: ({}|number), colSpan: ({}|number)}}}
  1139. */
  1140. getCustomRenderFormat (record, key, type) {
  1141. const obj = {
  1142. children: null,
  1143. attrs: {
  1144. rowSpan: this.getSpan(record['mergeMap'], key, 'rowSpan'),
  1145. colSpan: this.getSpan(record['mergeMap'], key, 'cellSpan')
  1146. }
  1147. }
  1148. let result = this.getRecordText(record, key)
  1149. if (type === 2) {
  1150. result = BaseTool.Amount.formatter(result)
  1151. }
  1152. if (type === 3) {
  1153. result = BaseTool.Amount.divTenThousandFormatter(result)
  1154. }
  1155. obj.children = result
  1156. return obj
  1157. },
  1158. getCustomRenderMultiDictValue (text, dictMap) {
  1159. if (BaseTool.Object.isBlank(text)) {
  1160. return ''
  1161. }
  1162. const values = text.split(',')
  1163. const texts = new Array()
  1164. values.forEach((item, index, array) => {
  1165. texts.push(dictMap[item])
  1166. })
  1167. return texts.join(',')
  1168. },
  1169. getCustomRenderDictValue (text, dictMap) {
  1170. if (BaseTool.Object.isBlank(text)) {
  1171. return ''
  1172. }
  1173. return dictMap[text]
  1174. }
  1175. }
  1176. BaseTool.TREE = {
  1177. treeFilter (list = [], f = (item) => { return item }) {
  1178. const r = []
  1179. list.forEach(item => {
  1180. if (item.children && item.children.length > 0) {
  1181. item.children = this.treeFilter(item.children, f)
  1182. }
  1183. const i = f(item)
  1184. if (i) {
  1185. if (!i.delete) {
  1186. r.push(i)
  1187. }
  1188. } else {
  1189. r.push(item)
  1190. }
  1191. })
  1192. return r
  1193. }
  1194. }
  1195. BaseTool.ANTD = {
  1196. selectFilterOption (input, option) {
  1197. return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  1198. }
  1199. }
  1200. export default BaseTool