LoginData.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import JsbSystem from "../../mk/system/JsbSystem";
  2. import { DataEventId } from "./GameData";
  3. import { StorageKey } from "./StorageData";
  4. /**
  5. * @description 登录数据
  6. * @author 邹勇
  7. */
  8. export class LoginData {
  9. public loadResCompelete: boolean = false;
  10. /** 是否登录 */
  11. public isLogin: boolean = false;
  12. public sessionKey: string = '';
  13. public randomKey: string = "abcdefghijklopqrstuvwxyzabcdefgh";
  14. /** 玩家唯一id */
  15. public uin: string = '';
  16. /** 玩家临时id */
  17. public tempUin: string = '';
  18. public loginTicket: string = '';
  19. public isFirstIn: boolean = false;
  20. public isNew: boolean;
  21. /** 是否授权 */
  22. public isAuth: boolean;
  23. /** 服务协议 */
  24. public rule_data: string[];
  25. /** 隐私协议 */
  26. public privacy_data: string[];
  27. /** 身份认证开关 */
  28. public popIdentifySwitch: string[] = [];
  29. /** 身份认证通过 */
  30. public passAnti = false;
  31. public passPrivacy = false;
  32. public userChannel = 1;
  33. public isMatched = 1;
  34. public toponStr = '';
  35. public curTime = 0;
  36. /**
  37. * 初始化本地配置
  38. * @returns
  39. */
  40. public async loadLocalRes() {
  41. let result = await mk.loader.load("data/rule_data", cc.JsonAsset);
  42. if (result && result.json) {
  43. this.rule_data = result.json.server;
  44. this.privacy_data = result.json.privacy;
  45. this.loadResCompelete = true;
  46. }
  47. }
  48. /**
  49. * 初始化
  50. */
  51. public init() {
  52. let uin = mk.storage.getStorage(StorageKey.uin);
  53. this.uin = uin ? uin : "";
  54. let sessionKey = mk.storage.getStorage(StorageKey.sessionKey);
  55. this.sessionKey = sessionKey ? sessionKey : "";
  56. let loginTicket = mk.storage.getStorage(StorageKey.loginTicket);
  57. this.loginTicket = loginTicket ? loginTicket : "";
  58. // console.log(`===[LoginData] uin =${uin} sessionKey = ${sessionKey}"`);
  59. }
  60. /**
  61. * 登录流程
  62. * @description initApp-->initAd-->connect-->wxLogin-->checkLogin-->getAccountInfo
  63. */
  64. public async login() {
  65. mk.console.logSingle("login...", this.sessionKey + "..." + this.uin);
  66. //有登录记录直接跳转到登录流程最后一步
  67. if (this.sessionKey != '' && this.uin != '' && this.loginTicket != '') {
  68. console.log("=== login 有登录记录直接跳转到登录流程最后一步");
  69. // this.getAccountInfo();
  70. this.checkLogin();
  71. }
  72. else {
  73. console.log("=== login 无登录记录");
  74. this.connect();
  75. }
  76. }
  77. /**
  78. * 链接
  79. * @description 获取临时的uin
  80. */
  81. private async connect() {
  82. //connect
  83. this.randomKey = mk.encrypt.randomString();
  84. let data: any = {
  85. "mac": gData.appData.mac,
  86. "psk": this.randomKey,
  87. "appId": gData.appData.appId
  88. }
  89. console.log('==mac ', gData.appData.mac);
  90. let response = await mk.http.sendRequest('connectTest', 'POST', JSON.stringify(data));
  91. if (response.errcode != 0) {
  92. return;
  93. }
  94. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  95. mk.console.logSingle("[FC]connectTest decryptData", data);
  96. this.tempUin = JSON.parse(data).tmp_uin;
  97. mk.storage.setStorage(StorageKey.uin, this.tempUin);
  98. this.wxLogin();
  99. }
  100. public async wxLogin() {
  101. //wxLogin
  102. let clientType = 3
  103. if (cc.sys.os == cc.sys.OS_ANDROID) {
  104. clientType = 1
  105. }
  106. else if (cc.sys.os == cc.sys.OS_IOS) {
  107. clientType = 2
  108. }
  109. let aa =
  110. {
  111. "code": gData.wechatData.code,
  112. "clientType": clientType,
  113. "deviceToken": mk.aliRiskManager.deviceToken,
  114. "appVersion": gData.appData.appVersion,
  115. "tfChannel": gData.appData.tfChannel,
  116. "android_id": gData.appData.machineInfo.android_id,
  117. "versioncfg": gData.appData.version,
  118. "idfa": gData.appData.machineInfo.idfa,
  119. "imei": gData.appData.machineInfo.imei,
  120. "mac": gData.appData.machineInfo.mac,
  121. "oaid": gData.appData.machineInfo.oaid,
  122. "szlm_did": mk.shuzilmM.szlm_did,
  123. "black_box": mk.tongdunM.tdEquipID,
  124. "inviteUin": ''
  125. }
  126. // mk.console.logSingle('wxlogin aa >> ', aa);
  127. // console.log('wxLogin code ', aa.code)
  128. // console.log('wxLogin clientType ', aa.clientType)
  129. // console.log('wxLogin deviceToken ', aa.deviceToken)
  130. // console.log('wxLogin appVersion ', aa.appVersion)
  131. // console.log('wxLogin tfChannel ', aa.tfChannel)
  132. // console.log('wxLogin android_id ', aa.android_id)
  133. // console.log('wxLogin versioncfg ', aa.versioncfg)
  134. // console.log('wxLogin idfa ', aa.idfa)
  135. // console.log('wxLogin imei ', aa.imei)
  136. // console.log('wxLogin mac ', aa.mac)
  137. // console.log('wxLogin oaid ', aa.oaid)
  138. let data;
  139. let encode;
  140. if (gData.wechatData.code != null) {
  141. //微信登录
  142. encode = mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId);
  143. data = {
  144. "uin": this.uin,
  145. "encrypt": encode
  146. };
  147. }
  148. else {
  149. //游客登录
  150. encode = mk.encrypt.encrypt(JSON.stringify(aa), this.randomKey, gData.appData.appId);
  151. data = {
  152. "uin": this.tempUin,
  153. "encrypt": encode
  154. };
  155. }
  156. let response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  157. if (response.errcode != 0) {
  158. if (response.errcode == 41003) {
  159. mk.storage.setStorage("alifengkong", 1);
  160. console.log('==== ali block')
  161. }
  162. else if (response.errcode == 41004) {
  163. mk.storage.setStorage("alifengkong", 2);
  164. console.log('==== shuzilm set block')
  165. }
  166. else if (response.errcode == 41005) {
  167. mk.storage.setStorage("alifengkong", 3);
  168. console.log('==== tongdun set block')
  169. }
  170. else if (response.errcode == -10003 || response.errcode == -20003) {
  171. //清除缓存
  172. console.log('restart errcode ', response.errcode)
  173. this.reload();
  174. } else if (response.errcode == 403) {
  175. console.log('block 403');
  176. gData.warnTipData.setSolution(4);
  177. }
  178. return;
  179. }
  180. if (gData.wechatData.code != null) {
  181. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  182. }
  183. else {
  184. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  185. }
  186. data = JSON.parse(data);
  187. this.loginTicket = data.login_ticket;
  188. mk.console.logSingle("[FC]wxLogin decrypt", data);
  189. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  190. let isFirstIn = mk.storage.getStorage(StorageKey.isFirstIn);
  191. if (isFirstIn == null) {
  192. this.isFirstIn = true;
  193. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  194. }
  195. if (this.uin != data.uin) {
  196. this.uin = data.uin;
  197. this.isNew = false;
  198. }
  199. else {
  200. this.isNew = true;
  201. }
  202. mk.storage.setStorage(StorageKey.uin, this.uin);
  203. this.checkLogin();
  204. }
  205. /**
  206. * 检测登录
  207. * @returns
  208. */
  209. private async checkLogin() {
  210. //checkLogin
  211. let tmp_key = '';
  212. if (gData.wechatData.code != null) {
  213. tmp_key = this.sessionKey;
  214. }
  215. else {
  216. tmp_key = mk.encrypt.randomString();
  217. }
  218. let data = {
  219. "tmp_key": tmp_key,
  220. "uin": this.uin,
  221. "login_ticket": this.loginTicket,
  222. "appId": gData.appData.appId
  223. }
  224. let response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  225. if (response.errcode != 0) {
  226. if (response.errcode == 403) {
  227. console.log('block 403');
  228. gData.warnTipData.setSolution(4);
  229. }
  230. return;
  231. }
  232. let decryptData;
  233. if (gData.wechatData.code != null) {
  234. decryptData = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  235. }
  236. else {
  237. decryptData = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  238. }
  239. let parseData = JSON.parse(decryptData);
  240. this.sessionKey = parseData.session_key;
  241. mk.console.logSingle("[FC]checkLogin decrypt", parseData);
  242. console.log("this.sessionKey ", this.sessionKey);
  243. mk.storage.setStorage(StorageKey.sessionKey, this.sessionKey);
  244. if (gData.wechatData.code != null) {
  245. if (this.isNew) {
  246. console.log('KKKK 1 ', this.uin)
  247. this.getUserInfo();
  248. }
  249. else {
  250. console.log('KKKK 2 ', this.uin)
  251. this.isNew = false;
  252. gData.wechatData.code = null;
  253. cc.audioEngine.stopAll();
  254. //调用Android的更好
  255. cc.game.restart();
  256. }
  257. }
  258. else {
  259. console.log('KKKK 3 ', this.uin)
  260. this.getUserInfo();
  261. }
  262. }
  263. private async getUserInfo() {
  264. this.getAccountInfo();
  265. }
  266. private async getAccountInfo() {
  267. console.log('uin ', gData.loginData.uin)
  268. //更新设备信息
  269. mk.console.logSingle("machineInfo", gData.appData.machineInfo);
  270. await mk.http.sendData('user/machine', gData.appData.machineInfo);
  271. //上传appVersion
  272. let data1 = { 'version': gData.appData.appVersion };
  273. mk.http.sendData('updateVersion', data1);
  274. //获取user
  275. let aa = {
  276. "uin": this.uin,
  277. "login_ticket": this.loginTicket,
  278. "szlm_did": mk.shuzilmM.szlm_did,
  279. "black_box": mk.tongdunM.tdEquipID,
  280. "mac": gData.appData.machineInfo.mac,
  281. "appVersion": gData.appData.appVersion,
  282. "tfChannel": gData.appData.tfChannel,
  283. };
  284. let data = {
  285. "uin": this.uin,
  286. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  287. };
  288. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  289. if (response.errcode != 0) {
  290. if (response.errcode == 41003) {
  291. mk.storage.setStorage("alifengkong", 1);
  292. console.log('==== ali block')
  293. }
  294. else if (response.errcode == 41004) {
  295. mk.storage.setStorage("alifengkong", 2);
  296. console.log('==== shuzilm set block')
  297. }
  298. else if (response.errcode == 41005) {
  299. mk.storage.setStorage("alifengkong", 3);
  300. console.log('==== tongdun set block')
  301. }
  302. else if (response.errcode == -10003 || response.errcode == -20003) {
  303. //清除缓存
  304. console.log('AA restart errcode ', response.errcode)
  305. this.reload();
  306. }
  307. return;
  308. }
  309. response = mk.http.checkData(response, true);
  310. let resData = response.data;
  311. mk.console.logSingle("getAccountInfo user", resData);
  312. this.isAuth = resData.is_auth;
  313. gData.wechatData.unionid = this.uin;
  314. if (this.isAuth) {
  315. gData.wechatData.nickName = resData.nickname;
  316. gData.wechatData.avatar = resData.headimgurl;
  317. gData.gameData.init_head = true;
  318. mk.storage.setStorage(StorageKey.isAuth, 1);
  319. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  320. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  321. }
  322. //获取游戏数据
  323. gData.gameData.init();
  324. }
  325. public async getUserChannel(isDoIdentify = true) {
  326. let data = {};
  327. let response = await mk.http.sendData('user/income-strategy', data);
  328. mk.console.logSingle('getUserChannel=>', response);
  329. if (response.errcode != 0) {
  330. if (isDoIdentify) {
  331. this.passAnti = true;
  332. }
  333. //初始化topon
  334. mk.ad.init();
  335. }
  336. else {
  337. let index = response.data.tfChannel;
  338. this.isMatched = response.data.isMatched;
  339. this.toponStr = response.data.strategyContent;
  340. //初始化topon
  341. mk.ad.init();
  342. console.log(`userChannel=========${index}`);
  343. console.log(`isMatched=========${this.isMatched}`);
  344. console.log(`toponStr=========${this.toponStr}`);
  345. mk.data.setTAEventUserStr(0, 'kncdsf_zhubao_qudaopipei', `${index}_${this.isMatched}`);
  346. if (isDoIdentify) {
  347. switch (index) {
  348. case 1:
  349. this.userChannel = 0;
  350. break;
  351. case 24:
  352. this.userChannel = 1;
  353. break;
  354. case 4:
  355. this.userChannel = 2;
  356. break;
  357. case 5:
  358. this.userChannel = 3;
  359. break;
  360. default:
  361. this.userChannel = 2;
  362. break;
  363. }
  364. let str = this.popIdentifySwitch[this.userChannel].split(",");
  365. if (str[1 - this.isMatched] == "1") {
  366. JsbSystem.ysdkInit();
  367. JsbSystem.autologin();
  368. mk.data.sendDataEvent(DataEventId.realName, `实名认证验证人数`);
  369. } else {
  370. this.passAnti = true;
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * 本地数据不是本账户的 清空数据 重新登录游戏
  377. */
  378. public reload() {
  379. setTimeout(() => {
  380. this.sessionKey = '';
  381. this.uin = '';
  382. this.loginTicket = '';
  383. this.isAuth = false;
  384. this.tempUin = '';
  385. this.randomKey = '';
  386. gData.wechatData.nickName = '';
  387. gData.wechatData.avatar = '';
  388. cc.sys.localStorage.removeItem(StorageKey.sessionKey);
  389. cc.sys.localStorage.removeItem(StorageKey.uin);
  390. cc.sys.localStorage.removeItem(StorageKey.loginTicket);
  391. cc.sys.localStorage.removeItem(StorageKey.isAuth);
  392. cc.sys.localStorage.removeItem(StorageKey.nickname);
  393. cc.sys.localStorage.removeItem(StorageKey.avatar);
  394. cc.audioEngine.stopAll();
  395. //调用Android的更好
  396. cc.game.restart();
  397. }, 500)
  398. }
  399. }