| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { _decorator, Component, ProgressBar, director, Enum, Button, Node, Toggle } from 'cc';
- import { Http, HttpMethod, ResponseType } from '../core/net/Http';
- import { HttpSystem } from '../core/net/HttpSystem';
- import { BitmapFont } from '../core/ui/BitmapFont';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- 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({ type: Node, tooltip: "加载进度文本" })
- public progressLabelNode: Node;
- @property({ type: BitmapFont, tooltip: "加载进度" })
- loadProgressBitmapFont: BitmapFont;
- @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;
- @property({ type: Toggle, tooltip: '协议同意' })
- public agreementToogle: Toggle;
- start() {
- 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 {
- platform.reportThinkingOnce('launchLoaded');
- this.progressLabelNode.active = this.progressBar.node.active = false;
- this.wechatButton.node.active = true;
- }
- }
- }
- public onXieyi(e: TouchEvent, type: string): void {
- WindowManager.open("Prefabs/WebWindow", WindowOpenMode.NotCloseAndAdd, type);
- }
- public onWechatLogin() {
- if (this.agreementToogle.isChecked) {
- platform.wxLogin();
- } else {
- WindowManager.showTips('请阅读用户协议和隐私保护政策并同意');
- }
- }
- update() {
- if (g.wechatData.code) {
- this.progressLabelNode.active = 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;
- platform.reportThinkingOnce('wxLoginSuccess');
- }
- }
- 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.progressLabelNode.active = 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.userData.account);
- g.userData.setData(result.data.userData);
- platform.setThinkingID(result.data.userData.id);
- platform.reportThinkingUserInfo(JSON.stringify(result.data.userData));
- HttpSystem.initServerTimestamp(result.data.userData.timestamp)
- HttpSystem.addTail(`sessionID=${g.userData.token}&newApp=true`);
- HttpSystem.setEncryptionKey(g.userData.id);
- let gameData = await this.http.send("/api/user/GetUserDatas", null, HttpMethod.GET, true);
- if (gameData.code == 0) {
- g.gameData.setData(gameData.data);
- g.gameData.setData(result.data.offData);
- g.gameData.openRecommend = result.data.openRecommend == 1;
- console.log(g.gameData.generals);
- 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.loadProgressBitmapFont.string = Math.floor(completedCount / totalCount * 100) + '%'
- this.progressBar.progress = completedCount / totalCount;
- }
- private onLoaded(): void {
- director.loadScene("Main");
- platform.reportThinkingOnce('gotoGame');
- }
- }
|