Login.ts 11 KB

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