schangxiang@126.com
2025-09-19 a68302033da081f9ad5e82268a01892c3e129cc1
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import common from './common.js'
 
function req(obj) {
    return new Promise((resolve, reject) => {
        // 这个地址是 给 收货配置的地址 
        //const HOST = 'http://192.168.0.191:7001' // 正式生产线上地址
        const HOST = 'http://127.0.0.1:7001'  // 本地地址
         
        var method = obj.method || "GET";
        var url = HOST + obj.url || "";
        if (obj.url.indexOf('http') != -1) {
            url = obj.url;
        }
        var data = obj.data || {};
 
        // 前面信息
        var timestamp = new Date().valueOf();
        var nonce = Math.random()
            .toString(36)
            .substr(2);
        let tokenInfo = JSON.parse(uni.getStorageSync('$tokenInfouser') || '{}')
        var guid = tokenInfo.guid
        var accessToken = uni.getStorageSync('token') || ''
 
        var signature = common.getSignature(timestamp, nonce, tokenInfo, data);
        var headers = Object.assign({
            timestamp: timestamp,
            nonce: nonce,
            guid: guid,
            accessToken: accessToken,
            signature: signature,
            'Content-Type': 'application/json;charset=UTF-8'
        });
        var header = {
            ...obj.header,
            ...headers
        };
        var success = obj.success; // 成功回调函数
        var fail = obj.fail; //表示失败后,要执行的回调函数
        uni.showLoading({
            title: '加载中...',
            mask: true
        });
        console.log('xxxxx', obj)
        uni.request({
            url: url,
            data: data,
            method: method,
            header: header,
            success: ((res) => {
                console.log('success', res)
                // debugger
                uni.hideLoading();
                if (res.statusCode == 403 || res.statusCode == 401) {
                    // 错误处理,返回登录页
                    uni.reLaunch({
                        url: '/pages/Login/index'
                    })
                } else if (res.data.message == '用户未登录,请先登录程序') {
                    uni.showToast({
                        title: '用户登陆状态失效',
                        icon: 'none',
                        duration: 2000,
                        success: function(res) {
                            setTimeout(() => {
                                uni.reLaunch({
                                    url: '/pages/login/index'
                                })
                            }, 1000)
                        }
                    });
                } else if (res.statusCode == 200) {
                    resolve(res)
                    uni.hideLoading();
                } else {
                    let msg = res.errMsg || `接口调用出错:${res.statusCode}`
                    uni.showModal({
                        title: '系统提示',
                        content: msg,
                        showCancel: false
                    })
                    reject(res)
                    uni.hideLoading();
                }
            }),
            fail: ((err) => {
                uni.hideLoading();
                reject("失败")
            }),
            complete: (e) => {
                console.log('complete')
                uni.hideLoading();
            }
        })
    })
}
export default req