LoginData.ts 9.8 KB

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