8
0

index.js 977 B

123456789101112131415161718192021222324252627282930313233
  1. import { httpApi } from 'hc-vue3-ui'
  2. import router from '~src/router/index'
  3. import pinia from '~src/store/init'
  4. import { useAppStore } from '~src/store'
  5. import website from '~src/config'
  6. //初始变量
  7. const store = useAppStore(pinia)
  8. //封装的请求
  9. export const HcApi = async (obj) => {
  10. return new Promise((resolve) => {
  11. //处理统一的请求头
  12. obj.headers = obj.headers ?? {}
  13. obj.headers['Client-Id'] = obj.headers['Client-Id'] ?? website.clientId
  14. obj.headers['Tenant-Id'] = obj.headers['Tenant-Id'] ?? store.tenantId
  15. //发起请求
  16. httpApi(obj).then((response) => {
  17. resolve(response)
  18. }).catch((response) => {
  19. getResData(response)
  20. resolve(response)
  21. })
  22. })
  23. }
  24. //处理数据
  25. const getResData = async ({ code }) => {
  26. if (code === 401) {
  27. window.$message?.error('身份失效,请重新登录!')
  28. router.push({ path: '/login' }).then()
  29. }
  30. }