1
moli
2022-04-29 e1c56f4492962533156f5a428e5f30b34b0ebc2f
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
import config from '../../config/systemconfig'
 
let mask;  //遮罩层对象
 
/**
 * 打开遮罩层(待实现)
 * @param {String} msg 提示消息
 * @param {Menu} type 默认0(半透明),1、不透明,2、完全透明
 */
let maskOn = function (msg, type) {
 
}
 
/**
 * 关闭遮罩层(待实现)
 */
let maskOff = function () {
 
}
 
/**
 * 
 * @param {Json Object} options 请求参数
 * @returns 处理后的参数或设置了默认值的参数
 */
let setRequestParam = function (options) {
    let requestConfig = {
        baseUrl: config.Host,
        url: '',
        data: {},
        async: config.RequestAsync,
        timeout: config.RequestTimeOut,
        responseType: 'json',
        contentType: config.ContentType,
        headers: {},
        success: function (data) { },
        error: function (xhr, response, msg) { },
        before: function () { },
        after: function () { }
    };
 
    //baseUrl
    if (!!options.baseUrl) {
        requestConfig.baseUrl = options.baseUrl;
    }
    //baseUrl end
 
    //Content-Type
    if (!!options.contentType) {
        requestConfig.contentType = options.contentType;
    }
    //Content-Type end
 
    //url
    if (!options.url) {
        console.error('url参数不能为空');
    }
    else {
        requestConfig.url = options.url;
    }
    //url end
 
    //data
    if (!!options.data) {
        requestConfig.data = options.data;
    }
    //data end
 
    //headers
    if (!!options.headers) {
        requestConfig.headers = options.headers;
    }
    //header end
 
    //responseType
    if (!!options.responseType) {
        requestConfig.responseType = options.response;
    }
    //responseType end
 
    //before
    if (!!options.before) {
        requestConfig.before = function () {
            options.before();
        }
    }
    //before end
 
    //after
    if (!!options.after) {
        requestConfig.after = function () {
            options.after();
        }
    }
    //after end
 
    //async
    if (options.async != undefined && options.async != null) {
        requestConfig.async = options.async;
    }
    //async end
 
    //success
    if (!!options.success) {
        requestConfig.success = function (data, xhr) {
            options.success(data, xhr);
        }
    }
    else {
        requestConfig.success = function (data, xhr) {
            console.log(xhr.responseText);
        }
    }
    //success end
 
    //error
    if (!!options.error) {
        requestConfig.error = function (xhr, response, msg) {
            options.error(xhr, response, msg);
        }
    }
    else {
        requestConfig.error = function (xhr) {
            if (xhr.status % 100 > 10 && xhr.status != 415) {
                //后端自定义返回的错误码
                try {
                    let json = JSON.parse(xhr.responseText);
                    if (!!json.msg) {
                        _alert(xhr.msg, "error");
                    }
                }
                catch (err) {
                    _alert(xhr.responseText, "error");
                }
            }
            else {
                //预期之外的
                _alert("网络连接错误,请刷新页面!", "error");
            }
        }
    }
    //error end
 
    return requestConfig;
}
 
/**
 * 实例化一个xhr对象
 * @param {String} type GET/POST等
 * @param {Json Object} requestConfig 处理后的请求配置
 * @returns 一个即将可以发送的xhr对象
 */
let setXhrRequest = function (type, requestConfig) {
    let xhr = new XMLHttpRequest();
 
    //异步才存在超时,同步无超时限制
    if (requestConfig.async) {
        xhr.timeout = requestConfig.timeout;
    } else { }
    //异步才存在超时,同步无超时限制 end
 
    xhr.onreadystatechange = function () { //当请求状态改变时触发此事件
        if (xhr.readyState == 4) { //请求结束后
            if (xhr.status >= 200 && xhr.status < 300) {
                //成功,
                if (requestConfig.responseType.toLowerCase() == 'json') {
                    let data = JSON.parse(xhr.responseText);
                    requestConfig.success(data, xhr);
                }
                else if (requestConfig.responseType.toLowerCase() == 'text'
                    || requestConfig.responseType.toLowerCase() == 'string') {
                    requestConfig.success(xhr.responseText, xhr);
                }
                else {
                    requestConfig.success(xhr.responseText, xhr);
                }
 
            }
            else if (xhr.status >= 300 && xhr.status < 400) {
                //重定向,待遇到再处理
            }
            else if (xhr.status >= 100 && xhr < 200) {
                //连接中,待遇到再处理
            }
            else {
                //其他,包含400系列、500系列
                requestConfig.error(xhr, xhr.response, xhr.responseText);
            }
        }
    }
    if (type.toLowerCase() == "get") {
        let url = requestConfig.baseUrl + requestConfig.url;
        if(requestConfig.url.indexOf("://")){//url包含有一定的协议时,只取url,不取baseUrl
            url = requestConfig.url;
        }
        if (!!requestConfig.data && JSON.stringify(requestConfig.data) != "{}") {
            if (JSON.stringify(requestConfig.data).length > 4000) {
                throw "Get请求限制最大传输4000字节!";
            }
            url = url + "?" + convertToUrl(requestConfig.data);
        }
        xhr.open(type, url, requestConfig.async);
    }
    else {
        xhr.setRequestHeader("Content-Type", requestConfig.contentType);
        xhr.open(type, requestConfig.baseUrl + requestConfig.url, requestConfig.async);
    }
    //设置headers
    if (!!requestConfig.headers) {
        for (var key in requestConfig.headers) {
            xhr.setRequestHeader(key, requestConfig.headers[key]);
        }
    }
    if (!!config.RequestHeaders) {
        for (var key in config.RequestHeaders) {
            xhr.setRequestHeader(key, config.RequestHeaders[key]);
        }
    }
    //设置headers end
    return xhr;
}
 
/**
 * 将对象参数转成url参数
 * @param {Object} obj 待转换的对象
 * @returns a=1&b=2&c=%7B%22a%22:1,%22b%22:2,%22c%22:%5B%7B%22aa%22:11,%22bb%22:22%7D%5D%7D
 */
let convertToUrl = function (obj) {
    if (Object.prototype.toString.call(obj) == '[object Array]') {
        return "=" + encodeURIComponent(JSON.stringify(obj));
    }
    else if (Object.prototype.toString.call(obj) == '[object Undefined]'
        || Object.prototype.toString.call(obj) == '[object Function]') {
        return "=";
    }
    else if (Object.prototype.toString.call(obj) == '[object Number]'
        || Object.prototype.toString.call(obj) == '[object Boolean]') {
        return "=" + obj;
    }
    else if (Object.prototype.toString.call(obj) == '[object String]'
        && obj.indexOf('&') > -1) {
        return encodeURI(obj);
    }
    else if (Object.prototype.toString.call(obj) == '[object String]'
        && obj.indexOf('?') == 0) {
        return encodeURI(obj)
    }
    else if (Object.prototype.toString.call(obj) == '[object String]'
        && obj.indexOf('&') == -1) {
        return "=" + encodeURI(obj);
    }
    var arr = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
            if (Object.prototype.toString.call(obj[p]) == '[object Array]'
                || Object.prototype.toString.call(obj[p]) == '[object Object]') {
                arr.push(encodeURI(p) + "=" + encodeURI(JSON.stringify(obj)));
            } else {
                arr.push(encodeURI(p) + "=" + encodeURI(obj[p]));
            }
        }
    return arr.join("&");
}
 
 
let out = {
    setRequestParam,
    setXhrRequest,
    convertToUrl,
    maskOn,
    maskOff
}
 
export default out;