Login.ts 6.4 KB

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