| | |
| | | |
| | | // 引入luch-request |
| | | import { http } from '@/uni_modules/uview-plus' |
| | | import { |
| | | http |
| | | } from '@/uni_modules/uview-plus' |
| | | import { |
| | | apiBaseUrl |
| | | } from '@/common/setting/constVarsHelper.js'; |
| | | import * as db from '@/common/utils/dbHelper.js' //引入common |
| | | // 初始化请求配置 |
| | | const initRequest=(vm)=>{ |
| | | const initRequest = (vm) => { |
| | | http.setConfig((defaultConfig) => { |
| | | /* defaultConfig 为默认全局配置 */ |
| | | defaultConfig.baseURL = apiBaseUrl; /* 根域名 */ |
| | |
| | | // 获取用户token |
| | | const userToken = db.get("userToken"); |
| | | if (!userToken) { |
| | | //console.log("开启弹窗"); |
| | | vm.$store.commit('showLoginTip', true); |
| | | console.log("开启弹窗",vm.$store); |
| | | // vm.$store.commit('showLoginTip', true); |
| | | //console.log("弹窗已经开启"); |
| | | // return false; |
| | | } else { |
| | | config.header.Authorization = 'Bearer ' + userToken; |
| | | } |
| | | |
| | | console.log(config, config?.custom?.needToken, 'config?.custom?.needToken'); |
| | | } |
| | | |
| | | //额外需求 |
| | | if (config.custom.methodName == 'user.share' || config.custom.methodName == "pages.getpageconfig" || |
| | | config.custom.methodName == "goods.goodsList") { |
| | |
| | | // 响应拦截 |
| | | http.interceptors.response.use((response) => { |
| | | /* 对响应成功做点什么 可使用async await 做异步操作*/ |
| | | //console.log(response); |
| | | console.log(response,'response,'); |
| | | const data = response.data |
| | | if (response.statusCode == 200) { |
| | | let pages = getCurrentPages(); |
| | |
| | | |
| | | if (!data.status && page) { |
| | | // 登录信息过期或者未登录 |
| | | if (data.data === 14007 || data.data === 14006) { |
| | | if (data.data === 401 || data.data === 14006) { |
| | | // #ifdef APP-PLUS || APP-PLUS-NVUE |
| | | if (page.route.indexOf('pages/login/index') < 0) { |
| | | db.del("userToken"); |
| | |
| | | // #endif |
| | | // #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO |
| | | db.del("userToken"); |
| | | vm.$store.commit('userInfo', null); |
| | | // vm.$store.commit('userInfo', null); |
| | | //console.log("开启登录弹窗"); |
| | | vm.$store.commit('hasLogin', false); |
| | | // vm.$store.commit('hasLogin', false); |
| | | // #endif |
| | | } |
| | | } |
| | | }else if(response.statusCode==401){ |
| | | db.del("userToken"); |
| | | uni.showToast({ |
| | | title: '登录失效,请重新登录!', |
| | | icon: 'none', |
| | | duration: 1000, |
| | | complete: function() { |
| | | setTimeout(function() { |
| | | uni.hideToast(); |
| | | uni.redirectTo({ |
| | | url: '/pages/login/index' |
| | | }); |
| | | }, |
| | | 1000); |
| | | } |
| | | }); |
| | | } |
| | | return data === undefined ? {} : data |
| | | }, (response) => { |
| | |
| | | } |
| | | export { |
| | | initRequest |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |