LoginData.ts 9.7 KB

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