LoginData.ts 7.1 KB

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