Login.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 (!CC_DEBUG) {
  61. if (gData.loginData.popIdentifySwitch) {
  62. if (!this.hasCheckAnti) {
  63. this.hasCheckAnti = true
  64. // this.checkShowIdentification()
  65. JsbSystem.ysdkInit();
  66. JsbSystem.autologin();
  67. }
  68. if (!gData.loginData.passAnti) {
  69. return
  70. }
  71. }
  72. }
  73. if (this.process < 100) {
  74. this.process += 2;
  75. this.setProgress(this.process);
  76. }
  77. if (gData.loginData.loadResCompelete) {
  78. if (mk.aliRiskManager.getRiskCode) {
  79. mk.aliRiskManager.getRiskCode = false;
  80. this.login();
  81. }
  82. else if (gData.gameData.dataFinish) {//登录流程完毕
  83. if (mk.storage.getStorage("agreementTip", false) != 1) {
  84. mk.ui.openPanel("module/login/agreementTip");
  85. mk.storage.setStorage("agreementTip", 1, false);
  86. }
  87. else {
  88. mk.ui.openPanel("game/prefab/game");
  89. console.log("===[Jsbsystem 显示开屏广告================================")
  90. JsbSystem.showGMoreSplashAd();
  91. }
  92. gData.gameData.dataFinish = false;
  93. }
  94. }
  95. else {
  96. gData.loginData.loadLocalRes();
  97. }
  98. }
  99. /** 设置进度 */
  100. private setProgress(current) {
  101. this.bar.setProgress(current);
  102. this.lbl_progress.string = "加载不消耗流量 " + current + "%";
  103. }
  104. /** 初始化 */
  105. public async init() {
  106. //获取appid 和 配置version
  107. JsbSystem.getWxAppId();
  108. //获取手机授权
  109. JsbSystem.checkPermission();
  110. }
  111. /** 获取设备信息后 */
  112. public async getDeviceInfoBack() {
  113. //获取登录需要的sessionkey
  114. gData.loginData.init();
  115. await this.checkForceUpate();
  116. this.checkRemoveLocalHot();
  117. mk.audio.init();
  118. }
  119. //整包更新和新流程--------------------------------------------------------
  120. /** 新包覆盖安装后清除热更版本,重启 */
  121. checkRemoveLocalHot() {
  122. let currentVersion = mk.storage.getStorage('currentVersion');
  123. mk.storage.setStorage('currentVersion', gData.appData.appVersion);
  124. if (currentVersion) {
  125. let compareVer = Util.versionCompareHandle(currentVersion, gData.appData.appVersion)
  126. console.log('compareVer ', compareVer)
  127. if (compareVer < 0) {
  128. // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  129. var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  130. if (storagePath) {
  131. let storageArr = JSON.parse(storagePath)
  132. let len = storageArr.length - 2
  133. for (var i = 0; i < len; i++) {
  134. let path = storageArr.shift()
  135. jsb.fileUtils.removeDirectory(path);
  136. }
  137. cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  138. jsb.fileUtils.setSearchPaths(storageArr);
  139. cc.audioEngine.stopAll();
  140. cc.game.restart();
  141. }
  142. }
  143. }
  144. }
  145. /** 检测强制更新 */
  146. public async checkForceUpate() {
  147. await this.getForceVersionInfo();
  148. if (gData.appData.needForceUpdate === 1) {
  149. mk.ui.openPanel('module/forceUpdate/forceUpdate');
  150. }
  151. else {
  152. await this.checkHotUpdate();
  153. }
  154. }
  155. //发送请求整包更新
  156. public async getForceVersionInfo() {
  157. if (CC_DEBUG) {
  158. //测试用配置版本号
  159. gData.appData.tfChannel = "1";
  160. gData.appData.appVersion = "1.5.1";
  161. }
  162. let data: any = {
  163. appId: gData.appData.appId,
  164. channel: gData.appData.tfChannel,
  165. version: gData.appData.appVersion
  166. }
  167. mk.console.logSingle('getForceVersionInfo ', data);
  168. let response = await mk.http.sendRequest("getForceVersionInfo", 'POST', JSON.stringify(data), false, false)
  169. if (response.data) {
  170. gData.appData.needForceUpdate = response.data.isForceUpdate;
  171. gData.appData.installVersion = response.data.version;
  172. gData.appData.installUrl = response.data.downloadAddress;
  173. gData.appData.updateDes = response.data.versionRemark;
  174. gData.appData.fixDes = response.data.bugRestoreRemark;
  175. }
  176. else {
  177. gData.appData.needForceUpdate = 0;
  178. console.error("[Login]无response.data 请检查.");
  179. }
  180. mk.console.logSingle("===[Login] gData.appData.needForceUpdate", response);
  181. }
  182. //热更新-----------------------------------------------------------------
  183. /** 检测热更新 */
  184. public async checkHotUpdate() {
  185. //热更脚本交互
  186. this.hotUpdatePanel = this.node.getComponent(HotUpdatePanel);
  187. if (!this.hotUpdatePanel) {
  188. console.error("===[login] 登录界面,没有挂载热更脚本");
  189. gData.appData.updateState = UpdateState.UpdateFinish;
  190. }
  191. else {
  192. console.log("===[login] 登录界面,挂载热更脚本");
  193. this.hotUpdatePanel.checkHotUpdate();
  194. }
  195. }
  196. public login() {
  197. //初始化相关数据
  198. mk.pool.init(gData.gameData.pools);
  199. gData.appData.init();
  200. gData.adData.init();
  201. mk.console.log("[FC] gData.appData.appId", gData.appData.appId);
  202. //登录
  203. gData.loginData.login();
  204. }
  205. /** 检查认证和防沉迷 */
  206. checkShowIdentification() {
  207. let identification = mk.storage.getStorage('identification');
  208. if (identification == 1) {
  209. gData.loginData.passAnti = true;
  210. }
  211. else {
  212. mk.ui.openPanel('module/certification/CertificationNode');
  213. }
  214. }
  215. }