goview_vue/src/utils/utils.ts

148 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-12-20 14:29:29 +08:00
import { h } from 'vue'
import { NIcon } from 'naive-ui'
import screenfull from 'screenfull'
2022-02-03 22:54:31 +08:00
import throttle from 'lodash/throttle'
2021-12-20 14:29:29 +08:00
/**
2022-01-16 22:17:34 +08:00
* * ID
2021-12-20 14:29:29 +08:00
* @param { Number } randomLength
*/
2022-01-25 22:29:44 +08:00
export function getUUID(randomLength = 10) {
2021-12-20 14:29:29 +08:00
return Number(
Math.random().toString().substr(2, randomLength) + Date.now()
).toString(36)
}
/**
* * render
* @param icon
* @param set
2021-12-20 14:29:29 +08:00
*/
export const renderIcon = (icon: any, set = {}) => {
return () => h(NIcon, set, { default: () => h(icon) })
}
/**
* * render
* @param lang
* @param set
* @param tag
*/
export const renderLang = (lang: string, set = {}, tag = 'span') => {
return () => h(tag, set, { default: () => window['$t'](lang) })
}
2021-12-20 14:29:29 +08:00
/**
2021-12-20 18:06:08 +08:00
* * vite 使 require utils
* @param path
2021-12-20 14:29:29 +08:00
* @param name
* @returns url
*/
export const requireUrl = (path: string, name: string) => {
return new URL(`${path}/${name}`, import.meta.url).href
}
2021-12-21 10:06:03 +08:00
2021-12-20 18:06:08 +08:00
/**
* * 404
* @param path
* @param name
* @returns url
*/
export const requireFallbackImg = (path?: string, name?: string) => {
const url = path && name
2021-12-21 10:06:03 +08:00
return new URL(
url ? `${path}/${name}` : '../assets/images/exception/image-404.png',
import.meta.url
).href
2021-12-20 18:06:08 +08:00
}
2021-12-20 14:29:29 +08:00
/**
2022-01-20 21:25:35 +08:00
* *
* @param isFullscreen
* @param isEnabled
* @returns
*/
2021-12-20 14:29:29 +08:00
export const screenfullFn = (isFullscreen?: boolean, isEnabled?: boolean) => {
// 是否是全屏
if (isFullscreen) return screenfull.isFullscreen
// 是否支持全屏
if (isEnabled) return screenfull.isEnabled
if (screenfull.isEnabled) {
screenfull.toggle()
return
}
// TODO lang
window['$message'].warning('您的浏览器不支持全屏功能!')
}
2021-12-21 10:06:03 +08:00
2022-01-20 21:25:35 +08:00
/**
* *
* @param HTMLElement
* @param key
* @param value
*/
2022-01-23 19:22:54 +08:00
export const setDomAttribute = <K extends keyof CSSStyleDeclaration, V extends CSSStyleDeclaration[K]>(
2022-01-20 21:25:35 +08:00
HTMLElement: HTMLElement,
2022-01-23 19:22:54 +08:00
key: K,
value: V
2022-01-20 21:25:35 +08:00
) => {
if (HTMLElement) {
HTMLElement.style[key] = value
}
}
2022-02-06 21:35:38 +08:00
2022-02-03 22:54:31 +08:00
/**
* * mac
* @returns boolean
*/
export const isMac = () => {
return /macintosh|mac os x/i.test(navigator.userAgent)
}
2022-02-06 21:35:38 +08:00
/**
* * file转base64
*/
export const fileTobase64 = (file:File, callback: Function) => {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e: ProgressEvent<FileReader>) {
if(e.target) {
let base64 = e.target.result;
callback(base64)
}
};
};
2021-12-21 10:06:03 +08:00
/**
* *
*/
2022-02-03 22:54:31 +08:00
export const addEventListener = <K extends keyof WindowEventMap>(
target: HTMLElement | Document,
2021-12-21 10:06:03 +08:00
type: K,
listener: any,
2021-12-21 10:06:03 +08:00
options?: boolean | AddEventListenerOptions | undefined
) => {
if (!target) return
2021-12-21 10:06:03 +08:00
target.addEventListener(
type,
2022-02-03 22:54:31 +08:00
throttle(listener, 300, {
2021-12-21 10:06:03 +08:00
leading: true,
trailing: false
}),
options
)
}
2021-12-21 10:06:03 +08:00
/**
* *
*/
2022-02-03 22:54:31 +08:00
export const removeEventListener = <K extends keyof WindowEventMap>(
target: HTMLElement | Document,
2021-12-21 10:06:03 +08:00
type: K,
2022-02-03 22:54:31 +08:00
listener: any
2021-12-21 10:06:03 +08:00
) => {
if (!target) return
2021-12-21 10:06:03 +08:00
target.removeEventListener(type, listener)
}