LoginManager.ts 17 KB

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