LoginManager.ts 16 KB

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