wankeda
2025-06-24 6cc35000a6e138cfad96e7b02f8aeddcdb4ba6bf
´úÂë¹ÜÀí/NEWCode/WIDESEAWCS_PDA/common/http.interceptor.js
@@ -1,10 +1,10 @@
//let baseUrl ='http://192.168.1.54:8098' //'http://192.168.11.58:8098'
 // let baseUrl = 'http://192.168.12.240:8098'
let baseUrl = 'http://127.0.0.1:9291'
let baseUrl = ''
const value = uni.getStorageSync('storage_key_ipAddress');
const install = (Vue, vm) => {
   let resolvedBaseUrl = baseUrl === '' ? (value && value.WCSIP ? value.WCSIP : "") : baseUrl;
   // æ­¤ä¸ºè‡ªå®šä¹‰é…ç½®å‚数,具体参数见上方说明
   Vue.prototype.$u.http.setConfig({
      baseUrl: baseUrl,
      baseUrl: resolvedBaseUrl,
      loadingText: '努力加载中~',
      loadingTime: 5000,
      originalData: true,
@@ -13,22 +13,38 @@
   // è¯·æ±‚拦截,配置Token等参数
   Vue.prototype.$u.http.interceptor.request = (config) => {
      // å¼•用token
      // æ–¹å¼ä¸€ï¼Œå­˜æ”¾åœ¨vuex的token,假设使用了uView封装的vuex方式
      // è§ï¼šhttps://uviewui.com/components/globalVariable.html
      // config.header.token = vm.token;
      // åˆ¤æ–­æ˜¯å¦ä¸ºå®Œæ•´ URL,如果是则不使用 baseUrl
      if (resolvedBaseUrl === '') {
         if (config.url.includes('http://') || config.url.includes('https://')) {
            config.baseURL = '';
            resolve(config);
         } else {
            const storedValue = uni.getStorageSync('storage_key_ipAddress');
            if (storedValue && storedValue.WCSIP) {
               config.baseURL = storedValue.WCSIP;
               baseUrl = storedValue.WCSIP;
            } else {
               vm.$u.toast('请先添加后台IP地址');
               return false; // å–消本次请求
            }
            // uni.getStorage({
            //    key: 'storage_key_ipAddress',
            //    success: (res) => {
            //       debugger;
            //       console.log(res.data.WCSIP);
            //       config.baseURL = res.data.WCSIP;
            //       baseUrl = res.data.WCSIP
            //    },
            //    fail: (err) => {
            //       debugger;
            //       vm.$u.toast('请先添加后台IP地址');
            //    },
            // });
         }
      } else {
            config.baseURL = resolvedBaseUrl;
      }
      // æ–¹å¼äºŒï¼Œå¦‚果没有使用uView封装的vuex方法,那么需要使用$store.state获取
      // config.header.token = vm.$store.state.token;
      // æ–¹å¼ä¸‰ï¼Œå¦‚æžœtoken放在了globalData,通过getApp().globalData获取
      // config.header.token = getApp().globalData.username;
      // æ–¹å¼å››ï¼Œå¦‚æžœtoken放在了Storage本地存储中,拦截是每次请求都执行的
      // æ‰€ä»¥å“ªæ€•您重新登录修改了Storage,下一次的请求将会是最新值
      // const token = uni.getStorageSync('token');
      // config.header.token = token;
      console.log(2)
      config.header.Token = 'xxxxxx';
      config.header.Authorization = "Bearer " + uni.getStorageSync('jo_id_token');
      // å¯ä»¥å¯¹æŸä¸ªurl进行特别处理,此url参数为this.$u.get(url)中的url值 
@@ -41,9 +57,7 @@
   // å“åº”拦截,判断状态码是否通过
   Vue.prototype.$u.http.interceptor.response = (res) => {
      console.log(1)
      if (res.statusCode == 200) {
         console.log(0)
         // res为服务端返回值,可能有code,result等字段
         // è¿™é‡Œå¯¹res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
         // å¦‚果配置了originalData为true,请留意这里的返回值
@@ -79,38 +93,7 @@
               url: '/pages/login/login'
            });
         })
         // uni.request({
         //    url: "http://192.168.12.245:8099/api/User/replaceToken",
         //    param: {},
         //    method: 'POST',
         //    responseType: "text",
         //    header: {
         //       Authorization: "Bearer " + vm.vuex_token
         //    },
         //    async: false,
         //    success: function(x) {
         //       if (x.data.status) {
         //          vm.$u.vuex('vuex_token',x.data.data)
         //          vm.$u.route({
         //             type: "navigateBack",
         //             delta: -1
         //          })
         //       } else {
         //          console.log(x.data.message);
         //          vm.$u.toast('验证过期,请重新登录');
         //          setTimeout(() => {
         //             // æ­¤ä¸ºuView的方法,详见路由相关文档
         //             vm.$u.route('/pages/user/login')
         //          }, 1500)
         //       }
         //    },
         //    errror: function(ex) {
         //       console.log(ex);
         //       uni.reLaunch({
         //          url: '/pages/user/login'
         //       });
         //    },
         // });
         // return false; // return false后不再进入then回调,但会进入catch回调
         return false;
      }
   }