LoginData.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. //有登录记录直接跳转到登录流程最后一步
  52. if (this.sessionKey != '' && this.uin != '') {
  53. console.log("=== login 有登录记录直接跳转到登录流程最后一步");
  54. this.getAccountInfo();
  55. return;
  56. }
  57. else {
  58. console.log("=== login 无登录记录");
  59. this.connect();
  60. }
  61. }
  62. /**
  63. * 链接
  64. * @description 获取临时的uin
  65. */
  66. private async connect() {
  67. //connect
  68. this.randomKey = mk.encrypt.randomString();
  69. let data: any = {
  70. "mac": gData.appData.mac,
  71. "psk": this.randomKey,
  72. "appId": gData.appData.appId
  73. }
  74. let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
  75. if (response.errcode != 0) {
  76. return;
  77. }
  78. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  79. console.log("[FC]connectTest decryptData", data);
  80. this.tempUin = JSON.parse(data).tmp_uin;
  81. mk.storage.setStorage(StorageKey.uin, this.tempUin);
  82. this.wxLogin();
  83. }
  84. public async wxLogin() {
  85. //wxLogin
  86. let data;
  87. let encode;
  88. if (gData.wechatData.code != null) {
  89. //微信登录
  90. encode = mk.encrypt.encrypt(JSON.stringify({ "code": gData.wechatData.code }), this.sessionKey, gData.appData.appId);
  91. data = {
  92. "uin": this.uin,
  93. "encrypt": encode
  94. };
  95. }
  96. else {
  97. //游客登录
  98. encode = mk.encrypt.encrypt(JSON.stringify({ "code": '' }), this.randomKey, gData.appData.appId);
  99. data = {
  100. "uin": this.tempUin,
  101. "encrypt": encode
  102. };
  103. }
  104. let response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  105. if (response.errcode != 0) {
  106. if (response.errcode == -10003 || response.errcode == -20003) {
  107. //清除缓存
  108. this.reload();
  109. }
  110. return;
  111. }
  112. if (gData.wechatData.code != null) {
  113. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  114. }
  115. else {
  116. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  117. }
  118. data = JSON.parse(data);
  119. this.loginTicket = data.login_ticket;
  120. console.log("[FC]wxLoginTest decrypt", data);
  121. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  122. let isFirstIn = mk.storage.getStorage(StorageKey.isFirstIn);
  123. if (isFirstIn == null) {
  124. this.isFirstIn = true;
  125. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  126. }
  127. if (this.uin != data.uin) {
  128. this.uin = data.uin;
  129. this.isNew = false;
  130. }
  131. else {
  132. this.isNew = true;
  133. }
  134. mk.storage.setStorage(StorageKey.uin, this.uin);
  135. this.checkLogin();
  136. }
  137. /**
  138. * 检测登录
  139. * @returns
  140. */
  141. private async checkLogin() {
  142. //checkLogin
  143. let tmp_key = '';
  144. if (gData.wechatData.code != null) {
  145. tmp_key = this.sessionKey;
  146. }
  147. else {
  148. tmp_key = mk.encrypt.randomString();
  149. }
  150. let data = {
  151. "tmp_key": tmp_key,
  152. "uin": this.uin,
  153. "login_ticket": this.loginTicket,
  154. "appId": gData.appData.appId
  155. }
  156. let response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  157. if (response.errcode != 0) {
  158. return;
  159. }
  160. let decryptData;
  161. if (gData.wechatData.code != null) {
  162. decryptData = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  163. }
  164. else {
  165. decryptData = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  166. }
  167. let parseData = JSON.parse(decryptData);
  168. this.sessionKey = parseData.session_key;
  169. console.log("[FC]checkLogin decrypt", parseData);
  170. mk.storage.setStorage(StorageKey.sessionKey, this.sessionKey);
  171. this.getUserInfo();
  172. }
  173. private async getUserInfo() {
  174. // if (gData.wechatData.code != null) {
  175. // if (this.isNew) {//玩过此游戏并且本地数据是本机的
  176. // this.getAccountInfo();
  177. // }
  178. // else {//玩过此游戏 但 本地数据不是本账户的 清空数据 重新登录游戏
  179. // this.isNew = false;
  180. // gData.wechatData.code = null;
  181. // this.reload();
  182. // }
  183. // }
  184. // else {
  185. // this.getAccountInfo();
  186. // }
  187. this.getAccountInfo();
  188. }
  189. private async getAccountInfo() {
  190. // let nickname = mk.storage.getStorage(StorageKey.nickname);
  191. // if (nickname) {
  192. // gData.wechatData.nickName = nickname;
  193. // }
  194. // else {
  195. let aa = {
  196. "uin": this.uin,
  197. "login_ticket": this.loginTicket
  198. };
  199. let data: any = {
  200. "uin": this.uin,
  201. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  202. };
  203. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  204. if (response.errcode != 0) {
  205. if (response.errcode == -10003 || response.errcode == -20003) {
  206. //清除缓存
  207. this.reload();
  208. }
  209. return;
  210. }
  211. response = mk.http.checkData(response, true);
  212. data = response.data;
  213. mk.console.logSingle("[FC] getAccountInfo decrypt", data);
  214. this.isAuth = data.is_auth;
  215. if (this.isAuth) {
  216. gData.wechatData.nickName = data.nickname;
  217. gData.wechatData.avatar = data.headimgurl;
  218. gData.wechatData.unionid = this.uin;
  219. gData.gameData.init_head = true;
  220. mk.storage.setStorage(StorageKey.isAuth, 1);
  221. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  222. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  223. }
  224. // }
  225. //获取游戏数据
  226. gData.gameData.init();
  227. }
  228. /**
  229. * 本地数据不是本账户的 清空数据 重新登录游戏
  230. */
  231. public reload() {
  232. // let path = cc.sys.localStorage.getItem('HotUpdateSearchPaths');
  233. // gData.storageData.clear();
  234. // cc.sys.localStorage.setItem('HotUpdateSearchPaths', path);
  235. setTimeout(() => {
  236. this.sessionKey = '';
  237. this.uin = '';
  238. this.loginTicket = '';
  239. this.isAuth = false;
  240. this.tempUin = '';
  241. this.randomKey = '';
  242. gData.wechatData.nickName = '';
  243. gData.wechatData.avatar = '';
  244. cc.sys.localStorage.removeItem(StorageKey.sessionKey);
  245. cc.sys.localStorage.removeItem(StorageKey.uin);
  246. cc.sys.localStorage.removeItem(StorageKey.loginTicket);
  247. cc.sys.localStorage.removeItem(StorageKey.isAuth);
  248. cc.sys.localStorage.removeItem(StorageKey.nickname);
  249. cc.sys.localStorage.removeItem(StorageKey.avatar);
  250. cc.audioEngine.stopAll();
  251. //调用Android的更好
  252. cc.game.restart();
  253. })
  254. }
  255. }