errorInfo.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { toRefs, shallowReadonly, ShallowRef } from 'vue'
  2. import { queryErrorInfos } from '@/services/api/common'
  3. import { VueStore } from '../base'
  4. import WebStorage from '@/utils/storage/base'
  5. interface StoreState {
  6. loading: boolean;
  7. errorInfos: ShallowRef<Ermcp.ErrorInfosRsp[]>;
  8. }
  9. /**
  10. * 错误信息存储类
  11. */
  12. const store = new (class extends VueStore<StoreState>{
  13. constructor() {
  14. const storage = new WebStorage<Ermcp.ErrorInfosRsp[]>(sessionStorage, 'errorInfos', [])
  15. const state: StoreState = {
  16. loading: false,
  17. errorInfos: storage.getRef(),
  18. }
  19. super(state)
  20. }
  21. actions = {
  22. /** 获取系统错误信息 */
  23. getErrorInfoList: () => {
  24. if (this.state.errorInfos.length) {
  25. return Promise.resolve()
  26. }
  27. this.state.loading = true
  28. return queryErrorInfos({
  29. success: (res) => {
  30. this.state.errorInfos = res.data
  31. },
  32. complete: () => {
  33. this.state.loading = false
  34. }
  35. })
  36. },
  37. /** 根据 code 获取错误信息 */
  38. getErrorInfoByCode: (code: number) => {
  39. const errorInfos = this.state.errorInfos
  40. const error = errorInfos.find((e) => e.errorid === code)
  41. return error?.description
  42. }
  43. }
  44. })
  45. export function useErrorInfoStore() {
  46. return shallowReadonly({
  47. ...toRefs(store.state),
  48. ...store.actions,
  49. ...store.methods,
  50. })
  51. }