| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import { StorageKey } from "./StorageData";
- /**
- * @description 登录数据
- * @author 邹勇
- */
- export class LoginData {
- public loadResCompelete:boolean = false;
- /** 是否登录 */
- public isLogin: boolean = false;
- public sessionKey: string = '';
- /** 玩家唯一id */
- public uin: string = '';
- /** 玩家临时id */
- public tempUin: string = '';
- public loginTicket: string = '';
- public isFirstIn: boolean;
- public isNew: boolean;
- /** 是否授权 */
- public isAuth: boolean;
- /**
- * 初始化本地配置
- * @returns
- */
- public loadLocalRes(){
-
- this.loadResCompelete = true;
- }
- /**
- * 登录流程 initApp-->initAd-->connect-->wxLogin-->checkLogin-->getAccountInfo
- */
- public async login() {
- this.isLogin = true;
- gData.appData.init();
- gData.adData.init();
- //有登录记录直接跳转到登录流程最后一步
- if (this.sessionKey != '' && this.uin != '') {
- this.getAccountInfo();
- return;
- }
- let ranKey = mk.encrypt.randomString();
- let data: any = {
- "mac": gData.appData.mac,
- "psk": ranKey,
- "appId": gData.appData.appId
- }
- //connect
- let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
- if (response.errcode != 0) {
- return;
- }
- data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
- this.tempUin = JSON.parse(data).tmp_uin;
- //wxLogin
- let encode;
- if (gData.wechatData.code != null) {//微信登录
- encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), this.sessionKey, gData.appData.appId);
- data = {
- "uin": this.uin,
- "encrypt": encode
- };
- }
- else {//游客登录
- encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), ranKey, gData.appData.appId);
- data = {
- "uin": this.tempUin,
- "encrypt": encode
- };
- }
- response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
- if (response.errcode != 0) {
- return;
- }
- if (gData.wechatData.code != null) {
- data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
- }
- else {
- data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
- }
- data = JSON.parse(data);
- this.loginTicket = data.login_ticket;
- mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
- let isFirstIn = mk.storage.getStorage('isFirstIn');
- if (isFirstIn == null) {
- this.isFirstIn = true;
- mk.storage.setStorage(StorageKey.isFirstIn, 1);
- }
- if (this.uin != data.uin) {
- this.uin = data.uin;
- this.isNew = false;
- }
- else {
- this.isNew = true;
- }
- mk.storage.setStorage(StorageKey.uin, this.uin);
- //checkLogin
- let tmp_key = '';
- if (gData.wechatData.code != null) {
- tmp_key = this.sessionKey;
- }
- else {
- tmp_key = mk.encrypt.randomString();
- }
- data = {
- "tmp_key": tmp_key,
- "uin": this.uin,
- "login_ticket": this.loginTicket,
- "appId": gData.appData.appId
- }
- response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
- if (response.errcode != 0) {
- return;
- }
- if (gData.wechatData.code != null) {
- data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
- }
- else {
- data = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
- }
- data = JSON.parse(data);
- this.sessionKey = data.session_key;
- mk.storage.setStorage('sessionKey', this.sessionKey);
- if (gData.wechatData.code != null) {
- if (this.isNew) {//玩过此游戏并且本地数据是本机的
- this.getAccountInfo();
- }
- else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
- this.isNew = false;
- gData.wechatData.code = null;
- this.reload();
- }
- }
- else {
- this.getAccountInfo();
- }
- }
- private async getAccountInfo() {
- let nickname = mk.storage.getStorage(StorageKey.nickname);
- if (nickname) {
- gData.wechatData.nickName = nickname;
- }
- else {
- let aa = {
- "uin": this.uin,
- "login_ticket": this.loginTicket
- };
- let data: any = {
- "uin": this.uin,
- "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
- };
- let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
- if (response.errcode != 0) {
- return;
- }
- response = mk.http.checkData(response, true);
- data = response.data;
- this.isAuth = data.is_auth;
- if (this.isAuth) {
- gData.wechatData.nickName = data.nickname;
- gData.wechatData.avatar = data.headimgurl;
- mk.storage.setStorage(StorageKey.isAuth, 1);
- mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
- mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
- }
- }
- //获取游戏数据
- gData.gameData.init();
- }
- /**
- * 本地数据不是本账户的 清空数据 重新登录游戏
- */
- private reload() {
- }
- }
|