LoginManager.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.inverntory) {
  182. if (data.inverntory.material) {
  183. GoodsData.setAllGoods(data.inverntory.material)
  184. }
  185. }
  186. let min = Number(this._server_url.indexOf('//'))
  187. let max = this._server_url.lastIndexOf(':')
  188. let server = this._server_url.substring(min, max)
  189. let ws_url = `ws:${server}:${data.wss_port}`
  190. // let ws_url = 'ws://172.31.244.30:61114'
  191. // ws://42.192.10.28:4003
  192. this.ws_url = ws_url;
  193. let req = {
  194. mod: 'user',
  195. act: 'handshake',
  196. args: {
  197. 'auth_key': backdata.auth_key,
  198. 'auth_time': backdata.auth_time,
  199. 'openid': this.serverOpenId,
  200. },
  201. uid: backdata.uid,
  202. }
  203. this.we_req = req
  204. NetMgr.connect(this.ws_url, () => {
  205. NetMgr.send(this.we_req)
  206. OpenUIBack()
  207. })
  208. }, args)
  209. }
  210. sendPost(mod, act, backFun, argsObj?) {
  211. if (this._seq != this._last_seq + 1) {
  212. console.error(`sendPost seq error: seq ${this._seq},last_seq${this._last_seq}`)
  213. }
  214. if (!this.post_server[mod + act]) {
  215. this.post_server[mod + act] = 0
  216. } else {
  217. if (this.post_server[mod + act] == 0) {
  218. return;
  219. } else {
  220. this.post_server[mod + act] = 0;
  221. }
  222. }
  223. let args = {}
  224. if (argsObj) {
  225. args = argsObj
  226. }
  227. let postData = {
  228. mod: mod,
  229. act: act,
  230. back: backFun,
  231. args: args,
  232. }
  233. this.postList.push(postData)
  234. if (!this.serverPost) {
  235. if (this.postList.length == 1) {
  236. this.shiftPostList()
  237. }
  238. }
  239. }
  240. shiftPostList() {
  241. this.serverPost = true;
  242. let postData = this.postList.shift()
  243. let nextPostTask = () => {
  244. if (this.postList.length > 0) {
  245. this.shiftPostList()
  246. } else {
  247. this.serverPost = false
  248. }
  249. }
  250. this.PostRequest(postData, nextPostTask)
  251. }
  252. // Post请求
  253. PostRequest(postData, nextPostTask?) {
  254. let nowNum = this.postNum + 1;
  255. if (!this.timeOut[nowNum]) {
  256. this.timeOut[nowNum] = true;
  257. setTimeout(() => {
  258. if (this.timeOut[nowNum]) {
  259. this.ReLoginUI_open = true;
  260. Framework.layer.open(ViewID.MaskUI)
  261. }
  262. }, 1000)
  263. }
  264. let nowtime = TimeUtil.getTimeStamp()
  265. let authSig = md5(this._key + `|||${this.gatewayData.uid}---` + nowtime).substring(0, 10)
  266. let data = `act=${postData.act}&mod=${postData.mod}&seq=${this._seq}&uid=${this.gatewayData.uid}&openid=${this.serverOpenId}`;
  267. data += `&auth_key=${this.gatewayData.auth_key}&auth_time=${this.gatewayData.auth_time}`
  268. data += `&last_seq=0&args=${JSON.stringify(postData.args)}`
  269. data += `&stime=${nowtime}&sig=${authSig}`
  270. HttpUtil.post(this._server_url, (state: boolean, resp: any | null) => {
  271. this.post_server[postData.mod + postData.act] = 1;
  272. this.timeOut[nowNum] = false;
  273. if (this.ReLoginUI_open) {
  274. this.ReLoginUI_open = false
  275. console.log('关闭转圈');
  276. Framework.layer.close(ViewID.MaskUI)
  277. }
  278. this.postNum++
  279. if (state && resp) {
  280. console.log(resp);
  281. if (resp.code == 1 && resp.desc == 'lock') {
  282. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  283. return;
  284. }
  285. this._last_seq = Number(resp.last_seq)
  286. if (this._seq != this._last_seq) {
  287. console.error(`server seq error: this.seq ${this._seq},last_seq ${this._last_seq}`)
  288. }
  289. this._seq++
  290. AccountData.serverTime = resp.serverTime;
  291. if (resp.code == 0) {
  292. if (Object.keys(resp.data).length > 0) {
  293. postData.back(resp.data)
  294. } else {
  295. postData.back()
  296. }
  297. // if (resp.data.have_pay) {
  298. // if (!resp.data.pay_notify) {
  299. // this.sendPost('user', 'get_cash', (data) => {
  300. // if (data.code) {
  301. // return;
  302. // }
  303. // if (data.ext_pay_info) {
  304. // let awardsMsg = [];
  305. // if (data.ext_pay_info.awards) {
  306. // for (const i in data.ext_pay_info.awards) {
  307. // awardsMsg.push(data.ext_pay_info.awards[i]);
  308. // }
  309. // }
  310. // if (awardsMsg.length > 0) {
  311. // // UserData.BackAwards(awardsMsg);
  312. // }
  313. // if (data.ext_pay_info.cash) {
  314. // awardsMsg.push(['user', 'cash', data.ext_pay_info.cash]);
  315. // }
  316. // if (awardsMsg.length > 0) {
  317. // // Framework.layer.open(ViewID.Reward, null, awardsMsg);
  318. // }
  319. // }
  320. // if (data.cash) {
  321. // UserData['status']['cash'] = data.cash;
  322. // // Framework.event.fireEvent(GameEvent.Property_Renew, 'cash')
  323. // }
  324. // if (data.month_card) {
  325. // UserData['activity']['month_card'] = data.month_card;
  326. // }
  327. // if (data.life_card) {
  328. // UserData['activity']['life_card'] = data.life_card;
  329. // }
  330. // if (data.fund) {
  331. // UserData['activity']['fund'] = data.fund;
  332. // }
  333. // // Framework.event.fireEvent(GameEvent.Recharge_Referesh);
  334. // }, {});
  335. // }
  336. // }
  337. nextPostTask && nextPostTask();
  338. } else {
  339. if (postData.act == 'auto_open') {
  340. this.autoOpenBox = false;
  341. }
  342. this.errorCode(resp.code, resp.desc, () => {
  343. nextPostTask && nextPostTask();
  344. })
  345. nextPostTask && nextPostTask();
  346. }
  347. } else {
  348. nextPostTask && nextPostTask();
  349. }
  350. return;
  351. }, data);
  352. }
  353. errorCode(code, desc, next) {
  354. switch (code) {
  355. case 1:
  356. // 异地登录
  357. Framework.tips.setTips(desc)
  358. next()
  359. return;
  360. case 4:
  361. // 异地登录
  362. // Framework.layer.open(ViewID.Restart, null, '您的账号已在其他设备登录');
  363. Framework.tips.setTips('Remote login')
  364. return;
  365. case 2:
  366. // 服务器重启/网络断开(3是客户端自己调用写进来的)
  367. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  368. return;
  369. case 3:
  370. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  371. return;
  372. case 5:
  373. // Framework.layer.open(ViewID.Restart, null, '游戏跨天,为保证数据同步,请重新登录');
  374. return;
  375. case 10000:
  376. // Framework.layer.open(ViewID.Restart, null, '长时间未操作,请重新登录');
  377. return;
  378. case 10001:
  379. next()
  380. return;
  381. case 20000:
  382. // Framework.layer.open(ViewID.Restart, null, '数据异常,请重新登录');
  383. next()
  384. return;
  385. case 50000:
  386. next()
  387. // -- 加速器外挂/服务器lock报错,不提示任何信息
  388. return;
  389. case 300:
  390. Framework.tips.setTips('矿工已死亡')
  391. return;
  392. case 301:
  393. Framework.tips.setTips('操作不一致')
  394. return;
  395. case 302:
  396. Framework.tips.setTips('矿工无体力')
  397. return;
  398. case 303:
  399. Framework.tips.setTips('没有挖目标')
  400. return;
  401. case 304:
  402. Framework.tips.setTips('挖目标错误')
  403. return;
  404. case 305:
  405. Framework.tips.setTips('决策事件待处理')
  406. return;
  407. case 306:
  408. Framework.tips.setTips('炸弹不足')
  409. return;
  410. default:
  411. break;
  412. }
  413. //Framework.tips.setTips((ErrorcodeConf.data[code]) ? ErrorcodeConf.data[code].DescCn : desc);
  414. }
  415. }
  416. export let LoginMgr: LoginManager = new LoginManager;