use-yf-group.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { defineStore } from "pinia";
  2. import { getYpZdGroup } from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
  3. type CodeName = {
  4. code: string;
  5. name: string;
  6. isDefault: true;
  7. };
  8. export const useYfGroupStore = defineStore("groupStore", {
  9. state: () => ({
  10. // 西药
  11. xy: null as CodeName[],
  12. // 草药
  13. cy: null as CodeName[],
  14. }),
  15. getters: {
  16. getXyCodeList(state) {
  17. return state.xy.map(item => item.code);
  18. },
  19. getXyCodeName(state) {
  20. return (code: string) =>
  21. state.xy.find(item => item.code === code)?.name || "";
  22. },
  23. getCyCodeList(state) {
  24. return state.cy.map(item => item.code);
  25. },
  26. getCyCodeName(state) {
  27. return (code: string) =>
  28. state.cy.find(item => item.code === code)?.name || "";
  29. },
  30. getDefaultCode(state) {
  31. return (code: keyof typeof state) => {
  32. const tmp = state[code];
  33. if (state[code]?.length > 0) {
  34. return tmp?.find(item => item.isDefault)?.code || tmp[0].code;
  35. }
  36. return "";
  37. };
  38. },
  39. getDefaultXyCode() {
  40. return this.getDefaultCode("xy");
  41. },
  42. getDefaultCyCode() {
  43. return this.getDefaultCode("cy");
  44. },
  45. },
  46. actions: {
  47. async init() {
  48. const tmp = await getYpZdGroup().catch(() => {
  49. return {
  50. // 西药
  51. xy: [],
  52. // 草药
  53. cy: [],
  54. };
  55. });
  56. this.xy = tmp.xy;
  57. this.cy = tmp.cy;
  58. },
  59. },
  60. });