Commit 597b630fba6ea3101eeeb7fc911ed61376164517

Authored by fengtao
1 parent 6c78ee70

refractor:删除一些无用代码,增加401踢回登录页

Showing 1 changed file with 4 additions and 33 deletions
... ... @@ -31,7 +31,6 @@ uni.$u.http.setConfig((config) => {
31 31 // 请求拦截
32 32 uni.$u.http.interceptors.request.use(
33 33 (config) => {
34   - // 可使用async await 做异步操作
35 34 // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
36 35 config.data = config.data || {};
37 36 // 根据custom参数中配置的是否需要token,添加对应的请求头
... ... @@ -41,7 +40,6 @@ uni.$u.http.interceptors.request.use(
41 40 uni.getStorageSync("userInfo").isToken ||
42 41 undefined;
43 42 }
44   - // console.log("请求开始", config);
45 43 if (config?.custom?.load) {
46 44 //打开加载动画
47 45 store.commit("setLoadingShow", true);
... ... @@ -50,7 +48,6 @@ uni.$u.http.interceptors.request.use(
50 48 return config;
51 49 },
52 50 (config) => {
53   - // 可使用async await 做异步操作
54 51 return Promise.reject(config);
55 52 }
56 53 );
... ... @@ -58,61 +55,35 @@ uni.$u.http.interceptors.request.use(
58 55 // 响应拦截
59 56 uni.$u.http.interceptors.response.use(
60 57 (response) => {
61   - /* 对响应成功做点什么 可使用async await 做异步操作*/
62 58 // 关闭加载动画
63 59 store.commit("setLoadingShow", false);
64 60 const data = response.data;
65   - // 自定义参数
66 61 const custom = response.config?.custom;
67 62 // code: 200、请求成功 其他,没有更多参数 401、被迫下线重新登录、
68 63 if (response.statusCode == 200) {
69 64 return Promise.resolve(data);
70   - // if (!custom.isFactory) {
71   - // return Promise.reject(response.data)
72   - // } else {
73   - // return data.data === undefined ? {} : Promise.reject(response.data)
74   - // }
75 65 } else if (response.statusCode == 401) {
76   - //被迫下线重新登录
77 66 // 清空登录信息
78 67 store.commit("emptyUserInfo");
79 68 // 20秒节流,弹窗登录
80 69 uni.$u.throttle(judgeLogin(), 20000);
81 70 return new Promise(() => {});
82   - } else {
83   - // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
84   - if (custom.toast !== false) {
85   - // uni.$u.toast(data.message || data.msg);
86   - }
87   - // 如果需要catch返回,则进行reject
88   - if (custom?.catch) {
89   - return Promise.reject(data);
90   - } else {
91   - // 否则返回一个pending中的promise,请求不会进入catch中
92   - return new Promise(() => {});
93   - }
94   - }
  71 + } else {}
95 72 },
96 73 (response) => {
  74 + console.log('Response error', response)
97 75 // 关闭加载动画
98 76 store.commit("setLoadingShow", false);
99   - // 对响应错误做点什么 (statusCode !== 200)
100   - let errorData = "请检查网络或服务器";
  77 + let errorData = '请检查网络或服务器'
101 78 let message = response.data?.message || response?.errMsg;
102 79 if (message == "request:fail url not in domain list") {
103 80 errorData = "检查请求域名是否添加了域名白名单";
104 81 } else if (message == "request:fail timeout") {
105 82 errorData = "请求超时:请检查网络";
106   - } else if (message == "Token has expired") {
  83 + } else if (response.data.status == 401) {
107 84 uni.reLaunch({
108 85 url: "/publicLoginSubPage/public/login",
109 86 });
110   - store.commit("emptyUserInfo");
111   - } else if (message == "Invalid username or password") {
112   - uni.reLaunch({
113   - url: "/publicLoginSubPage/public/login",
114   - });
115   - store.commit("emptyUserInfo");
116 87 } else {
117 88 errorData = message || "";
118 89 }
... ...