utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  9. * * 判断是否是开发环境
  10. * @return { Boolean }
  11. */
  12. export const isDev = () => {
  13. return import.meta.env.DEV
  14. }
  15. /**
  16. * * 生成一个不重复的ID
  17. * @param { Number } randomLength
  18. */
  19. export const getUUID = (randomLength = 10) => {
  20. return Number(
  21. Math.random().toString().substr(2, randomLength) + Date.now()
  22. ).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 = <
  85. K extends keyof CSSStyleDeclaration,
  86. V extends CSSStyleDeclaration[K]
  87. >(
  88. HTMLElement: HTMLElement,
  89. key: K,
  90. value: V
  91. ) => {
  92. if (HTMLElement) {
  93. HTMLElement.style[key] = value
  94. }
  95. }
  96. /**
  97. * * 判断是否是 mac
  98. * @returns boolean
  99. */
  100. export const isMac = () => {
  101. return /macintosh|mac os x/i.test(navigator.userAgent)
  102. }
  103. /**
  104. * * 挂载监听
  105. */
  106. export const addEventListener = <K extends keyof WindowEventMap>(
  107. target: HTMLElement | Document,
  108. type: K,
  109. listener: any,
  110. delay?: number,
  111. options?: boolean | AddEventListenerOptions | undefined
  112. ) => {
  113. if (!target) return
  114. target.addEventListener(
  115. type,
  116. throttle(listener, delay || 300, {
  117. leading: true,
  118. trailing: false,
  119. }),
  120. options
  121. )
  122. }
  123. /**
  124. * * 卸载监听
  125. */
  126. export const removeEventListener = <K extends keyof WindowEventMap>(
  127. target: HTMLElement | Document,
  128. type: K,
  129. listener: any
  130. ) => {
  131. if (!target) return
  132. target.removeEventListener(type, listener)
  133. }
  134. /**
  135. * * 截取画面为图片并下载
  136. * @param html 需要截取的 DOM
  137. */
  138. export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
  139. if (!html) {
  140. window['$message'].error('导出失败!')
  141. if (callback) callback()
  142. return
  143. }
  144. html2canvas(html).then((canvas: HTMLCanvasElement) => {
  145. window['$message'].success('导出成功!')
  146. downloadByA(canvas.toDataURL(), undefined, 'png')
  147. if (callback) callback()
  148. })
  149. }