zhangwei
2024-08-15 3fc70951bbd386c8c85e522cae80deba8823052e
src/common/request/request.js
@@ -1,10 +1,13 @@
import { apiBaseUrl } from '@/common/setting/constVarsHelper.js';

// 引入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
// 此vm参数为页面的实例,可以通过它引用vuex中的变量
module.exports = (vm) => {
    // 初始化请求配置
    uni.$u.http.setConfig((defaultConfig) => {
const initRequest=(vm)=>{
   http.setConfig((defaultConfig) => {
        /* defaultConfig 为默认全局配置 */
        defaultConfig.baseURL = apiBaseUrl; /* 根域名 */
        //defaultConfig.header = {
@@ -42,7 +45,7 @@
    })
    // 请求拦截
    uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
   http.interceptors.request.use((config) => { // 可使用async await 做异步操作
        // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
        config.data = config.data || {}
        if (config?.custom?.needToken) {
@@ -60,7 +63,8 @@
        } 
      
        //额外需求
        if (config.custom.methodName == 'user.share'||config.custom.methodName =="pages.getpageconfig" || config.custom.methodName == "goods.goodsList") {
      if (config.custom.methodName == 'user.share' || config.custom.methodName == "pages.getpageconfig" ||
         config.custom.methodName == "goods.goodsList") {
            const userToken = db.get("userToken");
            config.header.Authorization = 'Bearer ' + userToken;
        }
@@ -71,7 +75,8 @@
    })
    // 响应拦截
    uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
   http.interceptors.response.use((response) => {
      /* 对响应成功做点什么 可使用async await 做异步操作*/
        //console.log(response);
        const data = response.data
        if (response.statusCode == 200) {
@@ -114,4 +119,13 @@
        // 对响应错误做点什么 (statusCode !== 200)
        return Promise.reject(response)
    })
   // requestInterceptors()
   // responseInterceptors()
}
export {
   initRequest
}