| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899 |
- import GameM from "./GameM";
- import { Utils } from "../utils/Utils";
- import CommonData, { HTTP_TYPE } from "../datas/CommonData";
- import AdM from "./AdM";
- import UiM from "./UiM";
- import CashOut from "../ui/CashOut";
- import GlobalStorage, { STORAGE_KEY } from "../datas/GlobalStorage";
- import Main from "../Main";
- import Setting from "../ui/Setting";
- import LogUtil from "../utils/LogUtil";
- import NetDetaileNode from "../prefabs/NetDetaileNode";
- import CashPro from "../ui/CashPro";
- import SwitchM from "./SwitchM";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class HttpM {
- //正式服
- // private serverUrl = 'https://tcsg.duiweize.com/'
- //三国战戟预发服
- private serverUrl = 'https://tcsg-test.duiweize.com/'
- //老梅
- // private serverUrl = 'http://172.16.15.45:3942/'
- //老代
- // private serverUrl = 'http://172.16.13.93:8942/'
- //佳佳
- // private serverUrl = 'http://172.16.13.81:8942/'
- //zhang
- // private serverUrl = 'http://172.16.15.197:9132/'
- //zhang 2
- //private serverUrl = 'http://172.16.15.20:3942/'
- //xu
- // private serverUrl = 'http://172.16.15.137:9072/'
- //zhang 2 172.16.15.137
- //private serverUrl = 'http://172.16.15.20:3942/'
- //mei
- //private serverUrl = 'http://172.16.15.45:3942/'
- //zhao
- //private serverUrl = 'http://172.16.15.91:9072/'
- //swagger http://172.16.15.137:3942/swagger-ui/index.html#/%E7%94%A8%E6%88%B7%E6%93%8D%E4%BD%9C
- //swagger https://xiyou-test.duiweize.com/swagger-ui/index.html#/%E7%94%A8%E6%88%B7%E6%93%8D%E4%BD%9C
- //swagger tucao http://tcsg-test.duiweize.com/swagger-ui/#/%E7%BA%A2%E5%8C%85%E8%A7%86%E9%A2%91
- public isTry = ''
- private static instance: HttpM = null
- static get Instance(): HttpM {
- if (!this.instance) {
- this.instance = new HttpM()
- }
- return this.instance
- }
- init() {
- this.isTry = ''
- let value = GameM.globalStorage.getStorage(STORAGE_KEY.umFirst, 0)
- if (!value) {
- SwitchM.firstLoginLog = true
- GameM.globalStorage.setStorage(STORAGE_KEY.umFirst, 1, 0)
- }
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umLoadStart', '加载开始')
- }
- // GameM.httpM.connect()
- GameM.httpM.connectTest()
- }
- //加载json文件
- public loadJson(filepath, callback, cfgName) {
- let xhr = new XMLHttpRequest();
- xhr.open("GET", filepath, true)
- if (cc.sys.isNative) {
- xhr.setRequestHeader("Accept-Encoding", "text/html;charset=UTF-8");
- }
- xhr.timeout = 60000;
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4) {
- if (xhr.status >= 200 && xhr.status < 300) {
- let respone = xhr.responseText;
- if (callback) {
- callback(JSON.parse(respone), cfgName)
- }
- }
- else {
- console.error('There was a problem with the httpGetCom request.');
- }
- }
- }
- xhr.ontimeout = function (e) {
- console.error('The network is ontimeout ', e);
- // GameM.commonData.showDisconnectedNode()
- };
- xhr.onerror = function (e) {
- console.error('The network is onerror loadjson ', e);
- // GameM.commonData.showDisconnectedNode()
- };
- xhr.send();
- }
- /** 第一次请求 */
- httpGetFirst(url, methond: string = 'POST', data: string = null, callback: any = null) {
- let httpRequest = new XMLHttpRequest();
- // httpRequest.timeout = 60000
- httpRequest.onreadystatechange = function () {
- if (httpRequest.readyState === 4) {
- if (httpRequest.status === 200) {
- var responseStr = httpRequest.responseText;
- // console.log('responseStr ', responseStr)
- let response = JSON.parse(responseStr);
- if (response.code == 0) {
- if (callback) {
- callback(response)
- }
- else {
- console.error('httpGetFirst callback is null ')
- }
- }
- else {
- console.error('httpGetFirst callback ', response)
- }
- } else {
- console.error('There was a problem with the httpGetFirst request.');
- }
- }
- };
- httpRequest.ontimeout = function (e) {
- console.error('The network is ontimeout ', e);
- // GameM.commonData.showDisconnectedNode()
- };
- httpRequest.onerror = function (e) {
- console.error('The network is onerror first ', e);
- // GameM.commonData.showDisconnectedNode()
- };
- httpRequest.open(methond, this.serverUrl + url);
- httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
- httpRequest.send(data);
- }
- /** 通用接口 */
- httpGetCom(url, methond: string = 'POST', data: string = null, callback: any = null, failBack = null, param: any = null, async = true, encrypt = true) {
- let httpRequest = new XMLHttpRequest();
- // httpRequest.timeout = 60000
- httpRequest.onreadystatechange = function () {
- if (httpRequest.readyState === 4) {
- if (httpRequest.status === 200) {
- var responseStr = httpRequest.responseText;
- let response = JSON.parse(responseStr);
- // console.log('httpGetCom >>>>>>>>>>>> ', url, ' response ', response.errcode, ' ', response.errmsg, ' param ', param)
- if (response.errcode == 0) {
- // console.log('aaaaaaaaaaa GameM.commonData.disConnectNum ', GameM.commonData.disConnectNum)
- if (callback) {
- if (encrypt) {
- let data = GameM.httpM.checkData(response)
- if (response.timestamp) {
- GameM.commonData.gameTime = response.timestamp * 1000
- }
- callback(data, param)
- }
- else {
- callback(response)
- }
- }
- else {
- // console.error('httpGetCom callback is null ')
- }
- }
- else {
- if (failBack) {
- failBack(response.errmsg)
- }
- console.error('httpGetCom callback XXXXXXXXXXXXXXXXXXXXX ', response, url)
- if (response && response.errcode) {
- console.log('response ', response.errcode)
- }
- LogUtil.logV('response ', response)
- console.error('httpGetCom callback TTTTTTTTTTTTTTTTTTTTT ')
- if (response.errcode == -10003) {
- GameM.httpM.checklogin()
- }
- }
- } else {
- console.error('There was a problem with the httpGetCom request.');
- }
- }
- };
- httpRequest.ontimeout = function (e) {
- GameM.commonData.showDisconnectedNode()
- };
- httpRequest.onerror = function (e) {
- console.error('The network is onerror nor ', e);
- };
- httpRequest.open(methond, this.serverUrl + this.isTry + url, async);
- httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
- httpRequest.send(data);
- }
- /** 通用接口非对称 */
- httpGetComRsa(url, methond: string = 'POST', data: string = null, callback: any = null) {
- let httpRequest = new XMLHttpRequest();
- // httpRequest.timeout = 3000
- httpRequest.onreadystatechange = function () {
- if (httpRequest.readyState === 4) {
- if (httpRequest.status === 200) {
- var responseStr = httpRequest.responseText;
- let response = JSON.parse(responseStr);
- if (response.errcode == 0) {
- if (callback) {
- callback(response)
- }
- else {
- console.error('httpGetComRsa callback is null ')
- }
- }
- else {
- console.error('httpGetComRsa callback ', response.errcode, url)
- }
- } else {
- console.error('There was a problem with the httpGetComRsa request.');
- }
- }
- };
- let newData = Utils.RSAEncrypt(data)
- httpRequest.ontimeout = function (e) {
- console.error('The network is ontimeout ', e);
- // GameM.commonData.showDisconnectedNode()
- };
- httpRequest.onerror = function (e) {
- console.error('The network is onerror rsa ', e);
- };
- if (url == 'wxlogin') {
- httpRequest.open(methond, this.serverUrl + this.isTry + url, false);
- }
- else {
- httpRequest.open(methond, this.serverUrl + url, false);
- }
- httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
- httpRequest.send(newData as string);
- }
- /** 第一次握手 */
- connect() {
- GameM.commonData.ranKey = Utils.randomString()
- let data = {
- "psk": GameM.commonData.ranKey,
- "appId": GameM.commonData.appid
- }
- this.httpGetComRsa('connect', 'POST', JSON.stringify(data), this.connectBack)
- }
- /** 用mac地址 */
- connectTest() {
- // console.log('mac ', GameM.commonData.machineInfo.mac)
- let mac = ''
- if (CC_JSB) {
- if (GameM.commonData.machineInfo.mac.indexOf(',')) {
- mac = GameM.commonData.machineInfo.mac.split(',')[0];
- }
- else {
- mac = GameM.commonData.machineInfo.mac;
- }
- }
- else {
- mac = "dg:34:67:35:90:32"
- }
- GameM.commonData.ranKey = Utils.randomString()
- let data = {
- "mac": mac,
- // "mac": "dg:34:67:35:90:32",
- "psk": GameM.commonData.ranKey,
- "appId": GameM.commonData.appid
- }
- this.httpGetComRsa(HTTP_TYPE.connectTest, 'POST', JSON.stringify(data), this.connectBack)
- }
- connectBack(response) {
- // console.log('connectBack ', response.encrypt)
- // console.log('connectBack ')
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServer1', 'connectBack')
- }
- let data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKey, GameM.commonData.appid)
- // console.log('connectBack data ', data)
- if (data) {
- GameM.commonData.tmp_uin = JSON.parse(data).tmp_uin
- AdM.getInviteCode(true)
- GameM.httpM.wxlogin()
- }
- }
- /** 登录微信
- */
- wxlogin() {
- // console.log('wxlogin ')
- let aa;
- if (GameM.commonData.loginTest) {
- aa =
- {
- "code": "093AURkl2ySUy54Ccgml27Vy3c4AURkV"
- }
- }
- else {
- aa =
- {
- "code": GameM.commonData.wxCode
- }
- }
- let data
- let encode
- if (GameM.commonData.wxCode != '') {
- //微信登录
- encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
- data = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- }
- else {
- //游客登录
- encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.ranKey, GameM.commonData.appid)
- data = {
- "uin": GameM.commonData.tmp_uin,
- "encrypt": encode
- }
- }
- this.httpGetCom('wxlogin', 'POST', JSON.stringify(data), this.wxloginBack.bind(this), null, null, false, false)
- }
- wxloginBack(response) {
- // console.log('wxloginBack ', response.encrypt)
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServer2', 'wxloginBack')
- }
- let data
- if (GameM.commonData.wxCode != '') {
- data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
- }
- else {
- data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKey, GameM.commonData.appid)
- }
- if (data) {
- let dataN = JSON.parse(data)
- GameM.commonData.login_ticket = dataN.login_ticket
- let key = 'dddddddddddddddddddddddddddddddd'
- let d = Utils.Encrypt(GameM.commonData.login_ticket, key, GameM.commonData.appid)
- GlobalStorage.Instance.setStorage(STORAGE_KEY.login_ticket, d, 1)
- console.log('wxloginBack ', GameM.commonData.isFirstIn)
- let isFirstIn = GlobalStorage.Instance.getStorage(STORAGE_KEY.isFirstIn, 1)
- if (isFirstIn == null) {
- GameM.commonData.isFirstIn = true
- d = Utils.Encrypt(1, key, GameM.commonData.appid)
- GlobalStorage.Instance.setStorage(STORAGE_KEY.isFirstIn, d, 1)
- console.log('isFirstIn ')
- }
- if (GameM.commonData.uin != dataN.uin) {
- GameM.commonData.uin = dataN.uin
- GameM.commonData.isNew = false
- }
- else {
- GameM.commonData.isNew = true
- }
- console.log(">>>>>uin:" + GameM.commonData.uin);
- // console.log('checkloginBack uin ->>> ', GameM.commonData.isNew)
- d = Utils.Encrypt(GameM.commonData.uin, key, GameM.commonData.appid)
- GameM.globalStorage.setStorage(STORAGE_KEY.uin, d, 1)
- console.log(">>>>>2uin:" + GameM.commonData.uin);
- GameM.httpM.checklogin()
- }
- }
- /** checklogin接口 */
- checklogin() {
- // console.log('checklogin ')
- let tmp_key
- if (GameM.commonData.wxCode != '') {
- tmp_key = GameM.commonData.session_key
- }
- else {
- tmp_key = Utils.randomString()
- GameM.commonData.ranKeyNew = tmp_key
- }
- let aa =
- {
- "tmp_key": tmp_key,
- "uin": GameM.commonData.uin,
- "login_ticket": GameM.commonData.login_ticket,
- "appId": GameM.commonData.appid
- }
- this.httpGetComRsa('checklogin', 'POST', JSON.stringify(aa), this.checkloginBack)
- }
- checkloginBack(response) {
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServer3', 'checkloginBack')
- }
- // console.log('checkloginBack ', response.encrypt)
- let data
- if (GameM.commonData.wxCode != '') {
- data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
- }
- else {
- data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKeyNew, GameM.commonData.appid)
- }
- if (data) {
- let dataN = JSON.parse(data)
- GameM.commonData.session_key = dataN.session_key
- let key = 'dddddddddddddddddddddddddddddddd'
- let d = Utils.Encrypt(GameM.commonData.session_key, key, GameM.commonData.appid)
- GlobalStorage.Instance.setStorage(STORAGE_KEY.session_key, d, 1)
- if (GameM.commonData.wxCode != '') {
- AdM.onSendEvent('wxAuth', '玩家成功进行微信授权', 'wxAuth')
- //如果通过授权,就上报邀请
- GameM.ClubData.clubAuthPushInviteIn = true
- if (GameM.commonData.isNew) {
- //玩过此游戏并且本地数据是本机的
- console.log(">>>>>3uin:" + GameM.commonData.uin);
- GameM.httpM.getAccountInfo()
- // if (UiM.Instance.cashNode) {
- // UiM.Instance.cashNode.getComponent(CashOut).authBack()
- // }
- // UiM.Instance.redPackageNode.authBack()
- }
- else {
- //玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
- GameM.commonData.isNew = false
- GameM.commonData.wxCode = ''
- GameM.commonData.resetDatas()
- cc.director.loadScene('Loader')
- }
- }
- else {
- console.log(">>>>>4uin:" + GameM.commonData.uin);
- GameM.httpM.getAccountInfo()
- // AdM.setUserID()
- // GameM.commonData.getAllServerCfg()
- }
- }
- }
- /** 获取账号信息 */
- getAccountInfo() {
- // console.log('>>>>> getAccountInfo')
- this.isTry = ''
- let aa =
- {
- "uin": GameM.commonData.uin,
- "login_ticket": GameM.commonData.login_ticket
- }
- let encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
- // console.log(">>>>> " + GameM.commonData.uin, " ", GameM.commonData.appid)
- let data = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- AdM.setUserID()
- let nickname = GlobalStorage.Instance.getStorage(STORAGE_KEY.nickname, 1)
- if (nickname) {
- console.log('MMM has NickName')
- if (GameM.ClubData.clubAuthPushInviteIn) {
- AdM.getInviteCode()
- GameM.ClubData.clubAuthPushInviteIn = false
- }
- GameM.commonData.getAllServerCfg()
- }
- else {
- console.log('MMM getAccountInfoBack')
- this.httpGetCom('user', 'POST', JSON.stringify(data), this.getAccountInfoBack.bind(this), null, null, false)
- }
- }
- /** 获取账号信息返回信息 */
- getAccountInfoBack(data, param) {
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServer4', 'getAccountInfoBack')
- }
- // let data = GameM.httpM.checkData(response, true)
- if (data) {
- GameM.commonData.isAuth = data.is_auth
- if (data.is_auth) {
- let key = 'dddddddddddddddddddddddddddddddd'
- let d1 = Utils.Encrypt('true', key, GameM.commonData.appid)
- GlobalStorage.Instance.setStorage(STORAGE_KEY.isAuth, d1, 1)
- GameM.commonData.headimgurl = data.headimgurl
- GlobalStorage.Instance.setStorage(STORAGE_KEY.headimgurl, GameM.commonData.headimgurl, 1)
- GameM.commonData.nickname = data.nickname
- GlobalStorage.Instance.setStorage(STORAGE_KEY.nickname, GameM.commonData.nickname, 1)
- if (GameM.ClubData.clubAuthPushInviteIn) {
- AdM.getInviteCode()
- GameM.ClubData.clubAuthPushInviteIn = false
- }
- if (GameM.commonData.isNew) {
- if (UiM.Instance.cashNode) {
- UiM.Instance.cashNode.getComponent(CashOut).authBack()
- }
- if (UiM.Instance.redPackageNode && UiM.Instance.redPackageNode.node.active) {
- UiM.Instance.redPackageNode.authBack()
- }
- UiM.Instance.hallNode.getComponent(Main).setImg()
- if (UiM.Instance.settingNode) {
- UiM.Instance.settingNode.getComponent(Setting).setImg()
- }
- if (UiM.Instance.cashProNode) {
- UiM.Instance.cashProNode.getComponent(CashPro).authBack()
- }
- }
- else {
- GameM.commonData.getAllServerCfg()
- }
- }
- else {
- GameM.commonData.getAllServerCfg()
- }
- }
- }
- /** 是否是新的一天 */
- getUserIsNewDay() {
- console.log('GameM.commonData.uin ', GameM.commonData.uin)
- let aa = {}
- let encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
- let data = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- this.httpGetCom(HTTP_TYPE.getUserIsNewDay, 'POST', JSON.stringify(data), this.getUerIsNewDayBack.bind(this), null, null, false)
- }
- getUerIsNewDayBack(data, param) {
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServer6', 'getUerIsNewDayBack')
- }
- // let data = GameM.httpM.checkData(response)
- // console.log('getUerIsNewDayBack ', data)
- if (data) {
- GameM.commonData.userIsNewDay = data.userIsNewDay
- GameM.httpM.getInfo()
- }
- }
- getInfo() {
- console.log('getInfo ')
- let aa = { "versioncfg": GameM.commonData.version }
- let encode = ''
- let data = null
- if (GameM.commonData.isAuth) {
- // console.log('GameM.commonData.session_key ', GameM.commonData.session_key)
- // console.log('GameM.commonData.uin ', GameM.commonData.uin)
- encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
- data = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- }
- else {
- // console.log('GameM.commonData.tmp_uin ', GameM.commonData.tmp_uin)
- encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
- data = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- }
- this.httpGetCom('getInfoCrypt', 'POST', JSON.stringify(data), this.getInfoBack.bind(this), null, null, false)
- }
- getInfoBack(data, param) {
- console.log('getInfoBack ')
- // let data = GameM.httpM.checkData(response)
- if (data) {
- GameM.commonData.getDatasFromServer(data)
- GameM.commonData.serverFinish = true
- console.log("-->Loading serverFinish end");
- if (SwitchM.firstLoginLog) {
- AdM.sendUmeng('umServerFinish', '登录服务器消息结束')
- }
- console.log('MMM GameM.commonData.serverFinish ')
- }
- }
- /** 向服务器发送数据
- * @param url 发送消息类型
- * @param data 发送的数据
- */
- sendDatas(url: HTTP_TYPE, data: any = null, sendDatasBack = null, failBack = null) {
- let encode = ''
- let dataN = null
- if (!data) {
- data = {}
- }
- data.timestamp = Math.floor(new Date().getTime() * 0.001)
- data.appVersion = GameM.commonData.appVersion
- data.tfChannel = GameM.commonData.tf_channel
- data.versioncfg = GameM.commonData.version
- //console.log("UIN:" + GameM.commonData.uin);
- if (GameM.commonData.isAuth) {
- // console.log('isAuth ', GameM.commonData.isAuth)
- encode = Utils.Encrypt(JSON.stringify(data), GameM.commonData.session_key, GameM.commonData.appid)
- dataN = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- }
- else {
- encode = Utils.Encrypt(JSON.stringify(data), GameM.commonData.session_key, GameM.commonData.appid)
- dataN = {
- "uin": GameM.commonData.uin,
- "encrypt": encode
- }
- }
- LogUtil.logV("sendDatas url = ", url + ' data.tfChannel = ' + data.tfChannel)
- this.httpGetCom(url, 'POST', JSON.stringify(dataN), sendDatasBack, failBack, data)
- }
- /** 检测用明文还是密文
- * @param response 返回消息
- * @param forceData true 明文 false 有密文优先密文,没密文情况下用明文
- */
- checkData(response, forceData = false) {
- let data = null
- if (forceData) {
- data = response.data
- }
- else {
- if (response.encrypt) {
- data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
- data = JSON.parse(data)
- }
- else if (response.data) {
- data = response.data
- }
- }
- return data
- }
- //ZNH
- /**
- * Post请求
- * @param path 数据地址
- * @param data 数据
- * @param handler 回调
- * @param param 回调参数
- * @param isDecrypt 是否要解密数据 默认不用 只有在游戏中请求数据用 微信登录不用
- * @param failhandler 失败回调
- * @param isListenerNet 是否监听网络连接状况
- */
- PostRequest(path, data, handler = null, param = null, isDecrypt = false, failhandler = null, isListenerNet = false, isDebug = true) {
- let self = this;
- let xhr = cc.loader.getXMLHttpRequest();
- if (data == null) {
- data = {};
- }
- if (isListenerNet) {
- var timer = setTimeout(function () {
- HttpM.Instance.NetDetaileState(1, 1);
- }, 1000);
- }
- xhr.open('POST', this.serverUrl + path);
- xhr.setRequestHeader('Content-Type', "application/json;charset=UTF-8");
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 300)) {
- let ret = xhr.responseText;
- let data = JSON.parse(ret);
- if (!isDebug)
- console.log("Data:", data);
- if (data.errcode == 0) {
- if (handler) {
- self.RequestCallback(data, handler, param, isDecrypt, isDebug);
- } else {
- console.log('httpGetCom callback is nil');
- }
- } else {
- if (data.errcode == 500) {
- if (failhandler) {
- failhandler(data.errmsg);
- }
- } else if (data.errcode == 40001) {
- console.log("httpGetCom error:" + 40001);
- //HttpM.Instance.PostRequest(path, data, handler, param, isDecrypt, failhandler);
- }
- console.error('httpGetCom callback errcode: ' + data.errcode + " info:" + + JSON.stringify(data) + " path " + path);
- }
- if (isListenerNet) {
- clearTimeout(timer);//取消等待的超时
- HttpM.Instance.NetDetaileState(2, 1);
- }
- } else {
- if (!(xhr.status >= 200 && xhr.status <= 300) && isDebug)
- console.error('httpGetCom callback status ' + xhr.readyState + "-" + xhr.status + " path" + path);
- }
- }
- xhr.ontimeout = function (e) {
- console.error("The http is ontimeout" + " path " + path);
- //GameM.commonData.showDisconnectedNode();
- if (isListenerNet) {
- clearTimeout(timer);//取消等待的超时
- HttpM.Instance.NetDetaileState(1, 2);
- }
- };
- xhr.onerror = function (e) {
- console.log("PostRequest the http is onerror path " + path);
- if (failhandler) {
- HttpM.Instance.PostRequest(path, data, handler, param, isDecrypt, failhandler);
- }
- if (isListenerNet) {
- clearTimeout(timer);//取消等待的超时
- HttpM.Instance.NetDetaileState(1, 3);
- }
- }
- xhr.send(JSON.stringify(data));
- }
- /**
- * 向服务器发送数据 返回数据需要做 非空 null判断
- * @param url 消息类型
- * @param data 发送的数据
- * @param handler 回调 若存在回调函数 可以为空
- * @param param 回参 回调函数的参数
- * @param failhandler 失败回调
- * @param isListenerNet 是否监听网络连接状况
- */
- SendData(url: HTTP_TYPE, data: any, handler = null, param = null, failhandler = null, isListenerNet = false, isDebug = true) {
- if (CommonData.IsServer) {
- if (!isDebug)
- console.log("UIN:" + CommonData.Instance.uin);
- //GameFacade.GameMng.WaitForNetStart(isDebug);
- let encode = '';
- let dataN = null;
- data.timestamp = Math.floor(new Date().getTime() * 0.001);
- data.appVersion = CommonData.Instance.appVersion;
- data.tfChannel = CommonData.Instance.tf_channel;
- data.versioncfg = CommonData.Instance.version;
- //console.log("Param:",data);
- encode = Utils.Encrypt(JSON.stringify(data), CommonData.Instance.session_key, CommonData.Instance.appid);
- dataN = {
- "uin": CommonData.Instance.uin,
- "encrypt": encode
- }
- this.PostRequest(url, dataN, handler, param, true, failhandler, isListenerNet, isDebug);
- } else {
- this.RequestCallback("", handler, param, false, isDebug);
- }
- }
- /**
- * 服务器数据返回
- * @param responseData 未解密的数据
- * @param handler 回调函数
- * @param param 回调函数参数
- * @param isDecrypt 是否要解密数据 默认不用 只有在游戏中请求数据用 微信登录不用
- * @param isDebug 是否打印log
- */
- RequestCallback(responseData, handler, param = null, isDecrypt = false, isDebug = true) {
- let data = responseData;
- if (isDecrypt) {
- //console.log("RequesetCallBack", data);
- data = this.CheckNetData(responseData);
- if (responseData.timestamp) {
- CommonData.Instance.gameTime = responseData.timestamp * 1000;
- }
- }
- if (handler != null) {
- //进行data非空判断
- if (param != null) {
- handler(data, param);
- } else {
- handler(data);
- }
- }
- ///if (data && handler != null) {
- /// if (param != null) {
- /// handler(data, param);
- /// } else {
- /// handler(data);
- /// }
- ///}
- }
- /*
- RequesetCallBack {errcode: 0, errmsg: "ok", data: Array(4)}
- RequesetCallBack {errcode: 0, errmsg: "ok", encrypt: “”}
- */
- /**
- * 检查数据
- * @param responseData 服务器返回的已转成对象的数据
- * @param forceData 数据类型 true 明文 false 若为密文则解密后返回 若是明文则直接返回
- */
- private CheckNetData(responseData, forceData = false) {
- //let data = null;
- let data: any = {};
- data.errcode = responseData.errcode;
- data.errmsg = responseData.errmsg;
- if (forceData) {
- data.data = responseData.data;
- } else {
- if (responseData.encrypt) {
- data.data = Utils.Decrypt(responseData.encrypt, CommonData.Instance.session_key, CommonData.Instance.appid);
- data.data = JSON.parse(data);
- } else if (responseData.data) {
- data.data = responseData.data;
- }
- }
- return data;
- }
- private netDetaileNode: cc.Node = null;
- /**
- * 网络状态
- * @param operate 操作 1 打开 2(非1) 关闭
- * @param state 1 连接中(超过2秒-可设置) 2 连接超时 3 网络错误
- */
- async NetDetaileState(operate: number, state: number) {
- if (this.netDetaileNode == null) {
- this.netDetaileNode = cc.instantiate(await Utils.loadResPromise("prefabs/NetDetaileNode"));
- this.netDetaileNode.parent = cc.find("Canvas");
- }
- if (this.netDetaileNode != null) {
- this.netDetaileNode.setSiblingIndex(cc.find("Canvas").childrenCount - 1);
- //
- this.netDetaileNode.getComponent(NetDetaileNode).DisplayDetaileState(operate, state);
- }
- }
- }
|