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'
|
2022-02-28 20:26:04 +08:00
|
|
|
|
import Image_404 from '../assets/images/exception/image-404.png'
|
2022-04-05 19:01:52 +08:00
|
|
|
|
import html2canvas from 'html2canvas'
|
|
|
|
|
import { downloadByA } from './file'
|
2021-12-20 14:29:29 +08:00
|
|
|
|
|
2022-03-24 14:19:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* * 判断是否是开发环境
|
|
|
|
|
* @return { Boolean }
|
|
|
|
|
*/
|
|
|
|
|
export const isDev = () => {
|
|
|
|
|
return import.meta.env.DEV
|
|
|
|
|
}
|
|
|
|
|
|
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-02-28 20:26:04 +08:00
|
|
|
|
export const getUUID = (randomLength = 10) => {
|
2021-12-20 14:29:29 +08:00
|
|
|
|
return Number(
|
|
|
|
|
Math.random().toString().substr(2, randomLength) + Date.now()
|
|
|
|
|
).toString(36)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* * render 图标
|
2022-01-08 10:30:45 +08:00
|
|
|
|
* @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) })
|
|
|
|
|
}
|
2022-01-08 10:30:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* * 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
|
|
|
|
|
|
|
|
|
/**
|
2022-02-28 20:26:04 +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
|
|
|
|
|
*/
|
2022-02-28 20:26:04 +08:00
|
|
|
|
// 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
|
|
|
|
|
*/
|
2022-03-01 16:39:04 +08:00
|
|
|
|
export const requireErrorImg = () => {
|
2022-02-28 20:26:04 +08:00
|
|
|
|
return Image_404
|
2021-12-20 18:06:08 +08:00
|
|
|
|
}
|
2021-12-20 14:29:29 +08:00
|
|
|
|
|
2022-01-09 17:12:35 +08:00
|
|
|
|
/**
|
2022-01-20 21:25:35 +08:00
|
|
|
|
* * 全屏操作函数
|
|
|
|
|
* @param isFullscreen
|
|
|
|
|
* @param isEnabled
|
|
|
|
|
* @returns
|
2022-01-09 17:12:35 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
|
|
|
|
* * 设置元素属性
|
2022-02-28 20:26:04 +08:00
|
|
|
|
* @param HTMLElement 元素
|
2022-01-20 21:25:35 +08:00
|
|
|
|
* @param key 键名
|
|
|
|
|
* @param value 键值
|
|
|
|
|
*/
|
2022-02-28 20:26:04 +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
|
|
|
|
|
2022-02-28 20:26:04 +08:00
|
|
|
|
/**
|
2022-02-10 16:34:34 +08:00
|
|
|
|
* * file转url
|
2022-02-28 20:26:04 +08:00
|
|
|
|
*/
|
|
|
|
|
export const fileToUrl = (file: File): string => {
|
|
|
|
|
const Url = URL || window.URL || window.webkitURL
|
|
|
|
|
const ImageUrl = Url.createObjectURL(file)
|
2022-02-10 16:34:34 +08:00
|
|
|
|
return ImageUrl
|
2022-02-28 20:26:04 +08:00
|
|
|
|
}
|
2022-02-10 16:34:34 +08:00
|
|
|
|
|
2022-02-28 20:26:04 +08:00
|
|
|
|
/**
|
2022-02-06 21:35:38 +08:00
|
|
|
|
* * file转base64
|
2022-02-28 20:26:04 +08:00
|
|
|
|
*/
|
|
|
|
|
export const fileTobase64 = (file: File, callback: Function) => {
|
|
|
|
|
let reader = new FileReader()
|
|
|
|
|
reader.readAsDataURL(file)
|
2022-02-06 21:35:38 +08:00
|
|
|
|
reader.onload = function (e: ProgressEvent<FileReader>) {
|
2022-02-28 20:26:04 +08:00
|
|
|
|
if (e.target) {
|
|
|
|
|
let base64 = e.target.result
|
2022-02-06 21:35:38 +08:00
|
|
|
|
callback(base64)
|
|
|
|
|
}
|
2022-02-28 20:26:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-06 21:35:38 +08:00
|
|
|
|
|
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,
|
2022-01-09 17:12:35 +08:00
|
|
|
|
listener: any,
|
2021-12-21 10:06:03 +08:00
|
|
|
|
options?: boolean | AddEventListenerOptions | undefined
|
|
|
|
|
) => {
|
2022-01-08 10:30:45 +08:00
|
|
|
|
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,
|
2022-04-05 19:01:52 +08:00
|
|
|
|
trailing: false,
|
2021-12-21 10:06:03 +08:00
|
|
|
|
}),
|
|
|
|
|
options
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-01-09 17:12:35 +08:00
|
|
|
|
|
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
|
|
|
|
) => {
|
2022-01-08 10:30:45 +08:00
|
|
|
|
if (!target) return
|
2021-12-21 10:06:03 +08:00
|
|
|
|
target.removeEventListener(type, listener)
|
|
|
|
|
}
|
2022-04-05 19:01:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* * 截取画面为图片
|
|
|
|
|
* @param html 需要截取的 DOM
|
|
|
|
|
*/
|
|
|
|
|
export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
|
|
|
|
|
if (!html) {
|
|
|
|
|
window['$message'].error('导出失败!')
|
|
|
|
|
if (callback) callback()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html2canvas(html).then((canvas: HTMLCanvasElement) => {
|
|
|
|
|
window['$message'].success('导出成功!')
|
|
|
|
|
downloadByA(canvas.toDataURL(), undefined, 'png')
|
|
|
|
|
if (callback) callback()
|
|
|
|
|
})
|
|
|
|
|
}
|