LoginData.ts 13 KB

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