feat:first commit
This commit is contained in:
124
src/utils/util.ts
Normal file
124
src/utils/util.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import dayjs, { QUnitType } from 'dayjs'
|
||||
import duration from 'dayjs/plugin/duration'
|
||||
import md5 from 'crypto-js/md5'
|
||||
|
||||
const VUE_APP_STORAGE_PREFIX = process.env.VUE_APP_STORAGE_PREFIX
|
||||
export const setToken = (token: string) => {
|
||||
uni.setStorageSync(`${VUE_APP_STORAGE_PREFIX}_TOKEN`, token)
|
||||
}
|
||||
|
||||
export const getToken = () => {
|
||||
return uni.getStorageSync(`${VUE_APP_STORAGE_PREFIX}_TOKEN`)
|
||||
}
|
||||
|
||||
export const setUserInfo = (data: any) => {
|
||||
uni.setStorageSync(`${VUE_APP_STORAGE_PREFIX}_USER_INFO`, JSON.stringify(data))
|
||||
}
|
||||
|
||||
export const getUserInfo = () => {
|
||||
const data = uni.getStorageSync(`${VUE_APP_STORAGE_PREFIX}_USER_INFO`)
|
||||
|
||||
try {
|
||||
if (data) return JSON.parse(data)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化富文本内容,确保图片宽度自适应并移除不必要的标签属性。
|
||||
* @param {string} originalHtml - 原始的 HTML 字符串。
|
||||
* @returns {string} - 自适应图片宽度的格式化后的 HTML 字符串。
|
||||
*/
|
||||
export const formatRichText = (originalHtml: string): string => {
|
||||
if (!originalHtml) return ''
|
||||
|
||||
// 解码 HTML 实体并移除 <br/> 标签
|
||||
let formattedHtml = originalHtml
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/<br\s*\/?>/gi, '')
|
||||
|
||||
// 调整 <img> 标签以实现响应式,并移除宽度和高度的内联样式
|
||||
formattedHtml = formattedHtml.replace(/<img([^>]*?)(style="[^"]*")?([^>]*?)>/gi, (match, pre, style, post) => {
|
||||
return `<img${pre}${post} style="width:100%;height:auto;display:block;margin:0;">`
|
||||
})
|
||||
|
||||
// 确保所有内联样式使用 max-width 实现响应式
|
||||
formattedHtml = formattedHtml.replace(/<(table)?[^>]*style="([^"]*)"[^>]*>/gi, (fullMatch, isTable, styles) => {
|
||||
let responsiveStyles
|
||||
if (isTable) {
|
||||
responsiveStyles = styles.replace(/height\s*:\s*[^;]+;?/gi, 'height:auto;')
|
||||
} else {
|
||||
responsiveStyles = styles.replace(/width\s*:\s*[^;]+;?/gi, 'max-width:100%;').replace(/height\s*:\s*[^;]+;?/gi, 'height:auto;')
|
||||
}
|
||||
return fullMatch.replace(`style="${styles}"`, `style="${responsiveStyles}"`)
|
||||
})
|
||||
|
||||
// 保留 <a>、<table>、<blockquote> 等标签
|
||||
formattedHtml = formattedHtml.replace(/<(a|table|blockquote)([^>]*)>/gi, (match, tag, attrs) => {
|
||||
return `<${tag}${attrs}>`
|
||||
})
|
||||
|
||||
return formattedHtml
|
||||
}
|
||||
|
||||
// 显示toast提示
|
||||
export const showToast = (title: string = '加载中', duration: number = 2000, onClose?: Function) => {
|
||||
uni.showToast({
|
||||
title,
|
||||
duration,
|
||||
icon: 'none',
|
||||
mask: true
|
||||
})
|
||||
if (onClose) {
|
||||
setTimeout(() => {
|
||||
onClose()
|
||||
}, duration)
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化时间
|
||||
export const dateFormat = (time: string, format?: string, isTime = false) => {
|
||||
if (isTime) {
|
||||
dayjs.extend(duration)
|
||||
const timeData = dayjs.duration(time)
|
||||
return timeData.format(format)
|
||||
}
|
||||
return dayjs(time).format(format || 'YYYY-MM-DD HH:mm')
|
||||
}
|
||||
|
||||
// 求时间差
|
||||
export const diffTime = (time = new Date(), initTime = new Date(), unit: QUnitType = 'day') => {
|
||||
return dayjs(time).diff(initTime, unit)
|
||||
}
|
||||
|
||||
// 验证手机号
|
||||
export const checkMobile = (value: string) => {
|
||||
return /^1[3456789]\d{9}$/.test(value)
|
||||
}
|
||||
|
||||
// 验证邮箱
|
||||
export const checkEmail = (value: string) => {
|
||||
return /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])/.test(
|
||||
value
|
||||
)
|
||||
}
|
||||
|
||||
// 给手机号码格式化加*
|
||||
export const mobileFormat = (value: string) => {
|
||||
return value?.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
|
||||
}
|
||||
|
||||
// md5 加密
|
||||
export function encryptByMd5(password: string) {
|
||||
return md5(password).toString()
|
||||
}
|
||||
|
||||
// 深拷贝
|
||||
export const deepCopy = (obj: any) => JSON.parse(JSON.stringify(obj))
|
||||
|
||||
export const stringToObject = (data: string | object) => (typeof data === 'string' ? JSON.parse(data) : data)
|
||||
|
||||
export const API_URL = process.env.VUE_APP_BASE_API
|
||||
export const PUBLIC_PATH_URL = process.env.VUE_APP_PUBLIC_PATH_URL
|
||||
Reference in New Issue
Block a user