123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /*
- * @Author: fxs bjnsfxs@163.com
- * @Date: 2024-08-26 15:46:42
- * @LastEditors: fxs bjnsfxs@163.com
- * @LastEditTime: 2024-10-19 16:35:03
- * @FilePath: \Quantity-Creation-Management-System\src\utils\common\index.ts
- * @Description:
- *
- */
- import { ElMessage } from 'element-plus'
- import type { Reactive } from 'vue'
- /**
- * @description: 包装防抖函数
- * @param {array} func 函数
- * @param {number} delay 时间
- * @return {*}
- */
- export function debounceFunc<T extends (...args: any[]) => any>(
- func: T,
- delay: number,
- ): (...args: Parameters<T>) => void {
- let timer: ReturnType<typeof setTimeout> | null
- return (...args: Parameters<T>) => {
- if (timer) clearTimeout(timer)
- timer = setTimeout(() => {
- func(...args)
- }, delay)
- }
- }
- /**
- * @description: 包装节流函数
- * @param {T} func 函数
- * @param {number} delay 时间
- * @return {*}
- */
- export function throttleFunc<T extends (...args: any[]) => any>(
- func: T,
- delay: number,
- ): (...args: Parameters<T>) => void {
- let lastCall = 0
- return function (...args: Parameters<T>) {
- const now = Date.now()
- if (now - lastCall >= delay) {
- lastCall = now
- func(...args)
- }
- }
- }
- // 小数转百分比
- export function decimalToPercentage(
- decimal: number,
- decimalPlaces: number = 0,
- ): string {
- // Convert the decimal to a percentage by multiplying by 100
- const percentage = decimal * 100
- // Format the percentage to a fixed number of decimal places
- const formattedPercentage = percentage.toFixed(decimalPlaces)
- // Append the '%' symbol and return the result
- return `${formattedPercentage}%`
- }
- // 生成时间刻度
- export function generateHourlyArray(count: number) {
- const result = []
- for (let i = 0; i <= count; i++) {
- result.push(`${i}:00`)
- }
- return result
- }
- // 格式化时间,20240816=>2024-8-16
- export function formatDate(dateString: string) {
- // 从字符串中提取年份、月份和日期
- const year = dateString.slice(0, 4)
- const month = dateString.slice(4, 6)
- const day = dateString.slice(6, 8)
- // 将月份和日期转换为整数以去除前导零
- const formattedMonth = parseInt(month, 10)
- const formattedDay = parseInt(day, 10)
- // 生成新的日期字符串
- return `${year}-${formattedMonth}-${formattedDay}`
- }
- // 将date对象转为yaer-month-day的格式
- export function resetTimeToMidnight(dateTime: Date): string {
- // 创建一个 Date 对象来解析输入的日期时间字符串
- // 将时间部分设置为 00:00:00
- dateTime.setHours(0, 0, 0, 0)
- // 格式化日期为 'YYYY-MM-DD' 格式
- const year = dateTime.getFullYear()
- const month = String(dateTime.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要加1
- const day = String(dateTime.getDate()).padStart(2, '0')
- // 返回格式化的字符串
- return `${year}-${month}-${day}`
- }
- /**
- * @description: 创建一个日期范围
- * @param {*} day 天数
- * @return {*}
- */
- export const createDateRange = (day: number): Array<Date> => {
- const end = new Date()
- const start = new Date()
- start.setTime(start.getTime() - 3600 * 1000 * 24 * day)
- return [start, end]
- }
- /**
- * @description: 判断日期范围是否有效
- * @param {string} startTime 开始时间
- * @param {string} endTime 结束时间
- * @return {boolean} 返回是否有效
- */
- export const invaildDateRange = (
- startTime: string,
- endTime: string,
- ): boolean => {
- const start = new Date(startTime).getTime()
- const end = new Date(endTime).getTime()
- // 判断日期是否有效,`isNaN` 判断生成的时间是否为 "Invalid Date"
- return !isNaN(start) && !isNaN(end) && start < end
- }
- /**
- * @description: 生成一个打包后可以使用的url
- * @param {string} url 传入一个assets文件夹下的文件名
- * @return {*}
- */
- export const getAssetsImageUrl = (url: string) => {
- return new URL(`../../assets/${url}`, import.meta.url).href
- }
- /**
- * @description: 复制文字到剪贴板
- * @param {string} text
- * @return {*}
- */
- export const copyText = (text: string) => {
- return new Promise((reslove, reject) => {
- navigator.clipboard
- .writeText(text)
- .then(() => {
- ElMessage({
- type: 'success',
- message: '复制成功',
- })
- reslove(true)
- })
- .catch(err => {
- ElMessage({
- type: 'error',
- message: '复制失败',
- })
- reject(err)
- throw new Error(err)
- })
- })
- }
- /**
- * @description: 用于清除reactive对象的所有属性并保持响应式
- * @param {Record} data 传入的对象
- * @return {*}
- */
- export const clearReactiveData = (data: Record<string, any>) => {
- Object.keys(data).forEach(key => {
- delete data[key]
- })
- }
- /**
- * @description: 用于保存watch切换之前的数据
- * @param {any} data 需要保存的数据
- * @param {any} store 需要保存到的对象
- * @return {*}
- */
- export const saveWatchData = (data: any, store: any) => {
- if (Array.isArray(data)) {
- // 这里需要深拷贝,否则会导致引用问题
- store.splice(0, store.length, ...JSON.parse(JSON.stringify(data)))
- } else {
- Object.assign(store, JSON.parse(JSON.stringify(data)))
- }
- }
- /**
- * @description: 用于比较watch切换之前的数据
- * @param {any} data 需要比较的数据
- * @param {any} store 需要比较到的对象
- * @return {*} boolean
- */
- export const compareWatchData = (data: any, store: any): boolean => {
- return JSON.stringify(data) === JSON.stringify(store)
- }
- /**
- * @description: 模糊查询
- * @param {string} pattern 需要匹配的模式,即正则表达式
- * @param {string} text 需要被搜索的目标字符串
- * @param {boolean} matchCase 是否区分大小写,默认为true,即不区分大小写
- * @return {boolean} 返回匹配结果
- */
- export const fuzzySearch = (
- pattern: string,
- text: string,
- matchCase: boolean = true,
- ): boolean => {
- const regex = new RegExp(pattern, matchCase ? 'i' : '') // 'i' 标志表示忽略大小写
- return regex.test(text)
- }
- /**
- * @description: 清空reactive对象的所有属性并保持响应式
- * @param {Reactive} obj 传入的对象
- * @return {*}
- */
- export const resetReactive = (
- obj: Reactive<{ [key: string]: any } | Array<any>>,
- ) => {
- if (Array.isArray(obj)) {
- obj.splice(0, obj.length)
- } else {
- Object.keys(obj).forEach(key => {
- delete obj[key]
- })
- }
- }
|