-
zhangwei
2025-03-10 309cc3fe6303d8464951063e89fc9d623915501e
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
// 本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号: 91  510 13      1 3 3 20 061  9 3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。
/**
 * 网络请求
 * 文档地址:https://doc.firstui.cn/
 **/
 
import base from './fui-common.js'
import createTaskStore from './fui-taskStore.js'
 
const store = createTaskStore()
 
class FIRSTUI_INNER {
    constructor(initConfig = {}) {
        this.initConfig = initConfig
        this.request = []
        this.response = []
        this.cancelToken = {}
        this.dispatchRequest = this.dispatchRequest.bind(this)
        this.loading = false
    }
    dispatchRequest(config = {}) {
        let params = base.mergeConfig(this.initConfig, config)
        const task = {
            url: params.url,
            method: params.method,
            keys: params.keys,
            data: params.data
        }
        if (params.prevent && store.requestTaskStore(task)) {
            return new Promise((resolve, reject) => {
                reject({
                    statusCode: -9998,
                    errMsg: 'request:prevented'
                })
            });
        }
        const arrayFormat = params.arrayFormat;
        let options = base.getOptions(params)
        if (options.method.toLocaleLowerCase() === 'get' && arrayFormat !== 'comma') {
            options = base.getParamsHandle(options, arrayFormat)
        }
        let promise = Promise.resolve(options);
        promise = promise.then(config => {
            if (params.showLoading && !this.loading) {
                base.showLoading(params.loadingText)
                this.loading = true
            }
            return new Promise((resolve, reject) => {
                let requestTask = uni.request({
                    ...options,
                    success: (res) => {
                        if (params.showLoading && this.loading) {
                            uni.hideLoading()
                            this.loading = false
                        }
                        resolve(params.brief ? res.data : res)
                    },
                    fail: (err) => {
                        if (params.showLoading && this.loading) {
                            uni.hideLoading()
                            this.loading = false
                        }
 
                        if (params.errorMsg) {
                            base.toast(params.errorMsg)
                        }
                        reject(err)
                    },
                    complete: () => {
                        store.removeRequestTask(task)
                        if (params.cancelToken && this.cancelToken[params
                                .cancelToken]) {
                            delete this.cancelToken[params.cancelToken]
                        }
                    }
                })
                if (params.cancelToken) {
                    this.cancelToken[params.cancelToken] = requestTask;
                }
                if (params.timeout && typeof params.timeout === 'number' && params.timeout > 2000) {
                    setTimeout(() => {
                        try {
                            store.removeRequestTask(task)
                            if (params.cancelToken) {
                                delete this.cancelToken[params.cancelToken]
                            }
                            requestTask.abort();
                        } catch (e) {}
                        resolve({
                            statusCode: -9999,
                            errMsg: 'request:cancelled'
                        });
                    }, params.timeout)
                }
            });
        })
 
        return promise
    }
}
 
 
const inner = new FIRSTUI_INNER(base.config())
 
const http = {
    interceptors: {
        request: {
            use: (fulfilled, rejected) => {
                inner.request.push({
                    fulfilled,
                    rejected
                })
            },
            eject: (name) => {
                if (inner.request[name]) {
                    inner.request[name] = null;
                }
            }
        },
        response: {
            use: (fulfilled, rejected) => {
                inner.response.push({
                    fulfilled,
                    rejected
                })
            },
            eject: (name) => {
                if (inner.response[name]) {
                    inner.response[name] = null;
                }
            }
        }
    },
    create(config) {
        inner.initConfig = base.mergeConfig(base.config(), config, true);
    },
    get(url, config = {}) {
        config.method = 'GET'
        config.url = url || config.url || ''
        return http.request(config)
    },
    post(url, config = {}) {
        config.method = 'POST'
        config.url = url || config.url || ''
        return http.request(config)
    },
    all(requests) {
        return Promise.all(requests)
    },
    request(config = {}) {
        let chain = [inner.dispatchRequest, undefined];
        let promise = Promise.resolve(config);
 
        inner.request.forEach(interceptor => {
            chain.unshift(interceptor.fulfilled, interceptor.rejected);
        });
 
        inner.response.forEach(interceptor => {
            chain.push(interceptor.fulfilled, interceptor.rejected);
        });
 
        while (chain.length) {
            promise = promise.then(chain.shift(), chain.shift());
        }
 
        return promise;
    },
    abort(cancelToken) {
        if (!cancelToken) return;
        try {
            if (inner.cancelToken[cancelToken]) {
                inner.cancelToken[cancelToken].abort()
                delete inner.cancelToken[cancelToken]
                // console.log('request:cancelled')
            }
        } catch (e) {}
    }
}
export default http