LoginManager.ts 17 KB

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