zhangwei
2024-08-28 d148889f32babca7ffefa3336a22f9f38dc0c525
src/common/request/request.js
@@ -1,12 +1,13 @@

// 引入luch-request
import { http } from '@/uni_modules/uview-plus'
// 引入luch-request
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; /* 根域名 */
@@ -59,9 +60,8 @@
         } 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") {
@@ -77,7 +77,7 @@
   // 响应拦截
   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();
@@ -85,7 +85,7 @@
         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");
@@ -113,6 +113,22 @@
               // #endif
            }
         }
      }else if(response.statusCode==401){
         db.del("userToken");
         uni.showToast({
            title: '登录失效,请重新登录!',
            icon: 'none',
            duration: 1000,
            complete: function() {
               setTimeout(function() {
                     uni.hideToast();
                     uni.navigateTo({
                        url: '/pages/login/index'
                     });
                  },
                  1000);
            }
         });
      }
      return data === undefined ? {} : data
   }, (response) => {
@@ -124,8 +140,4 @@
}
export {
   initRequest
}
}