LoginData.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { StorageKey } from "./StorageData";
  2. /**
  3. * @description 登录数据
  4. * @author 邹勇
  5. */
  6. export class LoginData {
  7. public loadResCompelete: boolean = false;
  8. /** 是否登录 */
  9. public isLogin: boolean = false;
  10. public sessionKey: string = '';
  11. /** 玩家唯一id */
  12. public uin: string = '';
  13. /** 玩家临时id */
  14. public tempUin: string = '';
  15. public loginTicket: string = '';
  16. public isFirstIn: boolean;
  17. public isNew: boolean;
  18. /** 是否授权 */
  19. public isAuth: boolean;
  20. /**
  21. * 初始化本地配置
  22. * @returns
  23. */
  24. public loadLocalRes() {
  25. this.loadResCompelete = true;
  26. }
  27. /**
  28. * 登录流程 initApp-->initAd-->connect-->wxLogin-->checkLogin-->getAccountInfo
  29. */
  30. public async login() {
  31. this.isLogin = true;
  32. mk.pool.init(gData.gameData.pools);
  33. gData.appData.init();
  34. gData.adData.init();
  35. //有登录记录直接跳转到登录流程最后一步
  36. if (this.sessionKey != '' && this.uin != '') {
  37. this.getAccountInfo();
  38. return;
  39. }
  40. let ranKey = mk.encrypt.randomString();
  41. let data: any = {
  42. "mac": gData.appData.mac,
  43. "psk": ranKey,
  44. "appId": gData.appData.appId
  45. }
  46. //connect
  47. let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
  48. if (response.errcode != 0) {
  49. if(response.errcode == -10003 || response.errcode == -20003){
  50. //清除缓存
  51. this.reload();
  52. }
  53. return;
  54. }
  55. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  56. this.tempUin = JSON.parse(data).tmp_uin;
  57. //wxLogin
  58. let encode;
  59. if (gData.wechatData.code != null) {//微信登录
  60. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), this.sessionKey, gData.appData.appId);
  61. data = {
  62. "uin": this.uin,
  63. "encrypt": encode
  64. };
  65. }
  66. else {//游客登录
  67. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), ranKey, gData.appData.appId);
  68. data = {
  69. "uin": this.tempUin,
  70. "encrypt": encode
  71. };
  72. }
  73. response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  74. if (response.errcode != 0) {
  75. return;
  76. }
  77. if (gData.wechatData.code != null) {
  78. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  79. }
  80. else {
  81. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  82. }
  83. data = JSON.parse(data);
  84. this.loginTicket = data.login_ticket;
  85. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  86. let isFirstIn = mk.storage.getStorage('isFirstIn');
  87. if (isFirstIn == null) {
  88. this.isFirstIn = true;
  89. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  90. }
  91. if (this.uin != data.uin) {
  92. this.uin = data.uin;
  93. this.isNew = false;
  94. }
  95. else {
  96. this.isNew = true;
  97. }
  98. mk.console.log("uin:", this.uin);
  99. mk.storage.setStorage(StorageKey.uin, this.uin);
  100. console.log('uin ', this.uin)
  101. console.log('tempUin ', this.tempUin)
  102. //checkLogin
  103. let tmp_key = '';
  104. if (gData.wechatData.code != null) {
  105. tmp_key = this.sessionKey;
  106. }
  107. else {
  108. tmp_key = mk.encrypt.randomString();
  109. }
  110. data = {
  111. "tmp_key": tmp_key,
  112. "uin": this.uin,
  113. "login_ticket": this.loginTicket,
  114. "appId": gData.appData.appId
  115. }
  116. response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  117. if (response.errcode != 0) {
  118. return;
  119. }
  120. if (gData.wechatData.code != null) {
  121. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  122. }
  123. else {
  124. data = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  125. }
  126. data = JSON.parse(data);
  127. this.sessionKey = data.session_key;
  128. mk.storage.setStorage('sessionKey', this.sessionKey);
  129. if (gData.wechatData.code != null) {
  130. if (this.isNew) {//玩过此游戏并且本地数据是本机的
  131. this.getAccountInfo();
  132. }
  133. else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
  134. this.isNew = false;
  135. gData.wechatData.code = null;
  136. this.reload();
  137. }
  138. }
  139. else {
  140. this.getAccountInfo();
  141. }
  142. }
  143. private async getAccountInfo() {
  144. let nickname = mk.storage.getStorage(StorageKey.nickname);
  145. if (nickname) {
  146. gData.wechatData.nickName = nickname;
  147. }
  148. else {
  149. let aa = {
  150. "uin": this.uin,
  151. "login_ticket": this.loginTicket
  152. };
  153. let data: any = {
  154. "uin": this.uin,
  155. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  156. };
  157. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  158. if (response.errcode != 0) {
  159. return;
  160. }
  161. response = mk.http.checkData(response, true);
  162. data = response.data;
  163. this.isAuth = data.is_auth;
  164. if (this.isAuth) {
  165. gData.wechatData.nickName = data.nickname;
  166. gData.wechatData.avatar = data.headimgurl;
  167. mk.storage.setStorage(StorageKey.isAuth, 1);
  168. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  169. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  170. }
  171. }
  172. //获取游戏数据
  173. gData.gameData.init();
  174. }
  175. /**
  176. * 本地数据不是本账户的 清空数据 重新登录游戏
  177. */
  178. public reload() {
  179. }
  180. }