LoginManager.ts 16 KB

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