HttpM.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. import GameM from "./GameM";
  2. import { Utils } from "../utils/Utils";
  3. import CommonData, { HTTP_TYPE } from "../datas/CommonData";
  4. import AdM from "./AdM";
  5. import UiM from "./UiM";
  6. import CashOut from "../ui/CashOut";
  7. import GlobalStorage, { STORAGE_KEY } from "../datas/GlobalStorage";
  8. import Main from "../Main";
  9. import Setting from "../ui/Setting";
  10. import LogUtil from "../utils/LogUtil";
  11. import NetDetaileNode from "../prefabs/NetDetaileNode";
  12. import CashPro from "../ui/CashPro";
  13. import SwitchM from "./SwitchM";
  14. const { ccclass, property } = cc._decorator;
  15. @ccclass
  16. export default class HttpM {
  17. //正式服
  18. // private serverUrl = 'https://tcsg.duiweize.com/'
  19. //三国战戟预发服
  20. private serverUrl = 'https://tcsg-test.duiweize.com/'
  21. //老梅
  22. // private serverUrl = 'http://172.16.15.45:3942/'
  23. //老代
  24. // private serverUrl = 'http://172.16.13.93:8942/'
  25. //佳佳
  26. // private serverUrl = 'http://172.16.13.81:8942/'
  27. //zhang
  28. // private serverUrl = 'http://172.16.15.197:9132/'
  29. //zhang 2
  30. //private serverUrl = 'http://172.16.15.20:3942/'
  31. //xu
  32. // private serverUrl = 'http://172.16.15.137:9072/'
  33. //zhang 2 172.16.15.137
  34. //private serverUrl = 'http://172.16.15.20:3942/'
  35. //mei
  36. //private serverUrl = 'http://172.16.15.45:3942/'
  37. //zhao
  38. //private serverUrl = 'http://172.16.15.91:9072/'
  39. //swagger http://172.16.15.137:3942/swagger-ui/index.html#/%E7%94%A8%E6%88%B7%E6%93%8D%E4%BD%9C
  40. //swagger https://xiyou-test.duiweize.com/swagger-ui/index.html#/%E7%94%A8%E6%88%B7%E6%93%8D%E4%BD%9C
  41. //swagger tucao http://tcsg-test.duiweize.com/swagger-ui/#/%E7%BA%A2%E5%8C%85%E8%A7%86%E9%A2%91
  42. public isTry = ''
  43. private static instance: HttpM = null
  44. static get Instance(): HttpM {
  45. if (!this.instance) {
  46. this.instance = new HttpM()
  47. }
  48. return this.instance
  49. }
  50. init() {
  51. this.isTry = ''
  52. let value = GameM.globalStorage.getStorage(STORAGE_KEY.umFirst, 0)
  53. if (!value) {
  54. SwitchM.firstLoginLog = true
  55. GameM.globalStorage.setStorage(STORAGE_KEY.umFirst, 1, 0)
  56. }
  57. if (SwitchM.firstLoginLog) {
  58. AdM.sendUmeng('umLoadStart', '加载开始')
  59. }
  60. // GameM.httpM.connect()
  61. GameM.httpM.connectTest()
  62. }
  63. //加载json文件
  64. public loadJson(filepath, callback, cfgName) {
  65. let xhr = new XMLHttpRequest();
  66. xhr.open("GET", filepath, true)
  67. if (cc.sys.isNative) {
  68. xhr.setRequestHeader("Accept-Encoding", "text/html;charset=UTF-8");
  69. }
  70. xhr.timeout = 60000;
  71. xhr.onreadystatechange = function () {
  72. if (xhr.readyState == 4) {
  73. if (xhr.status >= 200 && xhr.status < 300) {
  74. let respone = xhr.responseText;
  75. if (callback) {
  76. callback(JSON.parse(respone), cfgName)
  77. }
  78. }
  79. else {
  80. console.error('There was a problem with the httpGetCom request.');
  81. }
  82. }
  83. }
  84. xhr.ontimeout = function (e) {
  85. console.error('The network is ontimeout ', e);
  86. // GameM.commonData.showDisconnectedNode()
  87. };
  88. xhr.onerror = function (e) {
  89. console.error('The network is onerror loadjson ', e);
  90. // GameM.commonData.showDisconnectedNode()
  91. };
  92. xhr.send();
  93. }
  94. /** 第一次请求 */
  95. httpGetFirst(url, methond: string = 'POST', data: string = null, callback: any = null) {
  96. let httpRequest = new XMLHttpRequest();
  97. // httpRequest.timeout = 60000
  98. httpRequest.onreadystatechange = function () {
  99. if (httpRequest.readyState === 4) {
  100. if (httpRequest.status === 200) {
  101. var responseStr = httpRequest.responseText;
  102. // console.log('responseStr ', responseStr)
  103. let response = JSON.parse(responseStr);
  104. if (response.code == 0) {
  105. if (callback) {
  106. callback(response)
  107. }
  108. else {
  109. console.error('httpGetFirst callback is null ')
  110. }
  111. }
  112. else {
  113. console.error('httpGetFirst callback ', response)
  114. }
  115. } else {
  116. console.error('There was a problem with the httpGetFirst request.');
  117. }
  118. }
  119. };
  120. httpRequest.ontimeout = function (e) {
  121. console.error('The network is ontimeout ', e);
  122. // GameM.commonData.showDisconnectedNode()
  123. };
  124. httpRequest.onerror = function (e) {
  125. console.error('The network is onerror first ', e);
  126. // GameM.commonData.showDisconnectedNode()
  127. };
  128. httpRequest.open(methond, this.serverUrl + url);
  129. httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  130. httpRequest.send(data);
  131. }
  132. /** 通用接口 */
  133. httpGetCom(url, methond: string = 'POST', data: string = null, callback: any = null, failBack = null, param: any = null, async = true, encrypt = true) {
  134. let httpRequest = new XMLHttpRequest();
  135. // httpRequest.timeout = 60000
  136. httpRequest.onreadystatechange = function () {
  137. if (httpRequest.readyState === 4) {
  138. if (httpRequest.status === 200) {
  139. var responseStr = httpRequest.responseText;
  140. let response = JSON.parse(responseStr);
  141. // console.log('httpGetCom >>>>>>>>>>>> ', url, ' response ', response.errcode, ' ', response.errmsg, ' param ', param)
  142. if (response.errcode == 0) {
  143. // console.log('aaaaaaaaaaa GameM.commonData.disConnectNum ', GameM.commonData.disConnectNum)
  144. if (callback) {
  145. if (encrypt) {
  146. let data = GameM.httpM.checkData(response)
  147. if (response.timestamp) {
  148. GameM.commonData.gameTime = response.timestamp * 1000
  149. }
  150. callback(data, param)
  151. }
  152. else {
  153. callback(response)
  154. }
  155. }
  156. else {
  157. // console.error('httpGetCom callback is null ')
  158. }
  159. }
  160. else {
  161. if (failBack) {
  162. failBack(response.errmsg)
  163. }
  164. console.error('httpGetCom callback XXXXXXXXXXXXXXXXXXXXX ', response, url)
  165. if (response && response.errcode) {
  166. console.log('response ', response.errcode)
  167. }
  168. LogUtil.logV('response ', response)
  169. console.error('httpGetCom callback TTTTTTTTTTTTTTTTTTTTT ')
  170. if (response.errcode == -10003) {
  171. GameM.httpM.checklogin()
  172. }
  173. }
  174. } else {
  175. console.error('There was a problem with the httpGetCom request.');
  176. }
  177. }
  178. };
  179. httpRequest.ontimeout = function (e) {
  180. GameM.commonData.showDisconnectedNode()
  181. };
  182. httpRequest.onerror = function (e) {
  183. console.error('The network is onerror nor ', e);
  184. };
  185. httpRequest.open(methond, this.serverUrl + this.isTry + url, async);
  186. httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  187. httpRequest.send(data);
  188. }
  189. /** 通用接口非对称 */
  190. httpGetComRsa(url, methond: string = 'POST', data: string = null, callback: any = null) {
  191. let httpRequest = new XMLHttpRequest();
  192. // httpRequest.timeout = 3000
  193. httpRequest.onreadystatechange = function () {
  194. if (httpRequest.readyState === 4) {
  195. if (httpRequest.status === 200) {
  196. var responseStr = httpRequest.responseText;
  197. let response = JSON.parse(responseStr);
  198. if (response.errcode == 0) {
  199. if (callback) {
  200. callback(response)
  201. }
  202. else {
  203. console.error('httpGetComRsa callback is null ')
  204. }
  205. }
  206. else {
  207. console.error('httpGetComRsa callback ', response.errcode, url)
  208. }
  209. } else {
  210. console.error('There was a problem with the httpGetComRsa request.');
  211. }
  212. }
  213. };
  214. let newData = Utils.RSAEncrypt(data)
  215. httpRequest.ontimeout = function (e) {
  216. console.error('The network is ontimeout ', e);
  217. // GameM.commonData.showDisconnectedNode()
  218. };
  219. httpRequest.onerror = function (e) {
  220. console.error('The network is onerror rsa ', e);
  221. };
  222. if (url == 'wxlogin') {
  223. httpRequest.open(methond, this.serverUrl + this.isTry + url, false);
  224. }
  225. else {
  226. httpRequest.open(methond, this.serverUrl + url, false);
  227. }
  228. httpRequest.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
  229. httpRequest.send(newData as string);
  230. }
  231. /** 第一次握手 */
  232. connect() {
  233. GameM.commonData.ranKey = Utils.randomString()
  234. let data = {
  235. "psk": GameM.commonData.ranKey,
  236. "appId": GameM.commonData.appid
  237. }
  238. this.httpGetComRsa('connect', 'POST', JSON.stringify(data), this.connectBack)
  239. }
  240. /** 用mac地址 */
  241. connectTest() {
  242. // console.log('mac ', GameM.commonData.machineInfo.mac)
  243. let mac = ''
  244. if (CC_JSB) {
  245. if (GameM.commonData.machineInfo.mac.indexOf(',')) {
  246. mac = GameM.commonData.machineInfo.mac.split(',')[0];
  247. }
  248. else {
  249. mac = GameM.commonData.machineInfo.mac;
  250. }
  251. }
  252. else {
  253. mac = "dg:34:67:35:90:32"
  254. }
  255. GameM.commonData.ranKey = Utils.randomString()
  256. let data = {
  257. "mac": mac,
  258. // "mac": "dg:34:67:35:90:32",
  259. "psk": GameM.commonData.ranKey,
  260. "appId": GameM.commonData.appid
  261. }
  262. this.httpGetComRsa(HTTP_TYPE.connectTest, 'POST', JSON.stringify(data), this.connectBack)
  263. }
  264. connectBack(response) {
  265. // console.log('connectBack ', response.encrypt)
  266. // console.log('connectBack ')
  267. if (SwitchM.firstLoginLog) {
  268. AdM.sendUmeng('umServer1', 'connectBack')
  269. }
  270. let data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKey, GameM.commonData.appid)
  271. // console.log('connectBack data ', data)
  272. if (data) {
  273. GameM.commonData.tmp_uin = JSON.parse(data).tmp_uin
  274. AdM.getInviteCode(true)
  275. GameM.httpM.wxlogin()
  276. }
  277. }
  278. /** 登录微信
  279. */
  280. wxlogin() {
  281. // console.log('wxlogin ')
  282. let aa;
  283. if (GameM.commonData.loginTest) {
  284. aa =
  285. {
  286. "code": "093AURkl2ySUy54Ccgml27Vy3c4AURkV"
  287. }
  288. }
  289. else {
  290. aa =
  291. {
  292. "code": GameM.commonData.wxCode
  293. }
  294. }
  295. let data
  296. let encode
  297. if (GameM.commonData.wxCode != '') {
  298. //微信登录
  299. encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
  300. data = {
  301. "uin": GameM.commonData.uin,
  302. "encrypt": encode
  303. }
  304. }
  305. else {
  306. //游客登录
  307. encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.ranKey, GameM.commonData.appid)
  308. data = {
  309. "uin": GameM.commonData.tmp_uin,
  310. "encrypt": encode
  311. }
  312. }
  313. this.httpGetCom('wxlogin', 'POST', JSON.stringify(data), this.wxloginBack.bind(this), null, null, false, false)
  314. }
  315. wxloginBack(response) {
  316. // console.log('wxloginBack ', response.encrypt)
  317. if (SwitchM.firstLoginLog) {
  318. AdM.sendUmeng('umServer2', 'wxloginBack')
  319. }
  320. let data
  321. if (GameM.commonData.wxCode != '') {
  322. data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
  323. }
  324. else {
  325. data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKey, GameM.commonData.appid)
  326. }
  327. if (data) {
  328. let dataN = JSON.parse(data)
  329. GameM.commonData.login_ticket = dataN.login_ticket
  330. let key = 'dddddddddddddddddddddddddddddddd'
  331. let d = Utils.Encrypt(GameM.commonData.login_ticket, key, GameM.commonData.appid)
  332. GlobalStorage.Instance.setStorage(STORAGE_KEY.login_ticket, d, 1)
  333. console.log('wxloginBack ', GameM.commonData.isFirstIn)
  334. let isFirstIn = GlobalStorage.Instance.getStorage(STORAGE_KEY.isFirstIn, 1)
  335. if (isFirstIn == null) {
  336. GameM.commonData.isFirstIn = true
  337. d = Utils.Encrypt(1, key, GameM.commonData.appid)
  338. GlobalStorage.Instance.setStorage(STORAGE_KEY.isFirstIn, d, 1)
  339. console.log('isFirstIn ')
  340. }
  341. if (GameM.commonData.uin != dataN.uin) {
  342. GameM.commonData.uin = dataN.uin
  343. GameM.commonData.isNew = false
  344. }
  345. else {
  346. GameM.commonData.isNew = true
  347. }
  348. console.log(">>>>>uin:" + GameM.commonData.uin);
  349. // console.log('checkloginBack uin ->>> ', GameM.commonData.isNew)
  350. d = Utils.Encrypt(GameM.commonData.uin, key, GameM.commonData.appid)
  351. GameM.globalStorage.setStorage(STORAGE_KEY.uin, d, 1)
  352. console.log(">>>>>2uin:" + GameM.commonData.uin);
  353. GameM.httpM.checklogin()
  354. }
  355. }
  356. /** checklogin接口 */
  357. checklogin() {
  358. // console.log('checklogin ')
  359. let tmp_key
  360. if (GameM.commonData.wxCode != '') {
  361. tmp_key = GameM.commonData.session_key
  362. }
  363. else {
  364. tmp_key = Utils.randomString()
  365. GameM.commonData.ranKeyNew = tmp_key
  366. }
  367. let aa =
  368. {
  369. "tmp_key": tmp_key,
  370. "uin": GameM.commonData.uin,
  371. "login_ticket": GameM.commonData.login_ticket,
  372. "appId": GameM.commonData.appid
  373. }
  374. this.httpGetComRsa('checklogin', 'POST', JSON.stringify(aa), this.checkloginBack)
  375. }
  376. checkloginBack(response) {
  377. if (SwitchM.firstLoginLog) {
  378. AdM.sendUmeng('umServer3', 'checkloginBack')
  379. }
  380. // console.log('checkloginBack ', response.encrypt)
  381. let data
  382. if (GameM.commonData.wxCode != '') {
  383. data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
  384. }
  385. else {
  386. data = Utils.Decrypt(response.encrypt, GameM.commonData.ranKeyNew, GameM.commonData.appid)
  387. }
  388. if (data) {
  389. let dataN = JSON.parse(data)
  390. GameM.commonData.session_key = dataN.session_key
  391. let key = 'dddddddddddddddddddddddddddddddd'
  392. let d = Utils.Encrypt(GameM.commonData.session_key, key, GameM.commonData.appid)
  393. GlobalStorage.Instance.setStorage(STORAGE_KEY.session_key, d, 1)
  394. if (GameM.commonData.wxCode != '') {
  395. AdM.onSendEvent('wxAuth', '玩家成功进行微信授权', 'wxAuth')
  396. //如果通过授权,就上报邀请
  397. GameM.ClubData.clubAuthPushInviteIn = true
  398. if (GameM.commonData.isNew) {
  399. //玩过此游戏并且本地数据是本机的
  400. console.log(">>>>>3uin:" + GameM.commonData.uin);
  401. GameM.httpM.getAccountInfo()
  402. // if (UiM.Instance.cashNode) {
  403. // UiM.Instance.cashNode.getComponent(CashOut).authBack()
  404. // }
  405. // UiM.Instance.redPackageNode.authBack()
  406. }
  407. else {
  408. //玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
  409. GameM.commonData.isNew = false
  410. GameM.commonData.wxCode = ''
  411. GameM.commonData.resetDatas()
  412. cc.director.loadScene('Loader')
  413. }
  414. }
  415. else {
  416. console.log(">>>>>4uin:" + GameM.commonData.uin);
  417. GameM.httpM.getAccountInfo()
  418. // AdM.setUserID()
  419. // GameM.commonData.getAllServerCfg()
  420. }
  421. }
  422. }
  423. /** 获取账号信息 */
  424. getAccountInfo() {
  425. // console.log('>>>>> getAccountInfo')
  426. this.isTry = ''
  427. let aa =
  428. {
  429. "uin": GameM.commonData.uin,
  430. "login_ticket": GameM.commonData.login_ticket
  431. }
  432. let encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
  433. // console.log(">>>>> " + GameM.commonData.uin, " ", GameM.commonData.appid)
  434. let data = {
  435. "uin": GameM.commonData.uin,
  436. "encrypt": encode
  437. }
  438. AdM.setUserID()
  439. let nickname = GlobalStorage.Instance.getStorage(STORAGE_KEY.nickname, 1)
  440. if (nickname) {
  441. console.log('MMM has NickName')
  442. if (GameM.ClubData.clubAuthPushInviteIn) {
  443. AdM.getInviteCode()
  444. GameM.ClubData.clubAuthPushInviteIn = false
  445. }
  446. GameM.commonData.getAllServerCfg()
  447. }
  448. else {
  449. console.log('MMM getAccountInfoBack')
  450. this.httpGetCom('user', 'POST', JSON.stringify(data), this.getAccountInfoBack.bind(this), null, null, false)
  451. }
  452. }
  453. /** 获取账号信息返回信息 */
  454. getAccountInfoBack(data, param) {
  455. if (SwitchM.firstLoginLog) {
  456. AdM.sendUmeng('umServer4', 'getAccountInfoBack')
  457. }
  458. // let data = GameM.httpM.checkData(response, true)
  459. if (data) {
  460. GameM.commonData.isAuth = data.is_auth
  461. if (data.is_auth) {
  462. let key = 'dddddddddddddddddddddddddddddddd'
  463. let d1 = Utils.Encrypt('true', key, GameM.commonData.appid)
  464. GlobalStorage.Instance.setStorage(STORAGE_KEY.isAuth, d1, 1)
  465. GameM.commonData.headimgurl = data.headimgurl
  466. GlobalStorage.Instance.setStorage(STORAGE_KEY.headimgurl, GameM.commonData.headimgurl, 1)
  467. GameM.commonData.nickname = data.nickname
  468. GlobalStorage.Instance.setStorage(STORAGE_KEY.nickname, GameM.commonData.nickname, 1)
  469. if (GameM.ClubData.clubAuthPushInviteIn) {
  470. AdM.getInviteCode()
  471. GameM.ClubData.clubAuthPushInviteIn = false
  472. }
  473. if (GameM.commonData.isNew) {
  474. if (UiM.Instance.cashNode) {
  475. UiM.Instance.cashNode.getComponent(CashOut).authBack()
  476. }
  477. if (UiM.Instance.redPackageNode && UiM.Instance.redPackageNode.node.active) {
  478. UiM.Instance.redPackageNode.authBack()
  479. }
  480. UiM.Instance.hallNode.getComponent(Main).setImg()
  481. if (UiM.Instance.settingNode) {
  482. UiM.Instance.settingNode.getComponent(Setting).setImg()
  483. }
  484. if (UiM.Instance.cashProNode) {
  485. UiM.Instance.cashProNode.getComponent(CashPro).authBack()
  486. }
  487. }
  488. else {
  489. GameM.commonData.getAllServerCfg()
  490. }
  491. }
  492. else {
  493. GameM.commonData.getAllServerCfg()
  494. }
  495. }
  496. }
  497. /** 是否是新的一天 */
  498. getUserIsNewDay() {
  499. console.log('GameM.commonData.uin ', GameM.commonData.uin)
  500. let aa = {}
  501. let encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
  502. let data = {
  503. "uin": GameM.commonData.uin,
  504. "encrypt": encode
  505. }
  506. this.httpGetCom(HTTP_TYPE.getUserIsNewDay, 'POST', JSON.stringify(data), this.getUerIsNewDayBack.bind(this), null, null, false)
  507. }
  508. getUerIsNewDayBack(data, param) {
  509. if (SwitchM.firstLoginLog) {
  510. AdM.sendUmeng('umServer6', 'getUerIsNewDayBack')
  511. }
  512. // let data = GameM.httpM.checkData(response)
  513. // console.log('getUerIsNewDayBack ', data)
  514. if (data) {
  515. GameM.commonData.userIsNewDay = data.userIsNewDay
  516. GameM.httpM.getInfo()
  517. }
  518. }
  519. getInfo() {
  520. console.log('getInfo ')
  521. let aa = { "versioncfg": GameM.commonData.version }
  522. let encode = ''
  523. let data = null
  524. if (GameM.commonData.isAuth) {
  525. // console.log('GameM.commonData.session_key ', GameM.commonData.session_key)
  526. // console.log('GameM.commonData.uin ', GameM.commonData.uin)
  527. encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
  528. data = {
  529. "uin": GameM.commonData.uin,
  530. "encrypt": encode
  531. }
  532. }
  533. else {
  534. // console.log('GameM.commonData.tmp_uin ', GameM.commonData.tmp_uin)
  535. encode = Utils.Encrypt(JSON.stringify(aa), GameM.commonData.session_key, GameM.commonData.appid)
  536. data = {
  537. "uin": GameM.commonData.uin,
  538. "encrypt": encode
  539. }
  540. }
  541. this.httpGetCom('getInfoCrypt', 'POST', JSON.stringify(data), this.getInfoBack.bind(this), null, null, false)
  542. }
  543. getInfoBack(data, param) {
  544. console.log('getInfoBack ')
  545. // let data = GameM.httpM.checkData(response)
  546. if (data) {
  547. GameM.commonData.getDatasFromServer(data)
  548. GameM.commonData.serverFinish = true
  549. console.log("-->Loading serverFinish end");
  550. if (SwitchM.firstLoginLog) {
  551. AdM.sendUmeng('umServerFinish', '登录服务器消息结束')
  552. }
  553. console.log('MMM GameM.commonData.serverFinish ')
  554. }
  555. }
  556. /** 向服务器发送数据
  557. * @param url 发送消息类型
  558. * @param data 发送的数据
  559. */
  560. sendDatas(url: HTTP_TYPE, data: any = null, sendDatasBack = null, failBack = null) {
  561. let encode = ''
  562. let dataN = null
  563. if (!data) {
  564. data = {}
  565. }
  566. data.timestamp = Math.floor(new Date().getTime() * 0.001)
  567. data.appVersion = GameM.commonData.appVersion
  568. data.tfChannel = GameM.commonData.tf_channel
  569. data.versioncfg = GameM.commonData.version
  570. //console.log("UIN:" + GameM.commonData.uin);
  571. if (GameM.commonData.isAuth) {
  572. // console.log('isAuth ', GameM.commonData.isAuth)
  573. encode = Utils.Encrypt(JSON.stringify(data), GameM.commonData.session_key, GameM.commonData.appid)
  574. dataN = {
  575. "uin": GameM.commonData.uin,
  576. "encrypt": encode
  577. }
  578. }
  579. else {
  580. encode = Utils.Encrypt(JSON.stringify(data), GameM.commonData.session_key, GameM.commonData.appid)
  581. dataN = {
  582. "uin": GameM.commonData.uin,
  583. "encrypt": encode
  584. }
  585. }
  586. LogUtil.logV("sendDatas url = ", url + ' data.tfChannel = ' + data.tfChannel)
  587. this.httpGetCom(url, 'POST', JSON.stringify(dataN), sendDatasBack, failBack, data)
  588. }
  589. /** 检测用明文还是密文
  590. * @param response 返回消息
  591. * @param forceData true 明文 false 有密文优先密文,没密文情况下用明文
  592. */
  593. checkData(response, forceData = false) {
  594. let data = null
  595. if (forceData) {
  596. data = response.data
  597. }
  598. else {
  599. if (response.encrypt) {
  600. data = Utils.Decrypt(response.encrypt, GameM.commonData.session_key, GameM.commonData.appid)
  601. data = JSON.parse(data)
  602. }
  603. else if (response.data) {
  604. data = response.data
  605. }
  606. }
  607. return data
  608. }
  609. //ZNH
  610. /**
  611. * Post请求
  612. * @param path 数据地址
  613. * @param data 数据
  614. * @param handler 回调
  615. * @param param 回调参数
  616. * @param isDecrypt 是否要解密数据 默认不用 只有在游戏中请求数据用 微信登录不用
  617. * @param failhandler 失败回调
  618. * @param isListenerNet 是否监听网络连接状况
  619. */
  620. PostRequest(path, data, handler = null, param = null, isDecrypt = false, failhandler = null, isListenerNet = false, isDebug = true) {
  621. let self = this;
  622. let xhr = cc.loader.getXMLHttpRequest();
  623. if (data == null) {
  624. data = {};
  625. }
  626. if (isListenerNet) {
  627. var timer = setTimeout(function () {
  628. HttpM.Instance.NetDetaileState(1, 1);
  629. }, 1000);
  630. }
  631. xhr.open('POST', this.serverUrl + path);
  632. xhr.setRequestHeader('Content-Type', "application/json;charset=UTF-8");
  633. xhr.onreadystatechange = function () {
  634. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 300)) {
  635. let ret = xhr.responseText;
  636. let data = JSON.parse(ret);
  637. if (!isDebug)
  638. console.log("Data:", data);
  639. if (data.errcode == 0) {
  640. if (handler) {
  641. self.RequestCallback(data, handler, param, isDecrypt, isDebug);
  642. } else {
  643. console.log('httpGetCom callback is nil');
  644. }
  645. } else {
  646. if (data.errcode == 500) {
  647. if (failhandler) {
  648. failhandler(data.errmsg);
  649. }
  650. } else if (data.errcode == 40001) {
  651. console.log("httpGetCom error:" + 40001);
  652. //HttpM.Instance.PostRequest(path, data, handler, param, isDecrypt, failhandler);
  653. }
  654. console.error('httpGetCom callback errcode: ' + data.errcode + " info:" + + JSON.stringify(data) + " path " + path);
  655. }
  656. if (isListenerNet) {
  657. clearTimeout(timer);//取消等待的超时
  658. HttpM.Instance.NetDetaileState(2, 1);
  659. }
  660. } else {
  661. if (!(xhr.status >= 200 && xhr.status <= 300) && isDebug)
  662. console.error('httpGetCom callback status ' + xhr.readyState + "-" + xhr.status + " path" + path);
  663. }
  664. }
  665. xhr.ontimeout = function (e) {
  666. console.error("The http is ontimeout" + " path " + path);
  667. //GameM.commonData.showDisconnectedNode();
  668. if (isListenerNet) {
  669. clearTimeout(timer);//取消等待的超时
  670. HttpM.Instance.NetDetaileState(1, 2);
  671. }
  672. };
  673. xhr.onerror = function (e) {
  674. console.log("PostRequest the http is onerror path " + path);
  675. if (failhandler) {
  676. HttpM.Instance.PostRequest(path, data, handler, param, isDecrypt, failhandler);
  677. }
  678. if (isListenerNet) {
  679. clearTimeout(timer);//取消等待的超时
  680. HttpM.Instance.NetDetaileState(1, 3);
  681. }
  682. }
  683. xhr.send(JSON.stringify(data));
  684. }
  685. /**
  686. * 向服务器发送数据 返回数据需要做 非空 null判断
  687. * @param url 消息类型
  688. * @param data 发送的数据
  689. * @param handler 回调 若存在回调函数 可以为空
  690. * @param param 回参 回调函数的参数
  691. * @param failhandler 失败回调
  692. * @param isListenerNet 是否监听网络连接状况
  693. */
  694. SendData(url: HTTP_TYPE, data: any, handler = null, param = null, failhandler = null, isListenerNet = false, isDebug = true) {
  695. if (CommonData.IsServer) {
  696. if (!isDebug)
  697. console.log("UIN:" + CommonData.Instance.uin);
  698. //GameFacade.GameMng.WaitForNetStart(isDebug);
  699. let encode = '';
  700. let dataN = null;
  701. data.timestamp = Math.floor(new Date().getTime() * 0.001);
  702. data.appVersion = CommonData.Instance.appVersion;
  703. data.tfChannel = CommonData.Instance.tf_channel;
  704. data.versioncfg = CommonData.Instance.version;
  705. //console.log("Param:",data);
  706. encode = Utils.Encrypt(JSON.stringify(data), CommonData.Instance.session_key, CommonData.Instance.appid);
  707. dataN = {
  708. "uin": CommonData.Instance.uin,
  709. "encrypt": encode
  710. }
  711. this.PostRequest(url, dataN, handler, param, true, failhandler, isListenerNet, isDebug);
  712. } else {
  713. this.RequestCallback("", handler, param, false, isDebug);
  714. }
  715. }
  716. /**
  717. * 服务器数据返回
  718. * @param responseData 未解密的数据
  719. * @param handler 回调函数
  720. * @param param 回调函数参数
  721. * @param isDecrypt 是否要解密数据 默认不用 只有在游戏中请求数据用 微信登录不用
  722. * @param isDebug 是否打印log
  723. */
  724. RequestCallback(responseData, handler, param = null, isDecrypt = false, isDebug = true) {
  725. let data = responseData;
  726. if (isDecrypt) {
  727. //console.log("RequesetCallBack", data);
  728. data = this.CheckNetData(responseData);
  729. if (responseData.timestamp) {
  730. CommonData.Instance.gameTime = responseData.timestamp * 1000;
  731. }
  732. }
  733. if (handler != null) {
  734. //进行data非空判断
  735. if (param != null) {
  736. handler(data, param);
  737. } else {
  738. handler(data);
  739. }
  740. }
  741. ///if (data && handler != null) {
  742. /// if (param != null) {
  743. /// handler(data, param);
  744. /// } else {
  745. /// handler(data);
  746. /// }
  747. ///}
  748. }
  749. /*
  750. RequesetCallBack {errcode: 0, errmsg: "ok", data: Array(4)}
  751. RequesetCallBack {errcode: 0, errmsg: "ok", encrypt: “”}
  752. */
  753. /**
  754. * 检查数据
  755. * @param responseData 服务器返回的已转成对象的数据
  756. * @param forceData 数据类型 true 明文 false 若为密文则解密后返回 若是明文则直接返回
  757. */
  758. private CheckNetData(responseData, forceData = false) {
  759. //let data = null;
  760. let data: any = {};
  761. data.errcode = responseData.errcode;
  762. data.errmsg = responseData.errmsg;
  763. if (forceData) {
  764. data.data = responseData.data;
  765. } else {
  766. if (responseData.encrypt) {
  767. data.data = Utils.Decrypt(responseData.encrypt, CommonData.Instance.session_key, CommonData.Instance.appid);
  768. data.data = JSON.parse(data);
  769. } else if (responseData.data) {
  770. data.data = responseData.data;
  771. }
  772. }
  773. return data;
  774. }
  775. private netDetaileNode: cc.Node = null;
  776. /**
  777. * 网络状态
  778. * @param operate 操作 1 打开 2(非1) 关闭
  779. * @param state 1 连接中(超过2秒-可设置) 2 连接超时 3 网络错误
  780. */
  781. async NetDetaileState(operate: number, state: number) {
  782. if (this.netDetaileNode == null) {
  783. this.netDetaileNode = cc.instantiate(await Utils.loadResPromise("prefabs/NetDetaileNode"));
  784. this.netDetaileNode.parent = cc.find("Canvas");
  785. }
  786. if (this.netDetaileNode != null) {
  787. this.netDetaileNode.setSiblingIndex(cc.find("Canvas").childrenCount - 1);
  788. //
  789. this.netDetaileNode.getComponent(NetDetaileNode).DisplayDetaileState(operate, state);
  790. }
  791. }
  792. }