request.js
1.6 KB
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
class Ajax {
constructor(options) {
this.options = { data: null, method: 'GET', responseType: 'json', ...options };
}
send({ callback, error, options }) {
const { data, method, url, responseType } = Object.assign({}, this.options, options);
const xhr = new XMLHttpRequest();
xhr.responseType = responseType;
xhr.open(method, url);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
data !== null && xhr.setRequestHeader('Content-type', 'application/json;charset=utf-8');
xhr.addEventListener('load', function () {
const { status, response } = this;
if (status === 200 && callback && typeof callback === 'function') {
callback(response)
}
});
xhr.addEventListener('progress', function (event) {
// console.log('event', event)
});
xhr.addEventListener('error', function (err) {
error(err)
});
xhr.send(JSON.stringify(data));
}
set url(url) {
this.options.url = url;
}
get url() {
return this.options.url;
}
}
const guid = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const getSign = (data) => {
var skey = '5f8b53b10f0178856dc44986b36a673b'
var params = []
params = data.sort((a, b) => {
return (a + '').localeCompare(b + '')
})
params = params.join('').replace(/\=/g, '') + skey
return md5(params, 32)
}
const getTimestamp = () => {
return new Date().getTime()
}