LoginData.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. }
  120. // mk.console.logSingle('wxlogin aa >> ', aa);
  121. // console.log('wxLogin code ', aa.code)
  122. // console.log('wxLogin clientType ', aa.clientType)
  123. // console.log('wxLogin deviceToken ', aa.deviceToken)
  124. // console.log('wxLogin appVersion ', aa.appVersion)
  125. // console.log('wxLogin tfChannel ', aa.tfChannel)
  126. // console.log('wxLogin android_id ', aa.android_id)
  127. // console.log('wxLogin versioncfg ', aa.versioncfg)
  128. // console.log('wxLogin idfa ', aa.idfa)
  129. // console.log('wxLogin imei ', aa.imei)
  130. // console.log('wxLogin mac ', aa.mac)
  131. // console.log('wxLogin oaid ', aa.oaid)
  132. let data;
  133. let encode;
  134. if (gData.wechatData.code != null) {
  135. //微信登录
  136. encode = mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId);
  137. data = {
  138. "uin": this.uin,
  139. "encrypt": encode
  140. };
  141. }
  142. else {
  143. //游客登录
  144. encode = mk.encrypt.encrypt(JSON.stringify(aa), this.randomKey, gData.appData.appId);
  145. data = {
  146. "uin": this.tempUin,
  147. "encrypt": encode
  148. };
  149. }
  150. let response = await mk.http.sendRequest('wxlogin', 'POST', JSON.stringify(data), false, false);
  151. if (response.errcode != 0) {
  152. if (response.errcode == 41003) {
  153. mk.storage.setStorage("alifengkong", 1);
  154. console.log('ali block')
  155. }
  156. else if (response.errcode == -10003 || response.errcode == -20003) {
  157. //清除缓存
  158. console.log('restart errcode ', response.errcode)
  159. this.reload();
  160. }
  161. return;
  162. }
  163. if (gData.wechatData.code != null) {
  164. data = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  165. }
  166. else {
  167. data = mk.encrypt.decrypt(response.encrypt, this.randomKey, gData.appData.appId);
  168. }
  169. data = JSON.parse(data);
  170. this.loginTicket = data.login_ticket;
  171. mk.console.logSingle("[FC]wxLogin decrypt", data);
  172. mk.storage.setStorage(StorageKey.loginTicket, this.loginTicket);
  173. let isFirstIn = mk.storage.getStorage(StorageKey.isFirstIn);
  174. if (isFirstIn == null) {
  175. this.isFirstIn = true;
  176. mk.storage.setStorage(StorageKey.isFirstIn, 1);
  177. }
  178. if (this.uin != data.uin) {
  179. this.uin = data.uin;
  180. this.isNew = false;
  181. }
  182. else {
  183. this.isNew = true;
  184. }
  185. mk.storage.setStorage(StorageKey.uin, this.uin);
  186. this.checkLogin();
  187. }
  188. /**
  189. * 检测登录
  190. * @returns
  191. */
  192. private async checkLogin() {
  193. //checkLogin
  194. let tmp_key = '';
  195. if (gData.wechatData.code != null) {
  196. tmp_key = this.sessionKey;
  197. }
  198. else {
  199. tmp_key = mk.encrypt.randomString();
  200. }
  201. let data = {
  202. "tmp_key": tmp_key,
  203. "uin": this.uin,
  204. "login_ticket": this.loginTicket,
  205. "appId": gData.appData.appId
  206. }
  207. let response = await mk.http.sendRequest('checklogin', 'POST', JSON.stringify(data));
  208. if (response.errcode != 0) {
  209. if (response.errcode == 403) {
  210. gData.warnTipData.setSolution(4);
  211. }
  212. return;
  213. }
  214. let decryptData;
  215. if (gData.wechatData.code != null) {
  216. decryptData = mk.encrypt.decrypt(response.encrypt, this.sessionKey, gData.appData.appId);
  217. }
  218. else {
  219. decryptData = mk.encrypt.decrypt(response.encrypt, tmp_key, gData.appData.appId);
  220. }
  221. let parseData = JSON.parse(decryptData);
  222. this.sessionKey = parseData.session_key;
  223. mk.console.logSingle("[FC]checkLogin decrypt", parseData);
  224. mk.storage.setStorage(StorageKey.sessionKey, this.sessionKey);
  225. this.getUserInfo();
  226. }
  227. private async getUserInfo() {
  228. this.getAccountInfo();
  229. }
  230. private async getAccountInfo() {
  231. console.log('uin ', gData.loginData.uin)
  232. //更新设备信息
  233. mk.console.logSingle("machineInfo", gData.appData.machineInfo);
  234. await mk.http.sendData('user/machine', gData.appData.machineInfo);
  235. //上传appVersion
  236. let data1 = { 'version': gData.appData.appVersion };
  237. mk.http.sendData('updateVersion', data1);
  238. //获取user
  239. let aa = {
  240. "uin": this.uin,
  241. "login_ticket": this.loginTicket
  242. };
  243. let data = {
  244. "uin": this.uin,
  245. "encrypt": mk.encrypt.encrypt(JSON.stringify(aa), this.sessionKey, gData.appData.appId)
  246. };
  247. let response = await mk.http.sendRequest('user', 'POST', JSON.stringify(data), false, false);
  248. if (response.errcode != 0) {
  249. if (response.errcode == -10003 || response.errcode == -20003) {
  250. //清除缓存
  251. console.log('AA restart errcode ', response.errcode)
  252. this.reload();
  253. }
  254. return;
  255. }
  256. response = mk.http.checkData(response, true);
  257. let resData = response.data;
  258. mk.console.logSingle("getAccountInfo user", resData);
  259. this.isAuth = resData.is_auth;
  260. gData.wechatData.unionid = this.uin;
  261. if (this.isAuth) {
  262. gData.wechatData.nickName = resData.nickname;
  263. gData.wechatData.avatar = resData.headimgurl;
  264. gData.gameData.init_head = true;
  265. mk.storage.setStorage(StorageKey.isAuth, 1);
  266. mk.storage.setStorage(StorageKey.nickname, gData.wechatData.nickName, false);
  267. mk.storage.setStorage(StorageKey.avatar, gData.wechatData.avatar, false);
  268. }
  269. //获取游戏数据
  270. gData.gameData.init();
  271. }
  272. public async getUserChannel(isDoIdentify = true) {
  273. let data = {};
  274. let response = await mk.http.sendData('user/getUserChannel', data);
  275. mk.console.logSingle('getUserChannel=>', response);
  276. if (response.errcode != 0) {
  277. if(isDoIdentify)
  278. {
  279. this.passAnti = true;
  280. }
  281. return;
  282. }
  283. this.userChannel = response.data.channel;
  284. if(isDoIdentify)
  285. {
  286. if(this.userChannel <= 5)
  287. {
  288. if(this.popIdentifySwitch[this.userChannel - 1] == "1")
  289. {
  290. JsbSystem.ysdkInit();
  291. JsbSystem.autologin();
  292. mk.data.sendDataEvent(DataEventId.realName, `实名认证验证人数`);
  293. }else{
  294. this.passAnti = true;
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * 本地数据不是本账户的 清空数据 重新登录游戏
  301. */
  302. public reload() {
  303. setTimeout(() => {
  304. this.sessionKey = '';
  305. this.uin = '';
  306. this.loginTicket = '';
  307. this.isAuth = false;
  308. this.tempUin = '';
  309. this.randomKey = '';
  310. gData.wechatData.nickName = '';
  311. gData.wechatData.avatar = '';
  312. cc.sys.localStorage.removeItem(StorageKey.sessionKey);
  313. cc.sys.localStorage.removeItem(StorageKey.uin);
  314. cc.sys.localStorage.removeItem(StorageKey.loginTicket);
  315. cc.sys.localStorage.removeItem(StorageKey.isAuth);
  316. cc.sys.localStorage.removeItem(StorageKey.nickname);
  317. cc.sys.localStorage.removeItem(StorageKey.avatar);
  318. cc.audioEngine.stopAll();
  319. //调用Android的更好
  320. cc.game.restart();
  321. }, 500)
  322. }
  323. }