LoginData.ts 7.5 KB

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