Login.ts 12 KB

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