tool.js 33 KB

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