goview_vue/src/utils/utils.ts

264 lines
6.0 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'
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'
2022-07-06 20:14:41 +08:00
import { toString } from './type'
2022-07-16 19:01:05 +08:00
import cloneDeep from 'lodash/cloneDeep'
2022-07-20 12:19:24 +08:00
import { RequestHttpIntervalEnum, RequestParamsObjType } from '@/enums/httpEnum'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
2021-12-20 14:29:29 +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
*/
export const getUUID = (randomLength = 10) => {
return Number(Math.random().toString().substring(2, randomLength) + Date.now()).toString(36)
2021-12-20 14:29:29 +08:00
}
/**
* * 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
/**
* * 404
* @returns url
*/
export const requireErrorImg = () => {
return Image_404
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
/**
*
* @param target
* @param x X轴
* @param y Y轴
*/
export const setComponentPosition = (target: CreateComponentType | CreateComponentGroupType, x?: number, y?:number) => {
x && (target.attr.x = x)
y && (target.attr.y = y)
}
2022-01-20 21:25:35 +08:00
/**
* *
* @param HTMLElement
2022-01-20 21:25:35 +08:00
* @param key
* @param value
*/
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转url
*/
export const fileToUrl = (file: File): string => {
const Url = URL || window.URL || window.webkitURL
const ImageUrl = Url.createObjectURL(file)
return ImageUrl
}
/**
2022-02-06 21:35:38 +08:00
* * file转base64
*/
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>) {
if (e.target) {
let base64 = e.target.result
2022-02-06 21:35:38 +08:00
callback(base64)
}
}
}
2022-02-06 21:35:38 +08:00
2021-12-21 10:06:03 +08:00
/**
* *
*/
// eslint-disable-next-line no-undef
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,
2022-04-07 15:28:25 +08:00
delay?: number,
// eslint-disable-next-line no-undef
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-04-07 15:28:25 +08:00
throttle(listener, delay || 300, {
2021-12-21 10:06:03 +08:00
leading: true,
trailing: false
2021-12-21 10:06:03 +08:00
}),
options
)
}
2021-12-21 10:06:03 +08:00
/**
* *
*/
// eslint-disable-next-line no-undef
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)
}
2022-04-05 19:01:52 +08:00
/**
2022-04-07 15:28:25 +08:00
* *
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
}
2022-06-09 08:59:36 +08:00
html2canvas(html, {
backgroundColor: null,
allowTaint: true,
useCORS: true
2022-06-09 08:59:36 +08:00
}).then((canvas: HTMLCanvasElement) => {
2022-04-05 19:01:52 +08:00
window['$message'].success('导出成功!')
downloadByA(canvas.toDataURL(), undefined, 'png')
if (callback) callback()
})
}
/**
* *
* @param data
* @param res
* @param funcStr
* @param isToString
* @param errorCallBack
* @param successCallBack
* @returns
*/
export const newFunctionHandle = (
data: any,
res: any,
funcStr?: string,
2022-07-06 20:14:41 +08:00
isToString?: boolean,
errorCallBack?: Function,
successCallBack?: Function
) => {
try {
if (!funcStr) return data
const fn = new Function('data', 'res', funcStr)
const fnRes = fn(cloneDeep(data), cloneDeep(res))
2022-07-06 20:14:41 +08:00
const resHandle = isToString ? toString(fnRes) : fnRes
// 成功回调
successCallBack && successCallBack(resHandle)
return resHandle
} catch (error) {
// 失败回调
errorCallBack && errorCallBack(error)
return '函数执行错误'
}
}
2022-07-16 19:01:05 +08:00
/**
* *
* @param num
* @param unit RequestHttpIntervalEnum
* @return number
*/
export const intervalUnitHandle = (num: number, unit: RequestHttpIntervalEnum) => {
switch (unit) {
// 秒
case RequestHttpIntervalEnum.SECOND:
return num * 1000
// 分
case RequestHttpIntervalEnum.MINUTE:
return num * 1000 * 60
// 时
case RequestHttpIntervalEnum.HOUR:
return num * 1000 * 60 * 60
// 天
case RequestHttpIntervalEnum.DAY:
return num * 1000 * 60 * 60 * 24
default:
return num * 1000
}
}
2022-07-20 12:19:24 +08:00
/**
* * cookie
* @param obj
2022-07-20 12:19:24 +08:00
* @returns string
*/
export const objToCookie = (obj: RequestParamsObjType) => {
if (!obj) return ''
2022-07-20 12:19:24 +08:00
let str = ''
for (const key in obj) {
str += key + '=' + obj[key] + ';'
}
return str.substring(0, str.length - 1)
2022-07-20 12:19:24 +08:00
}