2021-12-17 11:55:42 +08:00
|
|
|
import { h } from 'vue'
|
|
|
|
import { NIcon } from 'naive-ui'
|
2021-12-10 14:11:49 +08:00
|
|
|
|
|
|
|
/**
|
2021-12-14 15:53:30 +08:00
|
|
|
* * 生成一个用不重复的ID
|
2021-12-10 14:11:49 +08:00
|
|
|
* @param { Number } randomLength
|
|
|
|
*/
|
2021-12-14 15:53:30 +08:00
|
|
|
export function getUUID(randomLength: number) {
|
2021-12-10 14:11:49 +08:00
|
|
|
return Number(
|
2021-12-17 11:55:42 +08:00
|
|
|
Math.random().toString().substr(2, randomLength) + Date.now()
|
|
|
|
).toString(36)
|
2021-12-10 14:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-14 15:53:30 +08:00
|
|
|
* * render 图标
|
|
|
|
*/
|
2021-12-15 14:25:28 +08:00
|
|
|
export const renderIcon = (icon: typeof NIcon) => {
|
2021-12-17 11:55:42 +08:00
|
|
|
return () => h(NIcon, null, { default: () => h(icon) })
|
2021-12-14 15:53:30 +08:00
|
|
|
}
|
2021-12-10 14:11:49 +08:00
|
|
|
|
2021-12-15 14:25:28 +08:00
|
|
|
/**
|
|
|
|
* * 处理 vite 中无法使用 require 的问题
|
2021-12-17 11:55:42 +08:00
|
|
|
* @param name
|
|
|
|
* @returns url
|
2021-12-15 14:25:28 +08:00
|
|
|
*/
|
|
|
|
export const requireUrl = (path: string, name: string) => {
|
|
|
|
return new URL(`${path}/${name}`, import.meta.url).href
|
|
|
|
}
|
2021-12-17 11:55:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* * 存储本地会话数据
|
|
|
|
* @param k 键名
|
|
|
|
* @param v 键值
|
|
|
|
* @returns RemovableRef
|
|
|
|
*/
|
|
|
|
export const setLocalStorage = <T>(k: string, v: T) => {
|
|
|
|
try {
|
|
|
|
window.localStorage.setItem(k, JSON.stringify(v))
|
|
|
|
} catch (error) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* * 获取本地会话数据
|
|
|
|
* @returns any
|
|
|
|
*/
|
|
|
|
export const getLocalStorage: (k: string) => any = (k: string) => {
|
|
|
|
const item = window.localStorage.getItem(k)
|
|
|
|
try {
|
|
|
|
return item ? JSON.parse(item) : item
|
|
|
|
} catch (err) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* * 存储临时会话数据
|
|
|
|
* @param k 键名
|
|
|
|
* @param v 键值
|
|
|
|
* @returns RemovableRef
|
|
|
|
*/
|
|
|
|
export const setSessionStorage = <T>(k: string, v: T) => {
|
|
|
|
try {
|
|
|
|
window.sessionStorage.setItem(k, JSON.stringify(v))
|
|
|
|
} catch (error) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* * 获取临时会话数据
|
|
|
|
* @returns any
|
|
|
|
*/
|
|
|
|
export const getSessionStorage: (k: string) => any = (k: string) => {
|
|
|
|
const item = window.sessionStorage.getItem(k)
|
|
|
|
try {
|
|
|
|
return item ? JSON.parse(item) : item
|
|
|
|
} catch (err) {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|