util.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { tenderStore, userStore } from '@/store/mobx'
  2. import { message } from 'antd'
  3. import dayjs from 'dayjs'
  4. import history from './history'
  5. function getCookie(name: string) {
  6. const prefix = name + '='
  7. const start = document.cookie.indexOf(prefix)
  8. if (start === -1) {
  9. return null
  10. }
  11. let end = document.cookie.indexOf(';', start + prefix.length)
  12. if (end === -1) {
  13. end = document.cookie.length
  14. }
  15. const value = document.cookie.substring(start + prefix.length, end)
  16. return unescape(value)
  17. }
  18. // 本地存储封装
  19. const storage = {
  20. get(key: string) {
  21. const val: string | null = localStorage.getItem(key)
  22. if (val) {
  23. return JSON.parse(val)
  24. }
  25. return null
  26. },
  27. set(key: string, value: any) {
  28. if (value) {
  29. value = JSON.stringify(value)
  30. }
  31. localStorage.setItem(key, value)
  32. },
  33. del(key: string) {
  34. localStorage.removeItem(key)
  35. }
  36. }
  37. // 节流
  38. const throttle = (fn: Function, delay: number) => {
  39. // 定义上次触发时间
  40. let last: number = 0
  41. return (...args: any[]) => {
  42. const now: number = +Date.now()
  43. console.log('call', now, last, delay)
  44. if (now > last + delay) {
  45. last = now
  46. fn.apply(this, args)
  47. }
  48. }
  49. }
  50. const debounce = (fn: Function, delay: number) => {
  51. let timer: NodeJS.Timer | null = null
  52. return (...args: any[]) => {
  53. // 判断定时器是否存在,清除定时器
  54. if (timer) {
  55. clearTimeout(Number(timer))
  56. }
  57. // 重新调用setTimeout
  58. timer = setTimeout(() => {
  59. fn.apply(this, args)
  60. }, delay)
  61. }
  62. }
  63. /**
  64. * 将子组件路径还原成带前缀的路径
  65. * @param parentPath - 父组件路径
  66. * @param pathOfTargetConfig - 用户希望访问的组件的在路由配置信息中填写的路径
  67. * @returns 拼接后的path
  68. */
  69. const combinationPath = (parentPath: string | undefined, pathOfTargetConfig: string): string => {
  70. let combinedPath = !pathOfTargetConfig.startsWith('/') ? `/${pathOfTargetConfig}` : pathOfTargetConfig
  71. combinedPath = parentPath ? `${parentPath}${combinedPath}` : combinedPath
  72. return combinedPath
  73. }
  74. /**
  75. * 日期格式化
  76. * @param format - 格式
  77. */
  78. const dayjsFormat = (date: dayjs.ConfigType, format: string = 'YYYY-MM-DD HH:mm:ss') => {
  79. if (date === "0001-01-01 00:00:00") return ''
  80. return dayjs(date).format(format)
  81. }
  82. /**
  83. * 生成随机密码
  84. * @param len - 长度
  85. */
  86. const generatePsw = (len: number): string => {
  87. const pasArr = [
  88. 'a',
  89. 'b',
  90. 'c',
  91. 'd',
  92. 'e',
  93. 'f',
  94. 'g',
  95. 'h',
  96. 'i',
  97. 'j',
  98. 'k',
  99. 'l',
  100. 'm',
  101. 'n',
  102. 'o',
  103. 'p',
  104. 'q',
  105. 'r',
  106. 's',
  107. 't',
  108. 'u',
  109. 'v',
  110. 'w',
  111. 'x',
  112. 'y',
  113. 'z',
  114. 'A',
  115. 'B',
  116. 'C',
  117. 'D',
  118. 'E',
  119. 'F',
  120. 'G',
  121. 'H',
  122. 'I',
  123. 'J',
  124. 'K',
  125. 'L',
  126. 'M',
  127. 'N',
  128. 'O',
  129. 'P',
  130. 'Q',
  131. 'R',
  132. 'S',
  133. 'T',
  134. 'U',
  135. 'V',
  136. 'W',
  137. 'X',
  138. 'Y',
  139. 'Z',
  140. '0',
  141. '1',
  142. '2',
  143. '3',
  144. '4',
  145. '5',
  146. '6',
  147. '7',
  148. '8',
  149. '9',
  150. '_',
  151. '-',
  152. '$',
  153. '%',
  154. '&',
  155. '@',
  156. '+',
  157. '!'
  158. ]
  159. let password = ''
  160. for (let i = 0; i < len; i++) {
  161. const x = Math.floor(Math.random() * pasArr.length)
  162. password += pasArr[x]
  163. }
  164. return password
  165. }
  166. // 用于审批日志的日期的显示
  167. const formatDate = (d: string) => {
  168. if (!d) return ''
  169. const date = new Date(d)
  170. const year = date.getFullYear()
  171. let mon: number | string = date.getMonth() + 1
  172. let day: number | string = date.getDate()
  173. let hour: number | string = date.getHours()
  174. let minute: number | string = date.getMinutes()
  175. let scond: number | string = date.getSeconds()
  176. if (mon < 10) {
  177. mon = '0' + mon.toString()
  178. }
  179. if (day < 10) {
  180. day = '0' + day.toString()
  181. }
  182. if (hour < 10) {
  183. hour = '0' + hour.toString()
  184. }
  185. if (minute < 10) {
  186. minute = '0' + minute.toString()
  187. }
  188. if (scond < 10) {
  189. scond = '0' + scond.toString()
  190. }
  191. return `<span>${year}</span><span>${mon}-${day}</span><span>${hour}:${minute}:${scond}</span>`
  192. }
  193. // 数字千分位
  194. const formatMoney = (num: number | undefined) => {
  195. if (!num) return '0.00'
  196. return (Math.round(num) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
  197. }
  198. // 控制是否有权限进入标段
  199. const handleIntoBidsection = (permission_type: 'contract' | 'safe' | 'quality'): void => {
  200. const permission = !!tenderStore.tender.permission[permission_type].access
  201. if (permission) {
  202. history.push(`/console/${permission_type}/content/summary`)
  203. } else {
  204. message.error('暂无权限进入此标段,请联系管理员!')
  205. }
  206. }
  207. export { getCookie, storage, throttle, debounce, combinationPath, dayjsFormat, generatePsw, formatDate, formatMoney, handleIntoBidsection }