LoginData.ts 8.5 KB

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