| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /**
- * 响应拦截
- * @param {Object} http
- */
- export const responseInterceptors = (vm) => {
- if(!uni.$uv.http) return
- uni.$uv.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
- const data = response.data
- // 自定义参数
- const custom = response.config?.custom
- try {
- uni.hideLoading();
- } catch (error) {
-
- }
- if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
- // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
- if (custom.toast !== false) {
- // uni.$uv.toast(data.msg)
- setTimeout(() => {
- uni.showToast({
- title: data.msg,
- icon: 'none',
- duration: 3000
- })
- }, 300);
- }
- // const codeList = [100001, 100003, 100004, 100005, 100007]
- // if(codeList.includes(data.code)){
- // // 获取当前页面实例
- // const pages = getCurrentPages();
- // // 获取当前页面的实例
- // const currentPage = pages[pages.length - 1];
- // // 获取页面路径
- // const pagePath = currentPage.route;
- // // console.log('当前页面路径为:' + pagePath);
- // if(pagePath != 'pages/login/index'){
- // uni.setStorageSync('token', '')
- // uni.reLaunch({
- // url: "/pages/login/index"
- // })
- // }
- // }
- // 如果需要catch返回,则进行reject
- return Promise.reject(response.data)
- // if (custom?.catch) {
- // return Promise.reject(response.data)
- // } else {
- // // 否则返回一个pending中的promise
- // return new Promise(() => { })
- // }
- }
- return response.data || {}
- }, (response) => { /* 对响应错误做点什么 (statusCode !== 200)*/
- try {
- uni.hideLoading();
- } catch (error) {
- }
- return Promise.reject(response)
- })
- }
|