|
|
@@ -4,7 +4,116 @@
|
|
|
*/
|
|
|
export default class HttpSystem {
|
|
|
constructor() {
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+ /**
|
|
|
+ * 游戏接口,通用
|
|
|
+ * @param url 接口完整地址
|
|
|
+ * @param data 加密前数据
|
|
|
+ * @param errorPop 请求失败是否弹窗
|
|
|
+ * @param async 是否异步请求(后端要求防阻塞)
|
|
|
+ */
|
|
|
+ public async sendData(url, data: any, errorPop = true, async = true) {
|
|
|
+ let encode = '';
|
|
|
+ let dataN = null;
|
|
|
+ data.timestamp = Math.floor(new Date().getTime() / 1000);
|
|
|
+ data.appVersion = g.appData.appVersion;
|
|
|
+ data.tfChannel = g.appData.tfChannel;
|
|
|
+ data.versioncfg = g.appData.version;
|
|
|
+ encode = mk.encrypt.encrypt(JSON.stringify(data), g.loginData.sessionKey, g.appData.appId);
|
|
|
+ dataN = {
|
|
|
+ "uin": g.loginData.uin,
|
|
|
+ "encrypt": encode
|
|
|
+ }
|
|
|
+ mk.log.log('httpRequest', url, data, dataN);
|
|
|
+ let result = await this.sendRequest(url, 'POST', JSON.stringify(dataN), false, errorPop, async);
|
|
|
+ return this.checkData(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * http请求 登录用
|
|
|
+ * @param url 请求头
|
|
|
+ * @param method 'GET' || 'POST'
|
|
|
+ * @param data 数据
|
|
|
+ * @param isRsa 是否加密
|
|
|
+ * @param errorPop 请求失败时是否弹窗
|
|
|
+ * @param async 是否异步请求
|
|
|
+ */
|
|
|
+ public sendRequest(url, method: string = 'POST', data: string = null, isRsa: boolean = true, errorPop = true, async = true) {
|
|
|
+ return new Promise<any>((resolve: (data: any) => void, reject) => {
|
|
|
+ let requesttimes = 0;
|
|
|
+ let httpRequest = new XMLHttpRequest();
|
|
|
+ let send = () => {
|
|
|
+ httpRequest.open(method, url, async);
|
|
|
+ httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
|
|
|
+ if (isRsa && data != null) {
|
|
|
+ data = mk.encrypt.rsaEncrypt(data);
|
|
|
+ }
|
|
|
+ httpRequest.send(data);
|
|
|
+ };
|
|
|
+
|
|
|
+ // httpRequest.timeout = 30000;
|
|
|
+ httpRequest.onreadystatechange = function () {
|
|
|
+ if (httpRequest.readyState === 4) {
|
|
|
+ if (httpRequest.status === 200) {
|
|
|
+ var responseStr = httpRequest.responseText;
|
|
|
+ let response = JSON.parse(responseStr);
|
|
|
+ response.errorcode = response.errcode;
|
|
|
+ mk.log.log('httpResponse', url, response);
|
|
|
+ if (response.errcode == -10003) {
|
|
|
+ reject(10003);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (response.errcode != 0) {
|
|
|
+ resolve(response);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ mk.log.log("httpRequest.status", httpRequest.status);
|
|
|
+ reject(httpRequest.status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ httpRequest.ontimeout = function (e) {
|
|
|
+ requesttimes++;
|
|
|
+ if (requesttimes >= 3) {
|
|
|
+ mk.log.log("http.ontimeout", url);
|
|
|
+ reject("timeout");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ send();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ httpRequest.onerror = function (e) {
|
|
|
+ mk.log.log("http.onerror", url);
|
|
|
+ reject("onerror");
|
|
|
+ };
|
|
|
+ send();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检测用明文还是密文
|
|
|
+ * @param response 返回消息
|
|
|
+ * @param forceData true 明文 false 有密文优先密文,没密文情况下用明文
|
|
|
+ */
|
|
|
+ checkData(response, forceData = false) {
|
|
|
+ let data = null
|
|
|
+ if (forceData) {
|
|
|
+ data = response.data;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (response.encrypt) {
|
|
|
+ data = mk.encrypt.decrypt(response.encrypt, g.loginData.sessionKey, g.appData.appId);
|
|
|
+ data = JSON.parse(data);
|
|
|
+ }
|
|
|
+ else if (response.data) {
|
|
|
+ data = response.data;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ response.data = data;
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+}
|