LoginManager.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import { sys } from "cc";
  2. import { FrameworkConf } from "../../framework/config/FrameworkConf";
  3. import { ViewID } from "../../framework/config/LayerConf";
  4. import { Framework } from "../../framework/Framework";
  5. import { EncryptUtil } from "../../framework/storage/EncryptUtil";
  6. import { StringUtil } from "../../framework/util/StringUtil";
  7. import { GameEvent } from "../data/GameEvent";
  8. import { UserData } from "../data/UserData";
  9. import { HttpUtil } from "./HttpUtil";
  10. import { NetManager, NetMgr } from "./NetManager";
  11. import { md5 } from "../../framework/storage/Md5";
  12. import { TimeUtil } from "../../framework/util/TimeUtil";
  13. import { MailData } from "../data/MailData";
  14. import { RoleData } from "../data/RoleData";
  15. import { AccountData } from "../data/AccountData";
  16. import { GoodsData } from "../data/GoodsData";
  17. import { EquipData } from "../data/EquipData";
  18. import { BattleData } from "../data/BattleData";
  19. const Macro = {
  20. cur_uuid: "cur_uuid", //当前用户uuid
  21. cur_token: "cur_token", //当前用户token
  22. }
  23. //登录及登录后网络业务逻辑 网络业务逻辑层的NetMgr
  24. export class LoginManager {
  25. private _key = 'dd2edb87ea9eb7DFa32fd4IO0572ZX76d3a1fab861c1d5qishituan';
  26. private _iv = '096a4f23f1874640ab5f4bc82c7d3531';
  27. private _token = "";
  28. private _server_url = "http://118.178.135.110/sdk/debug/login.php?"
  29. private _channel: number | string = null;
  30. private _uuid: number | string = null;
  31. private _user_out = false; //是否被挤掉线
  32. private _class_id = StringUtil.getUUID(32);
  33. private _login_callback: (state: number) => void = null
  34. private _gateway_data: object = {};
  35. private userId: string = '';
  36. private gatewayData = null;
  37. private serverOpenId = null;
  38. private postList = [];
  39. // public time_gap: number = 0;
  40. // public time_gap2: number = 0;
  41. private serverPost: boolean = false;
  42. private autoOpenBox: boolean = false;
  43. private timeOut = [];
  44. private ReLoginUI_open = false;
  45. private postNum = 0;
  46. private ws_url = '';
  47. private we_req = null;
  48. private _seq = 1
  49. private _last_seq = 0;
  50. //重连次数
  51. private _reconnectNum = 0;
  52. // 服务器请求状态
  53. private post_server: object = {};
  54. constructor() {
  55. }
  56. init(){
  57. if(!NetMgr) NetManager.instance;
  58. Framework.event.addEvent("account_auth_userState", (res) => {
  59. res = JSON.parse(res);
  60. if (res.state == 2) {
  61. this._user_out = true;
  62. // Framework.layer.closeAll();
  63. }
  64. }, this, this._class_id);
  65. Framework.event.addEvent("gate_main_login", (res) => {
  66. res = JSON.parse(res);
  67. if (res.state == 0) {
  68. this._user_out = false;
  69. this._login_callback && this._login_callback(1);
  70. } else {
  71. this._login_callback && this._login_callback(0);
  72. }
  73. }, this, this._class_id);
  74. Framework.event.addEvent(FrameworkConf.Event.NET_CLOSE, (event) => {
  75. let NetWork = false;
  76. console.log('长链接握手');
  77. this._reconnectNum++;
  78. if(this._reconnectNum > 5) return;
  79. this.sendPost('user', 'check_connect', (data) => {
  80. NetWork = true;
  81. if (this.ws_url) {
  82. NetMgr.connect(this.ws_url, () => {
  83. NetMgr.send(this.we_req)
  84. })
  85. }
  86. })
  87. // setTimeout(() => {
  88. // }, 2000)
  89. }, this, this._class_id);
  90. Framework.event.addEvent(GameEvent.Ws_Hand, (event) => {
  91. this._reconnectNum = 0;
  92. }, this, this._class_id)
  93. }
  94. check(code: string, callback: Function) {
  95. let sign = "";
  96. if (sys.platform == sys.Platform.WECHAT_GAME) {
  97. sign = "wechat"
  98. } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  99. sign = "tiktok"
  100. }
  101. let url = "http://124.70.72.166:9526/auth/" + sign;
  102. let data = { code: code };
  103. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  104. if (state && resp) {
  105. let msg = JSON.parse(EncryptUtil.aesDecrypt(resp.d, this._key, this._iv));
  106. callback && callback(msg.openid);
  107. return;
  108. }
  109. callback && callback(null);
  110. }, EncryptUtil.aesEncrypt(JSON.stringify(data), this._key, this._iv));
  111. }
  112. get user_id() {
  113. return StringUtil.format("[{0}_{1}]", this._channel, this._uuid);
  114. }
  115. /**
  116. * 获取服务器列表
  117. * @param data 透传参数
  118. * @param callback 回调函数:返回获取数据
  119. * */
  120. getServerList(callback, data) {
  121. if (data) {
  122. this.userId = data.uid;
  123. this._server_url = data.url
  124. this._token = data.token
  125. }
  126. let sign = 'name=' + this.userId
  127. if (sys.platform == sys.Platform.WECHAT_GAME) {
  128. sign = "wechat"
  129. } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  130. sign = "tiktok"
  131. }
  132. let url = `${this._server_url}?uid=${this.userId}`;
  133. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  134. if (state && resp) {
  135. console.log('获取服务器列表返回');
  136. console.log(resp);
  137. callback && callback(resp);
  138. return;
  139. }
  140. callback && callback(null);
  141. }, null);
  142. }
  143. /**
  144. * 请求gateway
  145. * @param data 请求参数 {openid,openkey,sid}
  146. * @param gateway_url 请求服务器路径
  147. * */
  148. getGateway(data, gateway_url, OpenUIBack) {
  149. this.serverOpenId = data.openid
  150. let url = gateway_url + `s${data.sid}?act=login&openid=${this.serverOpenId}&openkey=${data.openkey}&user=` + this.userId
  151. // let gateway_url =
  152. // let data = {
  153. // openid: xxx, //上面获取到的openid
  154. // openkey: xx, //上面获取到的openkey
  155. // sid: xx, // 选择的服务器列表信息中的id
  156. // }
  157. HttpUtil.get(url, (code: number, resp: any | null) => {
  158. if (code == 1 && resp) {
  159. console.log('请求gateway返回');
  160. console.log(resp);
  161. this.gatewayData = resp.data;
  162. this._server_url = this.gatewayData.game_server;
  163. this.login(resp.data, OpenUIBack)
  164. // callback && callback(msg.openid);
  165. return;
  166. }
  167. });
  168. }
  169. // 登录
  170. login(backdata, OpenUIBack) {
  171. this._seq = 1
  172. this._last_seq = 0
  173. let args = { "headpic": "", "system": "Fucking windows", "platform": "", "device": "PC", "name": this.userId, "lang": "cn" }
  174. this.sendPost('user', 'login', (data) => {
  175. console.log('登录返回数据', data)
  176. UserData.init(data || {});
  177. if (data.get_mails) {
  178. MailData.setData(data.get_mails)
  179. }
  180. if (data.role_bag && data.role_bag.roles) {
  181. RoleData.setData(data.role_bag.roles)
  182. }
  183. if (data.equip && data.race_equip) {
  184. EquipData.setData(data.equip, data.race_equip)
  185. }
  186. if (data.fight_role) {
  187. RoleData.setFightRole(data.fight_role)
  188. }
  189. BattleData.setLoginData(data)
  190. if (data.inventory) {
  191. if (data.inventory.material) {
  192. GoodsData.setAllGoods(data.inventory.material)
  193. }
  194. }
  195. let min = Number(this._server_url.indexOf('//'))
  196. let max = this._server_url.lastIndexOf(':')
  197. let server = this._server_url.substring(min, max)
  198. let ws_url = `ws:${server}:${data.wss_port}`
  199. // let ws_url = 'ws://172.31.244.30:61114'
  200. // ws://42.192.10.28:4003
  201. this.ws_url = ws_url;
  202. let req = {
  203. mod: 'user',
  204. act: 'handshake',
  205. args: {
  206. 'auth_key': backdata.auth_key,
  207. 'auth_time': backdata.auth_time,
  208. 'openid': this.serverOpenId,
  209. },
  210. uid: backdata.uid,
  211. }
  212. this.we_req = req
  213. NetMgr.connect(this.ws_url, () => {
  214. NetMgr.send(this.we_req)
  215. OpenUIBack()
  216. })
  217. }, args)
  218. }
  219. sendPost(mod, act, backFun, argsObj?,errorFun?) {
  220. if (this._seq != this._last_seq + 1) {
  221. console.error(`sendPost seq error: seq ${this._seq},last_seq${this._last_seq}`)
  222. }
  223. if (!this.post_server[mod + act]) {
  224. this.post_server[mod + act] = 0
  225. } else {
  226. if (this.post_server[mod + act] == 0) {
  227. return;
  228. } else {
  229. this.post_server[mod + act] = 0;
  230. }
  231. }
  232. let args = {}
  233. if (argsObj) {
  234. args = argsObj
  235. }
  236. let postData = {
  237. mod: mod,
  238. act: act,
  239. back: backFun,
  240. args: args,
  241. errorFun: errorFun
  242. }
  243. this.postList.push(postData)
  244. if (!this.serverPost) {
  245. if (this.postList.length == 1) {
  246. this.shiftPostList()
  247. }
  248. }
  249. }
  250. shiftPostList() {
  251. this.serverPost = true;
  252. let postData = this.postList.shift()
  253. let nextPostTask = () => {
  254. if (this.postList.length > 0) {
  255. this.shiftPostList()
  256. } else {
  257. this.serverPost = false
  258. }
  259. }
  260. this.PostRequest(postData, nextPostTask)
  261. }
  262. // Post请求
  263. PostRequest(postData, nextPostTask?) {
  264. let nowNum = this.postNum + 1;
  265. if (!this.timeOut[nowNum]) {
  266. this.timeOut[nowNum] = true;
  267. setTimeout(() => {
  268. if (this.timeOut[nowNum]) {
  269. this.ReLoginUI_open = true;
  270. Framework.layer.open(ViewID.MaskUI)
  271. }
  272. }, 1000)
  273. }
  274. let nowtime = TimeUtil.getTimeStamp()
  275. let authSig = md5(this._key + `|||${this.gatewayData.uid}---` + nowtime).substring(0, 10)
  276. let data = `act=${postData.act}&mod=${postData.mod}&seq=${this._seq}&uid=${this.gatewayData.uid}&openid=${this.serverOpenId}`;
  277. data += `&auth_key=${this.gatewayData.auth_key}&auth_time=${this.gatewayData.auth_time}`
  278. data += `&last_seq=0&args=${JSON.stringify(postData.args)}`
  279. data += `&stime=${nowtime}&sig=${authSig}`
  280. HttpUtil.post(this._server_url, (state: boolean, resp: any | null) => {
  281. this.post_server[postData.mod + postData.act] = 1;
  282. this.timeOut[nowNum] = false;
  283. if (this.ReLoginUI_open) {
  284. this.ReLoginUI_open = false
  285. console.log('关闭转圈');
  286. Framework.layer.close(ViewID.MaskUI)
  287. }
  288. this.postNum++
  289. if (state && resp) {
  290. console.log(resp);
  291. if (resp.code == 1 && resp.desc == 'lock') {
  292. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  293. return;
  294. }
  295. this._last_seq = Number(resp.last_seq)
  296. if (this._seq != this._last_seq) {
  297. console.error(`server seq error: this.seq ${this._seq},last_seq ${this._last_seq}`)
  298. }
  299. this._seq++
  300. AccountData.serverTime = resp.serverTime;
  301. if (resp.code == 0) {
  302. if (Object.keys(resp.data).length > 0) {
  303. postData.back(resp.data)
  304. } else {
  305. postData.back()
  306. }
  307. // if (resp.data.have_pay) {
  308. // if (!resp.data.pay_notify) {
  309. // this.sendPost('user', 'get_cash', (data) => {
  310. // if (data.code) {
  311. // return;
  312. // }
  313. // if (data.ext_pay_info) {
  314. // let awardsMsg = [];
  315. // if (data.ext_pay_info.awards) {
  316. // for (const i in data.ext_pay_info.awards) {
  317. // awardsMsg.push(data.ext_pay_info.awards[i]);
  318. // }
  319. // }
  320. // if (awardsMsg.length > 0) {
  321. // // UserData.BackAwards(awardsMsg);
  322. // }
  323. // if (data.ext_pay_info.cash) {
  324. // awardsMsg.push(['user', 'cash', data.ext_pay_info.cash]);
  325. // }
  326. // if (awardsMsg.length > 0) {
  327. // // Framework.layer.open(ViewID.Reward, null, awardsMsg);
  328. // }
  329. // }
  330. // if (data.cash) {
  331. // UserData['status']['cash'] = data.cash;
  332. // // Framework.event.fireEvent(GameEvent.Property_Renew, 'cash')
  333. // }
  334. // if (data.month_card) {
  335. // UserData['activity']['month_card'] = data.month_card;
  336. // }
  337. // if (data.life_card) {
  338. // UserData['activity']['life_card'] = data.life_card;
  339. // }
  340. // if (data.fund) {
  341. // UserData['activity']['fund'] = data.fund;
  342. // }
  343. // // Framework.event.fireEvent(GameEvent.Recharge_Referesh);
  344. // }, {});
  345. // }
  346. // }
  347. nextPostTask && nextPostTask();
  348. } else {
  349. if (postData.act == 'auto_open') {
  350. this.autoOpenBox = false;
  351. }
  352. if(postData.errorFun){
  353. postData.errorFun(resp.code, resp.desc);
  354. }
  355. else{
  356. this.errorCode(resp.code, resp.desc, () => {
  357. // nextPostTask && nextPostTask();
  358. })
  359. }
  360. nextPostTask && nextPostTask();
  361. }
  362. } else {
  363. nextPostTask && nextPostTask();
  364. }
  365. return;
  366. }, data);
  367. }
  368. errorCode(code, desc, next) {
  369. switch (code) {
  370. case 1:
  371. // 异地登录
  372. Framework.tips.setTips(desc)
  373. next()
  374. return;
  375. case 4:
  376. // 异地登录
  377. // Framework.layer.open(ViewID.Restart, null, '您的账号已在其他设备登录');
  378. Framework.tips.setTips('Remote login')
  379. return;
  380. case 2:
  381. // 服务器重启/网络断开(3是客户端自己调用写进来的)
  382. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  383. return;
  384. case 3:
  385. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  386. return;
  387. case 5:
  388. // Framework.layer.open(ViewID.Restart, null, '游戏跨天,为保证数据同步,请重新登录');
  389. return;
  390. case 10000:
  391. // Framework.layer.open(ViewID.Restart, null, '长时间未操作,请重新登录');
  392. return;
  393. case 10001:
  394. next()
  395. return;
  396. case 20000:
  397. // Framework.layer.open(ViewID.Restart, null, '数据异常,请重新登录');
  398. next()
  399. return;
  400. case 50000:
  401. next()
  402. // -- 加速器外挂/服务器lock报错,不提示任何信息
  403. return;
  404. case 300:
  405. Framework.tips.setTips('矿工已死亡')
  406. return;
  407. case 301:
  408. Framework.tips.setTips('操作不一致')
  409. return;
  410. case 302:
  411. Framework.tips.setTips('矿工无体力')
  412. return;
  413. case 303:
  414. Framework.tips.setTips('没有挖目标')
  415. return;
  416. case 304:
  417. Framework.tips.setTips('挖目标错误')
  418. return;
  419. case 305:
  420. Framework.tips.setTips('决策事件待处理')
  421. return;
  422. case 306:
  423. Framework.tips.setTips('炸弹不足')
  424. return;
  425. default:
  426. break;
  427. }
  428. //Framework.tips.setTips((ErrorcodeConf.data[code]) ? ErrorcodeConf.data[code].DescCn : desc);
  429. }
  430. }
  431. export let LoginMgr: LoginManager = new LoginManager;