LoginManager.ts 16 KB

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