Login.ts 10 KB

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