useCommonStore.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. DictType,
  3. GlobalDict,
  4. getGlobalDict,
  5. upload,
  6. UploadData,
  7. } from '@/api/common';
  8. import { getSession } from '@/utils';
  9. import { Canceler } from 'axios';
  10. import isEmpty from 'lodash/isEmpty';
  11. import { defineStore } from 'pinia';
  12. import {
  13. zhdd_incident_level,
  14. zhdd_incident_source,
  15. zhdd_incident_status,
  16. zhdd_incident_type,
  17. zhdd_org_upload,
  18. zhdd_plan_type,
  19. zhdd_resource,
  20. } from './mock';
  21. export type GlobalDictStore = {
  22. [key in DictType]?: GlobalDict[];
  23. };
  24. export interface CommonStateType {
  25. globalDict: GlobalDictStore;
  26. loading: boolean;
  27. uploading: boolean;
  28. uploadFiles: UploadData[];
  29. }
  30. export interface CommonActionsType {
  31. toggleLoading(): void;
  32. getGlobalDict(dictType: DictType): void;
  33. upload(file: File): Promise<UploadData>;
  34. }
  35. export default defineStore<'common', CommonStateType, {}, CommonActionsType>(
  36. 'common',
  37. {
  38. state: () => ({
  39. globalDict: {
  40. zhdd_incident_level:
  41. getSession<GlobalDict[]>('zhdd_incident_level') ?? [],
  42. zhdd_incident_source:
  43. getSession<GlobalDict[]>('zhdd_incident_source') ?? [],
  44. zhdd_incident_status:
  45. getSession<GlobalDict[]>('zhdd_incident_status') ?? [],
  46. zhdd_incident_type:
  47. getSession<GlobalDict[]>('zhdd_incident_type') ?? [],
  48. zhdd_resource_goods:
  49. getSession<GlobalDict[]>('zhdd_resource_goods') ?? [],
  50. zhdd_org_upload: getSession<GlobalDict[]>('zhdd_org_upload') ?? [],
  51. zhdd_plan_type: getSession<GlobalDict[]>('zhdd_plan_type') ?? [],
  52. zhdd_resource: getSession<GlobalDict[]>('zhdd_resource') ?? [],
  53. },
  54. loading: false,
  55. uploading: false,
  56. uploadFiles: [],
  57. }),
  58. actions: {
  59. toggleLoading() {
  60. this.loading = !this.loading;
  61. },
  62. async getGlobalDict(params) {
  63. if (!isEmpty(this.globalDict[params])) return;
  64. try {
  65. this.toggleLoading();
  66. const { data } = await getGlobalDict(params);
  67. this.globalDict[params] = data;
  68. } finally {
  69. this.toggleLoading();
  70. }
  71. },
  72. async upload(params) {
  73. try {
  74. this.uploading = true;
  75. const { data } = await upload(params);
  76. this.uploadFiles.push(data);
  77. return data;
  78. } finally {
  79. this.uploading = false;
  80. }
  81. },
  82. },
  83. },
  84. );