Login.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import PoolMgr, { NODEPOOLPREFABTYPE } from "../../../before/mgr/PoolMgr";
  2. import Util from "../../../before/util/Util";
  3. import JsbSystem from "../../../mk/system/JsbSystem";
  4. import { OpenActionType } from "../../../mk/system/UISystem";
  5. import { UpdateState } from "../../data/AppData";
  6. import { DataEventId } from "../../data/GameData";
  7. import HotUpdatePanel from "../hotUpdate/HotUpdatePanel";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class Login extends cc.Component {
  11. @property(cc.Sprite)
  12. private bar: cc.Sprite = null;
  13. @property({ displayName: '更新文本', type: cc.Label })
  14. private lbl_progress: cc.Label = null;
  15. @property({ displayName: '加载动画节点', type: cc.Node })
  16. private node_load: cc.Node = null;
  17. @property({ displayName: '服务协议', type: cc.Label })
  18. private lbl_server: cc.Label = null;
  19. @property({ displayName: '隐私协议', type: cc.Label })
  20. private lbl_privacy: cc.Label = null;
  21. @property({ displayName: '加载部分', type: cc.Node })
  22. private node_loadPart: cc.Node = null;
  23. @property({ displayName: '登录按钮', type: cc.Node })
  24. private node_btnLogin: cc.Node = null;
  25. /** 加载进度 */
  26. private process: number = 0;
  27. /** 热更脚本(onLoad应先于Login) */
  28. public hotUpdatePanel: HotUpdatePanel = null;
  29. private hasDeviceBackInit = false;
  30. private hasLogin = false;
  31. private hasCheckPermission = false;
  32. private hasCheckPrivacy = false;
  33. private loadSceneState = false;
  34. private coreGame = null;
  35. private isLoadComplete = false;
  36. private hasCheckChannel = false;
  37. private initShuzilm = false;
  38. //debug使用
  39. private initOrderInfo = false;
  40. onLoad() {
  41. this.lbl_progress.node.active = false;
  42. this.node_load.active = true;
  43. this.node_loadPart.active = true;
  44. this.node_btnLogin.active = false;
  45. this.setProgress(0);
  46. gData.loginData.loadLocalRes();
  47. }
  48. async start() {
  49. if (mk.aliRiskManager.ifIsBlock()) {
  50. return;
  51. }
  52. if (CC_DEBUG) {
  53. mk.aliRiskManager.getRiskCode = true;
  54. mk.tongdunM.hasGot = true;
  55. mk.shuzilmM.hasGot = true;
  56. }
  57. else {
  58. mk.aliRiskManager.init();
  59. }
  60. mk.tongdunM.tongdunInit();
  61. this.loadMain();
  62. let value = mk.storage.getStorage('loadingStart');
  63. if (!value) {
  64. mk.storage.setStorage('loadingStart', 1);
  65. mk.data.sendDataEvent(DataEventId.loading, 'loading开始');
  66. }
  67. }
  68. addTime = 0;
  69. update() {
  70. gData.loginData.curTime += 1;
  71. //隐私弹窗
  72. if (!this.hasCheckPrivacy) {
  73. this.hasCheckPrivacy = true
  74. this.checkPrivacy()
  75. }
  76. if (!gData.loginData.passPrivacy) {
  77. return;
  78. }
  79. if (!this.hasCheckPermission) {
  80. this.hasCheckPermission = true;
  81. //获取手机授权
  82. JsbSystem.checkPermission();
  83. }
  84. if (!gData.appData.getDeviceInfoCompelete) {
  85. return
  86. }
  87. if (!this.hasDeviceBackInit) {
  88. this.hasDeviceBackInit = true;
  89. this.getAppVersion();
  90. }
  91. //如果更新没有结束,就不进入正常加载流程
  92. if (gData.appData.updateState == UpdateState.ForceUpdateFinish) {
  93. gData.appData.updateState = UpdateState.CheckHot;
  94. this.checkHotUpdate();
  95. }
  96. if (gData.appData.updateState == UpdateState.IsUpdate) {
  97. this.lbl_progress.node.active = true;
  98. this.node_load.active = false;
  99. this.setProgress(gData.appData.updatePercent * 100);
  100. return;
  101. }
  102. if (gData.appData.updateState != UpdateState.UpdateFinish) {
  103. return;
  104. }
  105. if (this.addTime < 120) {
  106. this.addTime += 1;
  107. this.setProgress(this.addTime);
  108. }
  109. if (gData.loginData.loadResCompelete) {
  110. //数盟初始化
  111. if (!this.initShuzilm) {
  112. console.log(`数盟初始化========${gData.loginData.curTime}`);
  113. this.initShuzilm = true;
  114. mk.shuzilmM.init();
  115. return;
  116. }
  117. if (mk.shuzilmM.hasGot && mk.tongdunM.hasGot && mk.aliRiskManager.getRiskCode) {
  118. //开始登陆
  119. if (!this.hasLogin) {
  120. this.hasLogin = true;
  121. console.log(`开始登陆========${gData.loginData.curTime}`);
  122. this.login();
  123. }
  124. else if (gData.gameData.dataFinish) {//登录流程完毕
  125. //微信登录按钮 注释
  126. // if (gData.loginData.isAuth || CC_DEBUG) {
  127. // if (!this.loadSceneState) {
  128. // this.loadSceneState = true;
  129. // this.loadMain();
  130. // }
  131. // }
  132. // else {
  133. // this.node_loadPart.active = false;
  134. // this.node_btnLogin.active = true;
  135. // }
  136. if (!CC_DEBUG) {
  137. //检查用户来源渠道
  138. if (!this.hasCheckChannel) {
  139. console.log(`检查用户来源渠道========${gData.loginData.curTime}`);
  140. this.hasCheckChannel = true;
  141. gData.loginData.getUserChannel();
  142. }
  143. if (!gData.loginData.passAnti) {
  144. return
  145. }
  146. } else {
  147. if (!this.initOrderInfo) {
  148. this.initOrderInfo = true;
  149. gData.adData.getOrderTaskInfo(1);
  150. }
  151. }
  152. if (this.isLoadComplete) {
  153. if (this.addTime >= 120) {
  154. this.addToScene();
  155. this.isLoadComplete = false;
  156. } else {
  157. return;
  158. }
  159. } else {
  160. return;
  161. }
  162. gData.gameData.dataFinish = false;
  163. }
  164. }
  165. }
  166. }
  167. private async loadMain() {
  168. this.coreGame = await mk.loader.load('game/coregame/coregame', cc.Prefab);
  169. this.isLoadComplete = true;
  170. //this.addToScene();
  171. }
  172. private addToScene() {
  173. let mainNode = cc.find('Canvas/主容器');
  174. let node = cc.find('Canvas/主界面层');
  175. mk.ui.openPanel("game/prefab/game", OpenActionType.closeOther, node);
  176. mainNode.addChild(cc.instantiate(this.coreGame));
  177. }
  178. private checkPrivacy() {
  179. if (mk.storage.getStorage("agreementTip") != 1) {
  180. mk.ui.openPanel("module/login/agreementTip");
  181. }
  182. else {
  183. gData.loginData.passPrivacy = true;
  184. }
  185. }
  186. /** 设置进度 */
  187. private setProgress(current) {
  188. let cur = current / 120;
  189. if (this.bar) {
  190. if (cur > this.bar.fillRange) {
  191. this.bar.fillRange = cur;
  192. this.lbl_progress.string = "加载中 " + current + "%";
  193. }
  194. }
  195. }
  196. /** 获取设备信息后 */
  197. public async getAppVersion() {
  198. //获取appid 和 配置version
  199. JsbSystem.getWxAppId();
  200. //获取登录需要的sessionkey
  201. gData.loginData.init();
  202. await this.checkForceUpate();
  203. this.checkRemoveLocalHot();
  204. mk.audio.init();
  205. }
  206. //整包更新和新流程--------------------------------------------------------
  207. /** 新包覆盖安装后清除热更版本,重启 */
  208. checkRemoveLocalHot() {
  209. let currentVersion = mk.storage.getStorage('currentVersion');
  210. mk.storage.setStorage('currentVersion', gData.appData.appVersion);
  211. if (currentVersion) {
  212. let compareVer = Util.versionCompareHandle(currentVersion, gData.appData.appVersion)
  213. console.log('compareVer ', compareVer)
  214. if (compareVer < 0) {
  215. // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  216. var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  217. if (storagePath) {
  218. let storageArr = JSON.parse(storagePath)
  219. let len = storageArr.length - 2
  220. for (var i = 0; i < len; i++) {
  221. let path = storageArr.shift()
  222. jsb.fileUtils.removeDirectory(path);
  223. }
  224. cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  225. jsb.fileUtils.setSearchPaths(storageArr);
  226. setTimeout(() => {
  227. cc.audioEngine.stopAll();
  228. cc.game.restart();
  229. }, 500);
  230. }
  231. }
  232. }
  233. }
  234. /** 检测强制更新 */
  235. public async checkForceUpate() {
  236. await this.getForceVersionInfo();
  237. if (gData.appData.needForceUpdate === 1) {
  238. mk.ui.openPanel('module/forceUpdate/forceUpdate');
  239. }
  240. else {
  241. await this.checkHotUpdate();
  242. }
  243. }
  244. //发送请求整包更新
  245. public async getForceVersionInfo() {
  246. if (CC_DEBUG) {
  247. //测试用配置版本号
  248. gData.appData.tfChannel = "1";
  249. gData.appData.appVersion = "1.1.9";
  250. }
  251. let data: any = {
  252. appId: gData.appData.appId,
  253. channel: gData.appData.tfChannel,
  254. version: gData.appData.appVersion
  255. }
  256. mk.console.logSingle('getForceVersionInfo ', data);
  257. let response = await mk.http.sendRequest("getForceVersionInfo", 'POST', JSON.stringify(data), false, false)
  258. if (response.data) {
  259. gData.appData.needForceUpdate = response.data.isForceUpdate;
  260. gData.appData.installVersion = response.data.version;
  261. gData.appData.installUrl = response.data.downloadAddress;
  262. gData.appData.updateDes = response.data.versionRemark;
  263. gData.appData.fixDes = response.data.bugRestoreRemark;
  264. }
  265. else {
  266. gData.appData.needForceUpdate = 0;
  267. console.error("[Login]无response.data 请检查.");
  268. }
  269. mk.console.logSingle("===[Login] gData.appData.needForceUpdate", response);
  270. }
  271. //热更新-----------------------------------------------------------------
  272. /** 检测热更新 */
  273. public async checkHotUpdate() {
  274. //热更脚本交互
  275. this.hotUpdatePanel = this.node.getComponent(HotUpdatePanel);
  276. if (!this.hotUpdatePanel) {
  277. console.error("===[login] 登录界面,没有挂载热更脚本");
  278. gData.appData.updateState = UpdateState.UpdateFinish;
  279. }
  280. else {
  281. console.log("===[login] 登录界面,挂载热更脚本");
  282. this.hotUpdatePanel.checkHotUpdate();
  283. }
  284. }
  285. public async login() {
  286. //初始化相关数据
  287. mk.pool.init(gData.gameData.pools);
  288. PoolMgr.Inst.initPoolPrefab();
  289. PoolMgr.Inst.initPoolPrefabByNum(NODEPOOLPREFABTYPE.CellItem, 64);
  290. //本地读取配置
  291. await gData.configData.loadConfig();
  292. gData.adData.init();
  293. mk.console.log("[FC] gData.appData.appId", gData.appData.appId);
  294. //登录
  295. gData.loginData.login();
  296. }
  297. /** 检查认证和防沉迷 */
  298. checkShowIdentification() {
  299. let identification = mk.storage.getStorage('identification');
  300. if (identification == 1) {
  301. gData.loginData.passAnti = true;
  302. }
  303. else {
  304. mk.ui.openPanel('module/certification/CertificationNode');
  305. }
  306. }
  307. private cool = false;
  308. clickLogin() {
  309. mk.audio.playEffect('button');
  310. if (this.cool) {
  311. return;
  312. }
  313. this.cool = true;
  314. this.scheduleOnce(() => {
  315. this.cool = false;
  316. }, 3)
  317. JsbSystem.WxAuth();
  318. }
  319. }