jhzh
2025-04-03 db12897dc68c68d40c557aa59ad78022e2b30ac2
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import app from "@/app.js"
import Cache from "../cache.js"
let settings = {
    method: "get",    // 默认请求方法
    contentType: "application/json", // 传参格式
    dataType: "json", // 返回值类型
    baseUrl: app.baseUrl + '/api/app', // 请求根地址
}
 
let loadNum = 0; //加载框的数量
let loadingShow = () => {
    loadNum++
    uni.showLoading({
        title: 'loading...'
    });
}
let loadingHide = () => {
    loadNum--
    if (loadNum <= 0) {
        uni.hideLoading();
    }
}
function x(options = null) {
 
    // 返回当前实例对象 无需手动return
    return new x.fn.init(options);
 
}
 
x.fn = x.prototype = {
 
    constructor: x,
 
    config(options) {
        // 解构并设置默认值
        let {
            baseUrl,
            url,
            data,
            method,
            contentType,
            dataType
        } = options;
 
        // 请求头参数 写入token和language
        let auth = null;
        if (uni.getStorageSync('token')) {
            auth = uni.getStorageSync('token');
        }
        
        let lang=uni.getStorageSync('language')
        const header = auth ? {
            'X-Requested-With': 'XMLHttpRequest',
            lang: lang || 'en',
            authorization: `bearer ${auth}`,
            'content-type': 'application/x-www-form-urlencoded'
        } : {
                'X-Requested-With': 'XMLHttpRequest',
                lang: lang || 'en',
                'content-type': 'application/x-www-form-urlencoded'
            };
 
        this.header = header;
 
        // 请求地址解析
        if (url.startsWith('http')) { // 外部链接
            this.url = url;
        } else { // 本地相对路径
            this.url = baseUrl + url;
        }
 
        if (data) this.data = data;
        if (method) this.method = method;
        if (contentType) this.contentType = contentType;
        if (dataType) this.dataType = dataType;
    },
 
    init(options) {
        // 将用户参数 写入配置信息
        this.config(Object.assign(settings, options));
        let { config = {} } = options
        return new Promise((resolve, reject) => {
            let reg=new RegExp('/','g')//g代表全部
            let newMsg=options.url.replace(reg,'_');    
            let arr = ['_user_machine_details',
            '_exchange_getCoinInfo',
            '_contract_getCurrentEntrust',
            '_wallet_getBalance',
            '_user_walletImage',
            '_otc_tradingEntrusts',
            '_otc_myOrders',
            '_otc_storeOrder',
            '_otc_otcTicker',
            '_user_getUserInfo'
            ];        
            let isCache=false;
             for(var i = 0; i < arr.length; i++){
                      if(newMsg === arr[i]){
                          isCache =false;
                     }
            }    
                
            if(Cache.get(newMsg).data && isCache){
                resolve(Cache.get(newMsg).data);
                uni.request({
                    url: this.url,
                    data: this.data,
                    method: this.method,
                    header: this.header,
                    dataType: this.dataType,
                    sslVerify: false,
                    success: (res) => {
                        let message = res.data.message
                        let code = res.data.code
                        if (code != 200) {
                            switch (code) {
                                case 1003: // 登陆失效 清除状态 重新登陆
                                    // 清除session
                                    uni.removeStorageSync('token');
                                    uni.redirectTo({
                                        url: "/pages/login/index",
                                    });
                                    break;
                                case 1021:
                                    resolve(res.data);
                                    break;
                                case 4001:
                                    resolve(res.data);
                                    break;
                                default:
                                    reject(message);
                                    break;
                            }
                            if (config.toast !== false&&message) {
                                uni.showToast({
                                    title: message,
                                    duration: 2000,
                                    icon: 'none'
                                });
                            }
                        } else {
                            Cache.set(newMsg,res.data);
                            resolve(res.data); // 直接返回数据
                            if (config.toast&&message) {
                                uni.showToast({
                                    title: message,
                                    duration: 2000,
                                    icon: 'none'
                                });
                            }
                        }
                    },
                    fail: (err) => {
                      //  console.log(err)
                        reject(err)
                        if (config.toast !== false) {
                            // uni.showToast({
                            //     title: 'error reload!',
                            //     icon: "none"
                            // });
                        }
                        if (err) {
                            throw new Error();
                        }
                    },
                    complete: (er) => {
                      //  console.log()
                        reject(er)
                    }
                })
            }else{
                // 提示状态
                if (config.loading) {
                    loadingShow()
                }
                uni.request({
                    url: this.url,
                    data: this.data,
                    method: this.method,
                    header: this.header,
                    dataType: this.dataType,
                    sslVerify: false,
                    success: (res) => {
                        let message = res.data.message
                        let code = res.data.code
                        if (code != 200) {
                            switch (code) {
                                case 1003: // 登陆失效 清除状态 重新登陆
                                    // 清除session
                                    uni.removeStorageSync('token');
                                    uni.redirectTo({
                                        url: "/pages/login/index",
                                    });
                                    break;
                                case 1021:
                                    resolve(res.data);
                                    break;
                                case 4001:
                                    resolve(res.data);
                                    break;
                                default:
                                    reject(message);
                                    break;
                            }
                            if (config.toast !== false&&message) {
                                uni.showToast({
                                    title: message,
                                    duration: 2000,
                                    icon: 'none'
                                });
                            }
                        } else {
                            Cache.set(newMsg,res.data);
                            resolve(Cache.get(newMsg).data); // 直接返回数据
                            if (config.toast&&message) {
                                uni.showToast({
                                    title: message,
                                    duration: 2000,
                                    icon: 'none'
                                });
                            }
                        }
                    },
                    fail: (err) => {
                        reject(err)
                        if (config.toast !== false) {
                            uni.showToast({
                                title: 'error reload!',
                                icon: "none"
                            });
                        }
                        if (err) {
                            throw new Error();
                        }
                    },
                    complete: () => {
                        loadingHide()
                    }
                })
            }
            
        })
 
    },
 
    // 使用promise封装同步化的确认框
    confirmSync(content, fullfilled, rejected = null) {
        let showCancel = false;
        if (rejected instanceof Function) {
            showCancel = true;
        }
        return new Promise(function (resolve, reject) {
            uni.showModal({
                content,
                showCancel,
                success(res) { // confirm or cancel
                    if (res.confirm) {
                        resolve(fullfilled()); // 执行动作 需要返回值 则标记到resolve中
                    } else if (res.cancel && rejected) {
                        reject(rejected()); // 执行动作 需要返回值 则标记到reject中
                    }
                }
            })
        })
    },
 
    get(url, data = null, config = {}) {
        return x({
            method: "get",
            url,
            data,
            config
        })
    },
 
    post(url, data, config = {}) {
        return x({
            method: "post",
            url,
            data,
            config
        })
    },
    // data 为uni的chooseImage
    uploadFile(url, data, config = {}) {
        let auth = null;
        if (uni.getStorageSync('token')) {
            auth = uni.getStorageSync('token');
        }
        let lang=uni.getStorageSync('language')
        let header = {
            'X-Requested-With': 'XMLHttpRequest',
            lang: lang || "en",
        }
        if (auth) header.authorization = `bearer ${auth}`;
        if (config.loading !== false) {
            loadingShow()
        }
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: settings.baseUrl + url, //仅为示例,非真实的接口地址
                filePath: data.tempFilePaths[0],
                name: 'image',
                formData: {},
                sslVerify: false,
                header,
                success: (res) => {
                    resolve(JSON.parse(res.data))
                },
                fail: () => {
                    reject()
                },
                complete: () => {
                    loadingHide()
                }
            });
        })
 
    },
    head() {
 
    },
    put() {
 
    },
    // ...
}
 
x.fn.init.prototype = x.fn, x.extend = x.fn.extend = function (obj, prop) {
    if (!prop) { //如果未设置prop 则表示给this扩展一个对象的内容
        prop = obj;
        obj = this;
    }
    for (var i in prop) obj[i] = prop[i];
}, x.extend(x.fn);
 
export default x;