LoginData.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return;
  50. }
  51. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  52. this.tempUin = JSON.parse(data).tmp_uin;
  53. //wxLogin
  54. let encode;
  55. if (gData.wechatData.code != null) {//微信登录
  56. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), this.sessionKey, gData.appData.appId);
  57. data = {
  58. "uin": this.uin,
  59. "encrypt": encode
  60. };
  61. }
  62. else {//游客登录
  63. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), ranKey, gData.appData.appId);
  64. data = {
  65. "uin": this.tempUin,
  66. "encrypt": encode
  67. };
  68. }
  69. response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  70. if (response.errcode != 0) {
  71. return;
  72. }
  73. if (gData.wechatData.code != null) {
  74. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  75. }
  76. else {
  77. data = mk.encrypt.decrypt(response.encrypt, ranKey, gData.appData.appId);
  78. }
  79. data = JSON.parse(data);
  80. this.loginTicket = data.login_ticket;
  81. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  82. let isFirstIn = mk.storage.getStorage('isFirstIn');
  83. if (isFirstIn == null) {
  84. this.isFirstIn = true;
  85. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  86. }
  87. if (this.uin != data.uin) {
  88. this.uin = data.uin;
  89. this.isNew = false;
  90. }
  91. else {
  92. this.isNew = true;
  93. }
  94. mk.console.log("uin:", this.uin);
  95. mk.storage.setStorage(StorageKey.uin, this.uin);
  96. //checkLogin
  97. let tmp_key = '';
  98. if (gData.wechatData.code != null) {
  99. tmp_key = this.sessionKey;
  100. }
  101. else {
  102. tmp_key = mk.encrypt.randomString();
  103. }
  104. data = {
  105. "tmp_key": tmp_key,
  106. "uin": this.uin,
  107. "login_ticket": this.loginTicket,
  108. "appId": gData.appData.appId
  109. }
  110. response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  111. if (response.errcode != 0) {
  112. return;
  113. }
  114. if (gData.wechatData.code != null) {
  115. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  116. }
  117. else {
  118. data = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  119. }
  120. data = JSON.parse(data);
  121. this.sessionKey = data.session_key;
  122. mk.storage.setStorage('sessionKey', this.sessionKey);
  123. if (gData.wechatData.code != null) {
  124. if (this.isNew) {//玩过此游戏并且本地数据是本机的
  125. this.getAccountInfo();
  126. }
  127. else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
  128. this.isNew = false;
  129. gData.wechatData.code = null;
  130. this.reload();
  131. }
  132. }
  133. else {
  134. this.getAccountInfo();
  135. }
  136. }
  137. private async getAccountInfo() {
  138. let nickname = mk.storage.getStorage(StorageKey.nickname);
  139. if (nickname) {
  140. gData.wechatData.nickName = nickname;
  141. }
  142. else {
  143. let aa = {
  144. "uin": this.uin,
  145. "login_ticket": this.loginTicket
  146. };
  147. let data: any = {
  148. "uin": this.uin,
  149. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  150. };
  151. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  152. if (response.errcode != 0) {
  153. return;
  154. }
  155. response = mk.http.checkData(response, true);
  156. data = response.data;
  157. this.isAuth = data.is_auth;
  158. if (this.isAuth) {
  159. gData.wechatData.nickName = data.nickname;
  160. gData.wechatData.avatar = data.headimgurl;
  161. mk.storage.setStorage(StorageKey.isAuth, 1);
  162. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  163. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  164. }
  165. }
  166. //获取游戏数据
  167. gData.gameData.init();
  168. }
  169. /**
  170. * 本地数据不是本账户的 清空数据 重新登录游戏
  171. */
  172. private reload() {
  173. }
  174. }