index.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // 工具函数集合
  2. import { ElMessage } from 'element-plus'
  3. // vue Storage 使用
  4. export { ls } from './vueStorage'
  5. import { get, set } from 'lodash-unified'
  6. export type Arrayable<T> = T | T[]
  7. export const getPropValue = <T = any>(obj: Record<string, any>, path: Arrayable<string>, defaultValue?: any): { value: T } => {
  8. return {
  9. get value() {
  10. return get(obj, path, defaultValue)
  11. },
  12. set value(val: any) {
  13. set(obj, path, val)
  14. }
  15. }
  16. }
  17. /**
  18. * 提取深层数据的值 (防止中间项 不存在导致的报错)
  19. * @param obj 【检测对象】
  20. * @param keyArr 【需要获取该对象内部数据key数组】
  21. * @returns {*}
  22. */
  23. export function getDeepValue(obj: object | string[] | any, keyArr: (string | number)[]) {
  24. return keyArr.reduce((acc, key) => acc && acc[key], obj)
  25. }
  26. /**
  27. * 针对本地开发 console.log 添加颜色打印
  28. */
  29. export const $log = (function () {
  30. const isDEV = import.meta.env.DEV // 开发环境
  31. if (!isDEV) {
  32. // eslint-disable-next-line
  33. // @ts-ignore
  34. return (content: any = 'content', title = 'title', background = '#007bfc') => {}
  35. }
  36. return (content: any = 'content', title = 'title', background = '#007bfc') => {
  37. content = typeof content === 'object' ? JSON.stringify(content) : content
  38. console.log.apply(null, [
  39. `%c ${title}: %c ${content} `,
  40. 'padding: 1px; border-radius: 10px; color: #fff; background: #9159B2;',
  41. `padding: 1px; border-radius: 10px; color: #fff; background: ${background};`
  42. ])
  43. }
  44. })()
  45. /**
  46. * @desc 函数防抖
  47. * @param func 函数
  48. * @param wait 延迟毫秒数
  49. * @param immediate true/false (是/否)即执行
  50. */
  51. export function debounce(func: () => any, wait: number, immediate?: boolean) {
  52. let timeout: any
  53. return function () {
  54. /* eslint-disable */
  55. // @ts-ignore
  56. const context = this
  57. const args: any = arguments
  58. /* eslint-enable */
  59. if (timeout) clearTimeout(timeout)
  60. if (immediate) {
  61. const callNow = !timeout
  62. timeout = setTimeout(() => {
  63. timeout = null
  64. }, wait)
  65. if (callNow) func.apply(context, args)
  66. } else {
  67. timeout = setTimeout(function () {
  68. func.apply(context, args)
  69. }, wait)
  70. }
  71. }
  72. }
  73. /**
  74. * 深层数据的集合进行有机合并
  75. * @param objs 需要合并的 对象集合 (后面的覆盖前面的参数)
  76. * @returns {{}}
  77. */
  78. export const objDeepMerge = (...objs: any[]) => {
  79. const res: any = {}
  80. const combine = (opt: any) => {
  81. for (const prop in opt) {
  82. // eslint-disable-next-line
  83. if (opt.hasOwnProperty(prop)) {
  84. if (Object.prototype.toString.call(opt[prop]) === '[object Object]') {
  85. res[prop] = objDeepMerge(res[prop], opt[prop])
  86. } else {
  87. res[prop] = opt[prop]
  88. }
  89. }
  90. }
  91. }
  92. objs.map(opts => {
  93. combine(opts)
  94. })
  95. return res
  96. }
  97. /**
  98. * 复制文字
  99. * @param val
  100. * @param sucTxt
  101. * @returns {boolean}
  102. */
  103. export function copyText(val: string, sucTxt = '复制成功~') {
  104. const textarea: any = document.createElement('textarea')
  105. // 将该 textarea 设为 readonly 防止 iOS 下自动唤起键盘,同时将 textarea 移出可视区域
  106. textarea.readOnly = 'readonly'
  107. textarea.style.position = 'absolute'
  108. textarea.style.left = '-9999px'
  109. textarea.value = val
  110. document.body.appendChild(textarea)
  111. textarea.select()
  112. const res = document.execCommand('copy')
  113. document.body.removeChild(textarea)
  114. // console.log(res, 'res 测试............')
  115. if (res) {
  116. ElMessage.success(sucTxt)
  117. }
  118. return res
  119. }
  120. /**
  121. * 异步操作避免重复触发加锁
  122. * @param syncFn
  123. * @returns {(function(...[*]): Promise<void>)|*}
  124. */
  125. export function asyncHandlerLockWrap(syncFn: () => any) {
  126. let is_lock = false
  127. /* eslint-disable */
  128. // @ts-ignore
  129. return async function (...args) {
  130. // @ts-ignore
  131. const context = this
  132. if (!is_lock) {
  133. is_lock = true
  134. Promise.resolve()
  135. // @ts-ignore
  136. .then(syncFn.bind(context, ...args))
  137. .finally(() => {
  138. is_lock = false
  139. })
  140. }
  141. }
  142. /* eslint-enable */
  143. }
  144. /**
  145. * 转化成带逗号的number格式
  146. * @param num
  147. * @param minimumFractionDigits 保留小数位
  148. * @returns {string}
  149. */
  150. export function formatNumber(num: number | string, minimumFractionDigits = 0) {
  151. // return xeUtils.commafy(+num, { digits: minimumFractionDigits }) || '-'
  152. return (+num || 0).toLocaleString('zh', { minimumFractionDigits })
  153. }
  154. /*
  155. const sortColumnChildren = (localColumn: LeTableColumnProps[], targetColumn: LeTableColumnProps[], localField = 'prop', targetField = 'prop') => {
  156. const cur_children = localColumn.children
  157. if (Array.isArray(cur_children) && Array.isArray(targetColumn.children)) {
  158. // console.error(JSON.stringify(cur_children), 'cur_children targetColumn_children', JSON.stringify(targetColumn.children))
  159. // children 排序
  160. localColumn.children = targetColumn.children
  161. .map(_column => {
  162. const findColumn = cur_children.find(l_column => l_column[localField] === _column[targetField])
  163. if (findColumn) {
  164. if (Array.isArray(findColumn.children)) {
  165. return sortColumnChildren(findColumn, _column, localField)
  166. }
  167. return findColumn
  168. }
  169. return false
  170. })
  171. .filter(Boolean)
  172. }
  173. return localColumn
  174. }
  175. export function updateNewColumns(localColumns: LeTableColumnProps[], targetColumn: LeTableColumnProps[], localField = 'prop', targetField = 'prop') {
  176. return localColumns
  177. .map(v => {
  178. const cur = targetColumn.find((column) => column[localField] === v[targetField])
  179. if (cur) {
  180. // children 内嵌处理
  181. return sortColumnChildren(cur, v)
  182. }
  183. })
  184. .filter(Boolean)
  185. }
  186. */
  187. // 获取assets静态资源(暂->已不再使用)
  188. export function getAssetsFile(url: string) {
  189. return new URL(`../assets/flow/${url}`, import.meta.url).href
  190. }
  191. export const flowIconPrefix = 'icon-flow-'
  192. export const format_milliseconds = (milliseconds: number) => {
  193. let timeString = ''
  194. if (milliseconds) {
  195. const hours = Math.floor(milliseconds / 3600000)
  196. const minutes = Math.floor((milliseconds % 3600000) / 60000)
  197. const seconds = Math.floor((milliseconds % 60000) / 1000)
  198. if (hours > 0) {
  199. timeString += hours + '小时 '
  200. }
  201. if (minutes > 0) {
  202. timeString += minutes + '分钟 '
  203. }
  204. if (seconds > 0) {
  205. timeString += seconds + '秒'
  206. }
  207. }
  208. return timeString
  209. }