LoginData.ts 12 KB

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