index.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { isArray } from '@/utils/is'
  2. import { FieldNamesProps } from '@/components/ProTable/interface'
  3. const mode = import.meta.env.VITE_ROUTER_MODE
  4. /**
  5. * @description 获取localStorage
  6. * @param {String} key Storage名称
  7. * @returns {String}
  8. */
  9. export function localGet(key: string) {
  10. const value = window.localStorage.getItem(key)
  11. try {
  12. return JSON.parse(window.localStorage.getItem(key) as string)
  13. } catch (error) {
  14. return value
  15. }
  16. }
  17. /**
  18. * @description 存储localStorage
  19. * @param {String} key Storage名称
  20. * @param {*} value Storage值
  21. * @returns {void}
  22. */
  23. export function localSet(key: string, value: any) {
  24. window.localStorage.setItem(key, JSON.stringify(value))
  25. }
  26. /**
  27. * @description 清除localStorage
  28. * @param {String} key Storage名称
  29. * @returns {void}
  30. */
  31. export function localRemove(key: string) {
  32. window.localStorage.removeItem(key)
  33. }
  34. /**
  35. * @description 清除所有localStorage
  36. * @returns {void}
  37. */
  38. export function localClear() {
  39. window.localStorage.clear()
  40. }
  41. /**
  42. * @description 判断数据类型
  43. * @param {*} val 需要判断类型的数据
  44. * @returns {String}
  45. */
  46. export function isType(val: any) {
  47. if (val === null) return 'null'
  48. if (typeof val !== 'object') return typeof val
  49. else return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase()
  50. }
  51. /**
  52. * @description 生成唯一 uuid
  53. * @returns {String}
  54. */
  55. export function generateUUID() {
  56. let uuid = ''
  57. for (let i = 0; i < 32; i++) {
  58. let random = (Math.random() * 16) | 0
  59. if (i === 8 || i === 12 || i === 16 || i === 20) uuid += '-'
  60. uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16)
  61. }
  62. return uuid
  63. }
  64. /**
  65. * 判断两个对象是否相同
  66. * @param {Object} a 要比较的对象一
  67. * @param {Object} b 要比较的对象二
  68. * @returns {Boolean} 相同返回 true,反之 false
  69. */
  70. export function isObjectValueEqual(a: { [key: string]: any }, b: { [key: string]: any }) {
  71. if (!a || !b) return false
  72. let aProps = Object.getOwnPropertyNames(a)
  73. let bProps = Object.getOwnPropertyNames(b)
  74. if (aProps.length != bProps.length) return false
  75. for (let i = 0; i < aProps.length; i++) {
  76. let propName = aProps[i]
  77. let propA = a[propName]
  78. let propB = b[propName]
  79. if (!b.hasOwnProperty(propName)) return false
  80. if (propA instanceof Object) {
  81. if (!isObjectValueEqual(propA, propB)) return false
  82. } else if (propA !== propB) {
  83. return false
  84. }
  85. }
  86. return true
  87. }
  88. /**
  89. * @description 生成随机数
  90. * @param {Number} min 最小值
  91. * @param {Number} max 最大值
  92. * @returns {Number}
  93. */
  94. export function randomNum(min: number, max: number): number {
  95. let num = Math.floor(Math.random() * (min - max) + max)
  96. return num
  97. }
  98. /**
  99. * @description 获取当前时间对应的提示语
  100. * @returns {String}
  101. */
  102. export function getTimeState() {
  103. let timeNow = new Date()
  104. let hours = timeNow.getHours()
  105. if (hours >= 6 && hours <= 10) return `早上好 ⛅`
  106. if (hours >= 10 && hours <= 14) return `中午好 🌞`
  107. if (hours >= 14 && hours <= 18) return `下午好 🌞`
  108. if (hours >= 18 && hours <= 24) return `晚上好 🌛`
  109. if (hours >= 0 && hours <= 6) return `凌晨好 🌛`
  110. }
  111. /**
  112. * @description 获取浏览器默认语言
  113. * @returns {String}
  114. */
  115. export function getBrowserLang() {
  116. let browserLang = navigator.language ? navigator.language : navigator.browserLanguage
  117. let defaultBrowserLang = ''
  118. if (['cn', 'zh', 'zh-cn'].includes(browserLang.toLowerCase())) {
  119. defaultBrowserLang = 'zh'
  120. } else {
  121. defaultBrowserLang = 'en'
  122. }
  123. return defaultBrowserLang
  124. }
  125. /**
  126. * @description 获取不同路由模式所对应的 url + params
  127. * @returns {String}
  128. */
  129. export function getUrlWithParams() {
  130. const url = {
  131. hash: location.hash.substring(1),
  132. history: location.pathname + location.search
  133. }
  134. return url[mode]
  135. }
  136. /**
  137. * @description 使用递归扁平化菜单,方便添加动态路由
  138. * @param {Array} menuList 菜单列表
  139. * @returns {Array}
  140. */
  141. export function getFlatMenuList(menuList: Menu.MenuOptions[]): Menu.MenuOptions[] {
  142. let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList))
  143. return newMenuList.flatMap(item => [item, ...(item.children ? getFlatMenuList(item.children) : [])])
  144. }
  145. /**
  146. * @description 使用递归过滤出需要渲染在左侧菜单的列表 (需剔除 hidden == true 的菜单)
  147. * @param {Array} menuList 菜单列表
  148. * @returns {Array}
  149. * */
  150. export function getShowMenuList(menuList: Menu.MenuOptions[]) {
  151. let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList))
  152. return newMenuList.filter(item => {
  153. item.children?.length && (item.children = getShowMenuList(item.children))
  154. return !item.hidden
  155. })
  156. }
  157. /**
  158. * @description 使用递归找出所有面包屑存储到 pinia/vuex 中
  159. * @param {Array} menuList 菜单列表
  160. * @param {Array} parent 父级菜单
  161. * @param {Object} result 处理后的结果
  162. * @returns {Object}
  163. */
  164. export const getAllBreadcrumbList = (menuList: Menu.MenuOptions[], parent = [], result: { [key: string]: any } = {}) => {
  165. for (const item of menuList) {
  166. result[item.path] = [...parent, item]
  167. if (item.children) getAllBreadcrumbList(item.children, result[item.path], result)
  168. }
  169. return result
  170. }
  171. /**
  172. * @description 使用递归处理路由菜单 path,生成一维数组 (第一版本地路由鉴权会用到,该函数暂未使用)
  173. * @param {Array} menuList 所有菜单列表
  174. * @param {Array} menuPathArr 菜单地址的一维数组 ['**','**']
  175. * @returns {Array}
  176. */
  177. export function getMenuListPath(menuList: Menu.MenuOptions[], menuPathArr: string[] = []): string[] {
  178. for (const item of menuList) {
  179. if (typeof item === 'object' && item.path) menuPathArr.push(item.path)
  180. if (item.children?.length) getMenuListPath(item.children, menuPathArr)
  181. }
  182. return menuPathArr
  183. }
  184. /**
  185. * @description 递归查询当前 path 所对应的菜单对象 (该函数暂未使用)
  186. * @param {Array} menuList 菜单列表
  187. * @param {String} path 当前访问地址
  188. * @returns {Object | null}
  189. */
  190. export function findMenuByPath(menuList: Menu.MenuOptions[], path: string): Menu.MenuOptions | null {
  191. for (const item of menuList) {
  192. if (item.path === path) return item
  193. if (item.children) {
  194. const res = findMenuByPath(item.children, path)
  195. if (res) return res
  196. }
  197. }
  198. return null
  199. }
  200. /**
  201. * @description 使用递归过滤需要缓存的菜单 name (该函数暂未使用)
  202. * @param {Array} menuList 所有菜单列表
  203. * @param {Array} keepAliveNameArr 缓存的菜单 name ['**','**']
  204. * @returns {Array}
  205. * */
  206. export function getKeepAliveRouterName(menuList: Menu.MenuOptions[], keepAliveNameArr: string[] = []) {
  207. menuList.forEach(item => {
  208. item.meta.noCache && item.name && keepAliveNameArr.push(item.name)
  209. item.children?.length && getKeepAliveRouterName(item.children, keepAliveNameArr)
  210. })
  211. return keepAliveNameArr
  212. }
  213. /**
  214. * @description 格式化表格单元格默认值 (el-table-column)
  215. * @param {Number} row 行
  216. * @param {Number} col 列
  217. * @param {*} callValue 当前单元格值
  218. * @returns {String}
  219. * */
  220. export function formatTableColumn(row: number, col: number, callValue: any) {
  221. // 如果当前值为数组,使用 / 拼接(根据需求自定义)
  222. if (isArray(callValue)) return callValue.length ? callValue.join(' / ') : '--'
  223. return callValue ?? '--'
  224. }
  225. /**
  226. * @description 处理 ProTable 值为数组 || 无数据
  227. * @param {*} callValue 需要处理的值
  228. * @returns {String}
  229. * */
  230. export function formatValue(callValue: any) {
  231. // 如果当前值为数组,使用 / 拼接(根据需求自定义)
  232. if (isArray(callValue)) return callValue.length ? callValue.join(' / ') : '--'
  233. return callValue ?? '--'
  234. }
  235. /**
  236. * @description 处理 prop 为多级嵌套的情况,返回的数据 (列如: prop: user.name)
  237. * @param {Object} row 当前行数据
  238. * @param {String} prop 当前 prop
  239. * @returns {*}
  240. * */
  241. export function handleRowAccordingToProp(row: { [key: string]: any }, prop: string) {
  242. if (!prop.includes('.')) return row[prop] ?? '--'
  243. prop.split('.').forEach(item => (row = row[item] ?? '--'))
  244. return row
  245. }
  246. /**
  247. * @description 处理 prop,当 prop 为多级嵌套时 ==> 返回最后一级 prop
  248. * @param {String} prop 当前 prop
  249. * @returns {String}
  250. * */
  251. export function handleProp(prop: string) {
  252. const propArr = prop.split('.')
  253. if (propArr.length == 1) return prop
  254. return propArr[propArr.length - 1]
  255. }
  256. /**
  257. * @description 根据枚举列表查询当需要的数据(如果指定了 label 和 value 的 key值,会自动识别格式化)
  258. * @param {String} callValue 当前单元格值
  259. * @param {Array} enumData 字典列表
  260. * @param {Array} fieldNames label && value && children 的 key 值
  261. * @param {String} type 过滤类型(目前只有 tag)
  262. * @returns {String}
  263. * */
  264. export function filterEnum(callValue: any, enumData?: any, fieldNames?: FieldNamesProps, type?: 'tag') {
  265. const value = fieldNames?.value ?? 'value'
  266. const label = fieldNames?.label ?? 'label'
  267. const children = fieldNames?.children ?? 'children'
  268. let filterData: { [key: string]: any } = {}
  269. // 判断 enumData 是否为数组
  270. if (Array.isArray(enumData)) filterData = findItemNested(enumData, callValue, value, children)
  271. // 判断是否输出的结果为 tag 类型
  272. if (type == 'tag') {
  273. return filterData?.listClass ? filterData.listClass : 'primary'
  274. } else {
  275. return filterData ? filterData[label] : '--'
  276. }
  277. }
  278. /**
  279. * @description 递归查找 callValue 对应的 enum 值
  280. * */
  281. export function findItemNested(enumData: any, callValue: any, value: string, children: string) {
  282. return enumData.reduce((accumulator: any, current: any) => {
  283. if (accumulator) return accumulator
  284. if (current[value] === callValue) return current
  285. if (current[children]) return findItemNested(current[children], callValue, value, children)
  286. }, null)
  287. }