import { _decorator, Component, ProgressBar, director, Enum, Button } from 'cc'; import { Http, HttpMethod, ResponseType } from '../core/net/Http'; import { HttpSystem } from '../core/net/HttpSystem'; import { WindowManager } from '../core/ui/window/WindowManager'; import { RC4 } from '../core/utils/RC4'; import { g } from '../Data/g'; import { ISJSB, platform } from '../Data/platform'; const { ccclass, property } = _decorator; export enum LoginType { guest, wechat } @ccclass('Login') export class Login extends Component { @property({ type: ProgressBar, tooltip: "加载进度条组件" }) public progressBar: ProgressBar; @property({ tooltip: "网络请求对象", type: Http }) public http: Http; @property({ tooltip: "测试账号" }) public account: string = 'test'; @property({ tooltip: "登录类型,游客、微信", type: Enum(LoginType) }) public loginType: LoginType = LoginType.guest; @property({ type: Button, tooltip: "微信登录按钮" }) public wechatButton: Button; start() { platform.reportThinking("loading", JSON.stringify({ is_first: localStorage.getItem("account") ? false : true })); this.wechatButton.node.active = false; } public async login(account: string = null) { if (this.loginType == LoginType.guest) { this.doLogin(account); } else if (this.loginType == LoginType.wechat) { account = localStorage.getItem("account"); if (account) { this.doLogin(account); } else { this.progressBar.node.active = false; // this.wechatButton.node.active = true; } } } public onWechatLogin() { platform.wxLogin(); } private async doLogin(account: string = null, nickName: string = '', avator: string = '') { account = account || this.account; let onlyLogin = false; if (ISJSB) { account = localStorage.getItem("account") || account; onlyLogin = !!localStorage.getItem("account"); } this.http.responseType = ResponseType.Arraybuffer; let data = await this.http.send("/api/user/logo"); if (!data) { if (this.loginType == LoginType.wechat) { this.progressBar.node.active = false; // this.wechatButton.node.active = true; } WindowManager.showTips("登录失败,请检查网络"); return; } let moka = String.fromCharCode.apply(null, new Uint8Array(data)); let code = ""; for (let i = 0; i < moka.length - 1; i++) { const charCode0 = moka.charCodeAt(i).toString(16); const charCode1 = moka.charCodeAt(i + 1).toString(16); if (charCode0 == "ff" && charCode1 == "d9") { code = moka.substring(i + 2); break; } } HttpSystem.publicKey = RC4.rc4(decodeURI(code), "moka"); this.http.responseType = ResponseType.Json; let result = await this.http.send("/api/user/login", { account: account, onlyLogin: onlyLogin, type: LoginType[this.loginType], nickName: nickName, avator: avator, channel: g.deviceData.channel || 0, fromUserID: 0, imei: g.deviceData.imei, oaid: g.deviceData.oaid, androId: g.deviceData.androId, mac: g.deviceData.mac }); if (result.data) {//如果登录成功 localStorage.setItem("account", result.data.account); g.userData.setData(result.data); platform.setThinkingID(result.data.id); platform.reportThinkingUserInfo(JSON.stringify(result.data)); g.gameData.needChildCount = result.needChildCount; g.gameData.threedayTaskTime = result.threedayTaskTime; HttpSystem.initServerTimestamp(result.data.timestamp); HttpSystem.addTail(`sessionID=${g.userData.token}`); HttpSystem.setEncryptionKey(result.data.id); let gameData = await this.http.send("/api/product/getBuildData", null, HttpMethod.GET, true); if (gameData.code == 0) { g.gameData.setGameData(gameData.data); director.preloadScene("Main", this.onProgress.bind(this), this.onLoaded.bind(this)); } else { WindowManager.showTips("获取游戏数据失败,请检查网络"); } } else { localStorage.removeItem("account"); if (this.loginType == LoginType.wechat) { this.progressBar.node.active = false; // this.wechatButton.node.active = true; } WindowManager.showTips("登录失败,请检查网络"); } } private onProgress(completedCount: number, totalCount: number, item: any): void { this.progressBar.progress = completedCount / totalCount; } private onLoaded(): void { platform.reportThinking("login"); director.loadScene("Main"); } /** --------------------------------- 通用登录 ------------------------------------------ */ public init() { director.preloadScene("Main", this.onProgress.bind(this), this.preLoadFinish.bind(this)); } private preLoadFinish(): void { gData.loginData.init() } update() { // if (g.wechatData.code) { // this.progressBar.node.active = true; // this.wechatButton.node.active = false; // this.doLogin(g.wechatData.code, g.wechatData.nickName, g.wechatData.avatar); // g.wechatData.code = null; // } if (gData.gameData.dataFinish) { platform.reportThinking("login"); director.loadScene("Main"); } } }