dictionary.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { store } from "@/store";
  2. import { defineStore } from "pinia";
  3. import { getUserDicts, getUserList } from "@/api/auth";
  4. export const useDictionaryStore = defineStore("dictionaryStore", () => {
  5. const types = [
  6. "defect_mana",
  7. "stage",
  8. "process_state",
  9. "outsource_state",
  10. "work_order_seq_state",
  11. "accessories_type",
  12. "danwei_type",
  13. ];
  14. const dicts = ref<{ [key: string]: any[] }>({});
  15. // 所有的用户列表
  16. const allUsers = ref<any[]>([]);
  17. function checkAllData() {
  18. if (JSON.stringify(dicts.value) === "{}") {
  19. getUserDicts(types).then((res) => {
  20. if (res.data) {
  21. dicts.value = res?.data ?? [];
  22. }
  23. });
  24. }
  25. if (allUsers.value.length === 0) {
  26. getUserList().then((res) => {
  27. allUsers.value = res.data || [];
  28. });
  29. }
  30. }
  31. function getLableByValue(type: string, value: string) {
  32. const dict = dicts.value[type];
  33. if (dict) {
  34. const lable = dict.find((item: any) => item.dictValue === value);
  35. return lable?.dictLabel;
  36. }
  37. return "";
  38. }
  39. return {
  40. types,
  41. dicts,
  42. allUsers,
  43. checkAllData,
  44. getLableByValue,
  45. };
  46. });
  47. export function useDictionaryStoreHook() {
  48. // console.log('dicts:',useDictionaryStore(store))
  49. return useDictionaryStore(store);
  50. }