LoginManager.ts 17 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 } from "./NetManager";
  11. import { md5 } from "../../framework/storage/Md5";
  12. import { TimeUtil } from "../../framework/util/TimeUtil";
  13. const Macro = {
  14. cur_uuid: "cur_uuid", //当前用户uuid
  15. cur_token: "cur_token", //当前用户token
  16. }
  17. //export let NetMgr: NetManager = null;
  18. export class LoginManager {
  19. private _key = 'dd2edb87ea9eb7DFa32fd4IO0572ZX76d3a1fab861c1d5qishituan';
  20. private _iv = '096a4f23f1874640ab5f4bc82c7d3531';
  21. private _token = "";
  22. private _server_url = "http://172.31.244.30/login_qishituan.php?"
  23. private _channel: number | string = null;
  24. private _uuid: number | string = null;
  25. private _user_out = false; //是否被挤掉线
  26. private _class_id = StringUtil.getUUID(32);
  27. private _login_callback: (state: number) => void = null
  28. private _gateway_data: object = {};
  29. private userId: string = '';
  30. private gatewayData = null;
  31. private serverOpenId = null;
  32. private postList = [];
  33. public time_gap: number = 0;
  34. public time_gap2: number = 0;
  35. private serverPost: boolean = false;
  36. private autoOpenBox: boolean = false;
  37. private timeOut = [];
  38. private ReLoginUI_open = false;
  39. private postNum = 0;
  40. private ws_url = '';
  41. private we_req = null;
  42. // 服务器请求状态
  43. private post_server: object = {};
  44. constructor() {
  45. //NetMgr = new NetManager();
  46. Framework.event.addEvent("account_auth_userState", (res) => {
  47. res = JSON.parse(res);
  48. if (res.state == 2) {
  49. this._user_out = true;
  50. // Framework.layer.closeAll();
  51. }
  52. }, this, this._class_id);
  53. Framework.event.addEvent("gate_main_login", (res) => {
  54. res = JSON.parse(res);
  55. if (res.state == 0) {
  56. this._user_out = false;
  57. this._login_callback && this._login_callback(1);
  58. } else {
  59. this._login_callback && this._login_callback(0);
  60. }
  61. }, this, this._class_id);
  62. }
  63. check(code: string, callback: Function) {
  64. let sign = "";
  65. if (sys.platform == sys.Platform.WECHAT_GAME) {
  66. sign = "wechat"
  67. } else if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  68. sign = "tiktok"
  69. }
  70. let url = "http://124.70.72.166:9526/auth/" + sign;
  71. let data = { code: code };
  72. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  73. if (state && resp) {
  74. let msg = JSON.parse(EncryptUtil.aesDecrypt(resp.d, this._key, this._iv));
  75. callback && callback(msg.openid);
  76. return;
  77. }
  78. callback && callback(null);
  79. }, EncryptUtil.aesEncrypt(JSON.stringify(data), this._key, this._iv));
  80. }
  81. get user_id() {
  82. return StringUtil.format("[{0}_{1}]", this._channel, this._uuid);
  83. }
  84. /**
  85. * 获取服务器列表
  86. * @param name 后缀渠道
  87. * @param callback 回调函数:返回获取数据
  88. * */
  89. getServerList(callback, name) {
  90. if (name) {
  91. this.userId = name
  92. }
  93. let sign = 'uid=' + this.userId
  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 = this._server_url + sign;
  100. HttpUtil.post(url, (state: boolean, resp: any | null) => {
  101. if (state && resp) {
  102. console.log('获取服务器列表返回');
  103. console.log(resp);
  104. callback && callback(resp);
  105. return;
  106. }
  107. callback && callback(null);
  108. }, null);
  109. }
  110. /**
  111. * 请求gateway
  112. * @param data 请求参数 {openid,openkey,sid}
  113. * @param gateway_url 请求服务器路径
  114. * */
  115. getGateway(data, gateway_url, OpenUIBack) {
  116. this.serverOpenId = data.openid
  117. let url = gateway_url + `s${data.sid}?act=login&openid=${this.serverOpenId}&openkey=${data.openkey}&user=` + this.userId
  118. // let gateway_url =
  119. // let data = {
  120. // openid: xxx, //上面获取到的openid
  121. // openkey: xx, //上面获取到的openkey
  122. // sid: xx, // 选择的服务器列表信息中的id
  123. // }
  124. HttpUtil.get(url, (code: number, resp: any | null) => {
  125. if (code == 1 && resp) {
  126. console.log('请求gateway返回');
  127. console.log(resp);
  128. this.gatewayData = resp.data;
  129. this._server_url = this.gatewayData.game_server;
  130. this.login(resp.data, OpenUIBack)
  131. // callback && callback(msg.openid);
  132. return;
  133. }
  134. });
  135. }
  136. // 登录
  137. login(backdata, OpenUIBack) {
  138. let args = { "headpic": "", "system": "Fucking windows", "platform": "", "device": "PC", "name": this.userId, "lang": "cn" }
  139. this.sendPost('user', 'login', (data) => {
  140. console.log('登录返回数据', data)
  141. UserData.init(this.userId, data || {});
  142. let min = Number(this._server_url.indexOf('//'))
  143. let max = this._server_url.lastIndexOf(':')
  144. let server = this._server_url.substring(min, max)
  145. let ws_url = `ws:${server}:${data.wss_port}`
  146. // let ws_url = 'ws://172.31.244.30:61114'
  147. // ws://42.192.10.28:4003
  148. this.ws_url = ws_url;
  149. let req = {
  150. mod: 'user',
  151. act: 'handshake',
  152. args: {
  153. 'auth_key': backdata.auth_key,
  154. 'auth_time': backdata.auth_time,
  155. 'openid': this.serverOpenId,
  156. },
  157. uid: backdata.uid,
  158. }
  159. this.we_req = req
  160. // NetMgr.connect(this.ws_url, () => {
  161. // NetMgr.send(this.we_req)
  162. // OpenUIBack()
  163. // })
  164. OpenUIBack()
  165. }, args)
  166. }
  167. sendPost(mod, act, backFun, argsObj?) {
  168. if (!this.post_server[mod + act]) {
  169. this.post_server[mod + act] = 0
  170. } else {
  171. if (this.post_server[mod + act] == 0) {
  172. return;
  173. } else {
  174. this.post_server[mod + act] = 0;
  175. }
  176. }
  177. let args = {}
  178. if (argsObj) {
  179. args = argsObj
  180. }
  181. let postData = {
  182. mod: mod,
  183. act: act,
  184. back: backFun,
  185. args: args,
  186. }
  187. this.postList.push(postData)
  188. if (!this.serverPost) {
  189. if (this.postList.length == 1) {
  190. this.shiftPostList()
  191. }
  192. }
  193. }
  194. shiftPostList() {
  195. this.serverPost = true;
  196. let postData = this.postList.shift()
  197. let nextPostTask = () => {
  198. if (this.postList.length > 0) {
  199. this.shiftPostList()
  200. } else {
  201. this.serverPost = false
  202. }
  203. }
  204. this.PostRequest(postData, nextPostTask)
  205. }
  206. // Post请求
  207. PostRequest(postData, nextPostTask?) {
  208. let nowNum = this.postNum + 1;
  209. if (!this.timeOut[nowNum]) {
  210. this.timeOut[nowNum] = true;
  211. setTimeout(() => {
  212. if (this.timeOut[nowNum]) {
  213. this.ReLoginUI_open = true;
  214. Framework.layer.open(ViewID.MaskUI)
  215. }
  216. }, 1000)
  217. }
  218. let nowtime = TimeUtil.getTimeStamp()
  219. let authSig = md5(this._key + `|||${this.gatewayData.uid}---` + nowtime).substring(0, 10)
  220. let data = `act=${postData.act}&mod=${postData.mod}&seq=0&uid=${this.gatewayData.uid}&openid=${this.serverOpenId}`;
  221. data += `&auth_key=${this.gatewayData.auth_key}&auth_time=${this.gatewayData.auth_time}`
  222. data += `&last_seq=0&args=${JSON.stringify(postData.args)}`
  223. data += `&stime=${nowtime}&sig=${authSig}`
  224. HttpUtil.post(this._server_url, (state: boolean, resp: any | null) => {
  225. this.post_server[postData.mod + postData.act] = 1;
  226. this.timeOut[nowNum] = false;
  227. if (this.ReLoginUI_open) {
  228. this.ReLoginUI_open = false
  229. console.log('关闭转圈');
  230. Framework.layer.close(ViewID.MaskUI)
  231. }
  232. this.postNum++
  233. if (state && resp) {
  234. console.log(resp);
  235. if (resp.code == 1 && resp.desc == 'lock') {
  236. Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  237. return;
  238. }
  239. (resp.serverTime) && (this.time_gap = resp.serverTime - TimeUtil.getTimeEx(0));
  240. (resp.serverMiliTime) && (this.time_gap2 = resp.serverMiliTime - TimeUtil.getTimeEx2(0));
  241. if (resp.code == 0) {
  242. if (Object.keys(resp.data).length > 0) {
  243. } else {
  244. postData.back()
  245. }
  246. if (resp.data.have_pay) {
  247. if (!resp.data.pay_notify) {
  248. this.sendPost('user', 'get_cash', (data) => {
  249. if (data.code) {
  250. return;
  251. }
  252. if (data.ext_pay_info) {
  253. let awardsMsg = [];
  254. if (data.ext_pay_info.awards) {
  255. for (const i in data.ext_pay_info.awards) {
  256. awardsMsg.push(data.ext_pay_info.awards[i]);
  257. }
  258. }
  259. if (awardsMsg.length > 0) {
  260. // UserData.BackAwards(awardsMsg);
  261. }
  262. if (data.ext_pay_info.cash) {
  263. awardsMsg.push(['user', 'cash', data.ext_pay_info.cash]);
  264. }
  265. if (awardsMsg.length > 0) {
  266. // Framework.layer.open(ViewID.Reward, null, awardsMsg);
  267. }
  268. }
  269. if (data.cash) {
  270. UserData['status']['cash'] = data.cash;
  271. // Framework.event.fireEvent(GameEvent.Property_Renew, 'cash')
  272. }
  273. if (data.month_card) {
  274. UserData['activity']['month_card'] = data.month_card;
  275. }
  276. if (data.life_card) {
  277. UserData['activity']['life_card'] = data.life_card;
  278. }
  279. if (data.fund) {
  280. UserData['activity']['fund'] = data.fund;
  281. }
  282. // Framework.event.fireEvent(GameEvent.Recharge_Referesh);
  283. }, {});
  284. }
  285. }
  286. nextPostTask && nextPostTask();
  287. } else {
  288. if (postData.act == 'auto_open') {
  289. this.autoOpenBox = false;
  290. }
  291. this.errorCode(resp.code, resp.desc, () => {
  292. nextPostTask && nextPostTask();
  293. })
  294. nextPostTask && nextPostTask();
  295. }
  296. } else {
  297. nextPostTask && nextPostTask();
  298. }
  299. return;
  300. }, data);
  301. }
  302. errorCode(code, desc, next) {
  303. switch (code) {
  304. case 1:
  305. // 异地登录
  306. Framework.tips.setTips(desc)
  307. next()
  308. return;
  309. case 4:
  310. // 异地登录
  311. Framework.layer.open(ViewID.Restart, null, '您的账号已在其他设备登录');
  312. Framework.tips.setTips('Remote login')
  313. return;
  314. case 2:
  315. // 服务器重启/网络断开(3是客户端自己调用写进来的)
  316. Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  317. return;
  318. case 3:
  319. Framework.layer.open(ViewID.Restart, null, '网络异常,请重新登录');
  320. return;
  321. case 5:
  322. Framework.layer.open(ViewID.Restart, null, '游戏跨天,为保证数据同步,请重新登录');
  323. return;
  324. case 10000:
  325. Framework.layer.open(ViewID.Restart, null, '长时间未操作,请重新登录');
  326. return;
  327. case 10001:
  328. next()
  329. return;
  330. case 20000:
  331. Framework.layer.open(ViewID.Restart, null, '数据异常,请重新登录');
  332. next()
  333. return;
  334. case 50000:
  335. next()
  336. // -- 加速器外挂/服务器lock报错,不提示任何信息
  337. return;
  338. case 300:
  339. Framework.tips.setTips('矿工已死亡')
  340. return;
  341. case 301:
  342. Framework.tips.setTips('操作不一致')
  343. return;
  344. case 302:
  345. Framework.tips.setTips('矿工无体力')
  346. return;
  347. case 303:
  348. Framework.tips.setTips('没有挖目标')
  349. return;
  350. case 304:
  351. Framework.tips.setTips('挖目标错误')
  352. return;
  353. case 305:
  354. Framework.tips.setTips('决策事件待处理')
  355. return;
  356. case 306:
  357. Framework.tips.setTips('炸弹不足')
  358. return;
  359. default:
  360. break;
  361. }
  362. //Framework.tips.setTips((ErrorcodeConf.data[code]) ? ErrorcodeConf.data[code].DescCn : desc);
  363. }
  364. /**
  365. * 自动开箱
  366. * @param state 状态
  367. * @param data 数据
  368. * */
  369. AutoBoxState(state: boolean, data_args?) {
  370. // console.log(UserData.getMaterialCount(GameConf.MaterialType.box));
  371. // if (UserData.getMaterialCount(GameConf.MaterialType.box) > 0) {
  372. // this.autoOpenBox = state;
  373. // if (this.autoOpenBox) {
  374. // let args = {}
  375. // let postData = {
  376. // mod: 'box',
  377. // act: 'auto_open',
  378. // back: null,
  379. // args: data_args,
  380. // }
  381. // let BackFun = (data) => {
  382. // // if (data.awards) {
  383. // // UserData.BackAwards(data.awards)
  384. // // }
  385. // if (data.cache == 0) {
  386. // Framework.event.fireEvent(GameEvent.Box_Update, GameConf.BoxEventType.AutoOpen, data.awards);
  387. // (data.awards) && (UserData.BackAwards(data.awards));
  388. // Framework.time.scheduleOnce(() => {
  389. // if (this.autoOpenBox) {
  390. // if (UserData.getMaterialCount(GameConf.MaterialType.box) > 0) {
  391. // this.PostRequest(postData)
  392. // } else {
  393. // this.autoOpenBox = false;
  394. // Framework.tips.setTips('没有箱子可以开了')
  395. // }
  396. // }
  397. // }, GameConf.Macro.open_box_time)
  398. // } else {
  399. // if (data.cache) {
  400. // UserData['box'].cache_equip = data.cache;
  401. // Framework.event.fireEvent(GameEvent.Box_Update, GameConf.BoxEventType.HaveEquip, data.awards);
  402. // }
  403. // (data.awards) && (UserData.BackAwards(data.awards));
  404. // // (data.awards) && (, data.awards));
  405. // this.autoOpenBox = false;
  406. // }
  407. // }
  408. // postData.back = BackFun
  409. // this.PostRequest(postData)
  410. // }
  411. // }
  412. }
  413. /**
  414. * 改变名字或性别
  415. * @param type 0 昵称 1 性别
  416. * @param value 值
  417. */
  418. changeNickname(type: number, value: string) {
  419. //NetMgr.send(reqBuf);
  420. }
  421. }