utils.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { h } from 'vue'
  2. import { NIcon } from 'naive-ui'
  3. import screenfull from 'screenfull'
  4. import throttle from 'lodash/throttle'
  5. import Image_404 from '../assets/images/exception/image-404.png'
  6. import html2canvas from 'html2canvas'
  7. import { downloadByA } from './file'
  8. import { toString } from './type'
  9. import cloneDeep from 'lodash/cloneDeep';
  10. /**
  11. * * 判断是否是开发环境
  12. * @return { Boolean }
  13. */
  14. export const isDev = () => {
  15. return import.meta.env.DEV
  16. }
  17. /**
  18. * * 生成一个不重复的ID
  19. * @param { Number } randomLength
  20. */
  21. export const getUUID = (randomLength = 10) => {
  22. return Number(Math.random().toString().substr(2, randomLength) + Date.now()).toString(36)
  23. }
  24. /**
  25. * * render 图标
  26. * @param icon 图标
  27. * @param set 设置项
  28. */
  29. export const renderIcon = (icon: any, set = {}) => {
  30. return () => h(NIcon, set, { default: () => h(icon) })
  31. }
  32. /**
  33. * * render 语言
  34. * @param lang 语言标识
  35. * @param set 设置项
  36. * @param tag 要渲染成的标签
  37. */
  38. export const renderLang = (lang: string, set = {}, tag = 'span') => {
  39. return () => h(tag, set, { default: () => window['$t'](lang) })
  40. }
  41. /**
  42. * ! 编译会报错,暂不使用
  43. * * 处理 vite 中无法使用 require 的问题,utils 文件为根路径
  44. * @param path
  45. * @param name
  46. * @returns url
  47. */
  48. // export const requireUrl = (path: string, name: string) => {
  49. // return new URL(`${path}/${name}`, import.meta.url).href
  50. // }
  51. /**
  52. * * 获取错误处理图片,默认 404 图
  53. * @param path
  54. * @param name
  55. * @returns url
  56. */
  57. export const requireErrorImg = () => {
  58. return Image_404
  59. }
  60. /**
  61. * * 全屏操作函数
  62. * @param isFullscreen
  63. * @param isEnabled
  64. * @returns
  65. */
  66. export const screenfullFn = (isFullscreen?: boolean, isEnabled?: boolean) => {
  67. // 是否是全屏
  68. if (isFullscreen) return screenfull.isFullscreen
  69. // 是否支持全屏
  70. if (isEnabled) return screenfull.isEnabled
  71. if (screenfull.isEnabled) {
  72. screenfull.toggle()
  73. return
  74. }
  75. // TODO lang
  76. window['$message'].warning('您的浏览器不支持全屏功能!')
  77. }
  78. /**
  79. * * 设置元素属性
  80. * @param HTMLElement 元素
  81. * @param key 键名
  82. * @param value 键值
  83. */
  84. export const setDomAttribute = <K extends keyof CSSStyleDeclaration, V extends CSSStyleDeclaration[K]>(
  85. HTMLElement: HTMLElement,
  86. key: K,
  87. value: V
  88. ) => {
  89. if (HTMLElement) {
  90. HTMLElement.style[key] = value
  91. }
  92. }
  93. /**
  94. * * 判断是否是 mac
  95. * @returns boolean
  96. */
  97. export const isMac = () => {
  98. return /macintosh|mac os x/i.test(navigator.userAgent)
  99. }
  100. /**
  101. * * file转url
  102. */
  103. export const fileToUrl = (file: File): string => {
  104. const Url = URL || window.URL || window.webkitURL
  105. const ImageUrl = Url.createObjectURL(file)
  106. return ImageUrl
  107. }
  108. /**
  109. * * file转base64
  110. */
  111. export const fileTobase64 = (file: File, callback: Function) => {
  112. let reader = new FileReader()
  113. reader.readAsDataURL(file)
  114. reader.onload = function (e: ProgressEvent<FileReader>) {
  115. if (e.target) {
  116. let base64 = e.target.result
  117. callback(base64)
  118. }
  119. }
  120. }
  121. /**
  122. * * 挂载监听
  123. */
  124. export const addEventListener = <K extends keyof WindowEventMap>(
  125. target: HTMLElement | Document,
  126. type: K,
  127. listener: any,
  128. delay?: number,
  129. options?: boolean | AddEventListenerOptions | undefined
  130. ) => {
  131. if (!target) return
  132. target.addEventListener(
  133. type,
  134. throttle(listener, delay || 300, {
  135. leading: true,
  136. trailing: false
  137. }),
  138. options
  139. )
  140. }
  141. /**
  142. * * 卸载监听
  143. */
  144. export const removeEventListener = <K extends keyof WindowEventMap>(
  145. target: HTMLElement | Document,
  146. type: K,
  147. listener: any
  148. ) => {
  149. if (!target) return
  150. target.removeEventListener(type, listener)
  151. }
  152. /**
  153. * * 截取画面为图片并下载
  154. * @param html 需要截取的 DOM
  155. */
  156. export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
  157. if (!html) {
  158. window['$message'].error('导出失败!')
  159. if (callback) callback()
  160. return
  161. }
  162. html2canvas(html, {
  163. backgroundColor: null
  164. }).then((canvas: HTMLCanvasElement) => {
  165. window['$message'].success('导出成功!')
  166. downloadByA(canvas.toDataURL(), undefined, 'png')
  167. if (callback) callback()
  168. })
  169. }
  170. /**
  171. * * 函数过滤器
  172. * @param data 数据值
  173. * @param funcStr 函数字符串
  174. * @param toString 转为字符串
  175. * @param errorCallBack 错误回调函数
  176. * @param successCallBack 成功回调函数
  177. * @returns
  178. */
  179. export const newFunctionHandle = (
  180. data: any,
  181. funcStr?: string,
  182. isToString?: boolean,
  183. errorCallBack?: Function,
  184. successCallBack?: Function
  185. ) => {
  186. try {
  187. if (!funcStr) return data
  188. const fn = new Function('data', funcStr)
  189. const fnRes = fn( cloneDeep(data))
  190. const resHandle = isToString ? toString(fnRes) : fnRes
  191. // 成功回调
  192. successCallBack && successCallBack(resHandle)
  193. return resHandle
  194. } catch (error) {
  195. // 失败回调
  196. errorCallBack && errorCallBack(error)
  197. return '函数执行错误'
  198. }
  199. }