wanshenmean
2025-04-08 517fb253f39bf8120f9bed693d35349727ae650c
PDA添加完整URL判断
已修改2个文件
90 ■■■■■ 文件已修改
代码管理/NEWCode/WIDESEAWCS_PDA/common/http.interceptor.js 59 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
代码管理/NEWCode/WIDESEAWCS_PDA/uview-ui/libs/request/index.js 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
´úÂë¹ÜÀí/NEWCode/WIDESEAWCS_PDA/common/http.interceptor.js
@@ -1,7 +1,6 @@
//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'
const install = (Vue, vm) => {
    // æ­¤ä¸ºè‡ªå®šä¹‰é…ç½®å‚数,具体参数见上方说明
    Vue.prototype.$u.http.setConfig({
        baseUrl: baseUrl,
@@ -13,22 +12,13 @@
    // è¯·æ±‚拦截,配置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 (config.url.includes('http://') || config.url.includes('https://')) {
            config.baseURL = '';
        } else {
            config.baseURL = baseUrl;
        }
        // æ–¹å¼äºŒï¼Œå¦‚果没有使用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 +31,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 +67,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;
        }
    }
´úÂë¹ÜÀí/NEWCode/WIDESEAWCS_PDA/uview-ui/libs/request/index.js
@@ -15,7 +15,7 @@
            let interceptorRequest = this.interceptor.request(options);
            if (interceptorRequest === false) {
                // è¿”回一个处于pending状态中的Promise,来取消原promise,避免进入then()回调
                return new Promise(()=>{});
                return new Promise(() => {});
            }
            this.options = interceptorRequest;
        }
@@ -34,7 +34,7 @@
                clearTimeout(this.config.timer);
                this.config.timer = null;
                // åˆ¤æ–­ç”¨æˆ·å¯¹æ‹¦æˆªè¿”回数据的要求,如果originalData为true,返回所有的数据(response)到拦截器,否则只返回response.data
                if(this.config.originalData) {
                if (this.config.originalData) {
                    // åˆ¤æ–­æ˜¯å¦å­˜åœ¨æ‹¦æˆªå™¨
                    if (this.interceptor.response && typeof this.interceptor.response === 'function') {
                        let resInterceptors = this.interceptor.response(response);
@@ -51,7 +51,8 @@
                    }
                } else {
                    if (response.statusCode == 200) {
                        if (this.interceptor.response && typeof this.interceptor.response === 'function') {
                        if (this.interceptor.response && typeof this.interceptor.response ===
                            'function') {
                            let resInterceptors = this.interceptor.response(response.data);
                            if (resInterceptors !== false) {
                                resolve(resInterceptors);
@@ -74,14 +75,20 @@
                }
            }
            // åˆ¤æ–­ç”¨æˆ·ä¼ é€’çš„URL是否/开头,如果不是,加上/,这里使用了uView的test.js验证库的url()方法
            options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url.indexOf('/') == 0 ?
                options.url : '/' + options.url));
            // åˆ¤æ–­æ˜¯å¦ä¸ºå®Œæ•´ URL,如果是则不使用 baseUrl
            if (options.url.includes('http://') || options.url.includes('https://')) {
                options.url = options.url;
            } else {
                // åˆ¤æ–­ç”¨æˆ·ä¼ é€’çš„URL是否/开头,如果不是,加上/,这里使用了uView的test.js验证库的url()方法
                options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url
                    .indexOf('/') == 0 ?
                    options.url : '/' + options.url));
            }
            // æ˜¯å¦æ˜¾ç¤ºloading
            // åŠ ä¸€ä¸ªæ˜¯å¦å·²æœ‰timer定时器的判断,否则有两个同时请求的时候,后者会清除前者的定时器id
            // è€Œæ²¡æœ‰æ¸…除前者的定时器,导致前者超时,一直显示loading
            if(this.config.showLoading && !this.config.timer) {
            if (this.config.showLoading && !this.config.timer) {
                this.config.timer = setTimeout(() => {
                    uni.showLoading({
                        title: this.config.loadingText,
@@ -116,7 +123,7 @@
            originalData: false, // æ˜¯å¦åœ¨æ‹¦æˆªå™¨ä¸­è¿”回服务端的原始数据,见文档说明
            loadingMask: true, // å±•示loading的时候,是否给一个透明的蒙层,防止触摸穿透
        }
        // æ‹¦æˆªå™¨
        this.interceptor = {
            // è¯·æ±‚前的拦截
@@ -144,7 +151,7 @@
                data
            })
        }
        // put请求,不支持支付宝小程序(HX2.6.15)
        this.put = (url, data = {}, header = {}) => {
            return this.request({
@@ -154,7 +161,7 @@
                data
            })
        }
        // delete请求,不支持支付宝和头条小程序(HX2.6.15)
        this.delete = (url, data = {}, header = {}) => {
            return this.request({
@@ -166,4 +173,4 @@
        }
    }
}
export default new Request
export default new Request