1
QYF-GitLab1
2024-07-26 e4278df3b718638bc5511ff34c4c571b0da305a3
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
/**
 * version 1.1.7
 */
 
class Request {
    constructor(config = {}) {
        this.config = {};
        this.config.baseUrl = config.baseUrl ? config.baseUrl : '';
        this.config.dataType = config.dataType ? config.dataType : 'json';
        this.config.responseType = config.responseType ? config.responseType : 'text';
        this.config.header = config.header ? config.header : {};
        this.config.timeout = config.timeout ? config.timeout : 60000
        this.reqInterceptors = null;
        this.resInterceptors = null;
        this.interceptors = {
            request: fn => {
                this.reqInterceptors = fn;
            },
            response: fn => {
                this.resInterceptors = fn;
            }
        }
    }
    get(url, config = {}) {
        return this._request('GET', url, config);
    }
    post(url, config = {}) {
        return this._request('POST', url, config);
    }
    put(url, config = {}) {
        return this._request('PUT', url, config);
    }
    delete(url, config = {}) {
        return this._request('DELETE', url, config);
    }
    setConfig(config = {}) {
        this.config = this._deepCopy(this._merge(this.config, config));
    }
    getConfig() {
        return this.config;
    }
    _request(method, url, config) {
        const _this = this;
        let newConfig = this._deepCopy(this._merge(this.config, config));
        let lastConfig = {};
        if (this.reqInterceptors && typeof this.reqInterceptors === 'function') {
            let reqInterceptors = this.reqInterceptors(newConfig);
            if (!reqInterceptors && process.env.NODE_ENV === "development") {
                console.warn('请求被拦截,此消息仅在开发环境显示。')
                return false;
            } else if (Object.prototype.toString.call(reqInterceptors) === "[object Promise]") {
                return reqInterceptors;
            }
            lastConfig = this._deepCopy(reqInterceptors);
        } else {
            lastConfig = this._deepCopy(newConfig);
        }
        let fullUrl = this._formatUrl(lastConfig.baseUrl, url);
        return new Promise((resolve, reject) => {
            uni.request({
                url: fullUrl,
                method,
                data: lastConfig.data ? lastConfig.data : {},
                header: lastConfig.header,
                dataType: lastConfig.dataType,
                responseType: lastConfig.responseType,
                async complete(response) {
                    let res = response;
                    if (_this.resInterceptors && typeof _this.resInterceptors === 'function') {
                        let resInterceptors = _this.resInterceptors(res);
 
                        if (!resInterceptors) {
                            reject('返回值已被您拦截!');
                            return;
                        } else if (Object.prototype.toString.call(resInterceptors) ===
                            "[object Promise]") {
                            try {
                                let promiseRes = await resInterceptors;
                                resolve(promiseRes)
                            } catch (error) {
                                reject(error)
                            }
                        } else {
                            res = resInterceptors;
                        }
                    }
                    resolve(res);
                }
            });
        })
    }
    _formatUrl(baseUrl, url) {
        if (!baseUrl) return url;
        let formatUrl = '';
        const baseUrlEndsWithSlash = baseUrl.endsWith('/');
        const urlStartsWithSlash = url.startsWith('/');
        if (baseUrlEndsWithSlash && urlStartsWithSlash) {
            formatUrl = baseUrl + url.substring(1);
        } else if (baseUrlEndsWithSlash || urlStartsWithSlash) {
            formatUrl = baseUrl + url;
        } else {
            formatUrl = baseUrl + '/' + url;
        }
        return formatUrl;
    }
    _merge(oldConfig, newConfig) {
        let mergeConfig = this._deepCopy(oldConfig);
        if (!newConfig || !Object.keys(newConfig).length) return mergeConfig;
        for (let key in newConfig) {
            if (key !== 'header') {
                mergeConfig[key] = newConfig[key];
            } else {
                if (Object.prototype.toString.call(newConfig[key]) === '[object Object]') {
                    for (let headerKey in newConfig[key]) {
                        mergeConfig[key][headerKey] = newConfig[key][headerKey];
                    }
                }
            }
        }
        return mergeConfig;
    }
    _deepCopy(obj) {
        let result = Array.isArray(obj) ? [] : {};
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                if (typeof obj[key] === 'object') {
                    result[key] = this._deepCopy(obj[key]);
                } else {
                    result[key] = obj[key];
                }
            }
        }
        return result;
    }
}
 
// if (!global.$request) {
//   global.$request = new Request();
// }
 
export default Request;