zhangwei
2024-08-15 3fc70951bbd386c8c85e522cae80deba8823052e
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Config from './baseConfig.js'
import Util from './util.js'
// import store from '@/store/index.js'
// #ifdef H5
const app_type = Util.isWeiXin() ? 'wechat' : 'h5';
const app_type_name = Util.isWeiXin() ? '微信公众号' : 'H5';
// #endif
 
// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif
 
// #ifdef MP-ALIPAY
const app_type = 'aliapp';
const app_type_name = '支付宝小程序';
// #endif
 
// #ifdef MP-BAIDU
const app_type = 'baiduapp';
const app_type_name = '百度小程序';
// #endif
 
// #ifdef MP-TOUTIAO
const app_type = 'MP-TOUTIAO';
const app_type_name = '头条小程序';
// #endif
 
// #ifdef MP-QQ
const app_type = 'MP-QQ';
const app_type_name = 'QQ小程序';
// #endif
 
// #ifdef APP-PLUS
const app_type = 'app';
const app_type_name = 'APP';
// #endif
 
export default {
    sendRequest(params) {
        var method = params.data != undefined ? 'POST' : 'POST', // 请求方式
            url = Config.baseUrl + params.url, // 请求路径
            data = {
                // app_type,
                // app_type_name
            };
 
        // token
        if (uni.getStorageSync('uid')) data.uid = uni.getStorageSync('uid');
 
        // 城市id
        if (uni.getStorageSync('city')) data.web_city = uni.getStorageSync('city').id;
 
        // 参数
        if (params.data != undefined) Object.assign(data, params.data);
 
        if (params.async === false) {
            //同步
            return new Promise((resolve, reject) => {
                uni.request({
                    url: url,
                    method: method,
                    data: data,
                    sslVerify: false,
                    header: params.header || {
                        'content-type': 'application/x-www-form-urlencoded;application/json'
                    },
                    dataType: params.dataType || 'json',
                    responseType: params.responseType || 'text',
                    success: (res) => {
                        if (res.data.refreshtoken) {
                            uni.setStorage({
                                key: 'uid',
                                data: res.data.refreshtoken
                            });
                        }
                        if (res.data.code == -10009 || res.data.code == -10010) {
                            uni.removeStorage({
                                key: 'uid'
                            })
                        }
                        resolve(res.data);
                    },
                    fail: (res) => {
                        reject(res);
                    },
                    complete: (res) => {
                        reject(res);
                    }
                });
            });
        } else {
            //异步
            uni.request({
                url: url,
                method: method,
                data: data,
                header: params.header || {
                    'content-type': 'application/x-www-form-urlencoded;application/json'
                },
                sslVerify: false,
                dataType: params.dataType || 'json',
                responseType: params.responseType || 'text',
                success: (res) => {
                    if (res.data.refreshtoken) {
                        uni.setStorage({
                            key: 'uid',
                            data: res.data.refreshtoken
                        });
                    }
                    if (res.data.code == -10009 || res.data.code == -10010) {
                        uni.removeStorage({
                            key: 'uid'
                        })
                    }
                    // if(res.code==-1&&res.error_code=='TOKEN_ERROR'){
                    //     this.$util.showToast({'title':'登录失败'});
                    //     let pageRouter=getCurrentPages();
                    //     let data=pageRouter[pageRouter.length-1].options;
                    //     let back=pageRouter[pageRouter.length-1].route;
                    //     data.back=back;
                    //     setTimeout(()=>{
                    //         this.$util.redirectTo('/pages/login/login/login', data);
                    //     },1500)
                    //     return;
                    // }
                    typeof params.success == 'function' && params.success(res.data);
                },
                fail: (res) => {
                    typeof params.fail == 'function' && params.fail(res);
                },
                complete: (res) => {
                    typeof params.complete == 'function' && params.complete(res);
                }
            });
        }
    }
}