dept.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { defineStore } from "pinia";
  2. import { ref } from "vue";
  3. import { getAllDeptList } from "@/api";
  4. export const useDeptStore = defineStore('dept', () => {
  5. const deptList = ref([])
  6. const deptTree = ref([])
  7. const buildTree = (data) => {
  8. const map = data.reduce((acc, item) => {
  9. acc[item.id] = { ...item, children: [] };
  10. return acc;
  11. }, {});
  12. const tree = [];
  13. data.forEach(item => {
  14. if (item.parentId && map[item.parentId]) {
  15. map[item.parentId].children.push(map[item.id]);
  16. } else if (item.parentId === 0) {
  17. tree.push(map[item.id]);
  18. }
  19. });
  20. return tree;
  21. }
  22. const loadDeptList = async () => {
  23. // const locale = uni.getLocale()
  24. // const list = (await getAllDeptList()).data.map((item) => {
  25. // if (item.name.includes('~~') && item.name.includes('**')) {
  26. // const s = item.name.split('~~')
  27. // if (locale.startsWith('zh')) {
  28. // item.name = s[0]
  29. // } else if (locale.startsWith('ru')) {
  30. // const s2 = s[2].split('**')
  31. // item.name = s2[1]
  32. // } else {
  33. // const s1 = s[1].split('**')
  34. // item.name = s1[1]
  35. // }
  36. // }
  37. // return JSON.parse(JSON.stringify(item))
  38. // })
  39. const list = (await getAllDeptList()).data
  40. deptList.value = list
  41. deptTree.value = buildTree(list)
  42. }
  43. return { deptList, deptTree, loadDeptList }
  44. })