Login.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { _decorator, Component, ProgressBar, director, Enum, Button } from 'cc';
  2. import { Http, HttpMethod, ResponseType } from '../core/net/Http';
  3. import { HttpSystem } from '../core/net/HttpSystem';
  4. import { WindowManager } from '../core/ui/window/WindowManager';
  5. import { RC4 } from '../core/utils/RC4';
  6. import { g } from '../Data/g';
  7. import { ISJSB, platform } from '../Data/platform';
  8. const { ccclass, property } = _decorator;
  9. export enum LoginType {
  10. guest,
  11. wechat
  12. }
  13. @ccclass('Login')
  14. export class Login extends Component {
  15. @property({ type: ProgressBar, tooltip: "加载进度条组件" })
  16. public progressBar: ProgressBar;
  17. @property({ tooltip: "网络请求对象", type: Http })
  18. public http: Http;
  19. @property({ tooltip: "测试账号" })
  20. public account: string = 'test';
  21. @property({ tooltip: "登录类型,游客、微信", type: Enum(LoginType) })
  22. public loginType: LoginType = LoginType.guest;
  23. @property({ type: Button, tooltip: "微信登录按钮" })
  24. public wechatButton: Button;
  25. start() {
  26. platform.reportThinking("loading", JSON.stringify({ is_first: localStorage.getItem("account") ? false : true }));
  27. this.wechatButton.node.active = false;
  28. }
  29. public async login(account: string = null) {
  30. if (this.loginType == LoginType.guest) {
  31. this.doLogin(account);
  32. }
  33. else if (this.loginType == LoginType.wechat) {
  34. account = localStorage.getItem("account");
  35. if (account) {
  36. this.doLogin(account);
  37. }
  38. else {
  39. this.progressBar.node.active = false;
  40. // this.wechatButton.node.active = true;
  41. }
  42. }
  43. }
  44. public onWechatLogin() {
  45. platform.wxLogin();
  46. }
  47. private async doLogin(account: string = null, nickName: string = '', avator: string = '') {
  48. account = account || this.account;
  49. let onlyLogin = false;
  50. if (ISJSB) {
  51. account = localStorage.getItem("account") || account;
  52. onlyLogin = !!localStorage.getItem("account");
  53. }
  54. this.http.responseType = ResponseType.Arraybuffer;
  55. let data = await this.http.send("/api/user/logo");
  56. if (!data) {
  57. if (this.loginType == LoginType.wechat) {
  58. this.progressBar.node.active = false;
  59. // this.wechatButton.node.active = true;
  60. }
  61. WindowManager.showTips("登录失败,请检查网络");
  62. return;
  63. }
  64. let moka = String.fromCharCode.apply(null, new Uint8Array(data));
  65. let code = "";
  66. for (let i = 0; i < moka.length - 1; i++) {
  67. const charCode0 = moka.charCodeAt(i).toString(16);
  68. const charCode1 = moka.charCodeAt(i + 1).toString(16);
  69. if (charCode0 == "ff" && charCode1 == "d9") {
  70. code = moka.substring(i + 2);
  71. break;
  72. }
  73. }
  74. HttpSystem.publicKey = RC4.rc4(decodeURI(code), "moka");
  75. this.http.responseType = ResponseType.Json;
  76. let result = await this.http.send("/api/user/login", {
  77. account: account,
  78. onlyLogin: onlyLogin,
  79. type: LoginType[this.loginType],
  80. nickName: nickName,
  81. avator: avator,
  82. channel: g.deviceData.channel || 0,
  83. fromUserID: 0,
  84. imei: g.deviceData.imei,
  85. oaid: g.deviceData.oaid,
  86. androId: g.deviceData.androId,
  87. mac: g.deviceData.mac
  88. });
  89. if (result.data) {//如果登录成功
  90. localStorage.setItem("account", result.data.account);
  91. g.userData.setData(result.data);
  92. platform.setThinkingID(result.data.id);
  93. platform.reportThinkingUserInfo(JSON.stringify(result.data));
  94. g.gameData.needChildCount = result.needChildCount;
  95. g.gameData.threedayTaskTime = result.threedayTaskTime;
  96. HttpSystem.initServerTimestamp(result.data.timestamp);
  97. HttpSystem.addTail(`sessionID=${g.userData.token}`);
  98. HttpSystem.setEncryptionKey(result.data.id);
  99. let gameData = await this.http.send("/api/product/getBuildData", null, HttpMethod.GET, true);
  100. if (gameData.code == 0) {
  101. g.gameData.setGameData(gameData.data);
  102. director.preloadScene("Main", this.onProgress.bind(this), this.onLoaded.bind(this));
  103. } else {
  104. WindowManager.showTips("获取游戏数据失败,请检查网络");
  105. }
  106. }
  107. else {
  108. localStorage.removeItem("account");
  109. if (this.loginType == LoginType.wechat) {
  110. this.progressBar.node.active = false;
  111. // this.wechatButton.node.active = true;
  112. }
  113. WindowManager.showTips("登录失败,请检查网络");
  114. }
  115. }
  116. private onProgress(completedCount: number, totalCount: number, item: any): void {
  117. this.progressBar.progress = completedCount / totalCount;
  118. }
  119. private onLoaded(): void {
  120. platform.reportThinking("login");
  121. director.loadScene("Main");
  122. }
  123. /** --------------------------------- 通用登录 ------------------------------------------ */
  124. public init() {
  125. director.preloadScene("Main", this.onProgress.bind(this), this.preLoadFinish.bind(this));
  126. }
  127. private preLoadFinish(): void {
  128. gData.loginData.init()
  129. }
  130. update() {
  131. // if (g.wechatData.code) {
  132. // this.progressBar.node.active = true;
  133. // this.wechatButton.node.active = false;
  134. // this.doLogin(g.wechatData.code, g.wechatData.nickName, g.wechatData.avatar);
  135. // g.wechatData.code = null;
  136. // }
  137. if (gData.gameData.dataFinish) {
  138. platform.reportThinking("login");
  139. director.loadScene("Main");
  140. }
  141. }
  142. }