device.js 1.5 KB

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