index.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-26 15:46:42
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-10-19 16:35:03
  6. * @FilePath: \Quantity-Creation-Management-System\src\utils\common\index.ts
  7. * @Description:
  8. *
  9. */
  10. import { ElMessage } from 'element-plus'
  11. import type { Reactive } from 'vue'
  12. /**
  13. * @description: 包装防抖函数
  14. * @param {array} func 函数
  15. * @param {number} delay 时间
  16. * @return {*}
  17. */
  18. export function debounceFunc<T extends (...args: any[]) => any>(
  19. func: T,
  20. delay: number,
  21. ): (...args: Parameters<T>) => void {
  22. let timer: ReturnType<typeof setTimeout> | null
  23. return (...args: Parameters<T>) => {
  24. if (timer) clearTimeout(timer)
  25. timer = setTimeout(() => {
  26. func(...args)
  27. }, delay)
  28. }
  29. }
  30. /**
  31. * @description: 包装节流函数
  32. * @param {T} func 函数
  33. * @param {number} delay 时间
  34. * @return {*}
  35. */
  36. export function throttleFunc<T extends (...args: any[]) => any>(
  37. func: T,
  38. delay: number,
  39. ): (...args: Parameters<T>) => void {
  40. let lastCall = 0
  41. return function (...args: Parameters<T>) {
  42. const now = Date.now()
  43. if (now - lastCall >= delay) {
  44. lastCall = now
  45. func(...args)
  46. }
  47. }
  48. }
  49. // 小数转百分比
  50. export function decimalToPercentage(
  51. decimal: number,
  52. decimalPlaces: number = 0,
  53. ): string {
  54. // Convert the decimal to a percentage by multiplying by 100
  55. const percentage = decimal * 100
  56. // Format the percentage to a fixed number of decimal places
  57. const formattedPercentage = percentage.toFixed(decimalPlaces)
  58. // Append the '%' symbol and return the result
  59. return `${formattedPercentage}%`
  60. }
  61. // 生成时间刻度
  62. export function generateHourlyArray(count: number) {
  63. const result = []
  64. for (let i = 0; i <= count; i++) {
  65. result.push(`${i}:00`)
  66. }
  67. return result
  68. }
  69. // 格式化时间,20240816=>2024-8-16
  70. export function formatDate(dateString: string) {
  71. // 从字符串中提取年份、月份和日期
  72. const year = dateString.slice(0, 4)
  73. const month = dateString.slice(4, 6)
  74. const day = dateString.slice(6, 8)
  75. // 将月份和日期转换为整数以去除前导零
  76. const formattedMonth = parseInt(month, 10)
  77. const formattedDay = parseInt(day, 10)
  78. // 生成新的日期字符串
  79. return `${year}-${formattedMonth}-${formattedDay}`
  80. }
  81. // 将date对象转为yaer-month-day的格式
  82. export function resetTimeToMidnight(dateTime: Date): string {
  83. // 创建一个 Date 对象来解析输入的日期时间字符串
  84. // 将时间部分设置为 00:00:00
  85. dateTime.setHours(0, 0, 0, 0)
  86. // 格式化日期为 'YYYY-MM-DD' 格式
  87. const year = dateTime.getFullYear()
  88. const month = String(dateTime.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1
  89. const day = String(dateTime.getDate()).padStart(2, '0')
  90. // 返回格式化的字符串
  91. return `${year}-${month}-${day}`
  92. }
  93. /**
  94. * @description: 创建一个日期范围
  95. * @param {*} day 天数
  96. * @return {*}
  97. */
  98. export const createDateRange = (day: number): Array<Date> => {
  99. const end = new Date()
  100. const start = new Date()
  101. start.setTime(start.getTime() - 3600 * 1000 * 24 * day)
  102. return [start, end]
  103. }
  104. /**
  105. * @description: 判断日期范围是否有效
  106. * @param {string} startTime 开始时间
  107. * @param {string} endTime 结束时间
  108. * @return {boolean} 返回是否有效
  109. */
  110. export const invaildDateRange = (
  111. startTime: string,
  112. endTime: string,
  113. ): boolean => {
  114. const start = new Date(startTime).getTime()
  115. const end = new Date(endTime).getTime()
  116. // 判断日期是否有效,`isNaN` 判断生成的时间是否为 "Invalid Date"
  117. return !isNaN(start) && !isNaN(end) && start < end
  118. }
  119. /**
  120. * @description: 生成一个打包后可以使用的url
  121. * @param {string} url 传入一个assets文件夹下的文件名
  122. * @return {*}
  123. */
  124. export const getAssetsImageUrl = (url: string) => {
  125. return new URL(`../../assets/${url}`, import.meta.url).href
  126. }
  127. /**
  128. * @description: 复制文字到剪贴板
  129. * @param {string} text
  130. * @return {*}
  131. */
  132. export const copyText = (text: string) => {
  133. return new Promise((reslove, reject) => {
  134. navigator.clipboard
  135. .writeText(text)
  136. .then(() => {
  137. ElMessage({
  138. type: 'success',
  139. message: '复制成功',
  140. })
  141. reslove(true)
  142. })
  143. .catch(err => {
  144. ElMessage({
  145. type: 'error',
  146. message: '复制失败',
  147. })
  148. reject(err)
  149. throw new Error(err)
  150. })
  151. })
  152. }
  153. /**
  154. * @description: 用于清除reactive对象的所有属性并保持响应式
  155. * @param {Record} data 传入的对象
  156. * @return {*}
  157. */
  158. export const clearReactiveData = (data: Record<string, any>) => {
  159. Object.keys(data).forEach(key => {
  160. delete data[key]
  161. })
  162. }
  163. /**
  164. * @description: 用于保存watch切换之前的数据
  165. * @param {any} data 需要保存的数据
  166. * @param {any} store 需要保存到的对象
  167. * @return {*}
  168. */
  169. export const saveWatchData = (data: any, store: any) => {
  170. if (Array.isArray(data)) {
  171. // 这里需要深拷贝,否则会导致引用问题
  172. store.splice(0, store.length, ...JSON.parse(JSON.stringify(data)))
  173. } else {
  174. Object.assign(store, JSON.parse(JSON.stringify(data)))
  175. }
  176. }
  177. /**
  178. * @description: 用于比较watch切换之前的数据
  179. * @param {any} data 需要比较的数据
  180. * @param {any} store 需要比较到的对象
  181. * @return {*} boolean
  182. */
  183. export const compareWatchData = (data: any, store: any): boolean => {
  184. return JSON.stringify(data) === JSON.stringify(store)
  185. }
  186. /**
  187. * @description: 模糊查询
  188. * @param {string} pattern 需要匹配的模式,即正则表达式
  189. * @param {string} text 需要被搜索的目标字符串
  190. * @param {boolean} matchCase 是否区分大小写,默认为true,即不区分大小写
  191. * @return {boolean} 返回匹配结果
  192. */
  193. export const fuzzySearch = (
  194. pattern: string,
  195. text: string,
  196. matchCase: boolean = true,
  197. ): boolean => {
  198. const regex = new RegExp(pattern, matchCase ? 'i' : '') // 'i' 标志表示忽略大小写
  199. return regex.test(text)
  200. }
  201. /**
  202. * @description: 清空reactive对象的所有属性并保持响应式
  203. * @param {Reactive} obj 传入的对象
  204. * @return {*}
  205. */
  206. export const resetReactive = (
  207. obj: Reactive<{ [key: string]: any } | Array<any>>,
  208. ) => {
  209. if (Array.isArray(obj)) {
  210. obj.splice(0, obj.length)
  211. } else {
  212. Object.keys(obj).forEach(key => {
  213. delete obj[key]
  214. })
  215. }
  216. }