LoginManager.ts 16 KB

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