LoginManager.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. const Macro = {
  16. cur_uuid: "cur_uuid", //当前用户uuid
  17. cur_token: "cur_token", //当前用户token
  18. }
  19. //业务逻辑层的NetMgr
  20. export class LoginManager {
  21. private _key = 'dd2edb87ea9eb7DFa32fd4IO0572ZX76d3a1fab861c1d5qishituan';
  22. private _iv = '096a4f23f1874640ab5f4bc82c7d3531';
  23. private _token = "";
  24. private _server_url = "http://118.178.135.110/sdk/debug/login.php?"
  25. private _channel: number | string = null;
  26. private _uuid: number | string = null;
  27. private _user_out = false; //是否被挤掉线
  28. private _class_id = StringUtil.getUUID(32);
  29. private _login_callback: (state: number) => void = null
  30. private _gateway_data: object = {};
  31. private userId: string = '';
  32. private gatewayData = null;
  33. private serverOpenId = null;
  34. private postList = [];
  35. public time_gap: number = 0;
  36. public time_gap2: number = 0;
  37. private serverPost: boolean = false;
  38. private autoOpenBox: boolean = false;
  39. private timeOut = [];
  40. private ReLoginUI_open = false;
  41. private postNum = 0;
  42. private ws_url = '';
  43. private we_req = null;
  44. private _seq = 1
  45. private _last_seq = 0;
  46. //重连次数
  47. private _reconnectNum = 0;
  48. // 服务器请求状态
  49. private post_server: object = {};
  50. constructor() {
  51. }
  52. init(){
  53. if(!NetMgr) NetManager.instance;
  54. Framework.event.addEvent("account_auth_userState", (res) => {
  55. res = JSON.parse(res);
  56. if (res.state == 2) {
  57. this._user_out = true;
  58. // Framework.layer.closeAll();
  59. }
  60. }, this, this._class_id);
  61. Framework.event.addEvent("gate_main_login", (res) => {
  62. res = JSON.parse(res);
  63. if (res.state == 0) {
  64. this._user_out = false;
  65. this._login_callback && this._login_callback(1);
  66. } else {
  67. this._login_callback && this._login_callback(0);
  68. }
  69. }, this, this._class_id);
  70. Framework.event.addEvent(FrameworkConf.Event.NET_CLOSE, (event) => {
  71. let NetWork = false;
  72. console.log('长链接握手');
  73. this._reconnectNum++;
  74. if(this._reconnectNum > 5) return;
  75. this.sendPost('user', 'check_connect', (data) => {
  76. NetWork = true;
  77. if (this.ws_url) {
  78. NetMgr.connect(this.ws_url, () => {
  79. NetMgr.send(this.we_req)
  80. })
  81. }
  82. })
  83. // setTimeout(() => {
  84. // }, 2000)
  85. }, this, this._class_id);
  86. Framework.event.addEvent(GameEvent.Ws_Hand, (event) => {
  87. this._reconnectNum = 0;
  88. }, this, this._class_id)
  89. }
  90. check(code: string, callback: Function) {
  91. let sign = "";
  92. if (sys.platform == sys.Platform.WECHAT_GAME) {
  93. sign = "wechat"
  94. } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  95. sign = "tiktok"
  96. }
  97. let url = "http://124.70.72.166:9526/auth/" + sign;
  98. let data = { code: code };
  99. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  100. if (state && resp) {
  101. let msg = JSON.parse(EncryptUtil.aesDecrypt(resp.d, this._key, this._iv));
  102. callback && callback(msg.openid);
  103. return;
  104. }
  105. callback && callback(null);
  106. }, EncryptUtil.aesEncrypt(JSON.stringify(data), this._key, this._iv));
  107. }
  108. get user_id() {
  109. return StringUtil.format("[{0}_{1}]", this._channel, this._uuid);
  110. }
  111. /**
  112. * 获取服务器列表
  113. * @param data 透传参数
  114. * @param callback 回调函数:返回获取数据
  115. * */
  116. getServerList(callback, data) {
  117. if (data) {
  118. this.userId = data.uid;
  119. this._server_url = data.url
  120. this._token = data.token
  121. }
  122. let sign = 'name=' + this.userId
  123. if (sys.platform == sys.Platform.WECHAT_GAME) {
  124. sign = "wechat"
  125. } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  126. sign = "tiktok"
  127. }
  128. let url = `${this._server_url}?uid=${this.userId}`;
  129. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  130. if (state && resp) {
  131. console.log('获取服务器列表返回');
  132. console.log(resp);
  133. callback && callback(resp);
  134. return;
  135. }
  136. callback && callback(null);
  137. }, null);
  138. }
  139. /**
  140. * 请求gateway
  141. * @param data 请求参数 {openid,openkey,sid}
  142. * @param gateway_url 请求服务器路径
  143. * */
  144. getGateway(data, gateway_url, OpenUIBack) {
  145. this.serverOpenId = data.openid
  146. let url = gateway_url + `s${data.sid}?act=login&openid=${this.serverOpenId}&openkey=${data.openkey}&user=` + this.userId
  147. // let gateway_url =
  148. // let data = {
  149. // openid: xxx, //上面获取到的openid
  150. // openkey: xx, //上面获取到的openkey
  151. // sid: xx, // 选择的服务器列表信息中的id
  152. // }
  153. HttpUtil.get(url, (code: number, resp: any | null) => {
  154. if (code == 1 && resp) {
  155. console.log('请求gateway返回');
  156. console.log(resp);
  157. this.gatewayData = resp.data;
  158. this._server_url = this.gatewayData.game_server;
  159. this.login(resp.data, OpenUIBack)
  160. // callback && callback(msg.openid);
  161. return;
  162. }
  163. });
  164. }
  165. // 登录
  166. login(backdata, OpenUIBack) {
  167. this._seq = 1
  168. this._last_seq = 0
  169. let args = { "headpic": "", "system": "Fucking windows", "platform": "", "device": "PC", "name": this.userId, "lang": "cn" }
  170. this.sendPost('user', 'login', (data) => {
  171. console.log('登录返回数据', data)
  172. UserData.init(data || {});
  173. let min = Number(this._server_url.indexOf('//'))
  174. let max = this._server_url.lastIndexOf(':')
  175. let server = this._server_url.substring(min, max)
  176. let ws_url = `ws:${server}:${data.wss_port}`
  177. // let ws_url = 'ws://172.31.244.30:61114'
  178. // ws://42.192.10.28:4003
  179. this.ws_url = ws_url;
  180. let req = {
  181. mod: 'user',
  182. act: 'handshake',
  183. args: {
  184. 'auth_key': backdata.auth_key,
  185. 'auth_time': backdata.auth_time,
  186. 'openid': this.serverOpenId,
  187. },
  188. uid: backdata.uid,
  189. }
  190. this.we_req = req
  191. NetMgr.connect(this.ws_url, () => {
  192. NetMgr.send(this.we_req)
  193. OpenUIBack()
  194. })
  195. }, args)
  196. }
  197. sendPost(mod, act, backFun, argsObj?) {
  198. if (this._seq != this._last_seq + 1) {
  199. console.error(`sendPost seq error: seq ${this._seq},last_seq${this._last_seq}`)
  200. }
  201. if (!this.post_server[mod + act]) {
  202. this.post_server[mod + act] = 0
  203. } else {
  204. if (this.post_server[mod + act] == 0) {
  205. return;
  206. } else {
  207. this.post_server[mod + act] = 0;
  208. }
  209. }
  210. let args = {}
  211. if (argsObj) {
  212. args = argsObj
  213. }
  214. let postData = {
  215. mod: mod,
  216. act: act,
  217. back: backFun,
  218. args: args,
  219. }
  220. this.postList.push(postData)
  221. if (!this.serverPost) {
  222. if (this.postList.length == 1) {
  223. this.shiftPostList()
  224. }
  225. }
  226. }
  227. shiftPostList() {
  228. this.serverPost = true;
  229. let postData = this.postList.shift()
  230. let nextPostTask = () => {
  231. if (this.postList.length > 0) {
  232. this.shiftPostList()
  233. } else {
  234. this.serverPost = false
  235. }
  236. }
  237. this.PostRequest(postData, nextPostTask)
  238. }
  239. // Post请求
  240. PostRequest(postData, nextPostTask?) {
  241. let nowNum = this.postNum + 1;
  242. if (!this.timeOut[nowNum]) {
  243. this.timeOut[nowNum] = true;
  244. setTimeout(() => {
  245. if (this.timeOut[nowNum]) {
  246. this.ReLoginUI_open = true;
  247. Framework.layer.open(ViewID.MaskUI)
  248. }
  249. }, 1000)
  250. }
  251. let nowtime = TimeUtil.getTimeStamp()
  252. let authSig = md5(this._key + `|||${this.gatewayData.uid}---` + nowtime).substring(0, 10)
  253. let data = `act=${postData.act}&mod=${postData.mod}&seq=${this._seq}&uid=${this.gatewayData.uid}&openid=${this.serverOpenId}`;
  254. data += `&auth_key=${this.gatewayData.auth_key}&auth_time=${this.gatewayData.auth_time}`
  255. data += `&last_seq=0&args=${JSON.stringify(postData.args)}`
  256. data += `&stime=${nowtime}&sig=${authSig}`
  257. HttpUtil.post(this._server_url, (state: boolean, resp: any | null) => {
  258. this.post_server[postData.mod + postData.act] = 1;
  259. this.timeOut[nowNum] = false;
  260. if (this.ReLoginUI_open) {
  261. this.ReLoginUI_open = false
  262. console.log('关闭转圈');
  263. Framework.layer.close(ViewID.MaskUI)
  264. }
  265. this.postNum++
  266. if (state && resp) {
  267. console.log(resp);
  268. if (resp.code == 1 && resp.desc == 'lock') {
  269. // Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  270. return;
  271. }
  272. this._last_seq = Number(resp.last_seq)
  273. if (this._seq != this._last_seq) {
  274. console.error(`server seq error: this.seq ${this._seq},last_seq ${this._last_seq}`)
  275. }
  276. this._seq++
  277. (resp.serverTime) && (this.time_gap = resp.serverTime - TimeUtil.getTimeEx(0));
  278. (resp.serverMiliTime) && (this.time_gap2 = resp.serverMiliTime - TimeUtil.getTimeEx2(0));
  279. if (resp.code == 0) {
  280. if (Object.keys(resp.data).length > 0) {
  281. postData.back(resp.data)
  282. } else {
  283. postData.back()
  284. }
  285. if (resp.data.get_mails) {
  286. MailData.setData(resp.data.get_mails)
  287. }
  288. if (resp.data.have_pay) {
  289. if (!resp.data.pay_notify) {
  290. this.sendPost('user', 'get_cash', (data) => {
  291. if (data.code) {
  292. return;
  293. }
  294. if (data.ext_pay_info) {
  295. let awardsMsg = [];
  296. if (data.ext_pay_info.awards) {
  297. for (const i in data.ext_pay_info.awards) {
  298. awardsMsg.push(data.ext_pay_info.awards[i]);
  299. }
  300. }
  301. if (awardsMsg.length > 0) {
  302. // UserData.BackAwards(awardsMsg);
  303. }
  304. if (data.ext_pay_info.cash) {
  305. awardsMsg.push(['user', 'cash', data.ext_pay_info.cash]);
  306. }
  307. if (awardsMsg.length > 0) {
  308. // Framework.layer.open(ViewID.Reward, null, awardsMsg);
  309. }
  310. }
  311. if (data.cash) {
  312. UserData['status']['cash'] = data.cash;
  313. // Framework.event.fireEvent(GameEvent.Property_Renew, 'cash')
  314. }
  315. if (data.month_card) {
  316. UserData['activity']['month_card'] = data.month_card;
  317. }
  318. if (data.life_card) {
  319. UserData['activity']['life_card'] = data.life_card;
  320. }
  321. if (data.fund) {
  322. UserData['activity']['fund'] = data.fund;
  323. }
  324. // Framework.event.fireEvent(GameEvent.Recharge_Referesh);
  325. }, {});
  326. }
  327. }
  328. nextPostTask && nextPostTask();
  329. } else {
  330. if (postData.act == 'auto_open') {
  331. this.autoOpenBox = false;
  332. }
  333. this.errorCode(resp.code, resp.desc, () => {
  334. nextPostTask && nextPostTask();
  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;