errorInfo.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { shallowRef } from 'vue'
  2. import { queryErrorInfos } from '@/services/api/common'
  3. import { defineStore } from '../store'
  4. import { sessionData } from '../storage'
  5. import { i18n } from '@/stores'
  6. /**
  7. * 错误信息存储对象
  8. */
  9. export const useErrorInfoStore = defineStore(() => {
  10. const loading = shallowRef(false)
  11. const errorInfos = sessionData.getRef('errorInfos')
  12. // 获取系统错误信息
  13. const getErrorInfoList = async () => {
  14. try {
  15. if (errorInfos.value.length) {
  16. return Promise.resolve()
  17. }
  18. loading.value = true
  19. const res = await queryErrorInfos()
  20. errorInfos.value = res.data
  21. } finally {
  22. loading.value = false
  23. }
  24. }
  25. // 根据 code 获取错误信息
  26. const getErrorInfoByCode = (code: number) => {
  27. const error = errorInfos.value.find((e) => e.errorid === code)
  28. switch (i18n.global.locale) {
  29. case 'zh-CN':
  30. return error?.description
  31. break;
  32. case 'en-US':
  33. return error?.descriptionen
  34. break;
  35. case 'zh-TW':
  36. return error?.descriptionzh_tw
  37. break;
  38. default:
  39. return error?.descriptionth
  40. break;
  41. }
  42. }
  43. return {
  44. loading,
  45. errorInfos,
  46. getErrorInfoList,
  47. getErrorInfoByCode,
  48. }
  49. })