Login.ts 11 KB

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