index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import CryptoJS from 'crypto-js'
  2. import { ORDER_STATUS } from '@/const'
  3. const payStatus = {
  4. WAIT_PAY: {
  5. label: '待支付',
  6. type: 'primary'
  7. },
  8. PAYED: {
  9. label: '已支付',
  10. type: 'success'
  11. },
  12. CLOSED: {
  13. label: '已关闭',
  14. type: 'info'
  15. },
  16. CANCELED: {
  17. label: '已取消',
  18. type: 'info'
  19. },
  20. REFUNDED: {
  21. label: '已退款',
  22. type: 'danger'
  23. },
  24. REFUNDING: {
  25. label: '退款中',
  26. type: 'warning'
  27. }
  28. }
  29. export function getPayStatus (str, attr = 'label') {
  30. return payStatus[str] ? payStatus[str][attr] : ''
  31. }
  32. export function getOrderStatus (str, attr = 'label') {
  33. return ORDER_STATUS[str] ? ORDER_STATUS[str][attr] : ''
  34. }
  35. export function parseTime (time, cFormat) {
  36. if (arguments.length === 0) {
  37. return null
  38. }
  39. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  40. let date
  41. if (typeof time === 'object') {
  42. date = time
  43. } else {
  44. if (('' + time).length === 10) time = parseInt(time) * 1000
  45. date = new Date(time)
  46. }
  47. const formatObj = {
  48. y: date.getFullYear(),
  49. m: date.getMonth() + 1,
  50. d: date.getDate(),
  51. h: date.getHours(),
  52. i: date.getMinutes(),
  53. s: date.getSeconds(),
  54. a: date.getDay()
  55. }
  56. const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  57. let value = formatObj[key]
  58. if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
  59. if (result.length > 0 && value < 10) {
  60. value = '0' + value
  61. }
  62. return value || 0
  63. })
  64. return timeStr
  65. }
  66. export function param2Obj (url) {
  67. const search = url.split('?')[1]
  68. if (!search) {
  69. return {}
  70. }
  71. return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
  72. }
  73. // 设置表格总计行高亮
  74. export function totalRowClassName ({row, rowIndex}) {
  75. for (let key in row) {
  76. if (row[key] === '总计') {
  77. return 'tableTotalRow'
  78. }
  79. if ((row[key] + '').indexOf('小计') > -1) {
  80. return 'tableTotalRow tableTotalRow1'
  81. }
  82. if ((row[key] + '').indexOf('总计') > -1) {
  83. return 'tableTotalRow tableTotalRow2'
  84. }
  85. }
  86. }
  87. const desKey = 'fnFACVwC'
  88. const key = CryptoJS.enc.Hex.parse('TgxRS8DG65EjK8cpKTiPAw==') // 密钥
  89. const iv = CryptoJS.enc.Hex.parse('') // 偏移量
  90. // aes加密
  91. export function encrypt (word) {
  92. let encrypted = ''
  93. if (typeof word === 'string') {
  94. const key = CryptoJS.enc.Utf8.parse(desKey)
  95. console.log(word, desKey, 'aaaa')
  96. encrypted = CryptoJS.DES.encrypt(word, key, {
  97. mode: CryptoJS.mode.ECB,
  98. padding: CryptoJS.pad.Pkcs7
  99. })
  100. return encrypted.toString()
  101. // const srcs = CryptoJS.enc.Hex.parse(word)
  102. // // var encryptedBase64Str = CryptoJS.enc.Base64.stringify(srcs)
  103. // encrypted = CryptoJS.AES.encrypt(srcs, key, {
  104. // mode: CryptoJS.mode.ECB,
  105. // padding: CryptoJS.pad.ZeroPadding
  106. // })
  107. } else if (typeof word === 'object') {
  108. // 对象格式的转成json字符串
  109. const data = JSON.stringify(word)
  110. const srcs = CryptoJS.enc.Utf8.parse(data)
  111. encrypted = CryptoJS.AES.encrypt(srcs, key, {
  112. iv: iv,
  113. mode: CryptoJS.mode.ECB,
  114. padding: CryptoJS.pad.Pkcs7
  115. })
  116. }
  117. return encrypted.ciphertext.toString()
  118. }
  119. // aes解密
  120. export function decrypt (word) {
  121. const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
  122. const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  123. const decrypt = CryptoJS.AES.decrypt(srcs, key, {
  124. iv: iv,
  125. mode: CryptoJS.mode.CBC,
  126. padding: CryptoJS.pad.Pkcs7
  127. })
  128. const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  129. return decryptedStr.toString()
  130. }