Login.ts 10 KB

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