common.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import getOrigin from '/@/utils/origin'
  2. /**
  3. * 通用js方法封装处理
  4. */
  5. const baseURL: string | undefined | boolean = getOrigin(import.meta.env.VITE_API_URL)
  6. export function getUpFileUrl(url: string) {
  7. if (!url) {
  8. return url
  9. }
  10. if (/^http|^blob/i.test(url)) {
  11. return url
  12. }
  13. let reg = new RegExp('^/*' + baseURL + "/*");
  14. return baseURL + "/" + url.replace(reg, '')
  15. }
  16. /**
  17. * 构造树型结构数据
  18. * @param {*} data 数据源
  19. * @param {*} id id字段 默认 'id'
  20. * @param {*} parentId 父节点字段 默认 'parentId'
  21. * @param {*} children 孩子节点字段 默认 'children'
  22. * @param {*} rootId 根Id 默认 0
  23. */
  24. export function handleTree(data: [], id: string, parentId: string, children: string, rootId: number) {
  25. id = id || 'id'
  26. parentId = parentId || 'parentId'
  27. children = children || 'children'
  28. rootId = rootId || 0
  29. //对源数据深度克隆
  30. const cloneData = JSON.parse(JSON.stringify(data))
  31. //循环所有项
  32. const treeData = cloneData.filter((father: any) => {
  33. let branchArr = cloneData.filter((child: any) => {
  34. //返回每一项的子级数组
  35. return father[id] === child[parentId]
  36. });
  37. branchArr.length > 0 ? father.children = branchArr : '';
  38. //返回第一层
  39. return father[parentId] === rootId;
  40. });
  41. return treeData != '' ? treeData : data;
  42. }
  43. // 回显数据字典
  44. export function selectDictLabel(data: any[], value: string): string {
  45. let actions: string[] = []
  46. data.map((item) => {
  47. if (item.value == value) {
  48. actions.push(item.label);
  49. return false;
  50. }
  51. })
  52. return actions.join('');
  53. }