utils.ts 5.8 KB

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