goview_vue/src/utils/index.ts

83 lines
1.7 KiB
TypeScript
Raw Normal View History

import { h } from 'vue'
import { NIcon } from 'naive-ui'
2021-12-10 14:11:49 +08:00
/**
* * ID
2021-12-10 14:11:49 +08:00
* @param { Number } randomLength
*/
export function getUUID(randomLength: number) {
2021-12-10 14:11:49 +08:00
return Number(
Math.random().toString().substr(2, randomLength) + Date.now()
).toString(36)
2021-12-10 14:11:49 +08:00
}
/**
* * render
*/
2021-12-15 14:25:28 +08:00
export const renderIcon = (icon: typeof NIcon) => {
return () => h(NIcon, null, { default: () => h(icon) })
}
2021-12-10 14:11:49 +08:00
2021-12-15 14:25:28 +08:00
/**
* * vite 使 require
* @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
}
/**
* *
* @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
}
}