LoginData.ts 7.7 KB

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