LoginData.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. }
  175. return;
  176. }
  177. if (gData.wechatData.code != null) {
  178. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  179. }
  180. else {
  181. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  182. }
  183. data = JSON.parse(data);
  184. this.loginTicket = data.login_ticket;
  185. mk.console.logSingle("[FC]wxLogin decrypt", data);
  186. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  187. let isFirstIn = mk.storage.getStorage(StorageKey.isFirstIn);
  188. if (isFirstIn == null) {
  189. this.isFirstIn = true;
  190. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  191. }
  192. if (this.uin != data.uin) {
  193. this.uin = data.uin;
  194. this.isNew = false;
  195. }
  196. else {
  197. this.isNew = true;
  198. }
  199. mk.storage.setStorage(StorageKey.uin, this.uin);
  200. this.checkLogin();
  201. }
  202. /**
  203. * 检测登录
  204. * @returns
  205. */
  206. private async checkLogin() {
  207. //checkLogin
  208. let tmp_key = '';
  209. if (gData.wechatData.code != null) {
  210. tmp_key = this.sessionKey;
  211. }
  212. else {
  213. tmp_key = mk.encrypt.randomString();
  214. }
  215. let data = {
  216. "tmp_key": tmp_key,
  217. "uin": this.uin,
  218. "login_ticket": this.loginTicket,
  219. "appId": gData.appData.appId
  220. }
  221. let response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  222. if (response.errcode != 0) {
  223. if (response.errcode == 403) {
  224. console.log('block 403');
  225. gData.warnTipData.setSolution(4);
  226. }
  227. return;
  228. }
  229. let decryptData;
  230. if (gData.wechatData.code != null) {
  231. decryptData = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  232. }
  233. else {
  234. decryptData = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  235. }
  236. let parseData = JSON.parse(decryptData);
  237. this.sessionKey = parseData.session_key;
  238. mk.console.logSingle("[FC]checkLogin decrypt", parseData);
  239. console.log("this.sessionKey ", this.sessionKey);
  240. mk.storage.setStorage(StorageKey.sessionKey, this.sessionKey);
  241. if (gData.wechatData.code != null) {
  242. if (this.isNew) {
  243. console.log('KKKK 1 ', this.uin)
  244. this.getUserInfo();
  245. }
  246. else {
  247. console.log('KKKK 2 ', this.uin)
  248. this.isNew = false;
  249. gData.wechatData.code = null;
  250. cc.audioEngine.stopAll();
  251. //调用Android的更好
  252. cc.game.restart();
  253. }
  254. }
  255. else {
  256. console.log('KKKK 3 ', this.uin)
  257. this.getUserInfo();
  258. }
  259. }
  260. private async getUserInfo() {
  261. this.getAccountInfo();
  262. }
  263. private async getAccountInfo() {
  264. console.log('uin ', gData.loginData.uin)
  265. //更新设备信息
  266. mk.console.logSingle("machineInfo", gData.appData.machineInfo);
  267. await mk.http.sendData('user/machine', gData.appData.machineInfo);
  268. //上传appVersion
  269. let data1 = { 'version': gData.appData.appVersion };
  270. mk.http.sendData('updateVersion', data1);
  271. //获取user
  272. let aa = {
  273. "uin": this.uin,
  274. "login_ticket": this.loginTicket,
  275. "szlm_did": mk.shuzilmM.szlm_did,
  276. "black_box": mk.tongdunM.tdEquipID,
  277. "mac": gData.appData.machineInfo.mac,
  278. "appVersion": gData.appData.appVersion
  279. };
  280. let data = {
  281. "uin": this.uin,
  282. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  283. };
  284. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  285. if (response.errcode != 0) {
  286. if (response.errcode == 41003) {
  287. mk.storage.setStorage("alifengkong", 1);
  288. console.log('==== ali block')
  289. }
  290. else if (response.errcode == 41004) {
  291. mk.storage.setStorage("alifengkong", 2);
  292. console.log('==== shuzilm set block')
  293. }
  294. else if (response.errcode == 41005) {
  295. mk.storage.setStorage("alifengkong", 3);
  296. console.log('==== tongdun set block')
  297. }
  298. else if (response.errcode == -10003 || response.errcode == -20003) {
  299. //清除缓存
  300. console.log('AA restart errcode ', response.errcode)
  301. this.reload();
  302. }
  303. return;
  304. }
  305. response = mk.http.checkData(response, true);
  306. let resData = response.data;
  307. mk.console.logSingle("getAccountInfo user", resData);
  308. this.isAuth = resData.is_auth;
  309. gData.wechatData.unionid = this.uin;
  310. if (this.isAuth) {
  311. gData.wechatData.nickName = resData.nickname;
  312. gData.wechatData.avatar = resData.headimgurl;
  313. gData.gameData.init_head = true;
  314. mk.storage.setStorage(StorageKey.isAuth, 1);
  315. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  316. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  317. }
  318. //获取游戏数据
  319. gData.gameData.init();
  320. }
  321. public async getUserChannel(isDoIdentify = true) {
  322. let data = {};
  323. let response = await mk.http.sendData('user/income-strategy', data);
  324. mk.console.logSingle('getUserChannel=>', response);
  325. if (response.errcode != 0) {
  326. if (isDoIdentify) {
  327. this.passAnti = true;
  328. }
  329. return;
  330. }
  331. let index = response.data.tfChannel;
  332. this.isMatched = response.data.isMatched;
  333. this.toponStr = response.data.strategyContent;
  334. //初始化topon
  335. mk.ad.init();
  336. console.log(`userChannel=========${index}`);
  337. console.log(`isMatched=========${this.isMatched}`);
  338. console.log(`toponStr=========${this.toponStr}`);
  339. mk.data.setTAEventUserStr(0, 'kncdsf_zhubao_qudaopipei', `${index}_${this.isMatched}`);
  340. if (isDoIdentify) {
  341. switch (index) {
  342. case 1:
  343. this.userChannel = 0;
  344. break;
  345. case 24:
  346. this.userChannel = 1;
  347. break;
  348. case 4:
  349. this.userChannel = 2;
  350. break;
  351. case 5:
  352. this.userChannel = 3;
  353. break;
  354. default:
  355. this.userChannel = 2;
  356. break;
  357. }
  358. let str = this.popIdentifySwitch[this.userChannel].split(",");
  359. if (str[1 - this.isMatched] == "1") {
  360. JsbSystem.ysdkInit();
  361. JsbSystem.autologin();
  362. mk.data.sendDataEvent(DataEventId.realName, `实名认证验证人数`);
  363. } else {
  364. this.passAnti = true;
  365. }
  366. }
  367. }
  368. /**
  369. * 本地数据不是本账户的 清空数据 重新登录游戏
  370. */
  371. public reload() {
  372. setTimeout(() => {
  373. this.sessionKey = '';
  374. this.uin = '';
  375. this.loginTicket = '';
  376. this.isAuth = false;
  377. this.tempUin = '';
  378. this.randomKey = '';
  379. gData.wechatData.nickName = '';
  380. gData.wechatData.avatar = '';
  381. cc.sys.localStorage.removeItem(StorageKey.sessionKey);
  382. cc.sys.localStorage.removeItem(StorageKey.uin);
  383. cc.sys.localStorage.removeItem(StorageKey.loginTicket);
  384. cc.sys.localStorage.removeItem(StorageKey.isAuth);
  385. cc.sys.localStorage.removeItem(StorageKey.nickname);
  386. cc.sys.localStorage.removeItem(StorageKey.avatar);
  387. cc.audioEngine.stopAll();
  388. //调用Android的更好
  389. cc.game.restart();
  390. }, 500)
  391. }
  392. }