Login.ts 11 KB

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