123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { commonConfig } from '@/common/config.js';
- import {againToken} from '../utils/againToken.js'
- import { showFullScreenLoading , tryHideFullScreenLoading } from '../utils/loading.js'
- let showModal = false;
- // 此vm参数为页面的实例,可以通过它引用vuex中的变量
- module.exports = (vm) => {
- // 初始化请求配置
- uni.$u.http.setConfig((config) => {
- /* config 为默认全局配置*/
- config.baseURL = commonConfig.baseUrl; /* 根域名 */
- return config
- })
-
- // 请求拦截
- uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
- // console.log('config========',config);
- // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
- config.data = config.data || {}
- // 根据custom参数中配置的是否需要token,添加对应的请求头
- if(!config?.custom?.auth) {
- // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
- // config.header.token = vm.vuex_user_info.token;
- config.header.Authorization = `Bearer ${vm.vuex_user_info.accessToken}`;
- }
- if(!config?.custom?.noload){
- showFullScreenLoading()
- }
- return config
- }, config => { // 可使用async await 做异步操作
- return Promise.reject(config)
- })
-
- let unlogin = function(){
- if(vm.vuex_user_info.userid&&vm.vuex_wechatOpenid) {
- againToken(vm.$u,vm.vuex_wechatOpenid,vm.vuex_user_info.userid)
- } else {
- let pages = getCurrentPages();
- // console.log('pages',pages);
- let backUrl = pages[pages.length - 1].route;
- let options =uni.$u.queryParams( pages[pages.length - 1].options);
- let fullBackUrl = backUrl+options;
-
- // const backArr = ['productdetails'];//需要登录返回的页面
- // const hasBackArr = backArr.some(backPage => backUrl.includes(backPage));
- // if(hasBackArr){
- // console.log('包含');
- // uni.setStorage({
- // key: 'backUrl',
- // data: fullBackUrl,
- // success: function () {
- // // console.log('setStorage success');
- // }
- // });
- // }else{
- // console.log('不包含');
- // uni.removeStorage({
- // key: 'backUrl',
- // success: function (res) {
- // // console.log('success');
- // }
- // });
- // }
- uni.setStorage({
- key: 'backUrl',
- data: fullBackUrl,
- success: function () {
- // console.log('setStorage success');
- }
- });
- tryHideFullScreenLoading()
- if(showModal){return}
- showModal = true;
- uni.showModal({
- title: '提示',
- content: '你需要登录后,才可使用此功能!',
- success: res => {
- if (res.confirm) {
- uni.$u.route('/pages/login/login');
- }else{
- uni.removeStorage({
- key: 'backUrl',
- success: function (res) {
- // console.log('success');
- }
- });
- let pages = getCurrentPages();
- // console.log('pages',pages);
- if(pages.length>1){
- uni.navigateBack()
- }
- }
- },
- complete() {
- showModal = false
- }
- })
- }
- }
-
- // 响应拦截
- uni.$u.http.interceptors.response.use((response) => {/* 对响应成功做点什么 可使用async await 做异步操作*/
- // console.log('response====response',response);
- const data = response.data
- // 自定义参数
- const custom = response.config?.custom
- if (data.code !== 200) {
- console.log('data====',data);
- // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
- if (custom.toast !== false) {
- const unshowmsg = ['令牌不能为空'];
- if (!unshowmsg.includes(data.msg)) {
- uni.$u.toast(data.msg)
- }
- // uni.$u.toast(data.msg)
- }
- if(data.msg == "令牌验证失败" || data.msg == "令牌不能为空" || data.code == 401){
- unlogin()
- }
-
- if(data.msg == "用户不存在!"||data.msg == "用户未注册"){
- uni.clearStorage();
- unlogin()
- }
-
- return Promise.reject(data)
- // 如果需要catch返回,则进行reject
- // if (custom?.catch) {
- // return Promise.reject(data)
- // } else {
- // // 否则返回一个pending中的promise,请求不会进入catch中
- // return new Promise(() => { })
- // }
- }
- // console.log('data--',data);
- tryHideFullScreenLoading()
- return data === undefined ? {} : data
- }, (response) => {
- tryHideFullScreenLoading()
- // console.log('response==',response);
- const data = response.data;
- // console.log('data==',data);
- // 对响应错误做点什么 (statusCode !== 200)
- let errMap = {
- '404':'接口不存在'
- };
- if (response.statusCode in errMap) {
- uni.$u.toast(errMap[response.statusCode])
- }
- if(data.msg == "令牌不能为空" || data.code == 401){
- unlogin()
- }
- return Promise.reject(response)
- })
- }
|