石广澎
2025-03-07 fb809790d865c5ec0a107c752f1a8c0be151cb9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
    ACCESSTOKEN,
    config,
    DEBUG
} from '@/common/config.js';
import {
    getPlat
} from 'common/util.js';
 
module.exports = (vm) => {
    uni.$u.http.setConfig((x) => {
        return x = {
            // baseURL: config.baseURL,
            dataType: 'json',
            timeout: 60000,
            showLoading: false, //是否显示全局loading
            timer: null,
            loadingTime: 800, //多少秒无返回再显示loading
            header: {
                'Clientid': '944c6aade52ebbffc015478e6ce51b5a',
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'Access-Control-Allow-Origin': '*'
            }
        }
    })
    // 请求拦截
    uni.$u.http.interceptors.request.use((x) => {
        DEBUG && console.log('请求参数', x);
        // 引用token
        const TOKEN = uni.getStorageSync(ACCESSTOKEN);
        if (TOKEN) {
            x.header[ACCESSTOKEN] = 'Bearer ' + TOKEN;
        }
        const plat = getPlat()
        if (plat == 2) { //微信
            const OPENID = uni.getStorageSync('OPENID') || null
            const UNIONID = uni.getStorageSync('UNIONID') || null
            if (OPENID) {
                x.header['Openid'] = OPENID;
            }
            if (UNIONID) {
                x.header['Unionid'] = UNIONID;
            }
        }
        if (plat == 5) { //支付宝
            const ALIUSERID = uni.getStorageSync('ALIUSERID') || null
            if (ALIUSERID) {
                x.header['Aliuserid'] = ALIUSERID;
            }
        }
        if (plat == 15) { //云闪付
            const UNIONPAYID = uni.getStorageSync('UNIONPAYID') || null
            if (UNIONPAYID) {
                x.header['Unionpayid '] = UNIONPAYID;
            }
        }
        return x;
    }, x => {
        return Promise.reject(x)
    })
    // 响应拦截
    uni.$u.http.interceptors.response.use((x) => {
        DEBUG && console.log('返回结果', x);
        let res = x.data
        if (res.code == 10000) {
            return res.data;
        } else if (res.code == 10002) {
            return Promise.reject(res)
        } else {
            vm.$u.toast(res.description || res.msg || res.info || "请求异常!"); //错误提示信息
            return Promise.reject(res)
        }
    }, (response) => {
        vm.$u.toast("请求异常!"); //错误提示信息
        return Promise.reject(response)
    })
}