index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-26 15:46:42
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-09-13 17:35:33
  6. * @FilePath: \Game-Backstage-Management-System\src\utils\common\index.ts
  7. * @Description:
  8. *
  9. */
  10. import { ElMessage } from 'element-plus'
  11. // 防抖
  12. export function debounceFunc<T extends (...args: any[]) => any>(
  13. func: T,
  14. delay: number
  15. ): (...args: Parameters<T>) => void {
  16. let timer: ReturnType<typeof setTimeout> | null
  17. return (...args: Parameters<T>) => {
  18. if (timer) clearTimeout(timer)
  19. timer = setTimeout(() => {
  20. func(...args)
  21. }, delay)
  22. }
  23. }
  24. // 小数转百分比
  25. export function decimalToPercentage(decimal: number, decimalPlaces: number = 0): string {
  26. // Convert the decimal to a percentage by multiplying by 100
  27. const percentage = decimal * 100
  28. // Format the percentage to a fixed number of decimal places
  29. const formattedPercentage = percentage.toFixed(decimalPlaces)
  30. // Append the '%' symbol and return the result
  31. return `${formattedPercentage}%`
  32. }
  33. // 生成时间刻度
  34. export function generateHourlyArray(count: number) {
  35. const result = []
  36. for (let i = 0; i <= count; i++) {
  37. result.push(`${i}:00`)
  38. }
  39. return result
  40. }
  41. // 格式化时间,20240816=>2024-8-16
  42. export function formatDate(dateString: string) {
  43. // 从字符串中提取年份、月份和日期
  44. const year = dateString.slice(0, 4)
  45. const month = dateString.slice(4, 6)
  46. const day = dateString.slice(6, 8)
  47. // 将月份和日期转换为整数以去除前导零
  48. const formattedMonth = parseInt(month, 10)
  49. const formattedDay = parseInt(day, 10)
  50. // 生成新的日期字符串
  51. return `${year}-${formattedMonth}-${formattedDay}`
  52. }
  53. // 将date对象转为yaer-month-day的格式
  54. export function resetTimeToMidnight(dateTime: Date): string {
  55. // 创建一个 Date 对象来解析输入的日期时间字符串
  56. // 将时间部分设置为 00:00:00
  57. dateTime.setHours(0, 0, 0, 0)
  58. // 格式化日期为 'YYYY-MM-DD' 格式
  59. const year = dateTime.getFullYear()
  60. const month = String(dateTime.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1
  61. const day = String(dateTime.getDate()).padStart(2, '0')
  62. // 返回格式化的字符串
  63. return `${year}-${month}-${day}`
  64. }
  65. /**
  66. * @description: 生成一个打包后可以使用的url
  67. * @param {string} url 传入一个assets文件夹下的文件名
  68. * @return {*}
  69. */
  70. export const getAssetsImageUrl = (url: string) => {
  71. return new URL(`../../assets/${url}`, import.meta.url).href
  72. }
  73. /**
  74. * @description: 复制文字到剪贴板
  75. * @param {string} text
  76. * @return {*}
  77. */
  78. export const copyText = (text: string) => {
  79. return new Promise((reslove, reject) => {
  80. navigator.clipboard
  81. .writeText(text)
  82. .then(() => {
  83. ElMessage({
  84. type: 'success',
  85. message: '复制成功'
  86. })
  87. reslove(true)
  88. })
  89. .catch((err) => {
  90. ElMessage({
  91. type: 'error',
  92. message: '复制失败'
  93. })
  94. reject(err)
  95. throw new Error(err)
  96. })
  97. })
  98. }
  99. /**
  100. * @description: 用于清除reactive对象的所有属性并保持响应式
  101. * @param {Record} data 传入的对象
  102. * @return {*}
  103. */
  104. export const clearReactiveData = (data: Record<string, any>) => {
  105. Object.keys(data).forEach((key) => {
  106. delete data[key]
  107. })
  108. }
  109. /**
  110. * @description: 用于保存watch切换之前的数据
  111. * @param {any} data 需要保存的数据
  112. * @param {any} store 需要保存到的对象
  113. * @return {*}
  114. */
  115. export const saveWatchData = (data: any, store: any) => {
  116. if (Array.isArray(data)) {
  117. // 这里需要深拷贝,否则会导致引用问题
  118. store.splice(0, store.length, ...JSON.parse(JSON.stringify(data)))
  119. } else {
  120. Object.assign(store, JSON.parse(JSON.stringify(data)))
  121. }
  122. }
  123. /**
  124. * @description: 用于比较watch切换之前的数据
  125. * @param {any} data 需要比较的数据
  126. * @param {any} store 需要比较到的对象
  127. * @return {*} boolean
  128. */
  129. export const compareWatchData = (data: any, store: any): boolean => {
  130. return JSON.stringify(data) === JSON.stringify(store)
  131. }
  132. /**
  133. * @description: 模糊查询
  134. * @param {string} pattern 需要匹配的模式,即正则表达式
  135. * @param {string} text 需要被搜索的目标字符串
  136. * @param {boolean} matchCase 是否区分大小写,默认为true,即不区分大小写
  137. * @return {boolean} 返回匹配结果
  138. */
  139. export const fuzzySearch = (pattern: string, text: string, matchCase: boolean = true): boolean => {
  140. const regex = new RegExp(pattern, matchCase ? 'i' : '') // 'i' 标志表示忽略大小写
  141. return regex.test(text)
  142. }