SDKHttp.ts
4.21 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
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
import { GAMEDATA } from "../base/SDKConst";
import SignUtils from "../utils/SignUtils";
export default class SDKHttp {
public static async httpRequest(url: string, method: string, data?: any, dataType: "json" | "string" = "json") {
return new Promise<IResult<any>>((resolve, reject) => {
data = {
...data,
ver: GAMEDATA.version,
gameid: GAMEDATA.game_id,
sign_type: 'md5',
time_stamp: (Math.floor(Date.now() / 1000)) + ''
}
// if (url.indexOf('totalrank')>-1) {
// // console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
// url = 'https://wxsdk-game-pre.d3games.com/game/totalrank/list';
// }
//生成sign
data = {
...data,
sign: SignUtils.I.createSign(data)
}
if (data && typeof data === "object") {
data = JSON.stringify(data);
}
// console.error("sign", url, JSON.stringify(data))
data = data || "";
if (method == "GET" && data != "") {
data = JSON.parse(data);
let str = ''
for (let key in data) {
str = str + `${key}` + '=' + `${data[key]}&`
}
url += "?" + str;
data = "";
}
let info = "[url:" + url + ", data:" + data + "]";
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 400) {
let responseText: any = xhr.responseText;
// cc.log("responseText", responseText)
try {
responseText = JSON.parse(responseText);
// cc.log("responseText22", responseText)
if (url.indexOf('.json') > -1) {
resolve({ code: 0, data: responseText, msg: responseText.msg });
} else {
resolve({ code: +responseText.code, data: responseText.data, msg: responseText.msg });
}
return
} catch (ex) {
// console.error("httpRequest[parseError]:responseText=" + xhr.responseText);
resolve({ msg: "JSON parse error:" + ex.message, code: -1 });
return;
}
} else {
console.error(xhr.status, '网络请求失败!');
resolve({ code: -1 });
}
}
};
xhr.ontimeout = function (info): void {
// cc.error("info1", info)
resolve({ msg: `请求超时!`, code: -1 });
}
xhr.onerror = function (info): void {
// cc.error("info2", info)
resolve({ msg: `请求失败!`, code: -1 });
}
xhr.onabort = function (info): void {
// cc.error("info3", info)
resolve({ msg: `请求失败!`, code: -1 });
}
xhr.open(method, url, true);
if (method == "POST") {
xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8")//application/x-www-form-urlencoded
// if (cc.sys.os === 'Android') {
// cc.error("http__uid", AppSdkData.I.uid);
// xhr.setRequestHeader('Uuid', `${AppSdkData.I.uid}`);
// } else {
// xhr.setRequestHeader('Uuid', `909`);
// }
}
xhr.timeout = 3000;
xhr.send(data);
});
}
public static async httpGet(url: string, data?: any, dataType: "json" | "string" = "json") {
return this.httpRequest(url, "GET", data, dataType);
}
public static httpPost(url: string, data?: any, dataType: "json" | "string" = "json") {
return this.httpRequest(url, "POST", data, dataType);
}
}