LoginData.ts 7.4 KB

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