Login.ts 10.0 KB

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