LoginData.ts 6.1 KB

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