فهرست منبع

修改网络请求

smallqiang 5 سال پیش
والد
کامیت
cde5a02c39
2فایلهای تغییر یافته به همراه103 افزوده شده و 5 حذف شده
  1. 102 4
      assets/scripts/net/Api.js
  2. 1 1
      assets/scripts/utils/Global.js

+ 102 - 4
assets/scripts/net/Api.js

@@ -77,7 +77,9 @@ class Api {
             }
         }
 
-        if (CC_WECHATGAME) {
+        if (window.tt != undefined) {
+            Api.ttGet(requestBody);
+        } else if (CC_WECHATGAME) {
             Api.wxGet(requestBody);
         } else if (CC_QQPLAY) {
             Api.qqGet(requestBody);
@@ -91,7 +93,11 @@ class Api {
      * @param {*} requestBody 
      */
     static httpPost(requestBody) {
-        if (CC_WECHATGAME) {
+        if (window.tt != undefined) {
+            requestBody.url = Api.requestUrl(requestBody.url);
+            requestBody.data = Api.baseParam(requestBody.data);
+            return Api.ttPost(requestBody);
+        } else if (CC_WECHATGAME) {
             requestBody.url = Api.requestUrl(requestBody.url);
             requestBody.data = Api.baseParam(requestBody.data);
             return Api.wxPost(requestBody);
@@ -164,6 +170,43 @@ class Api {
         });
     }
 
+    //头条
+    static ttGet(requestBody) {
+        let onSuccess = requestBody.success;
+        let onFail = requestBody.fail;
+        let onComplete = requestBody.complete;
+        tt.request({
+            url: requestBody.url,
+            data: requestBody.data,
+            method: requestBody.method === undefined ? 'GET' : 'POST',
+            header: requestBody.header === undefined ? {} : requestBody.header,
+            success: (res) => {
+                //直接处理登录失效的逻辑
+                if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
+                    GameGlobal.needLogin = true;
+                    cc.director.loadScene('login');
+                    return;
+                }
+
+                if (res.data.code === ResponseStateCode.OK) {
+                    if (onSuccess != undefined) {
+                        onSuccess(res.data.data);
+                    }
+                } else {
+                    if (onFail != undefined) {
+                        onFail(res.data.code, res.data.msg);
+                    }
+                }
+            },
+            fail: (res) => {
+                console.log('wxGet fail: ' + res);
+                if (onFail != undefined) {
+                    onFail(-1, res.errMsg);
+                }
+            },
+            complete: onComplete,
+        });
+    }
 
     static wxPost(requestBody) {
         requestBody.header = {
@@ -173,6 +216,14 @@ class Api {
         Api.wxGet(requestBody);
     }
 
+    static ttPost(requestBody) {
+        requestBody.header = {
+            "Content-Type": "application/x-www-form-urlencoded"
+        };
+        requestBody.method = 'POST';
+        Api.ttGet(requestBody);
+    }
+
    static qqGet(requestBody) {
         requestBody.header = {
             'Referer': 'https://hudong.qq.com',
@@ -310,7 +361,9 @@ class Api {
     static httpGetPromise(url, data) {
         url = Api.requestUrl(url);
         data = Api.baseParam(data);
-        if (CC_WECHATGAME) {
+        if (window.tt != undefined) {
+            return Api.ttGetP(url, data);
+        }else if (CC_WECHATGAME) {
             return Api.wxGetP(url, data);
         } else if (CC_QQPLAY) {
             return Api.qqGetP(url, data);
@@ -326,7 +379,9 @@ class Api {
     static httpPostPromise(url, data) {
         url = Api.requestUrl(url);
         data = Api.baseParam(data);
-        if (CC_WECHATGAME) {
+        if (window.tt != undefined) {
+            return Api.ttPostP(url, data);
+        } if (CC_WECHATGAME) {
             return Api.wxPostP(url, data);
         } else if (CC_QQPLAY) {
             return Api.qqPostP(url, data);
@@ -377,7 +432,50 @@ class Api {
         });
     }
 
+    //头条
+    static ttGetP(url, data) {
+        return Api.ttRequestPromise(url, data, 'GET');
+    }
+
+    static ttPostP(url, data) {
+        let header = {
+            "Content-Type": "application/x-www-form-urlencoded"
+        };
+        return Api.ttRequestPromise(url, data, 'POST', header);
+    }
+
+    static ttRequestPromise(url, data, method, header) {
+        return new Promise((resolve, reject) => {
+            tt.request({
+                url: url,
+                data: data,
+                method: method,
+                headers: header === undefined ? {} : header,
+                success: (res) => {
+                //直接处理登录失效的逻辑
+                if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
+                    GameGlobal.needLogin = true;
+                    cc.director.loadScene('login');
+                    reject(res.data);
+                }
+            if (res.data.code === ResponseStateCode.OK) {
+                resolve(res.data);
+            } else {
+                reject(res.data);
+            }
+        },
+            fail: (res) => {
+                reject({
+                    code: -1,
+                    msg: '头条登录接口调用失败'
+                });
+            },
+            complete: {},
+            });
+        });
+    }
 
+    //QQ
     static qqGetP(url, data) {
         url = Api.urlEncode(url, data);
         return Api.qqRequestPromise(url, '', 'GET');

+ 1 - 1
assets/scripts/utils/Global.js

@@ -11,7 +11,7 @@ window.GameGlobal = {
 
     debug: true,
 
-    ver: 100, //游戏版本号发版时需要核对
+    ver: 122, //游戏版本号发版时需要核对
     
     isCheck: false,