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