Login.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import Util from "../../../before/util/Util";
  2. import JsbSystem from "../../../mk/system/JsbSystem";
  3. import LoadingBar from "../../component/LoadingBar";
  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(LoadingBar)
  10. private bar: LoadingBar = null;
  11. @property({ displayName: '加载文字', type: cc.Label })
  12. private lbl_progress: cc.Label = null;
  13. @property({ displayName: '服务协议', type: cc.Label })
  14. private lbl_server: cc.Label = null;
  15. @property({ displayName: '隐私协议', type: cc.Label })
  16. private lbl_privacy: cc.Label = null;
  17. /** 加载进度 */
  18. private process: number = 0;
  19. /** 热更脚本(onLoad应先于Login) */
  20. public hotUpdatePanel: HotUpdatePanel = null;
  21. private hasDeviceBackInit = false;
  22. private hasCheckAnti = false;
  23. onLoad() {
  24. this.setProgress(0);
  25. }
  26. start() {
  27. if (mk.storage.getStorage("alifengkong") == 1) {
  28. return;
  29. }
  30. if (CC_DEBUG) {
  31. mk.aliRiskManager.getRiskCode = true;
  32. gData.appData.getDeviceInfoCompelete = true;
  33. }
  34. else {
  35. mk.aliRiskManager.init();
  36. }
  37. this.init();
  38. }
  39. update() {
  40. if (!gData.appData.getDeviceInfoCompelete) {
  41. return
  42. }
  43. if (!this.hasDeviceBackInit) {
  44. this.hasDeviceBackInit = true;
  45. this.getDeviceInfoBack();
  46. }
  47. //如果更新没有结束,就不进入正常加载流程
  48. if (gData.appData.updateState == UpdateState.ForceUpdateFinish) {
  49. gData.appData.updateState = UpdateState.CheckHot;
  50. this.checkHotUpdate();
  51. }
  52. if (gData.appData.updateState == UpdateState.IsUpdate) {
  53. this.lbl_progress.string = '正在更新中,请耐心等待';
  54. this.bar.setProgress(gData.appData.updatePercent);
  55. return;
  56. }
  57. if (gData.appData.updateState != UpdateState.UpdateFinish) {
  58. return;
  59. }
  60. if (gData.loginData.popIdentifySwitch) {
  61. if (!this.hasCheckAnti) {
  62. this.hasCheckAnti = true
  63. this.checkShowIdentification()
  64. }
  65. if (!gData.loginData.passAnti) {
  66. return
  67. }
  68. }
  69. if (this.process < 100) {
  70. this.process += 2;
  71. this.setProgress(this.process);
  72. }
  73. if (gData.loginData.loadResCompelete) {
  74. if (mk.aliRiskManager.getRiskCode) {
  75. mk.aliRiskManager.getRiskCode = false;
  76. this.login();
  77. }
  78. else if (gData.gameData.dataFinish) {//登录流程完毕
  79. if (mk.storage.getStorage("agreementTip", false) != 1) {
  80. mk.ui.openPanel("module/login/agreementTip");
  81. mk.storage.setStorage("agreementTip", 1, false);
  82. }
  83. else {
  84. mk.ui.openPanel("game/prefab/game");
  85. console.log("===[Jsbsystem 显示开屏广告================================")
  86. JsbSystem.showSplash();
  87. }
  88. gData.gameData.dataFinish = false;
  89. }
  90. }
  91. else {
  92. gData.loginData.loadLocalRes();
  93. }
  94. }
  95. /** 设置进度 */
  96. private setProgress(current) {
  97. this.bar.setProgress(current);
  98. this.lbl_progress.string = "加载不消耗流量 " + current + "%";
  99. }
  100. /** 初始化 */
  101. public async init() {
  102. //获取appid 和 配置version
  103. JsbSystem.getWxAppId();
  104. //获取手机授权
  105. JsbSystem.checkPermission();
  106. }
  107. /** 获取设备信息后 */
  108. public async getDeviceInfoBack() {
  109. //获取登录需要的sessionkey
  110. gData.loginData.init();
  111. await this.checkForceUpate();
  112. this.checkRemoveLocalHot();
  113. mk.audio.init();
  114. }
  115. //整包更新和新流程--------------------------------------------------------
  116. /** 新包覆盖安装后清除热更版本,重启 */
  117. checkRemoveLocalHot() {
  118. let currentVersion = mk.storage.getStorage('currentVersion');
  119. mk.storage.setStorage('currentVersion', gData.appData.appVersion);
  120. if (currentVersion) {
  121. let compareVer = Util.versionCompareHandle(currentVersion, gData.appData.appVersion)
  122. console.log('compareVer ', compareVer)
  123. if (compareVer < 0) {
  124. // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  125. var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  126. if (storagePath) {
  127. let storageArr = JSON.parse(storagePath)
  128. let len = storageArr.length - 2
  129. for (var i = 0; i < len; i++) {
  130. let path = storageArr.shift()
  131. jsb.fileUtils.removeDirectory(path);
  132. }
  133. cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  134. jsb.fileUtils.setSearchPaths(storageArr);
  135. cc.audioEngine.stopAll();
  136. cc.game.restart();
  137. }
  138. }
  139. }
  140. }
  141. /** 检测强制更新 */
  142. public async checkForceUpate() {
  143. await this.getForceVersionInfo();
  144. if (gData.appData.needForceUpdate === 1) {
  145. mk.ui.openPanel('module/forceUpdate/forceUpdate');
  146. }
  147. else {
  148. await this.checkHotUpdate();
  149. }
  150. }
  151. //发送请求整包更新
  152. public async getForceVersionInfo() {
  153. if (CC_DEBUG) {
  154. //测试用配置版本号
  155. gData.appData.tfChannel = "1";
  156. gData.appData.appVersion = "1.2.0";
  157. }
  158. let data: any = {
  159. appId: gData.appData.appId,
  160. channel: gData.appData.tfChannel,
  161. version: gData.appData.appVersion
  162. }
  163. mk.console.logSingle('getForceVersionInfo ', data);
  164. let response = await mk.http.sendRequest("getForceVersionInfo", 'POST', JSON.stringify(data), false, false)
  165. if (response.data) {
  166. gData.appData.needForceUpdate = response.data.isForceUpdate;
  167. gData.appData.installVersion = response.data.version;
  168. gData.appData.installUrl = response.data.downloadAddress;
  169. gData.appData.updateDes = response.data.versionRemark;
  170. gData.appData.fixDes = response.data.bugRestoreRemark;
  171. }
  172. else {
  173. gData.appData.needForceUpdate = 0;
  174. console.error("[Login]无response.data 请检查.");
  175. }
  176. mk.console.logSingle("===[Login] gData.appData.needForceUpdate", response);
  177. }
  178. //热更新-----------------------------------------------------------------
  179. /** 检测热更新 */
  180. public async checkHotUpdate() {
  181. //热更脚本交互
  182. this.hotUpdatePanel = this.node.getComponent(HotUpdatePanel);
  183. if (!this.hotUpdatePanel) {
  184. console.error("===[login] 登录界面,没有挂载热更脚本");
  185. gData.appData.updateState = UpdateState.UpdateFinish;
  186. }
  187. else {
  188. console.log("===[login] 登录界面,挂载热更脚本");
  189. this.hotUpdatePanel.checkHotUpdate();
  190. }
  191. }
  192. public login() {
  193. //初始化相关数据
  194. mk.pool.init(gData.gameData.pools);
  195. gData.appData.init();
  196. gData.adData.init();
  197. mk.console.log("[FC] gData.appData.appId", gData.appData.appId);
  198. //登录
  199. gData.loginData.login();
  200. }
  201. /** 检查认证和防沉迷 */
  202. checkShowIdentification() {
  203. let identification = mk.storage.getStorage('identification');
  204. if (identification == 1) {
  205. gData.loginData.passAnti = true;
  206. }
  207. else {
  208. mk.ui.openPanel('module/certification/CertificationNode');
  209. }
  210. }
  211. }