index.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-26 15:46:42
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-11-27
  6. * @Description:
  7. *
  8. */
  9. import { ElMessage } from 'element-plus'
  10. /**
  11. * 包装防抖函数
  12. *
  13. * @param {array} func 函数
  14. * @param {number} delay 时间
  15. */
  16. export function debounceFunc<T extends (...args: any[]) => any>(
  17. func: T,
  18. delay: number
  19. ): (...args: Parameters<T>) => void {
  20. let timer: ReturnType<typeof setTimeout> | null
  21. return (...args: Parameters<T>) => {
  22. if (timer) clearTimeout(timer)
  23. timer = setTimeout(() => {
  24. func(...args)
  25. }, delay)
  26. }
  27. }
  28. /**
  29. * 节流函数
  30. *
  31. * @param func 函数
  32. * @param delay 延迟
  33. */
  34. export function throttleFunc<T extends (...args: any[]) => any>(
  35. func: T,
  36. delay: number
  37. ): (...args: Parameters<T>) => void {
  38. let lastCall = 0
  39. return function (...args: Parameters<T>) {
  40. const now = Date.now()
  41. if (now - lastCall >= delay) {
  42. lastCall = now
  43. func(...args)
  44. }
  45. }
  46. }
  47. /**
  48. * 将小数转换为百分比字符串
  49. *
  50. * @param decimal 小数
  51. * @param decimalPlaces 保留几位小数
  52. */
  53. export function decimalToPercentage(decimal: number, decimalPlaces: number = 0): 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. * 格式化时间,如20240816=>2024-8-16
  63. *
  64. * @param dateString
  65. */
  66. export function formatDate(dateString: string) {
  67. // 从字符串中提取年份、月份和日期
  68. const year = dateString.slice(0, 4)
  69. const month = dateString.slice(4, 6)
  70. const day = dateString.slice(6, 8)
  71. // 将月份和日期转换为整数以去除前导零
  72. const formattedMonth = parseInt(month, 10)
  73. const formattedDay = parseInt(day, 10)
  74. if (year === '' || isNaN(formattedMonth) || isNaN(formattedDay)) {
  75. return '无'
  76. }
  77. // 生成新的日期字符串
  78. return `${year}-${formattedMonth}-${formattedDay}`
  79. }
  80. /**
  81. * 将时间转为year-month-day的格式
  82. *
  83. * @param dateTime Date对象
  84. */
  85. export function resetTimeToMidnight(dateTime: Date): string {
  86. // 创建一个 Date 对象来解析输入的日期时间字符串
  87. // 将时间部分设置为 00:00:00
  88. dateTime.setHours(0, 0, 0, 0)
  89. // 格式化日期为 'YYYY-MM-DD' 格式
  90. const year = dateTime.getFullYear()
  91. const month = String(dateTime.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1
  92. const day = String(dateTime.getDate()).padStart(2, '0')
  93. // 返回格式化的字符串
  94. return `${year}-${month}-${day}`
  95. }
  96. /**
  97. * 创建一个日期范围
  98. *
  99. * @param {*} day 天数
  100. */
  101. export const createDateRange = (day: number): [Date, Date] => {
  102. const end = new Date()
  103. const start = new Date()
  104. start.setTime(start.getTime() - 3600 * 1000 * 24 * day)
  105. return [start, end]
  106. }
  107. /**
  108. * 将时间戳转为日期字符串yyyy-MM-dd HH:mm:ss
  109. *
  110. * 根据isMilliseconds参数判断是否为毫秒,默认为秒级时间戳
  111. *
  112. * @param timestamp 时间戳
  113. * @param isMilliseconds 是否为毫秒级时间戳
  114. */
  115. export const formatTimestamp = (timestamp: number, isMilliseconds: boolean = false): string => {
  116. if (timestamp === 0) return '无'
  117. // 将秒级时间戳转换为毫秒级时间戳
  118. const adjustedTimestamp = isMilliseconds ? timestamp : timestamp * 1000
  119. const date = new Date(adjustedTimestamp)
  120. // 格式化为 yyyy-MM-dd HH:mm:ss
  121. const year = date.getFullYear()
  122. const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从 0 开始
  123. const day = String(date.getDate()).padStart(2, '0')
  124. const hours = String(date.getHours()).padStart(2, '0')
  125. const minutes = String(date.getMinutes()).padStart(2, '0')
  126. const seconds = String(date.getSeconds()).padStart(2, '0')
  127. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  128. }
  129. /**
  130. * 获取时间戳范围,时间戳从0:00开始,到23:59结束
  131. * @param dateRange 日期范围
  132. */
  133. export function getRangeTimestamps(dateRange: Date[]): [number, number] {
  134. const [startDate, endDate] = dateRange
  135. // 确保日期有效
  136. if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
  137. throw new Error('Invalid date')
  138. }
  139. // 设置开始日期为当天 UTC 00:00:00
  140. const rangeStart = new Date(startDate)
  141. rangeStart.setHours(0, 0, 0, 0)
  142. // 设置结束日期为当天 UTC 23:59:59.999
  143. const rangeEnd = new Date(endDate)
  144. rangeEnd.setHours(23, 59, 59, 999)
  145. // 返回时间戳
  146. return [rangeStart.getTime(), rangeEnd.getTime()]
  147. }
  148. /**
  149. * 复制文字到剪贴板
  150. *
  151. * @param {string} text
  152. */
  153. export const copyText = (text: string) => {
  154. return new Promise((resolve, reject) => {
  155. navigator.clipboard
  156. .writeText(text)
  157. .then(() => {
  158. ElMessage({
  159. type: 'success',
  160. message: '复制成功'
  161. })
  162. resolve(true)
  163. })
  164. .catch((err) => {
  165. ElMessage({
  166. type: 'error',
  167. message: '复制失败'
  168. })
  169. reject(err)
  170. throw new Error(err)
  171. })
  172. })
  173. }
  174. /**
  175. * 用于保存watch切换之前的数据
  176. *
  177. * @param {any} data 需要保存的数据
  178. * @param {any} store 需要保存到的对象
  179. */
  180. export const saveWatchData = (data: any, store: any) => {
  181. if (Array.isArray(data)) {
  182. // 这里需要深拷贝,否则会导致引用问题
  183. store.splice(0, store.length, ...JSON.parse(JSON.stringify(data)))
  184. } else {
  185. Object.assign(store, JSON.parse(JSON.stringify(data)))
  186. }
  187. }
  188. /**
  189. * 用于比较watch切换之前的数据
  190. *
  191. * @param {any} data 需要比较的数据
  192. * @param {any} store 需要比较到的对象
  193. */
  194. export const compareWatchData = (data: any, store: any): boolean => {
  195. return JSON.stringify(data) === JSON.stringify(store)
  196. }
  197. /**
  198. * 模糊查询
  199. * @param {string} pattern 需要匹配的模式,即正则表达式
  200. * @param {string} text 需要被搜索的目标字符串
  201. * @param {boolean} matchCase 是否区分大小写,默认为true,即不区分大小写
  202. */
  203. export const fuzzySearch = (pattern: string, text: string, matchCase: boolean = true): boolean => {
  204. const regex = new RegExp(pattern, matchCase ? 'i' : '') // 'i' 标志表示忽略大小写
  205. return regex.test(text)
  206. }
  207. /**
  208. * 生成一个随机文件名,以当前时间为种子,生成16位随机字符串
  209. */
  210. export const generateRandomFileName = () => {
  211. const timestamp = Date.now().toString(36) // 将时间戳转换为36进制字符串
  212. const randomString = Math.random().toString(36).substring(2, 8) // 生成8位随机字符串
  213. return timestamp + randomString
  214. }
  215. /**
  216. * 生成随机颜色
  217. * @param count 生成颜色的数量
  218. */
  219. export function generateUniqueColors(count: number) {
  220. const colors = []
  221. // 基础参数:控制颜色柔和度
  222. const baseSaturation = 90 // 饱和度范围 65-75%(避免过艳)
  223. const baseLightness = 70 // 亮度范围 65-75%(避免过深或过亮)
  224. for (let i = 0; i < count; i++) {
  225. // 1. 色相均匀分布(核心区分度)
  226. const hue = ((i * 360) / count) % 360
  227. // 2. 饱和度和亮度微调(增加次区分度)
  228. const saturation = baseSaturation + ((i % 3) - 1) * 5 // 波动 ±5%
  229. const lightness = baseLightness + ((i % 2) - 0.5) * 10 // 奇数项稍暗,偶数项稍亮
  230. // 3. 约束范围(确保不过深/过亮)
  231. const clampedSaturation = Math.min(75, Math.max(65, saturation))
  232. const clampedLightness = Math.min(75, Math.max(65, lightness))
  233. colors.push(`hsl(${hue}, ${clampedSaturation}%, ${clampedLightness}%)`)
  234. }
  235. return colors
  236. }
  237. export const getFileIconBySuffix = (filename: string, fileType: 'folder' | 'file'): string => {
  238. const iconBasePath = '/img/fileIcon/'
  239. if (fileType === 'folder') return iconBasePath + 'folder.png'
  240. const suffix = filename.split('.').pop()?.toLowerCase() || ''
  241. switch (suffix) {
  242. case 'json':
  243. return iconBasePath + 'json-file.png'
  244. case 'png':
  245. case 'jpg':
  246. case 'jpeg':
  247. case 'gif':
  248. return iconBasePath + 'img-file.png'
  249. case 'txt':
  250. return iconBasePath + 'txt-file.png'
  251. case 'mp3':
  252. case 'wav':
  253. return iconBasePath + 'mp3-file.png'
  254. default:
  255. return iconBasePath + 'unknow-file.png'
  256. }
  257. }
  258. // 快速选择日期
  259. export const shortcutsDate = [
  260. {
  261. text: '上一周',
  262. value: () => createDateRange(7)
  263. },
  264. {
  265. text: '上个月',
  266. value: () => createDateRange(30)
  267. },
  268. {
  269. text: '近三个月',
  270. value: () => createDateRange(90)
  271. }
  272. ]