LoginData.ts 12 KB

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