util.ts 4.2 KB

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