axiosReq.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import axios from "axios";
  2. import router from "@/router";
  3. import { ElLoading, ElMessage, ElMessageBox } from "element-plus";
  4. import { getToken, setToken, removeToken } from "@/utils/auth";
  5. import { AxiosConfigTy, AxiosReqTy, ObjTy } from "~/common";
  6. import { useUserStore } from "@/store/user";
  7. let reqConfig: any;
  8. let loadingE: any;
  9. const service: any = axios.create();
  10. // 请求拦截
  11. service.interceptors.request.use(
  12. (request: AxiosReqTy) => {
  13. const authMap = useUserStore().authMap;
  14. const data = request.data!;
  15. if (data.btnAuth) {
  16. if (authMap[data.btnAuth]) {
  17. request.headers["auth-id"] = authMap[data.btnAuth];
  18. sessionStorage.setItem("auth-id", authMap[data.btnAuth]);
  19. }
  20. delete data.btnAuth;
  21. } else {
  22. const route = router.currentRoute;
  23. if (!data.id || data.id !== DATACONTENT_ID.loginId) {
  24. const role = route.value.meta?.roles?.[0];
  25. if (role && authMap[role]) {
  26. request.headers["auth-id"] = authMap[role];
  27. }
  28. }
  29. }
  30. // token setting
  31. request.headers["token"] = getToken();
  32. /* download file*/
  33. if (request.isDownLoadFile) {
  34. request.responseType = "blob";
  35. }
  36. /* upload file*/
  37. if (request.isUploadFile) {
  38. request.headers["Content-Type"] = "application/json";
  39. }
  40. reqConfig = request;
  41. if (request.bfLoading) {
  42. loadingE = ElLoading.service({
  43. lock: true,
  44. text: "数据载入中",
  45. // spinner: 'el-icon-ElLoading',
  46. background: "rgba(0, 0, 0, 0.1)",
  47. });
  48. }
  49. /*
  50. *params会拼接到url上
  51. * */
  52. if (request.isParams) {
  53. request.params = request.data;
  54. request.data = {};
  55. }
  56. return request;
  57. },
  58. (err: any) => {
  59. Promise.reject(err);
  60. }
  61. );
  62. // 响应拦截
  63. service.interceptors.response.use(
  64. (response) => {
  65. const res = response.data;
  66. // if the custom code is not 20000, it is judged as an error.
  67. if (res.code != "0") {
  68. if (res.message && res.message.includes("Duplicate entry")) {
  69. ElMessage({
  70. message: "执行失败",
  71. type: "error",
  72. duration: 5 * 1000,
  73. });
  74. } else {
  75. ElMessage({
  76. message: res.message ?? "执行失败",
  77. type: "error",
  78. duration: 5 * 1000,
  79. });
  80. }
  81. return Promise.reject(res.message || res.returnData?.message || "Error");
  82. } else {
  83. return res;
  84. }
  85. },
  86. (error) => {
  87. const des500 = `${error}`.split(" ").includes("500");
  88. if (des500) {
  89. ElMessage({
  90. message: "身份令牌过期或失效,即将重新登录",
  91. type: "error",
  92. duration: 3 * 1000,
  93. onClose: async () => {
  94. sessionStorage.clear();
  95. removeToken(); // must remove token first
  96. removeToken("codeToken");
  97. removeToken("systemSet");
  98. removeToken("userid");
  99. removeToken("auth-id");
  100. // store.dispatch('app/toggleOutflag', false)
  101. // await store.dispatch('user/resetToken')
  102. useUserStore().resetState();
  103. router.push(`/login`);
  104. location.reload();
  105. },
  106. });
  107. } else {
  108. ElMessage({
  109. message: error.message,
  110. type: "error",
  111. duration: 5 * 1000,
  112. });
  113. }
  114. return Promise.reject(error);
  115. }
  116. );
  117. export function axiosReq({
  118. url,
  119. data,
  120. method,
  121. headers,
  122. isParams,
  123. bfLoading,
  124. afHLoading,
  125. isUploadFile,
  126. isDownLoadFile,
  127. baseURL,
  128. timeout,
  129. isAlertErrorMsg = true,
  130. }: AxiosConfigTy): any {
  131. return service({
  132. url: url,
  133. method: method ?? "get",
  134. data: data ?? {},
  135. headers: headers ?? {},
  136. isParams: isParams ?? false,
  137. bfLoading: bfLoading ?? false,
  138. afHLoading: afHLoading ?? true,
  139. isUploadFile: isUploadFile ?? false,
  140. isDownLoadFile: isDownLoadFile ?? false,
  141. isAlertErrorMsg: isAlertErrorMsg,
  142. // baseURL: baseURL ?? import.meta.env.VITE_APP_BASE_URL,
  143. baseURL: baseURL ?? PLATFROM_CONFIG.baseNewUrl,
  144. timeout: timeout ?? 15000,
  145. });
  146. }
  147. export default axiosReq;