utils.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
  12. /**
  13. * * 判断是否是开发环境
  14. * @return { Boolean }
  15. */
  16. export const isDev = () => {
  17. return import.meta.env.DEV
  18. }
  19. /**
  20. * * 生成一个不重复的ID
  21. * @param { Number } randomLength
  22. */
  23. export const getUUID = (randomLength = 10) => {
  24. return Number(Math.random().toString().substring(2, randomLength) + Date.now()).toString(36)
  25. }
  26. /**
  27. * * render 图标
  28. * @param icon 图标
  29. * @param set 设置项
  30. */
  31. export const renderIcon = (icon: any, set = {}) => {
  32. return () => h(NIcon, set, { default: () => h(icon) })
  33. }
  34. /**
  35. * * render 语言
  36. * @param lang 语言标识
  37. * @param set 设置项
  38. * @param tag 要渲染成的标签
  39. */
  40. export const renderLang = (lang: string, set = {}, tag = 'span') => {
  41. return () => h(tag, set, { default: () => window['$t'](lang) })
  42. }
  43. /**
  44. * * 获取错误处理图片,默认 404 图
  45. * @returns url
  46. */
  47. export const requireErrorImg = () => {
  48. return Image_404
  49. }
  50. /**
  51. * * 全屏操作函数
  52. * @param isFullscreen
  53. * @param isEnabled
  54. * @returns
  55. */
  56. export const screenfullFn = (isFullscreen?: boolean, isEnabled?: boolean) => {
  57. // 是否是全屏
  58. if (isFullscreen) return screenfull.isFullscreen
  59. // 是否支持全屏
  60. if (isEnabled) return screenfull.isEnabled
  61. if (screenfull.isEnabled) {
  62. screenfull.toggle()
  63. return
  64. }
  65. // TODO lang
  66. window['$message'].warning('您的浏览器不支持全屏功能!')
  67. }
  68. /**
  69. * 修改元素位置
  70. * @param target 对象
  71. * @param x X轴
  72. * @param y Y轴
  73. */
  74. export const setComponentPosition = (target: CreateComponentType | CreateComponentGroupType, x?: number, y?:number) => {
  75. x && (target.attr.x = x)
  76. y && (target.attr.y = y)
  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. // eslint-disable-next-line no-undef
  125. export const addEventListener = <K extends keyof WindowEventMap>(
  126. target: HTMLElement | Document,
  127. type: K,
  128. listener: any,
  129. delay?: number,
  130. // eslint-disable-next-line no-undef
  131. options?: boolean | AddEventListenerOptions | undefined
  132. ) => {
  133. if (!target) return
  134. target.addEventListener(
  135. type,
  136. throttle(listener, delay || 300, {
  137. leading: true,
  138. trailing: false
  139. }),
  140. options
  141. )
  142. }
  143. /**
  144. * * 卸载监听
  145. */
  146. // eslint-disable-next-line no-undef
  147. export const removeEventListener = <K extends keyof WindowEventMap>(
  148. target: HTMLElement | Document,
  149. type: K,
  150. listener: any
  151. ) => {
  152. if (!target) return
  153. target.removeEventListener(type, listener)
  154. }
  155. /**
  156. * * 截取画面为图片并下载
  157. * @param html 需要截取的 DOM
  158. */
  159. export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
  160. if (!html) {
  161. window['$message'].error('导出失败!')
  162. if (callback) callback()
  163. return
  164. }
  165. html2canvas(html, {
  166. backgroundColor: null,
  167. allowTaint: true,
  168. useCORS: true
  169. }).then((canvas: HTMLCanvasElement) => {
  170. window['$message'].success('导出成功!')
  171. downloadByA(canvas.toDataURL(), undefined, 'png')
  172. if (callback) callback()
  173. })
  174. }
  175. /**
  176. * * 函数过滤器
  177. * @param data 数据值
  178. * @param res 返回顶级对象
  179. * @param funcStr 函数字符串
  180. * @param isToString 是否转为字符串
  181. * @param errorCallBack 错误回调函数
  182. * @param successCallBack 成功回调函数
  183. * @returns
  184. */
  185. export const newFunctionHandle = (
  186. data: any,
  187. res: any,
  188. funcStr?: string,
  189. isToString?: boolean,
  190. errorCallBack?: Function,
  191. successCallBack?: Function
  192. ) => {
  193. try {
  194. if (!funcStr) return data
  195. const fn = new Function('data', 'res', funcStr)
  196. const fnRes = fn(cloneDeep(data), cloneDeep(res))
  197. const resHandle = isToString ? toString(fnRes) : fnRes
  198. // 成功回调
  199. successCallBack && successCallBack(resHandle)
  200. return resHandle
  201. } catch (error) {
  202. // 失败回调
  203. errorCallBack && errorCallBack(error)
  204. return '函数执行错误'
  205. }
  206. }
  207. /**
  208. * * 处理请求事件单位
  209. * @param num 时间间隔
  210. * @param unit RequestHttpIntervalEnum
  211. * @return number 秒数
  212. */
  213. export const intervalUnitHandle = (num: number, unit: RequestHttpIntervalEnum) => {
  214. switch (unit) {
  215. // 秒
  216. case RequestHttpIntervalEnum.SECOND:
  217. return num * 1000
  218. // 分
  219. case RequestHttpIntervalEnum.MINUTE:
  220. return num * 1000 * 60
  221. // 时
  222. case RequestHttpIntervalEnum.HOUR:
  223. return num * 1000 * 60 * 60
  224. // 天
  225. case RequestHttpIntervalEnum.DAY:
  226. return num * 1000 * 60 * 60 * 24
  227. default:
  228. return num * 1000
  229. }
  230. }
  231. /**
  232. * * 对象转换 cookie 格式
  233. * @param obj
  234. * @returns string
  235. */
  236. export const objToCookie = (obj: RequestParamsObjType) => {
  237. if (!obj) return ''
  238. let str = ''
  239. for (const key in obj) {
  240. str += key + '=' + obj[key] + ';'
  241. }
  242. return str.substring(0, str.length - 1)
  243. }