LoginData.ts 9.4 KB

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