LoginData.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.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. };
  278. let data = {
  279. "uin": this.uin,
  280. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  281. };
  282. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  283. if (response.errcode != 0) {
  284. if (response.errcode == 41003) {
  285. mk.storage.setStorage("alifengkong", 1);
  286. console.log('==== ali block')
  287. }
  288. else if (response.errcode == 41004) {
  289. mk.storage.setStorage("alifengkong", 2);
  290. console.log('==== shuzilm set block')
  291. }
  292. else if (response.errcode == 41005) {
  293. mk.storage.setStorage("alifengkong", 3);
  294. console.log('==== tongdun set block')
  295. }
  296. else if (response.errcode == -10003 || response.errcode == -20003) {
  297. //清除缓存
  298. console.log('AA restart errcode ', response.errcode)
  299. this.reload();
  300. }
  301. return;
  302. }
  303. response = mk.http.checkData(response, true);
  304. let resData = response.data;
  305. mk.console.logSingle("getAccountInfo user", resData);
  306. this.isAuth = resData.is_auth;
  307. gData.wechatData.unionid = this.uin;
  308. if (this.isAuth) {
  309. gData.wechatData.nickName = resData.nickname;
  310. gData.wechatData.avatar = resData.headimgurl;
  311. gData.gameData.init_head = true;
  312. mk.storage.setStorage(StorageKey.isAuth, 1);
  313. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  314. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  315. }
  316. //获取游戏数据
  317. gData.gameData.init();
  318. }
  319. public async getUserChannel(isDoIdentify = true) {
  320. let data = {};
  321. let response = await mk.http.sendData('user/income-strategy', data);
  322. mk.console.logSingle('getUserChannel=>', response);
  323. if (response.errcode != 0) {
  324. if (isDoIdentify) {
  325. this.passAnti = true;
  326. }
  327. return;
  328. }
  329. let index = response.data.tfChannel;
  330. this.isMatched = response.data.isMatched;
  331. this.toponStr = response.data.strategyContent;
  332. //初始化topon
  333. mk.ad.init();
  334. console.log(`userChannel=========${index}`);
  335. console.log(`isMatched=========${this.isMatched}`);
  336. console.log(`toponStr=========${this.toponStr}`);
  337. mk.data.setTAEventUserStr(0, 'kncdsf_zhubao_qudaopipei', `${index}_${this.isMatched}`);
  338. if (isDoIdentify) {
  339. switch (index) {
  340. case 1:
  341. this.userChannel = 0;
  342. break;
  343. case 24:
  344. this.userChannel = 1;
  345. break;
  346. case 4:
  347. this.userChannel = 2;
  348. break;
  349. case 5:
  350. this.userChannel = 3;
  351. break;
  352. default:
  353. this.userChannel = 2;
  354. break;
  355. }
  356. let str = this.popIdentifySwitch[this.userChannel].split(",");
  357. if (str[1 - this.isMatched] == "1") {
  358. JsbSystem.ysdkInit();
  359. JsbSystem.autologin();
  360. mk.data.sendDataEvent(DataEventId.realName, `实名认证验证人数`);
  361. } else {
  362. this.passAnti = true;
  363. }
  364. }
  365. }
  366. /**
  367. * 本地数据不是本账户的 清空数据 重新登录游戏
  368. */
  369. public reload() {
  370. setTimeout(() => {
  371. this.sessionKey = '';
  372. this.uin = '';
  373. this.loginTicket = '';
  374. this.isAuth = false;
  375. this.tempUin = '';
  376. this.randomKey = '';
  377. gData.wechatData.nickName = '';
  378. gData.wechatData.avatar = '';
  379. cc.sys.localStorage.removeItem(StorageKey.sessionKey);
  380. cc.sys.localStorage.removeItem(StorageKey.uin);
  381. cc.sys.localStorage.removeItem(StorageKey.loginTicket);
  382. cc.sys.localStorage.removeItem(StorageKey.isAuth);
  383. cc.sys.localStorage.removeItem(StorageKey.nickname);
  384. cc.sys.localStorage.removeItem(StorageKey.avatar);
  385. cc.audioEngine.stopAll();
  386. //调用Android的更好
  387. cc.game.restart();
  388. }, 500)
  389. }
  390. }