1
0

index.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import axios, { AxiosResponse } from "axios";
  2. import { ElMessage, ElMessageBox } from "element-plus";
  3. import qs from "qs";
  4. import { openLoading, closeLoading } from "./loading";
  5. import { openErrorMsg } from "./errorMsg";
  6. import {
  7. fromUrls,
  8. fileUrls,
  9. GetUrls,
  10. PostUrls,
  11. notLoginUrls,
  12. baseURL,
  13. unAuthCode,
  14. successCode,
  15. } from "./config";
  16. // import { show } from "@/store/case";
  17. import { strToParams } from "@/util";
  18. import { router } from "@/router";
  19. import { RouteName } from "@/router/config";
  20. // import { loginShow, setLoginShow } from "@/store/system";
  21. import { loginShow, setLoginShow } from "@/store/system";
  22. export * from "./urls";
  23. export * from "./config";
  24. export * from "./loading";
  25. export * from "./errorMsg";
  26. export type AuthHook = () => {
  27. token: string;
  28. userId: string;
  29. clear: () => void;
  30. };
  31. export const setLoginHook = (hook: AuthHook) => (getLogin = hook);
  32. let getLogin = () => ({ loginShow: "", userId: "0", setLoginShow: () => {} });
  33. export const setAuthHook = (hook: AuthHook) => (getAuth = hook);
  34. let getAuth: AuthHook = () => ({ token: "", userId: "0", clear: () => {} });
  35. axios.defaults.baseURL = baseURL;
  36. let messageBoxFlag = 0 // 默认 MessageBox 未打开
  37. const source = axios.CancelToken.source();
  38. axios.interceptors.request.use(async (config) => {
  39. console.log('config.params', config.params);
  40. if (config.method === "get" && config.params) {
  41. // for (const key in config.params) {
  42. // const val = config.params[key];
  43. // console.log('config.params', key, val);
  44. // try {
  45. // if (typeof val === "string" && !!val) {
  46. // config.params[key] = val.replaceAll(/[\[\]]/g, "").trim();
  47. // }
  48. // } catch (error) {
  49. // config.params[key] = val;
  50. // }
  51. // }
  52. }
  53. if (!config.url) {
  54. return config;
  55. }
  56. const PageParams = strToParams(window.location.search);
  57. let pageType = PageParams.show == 'true' ? 'view' : 'edit';
  58. const { token, userId } = getAuth();
  59. const route = router.currentRoute.value;
  60. let caseId = router.currentRoute.value?.params?.caseId
  61. if (!token && !~notLoginUrls.indexOf(config.url)) {
  62. console.log('用户未登录', caseId);
  63. router.replace({ name: RouteName.login});
  64. // let redirect = encodeURIComponent(`${window.location.href}`);
  65. // window.location.href = window.location.origin + "/#/login?redirect=" + redirect;
  66. throw "用户未登录";
  67. }
  68. config.headers.token = token;
  69. config.headers['caseid'] = caseId;
  70. // config.headers['Location'] = 'http://survey.4dkankan.com/';
  71. config.headers['page-type'] = pageType;
  72. config.headers.userid = userId;
  73. if (~GetUrls.indexOf(config.url)) {
  74. config.method = "GET";
  75. } else if (~PostUrls.indexOf(config.url)) {
  76. config.method = "POST";
  77. if (!config.data && config.params) {
  78. config.data = config.params;
  79. }
  80. }
  81. // 处理需要用表单上传的请求
  82. if (~fromUrls.indexOf(config.url)) {
  83. config.data = qs.stringify(config.data);
  84. config.headers["Content-Type"] =
  85. "application/x-www-form-urlencoded; charset=utf-8;";
  86. } else if (~fileUrls.indexOf(config.url)) {
  87. const fromData = new FormData();
  88. Object.keys(config.data).forEach((key) => {
  89. if (key === "files") {
  90. Array.from(config.data[key]).forEach((file) => {
  91. fromData.append("files", file as any as File);
  92. });
  93. } else {
  94. fromData.append(key, config.data[key]);
  95. }
  96. });
  97. config.data = fromData;
  98. config.headers["Content-Type"] = "multipart/form-data";
  99. }
  100. console.log("config", config);
  101. if (config.url === "/fusion/ai/getByImage") {
  102. return config;
  103. }
  104. openLoading(config.url);
  105. return config;
  106. });
  107. const responseInterceptor = (res: AxiosResponse<any, any>) => {
  108. closeLoading();
  109. let caseId = router.currentRoute.value?.params?.caseId
  110. let errMsg = res.data.msg || res.data.message;
  111. if (res.data.code === 40110) {
  112. ElMessageBox.alert(errMsg, "提示", {
  113. confirmButtonText: "去查看",
  114. type: "none",
  115. showClose: false,
  116. callback:async () => {
  117. window.location.href = window.location.origin + "/mix3d/?show=true#/abstract/"+caseId;
  118. }
  119. })
  120. throw res.data.msg;
  121. }
  122. if (res.data.code === 40111) {
  123. ElMessageBox.alert(errMsg, "提示", {
  124. confirmButtonText: "返回后台",
  125. type: "none",
  126. showClose: false,
  127. callback:async () => {
  128. // window.close()
  129. window.location.href = window.location.origin + "/admin/index.html";
  130. }
  131. })
  132. throw res.data.msg;
  133. }
  134. if (res.data.code === 4010 || res.data.code === 7012 ){
  135. if(messageBoxFlag === 0) {
  136. messageBoxFlag = 1 // 修改标记
  137. ElMessageBox.alert(errMsg||"您没有访问权限", "提示", {
  138. confirmButtonText: "我知道了",
  139. type: "none",
  140. showClose: false,
  141. callback:async () => {
  142. // window.close()
  143. messageBoxFlag = 0 // 修改标记
  144. router.replace({ name: RouteName.login});
  145. }
  146. })
  147. }
  148. throw res.data.msg;
  149. return
  150. }else if(!successCode.includes(res.data.code) && res.config?.responseType != "blob") {
  151. let errMsg = res.data.msg || res.data.message;
  152. openErrorMsg(errMsg);
  153. if (
  154. ~unAuthCode.indexOf(res.data.code) ||
  155. errMsg === "token已经失效,请重新登录"
  156. ) {
  157. // let redirect = encodeURIComponent(`${window.location.href}`);
  158. // window.location.href = window.location.origin + "/admin/#/login?redirect=" + redirect;
  159. // router.replace({ name: RouteName.login });
  160. router.replace({ name: RouteName.login});
  161. // getAuth().clear();
  162. }
  163. throw res.data.msg;
  164. }
  165. return res.data;
  166. };
  167. axios.interceptors.response.use(responseInterceptor, (error) => {
  168. closeLoading();
  169. if (error.response && error.response.data) {
  170. return responseInterceptor(error.response);
  171. } else {
  172. openErrorMsg(
  173. typeof error === "string" ? error : "请求失败,服务端发生了点小故障!"
  174. );
  175. return Promise.reject(error);
  176. }
  177. });
  178. export { axios };
  179. export type PaggingReq<T> = T & {
  180. pageNum: number;
  181. pageSize: number;
  182. };
  183. export type PaggingRes<T> = T & {
  184. total: number;
  185. list: T[];
  186. };