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