1
0

index.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if(res.config.url === "/package/data.json"){
  110. return res.data;
  111. }
  112. let caseId = router.currentRoute.value?.params?.caseId
  113. let errMsg = res.data.msg || res.data.message;
  114. if (res.data.code === 40110) {
  115. ElMessageBox.alert(errMsg, "提示", {
  116. confirmButtonText: "去查看",
  117. type: "none",
  118. showClose: false,
  119. callback:async () => {
  120. window.location.href = window.location.origin + "/mix3d/?show=true#/abstract/"+caseId;
  121. }
  122. })
  123. throw res.data.msg;
  124. }
  125. if (res.data.code === 40111) {
  126. ElMessageBox.alert(errMsg, "提示", {
  127. confirmButtonText: "返回后台",
  128. type: "none",
  129. showClose: false,
  130. callback:async () => {
  131. // window.close()
  132. window.location.href = window.location.origin + "/admin/index.html";
  133. }
  134. })
  135. throw res.data.msg;
  136. }
  137. if (res.data.code === 4010 || res.data.code === 7012 ){
  138. if(messageBoxFlag === 0) {
  139. messageBoxFlag = 1 // 修改标记
  140. ElMessageBox.alert(errMsg||"您没有访问权限", "提示", {
  141. confirmButtonText: "我知道了",
  142. type: "none",
  143. showClose: false,
  144. callback:async () => {
  145. // window.close()
  146. messageBoxFlag = 0 // 修改标记
  147. router.replace({ name: RouteName.login});
  148. }
  149. })
  150. }
  151. throw res.data.msg;
  152. return
  153. }else if(!successCode.includes(res.data.code) && res.config?.responseType != "blob") {
  154. let errMsg = res.data.msg || res.data.message;
  155. // openErrorMsg(errMsg);
  156. if (
  157. ~unAuthCode.indexOf(res.data.code) ||
  158. errMsg === "token已经失效,请重新登录"
  159. ) {
  160. // let redirect = encodeURIComponent(`${window.location.href}`);
  161. // window.location.href = window.location.origin + "/admin/#/login?redirect=" + redirect;
  162. // router.replace({ name: RouteName.login });
  163. router.replace({ name: RouteName.login});
  164. // getAuth().clear();
  165. }
  166. throw res.data.msg;
  167. }
  168. return res.data;
  169. };
  170. axios.interceptors.response.use(responseInterceptor, (error) => {
  171. closeLoading();
  172. if (error.response && error.response.data) {
  173. return responseInterceptor(error.response);
  174. } else {
  175. // openErrorMsg(
  176. // typeof error === "string" ? error : "请求失败,服务端发生了点小故障!"
  177. // );
  178. return Promise.reject(error);
  179. }
  180. });
  181. export { axios };
  182. export type PaggingReq<T> = T & {
  183. pageNum: number;
  184. pageSize: number;
  185. };
  186. export type PaggingRes<T> = T & {
  187. total: number;
  188. list: T[];
  189. };