Login.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import Util from "../../../before/util/Util";
  2. import MKSwitch from "../../../mk/MKSwitch";
  3. import JsbSystem from "../../../mk/system/JsbSystem";
  4. import LoadingBar from "../../component/LoadingBar";
  5. import { UpdateState } from "../../data/AppData";
  6. import HotUpdatePanel from "../hotUpdate/HotUpdatePanel";
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class Login extends cc.Component {
  10. @property(LoadingBar)
  11. private bar: LoadingBar = null;
  12. @property({ displayName: '加载文字', type: cc.Label })
  13. private lbl_progress: cc.Label = null;
  14. @property({ displayName: '服务协议', type: cc.Label })
  15. private lbl_server: cc.Label = null;
  16. @property({ displayName: '隐私协议', type: cc.Label })
  17. private lbl_privacy: cc.Label = null;
  18. @property({ displayName: '加载部分', type: cc.Node })
  19. private node_loadPart: cc.Node = null;
  20. @property({ displayName: '登录按钮', type: cc.Node })
  21. private node_btnLogin: cc.Node = null;
  22. /** 加载进度 */
  23. private process: number = 0;
  24. /** 热更脚本(onLoad应先于Login) */
  25. public hotUpdatePanel: HotUpdatePanel = null;
  26. private hasDeviceBackInit = false;
  27. private hasCheckAnti = false;
  28. private loadSceneState = false;
  29. private hasCheckPermission = false;
  30. private hasCheckPrivacy = false;
  31. onLoad() {
  32. this.setProgress(0);
  33. this.node_loadPart.active = true;
  34. this.node_btnLogin.active = false;
  35. }
  36. start() {
  37. if (mk.storage.getStorage("alifengkong") == 1) {
  38. return;
  39. }
  40. if (CC_DEBUG) {
  41. mk.aliRiskManager.getRiskCode = true;
  42. gData.appData.getDeviceInfoCompelete = true;
  43. mk.shuzilmM.hasGot = true;
  44. mk.tongdunM.hasGot = true;
  45. }
  46. else {
  47. mk.aliRiskManager.init();
  48. mk.tongdunM.init();
  49. mk.shuzilmM.init();
  50. }
  51. }
  52. update() {
  53. if (!mk.shuzilmM.hasGot || !mk.tongdunM.hasGot) {
  54. return;
  55. }
  56. if (!this.hasDeviceBackInit) {
  57. this.hasDeviceBackInit = true;
  58. this.getAppVersion();
  59. }
  60. // if (!this.hasDeviceBackInit) {
  61. // this.hasDeviceBackInit = true;
  62. // this.getDeviceInfoBack();
  63. // }
  64. //如果更新没有结束,就不进入正常加载流程
  65. if (gData.appData.updateState == UpdateState.ForceUpdateFinish) {
  66. gData.appData.updateState = UpdateState.CheckHot;
  67. this.checkHotUpdate();
  68. }
  69. if (gData.appData.updateState == UpdateState.IsUpdate) {
  70. this.lbl_progress.string = '正在更新中,请耐心等待';
  71. this.bar.setProgress(gData.appData.updatePercent);
  72. return;
  73. }
  74. if (gData.appData.updateState != UpdateState.UpdateFinish) {
  75. return;
  76. }
  77. if (!this.hasCheckPrivacy) {
  78. this.hasCheckPrivacy = true
  79. this.checkPrivacy()
  80. }
  81. if (!gData.loginData.passPrivacy) {
  82. return;
  83. }
  84. if (!this.hasCheckPermission) {
  85. this.hasCheckPermission = true;
  86. //获取手机授权
  87. JsbSystem.checkPermission();
  88. }
  89. if (!gData.appData.getDeviceInfoCompelete) {
  90. return
  91. }
  92. if (!CC_DEBUG) {
  93. if (gData.loginData.popIdentifySwitch) {
  94. if (!this.hasCheckAnti) {
  95. this.hasCheckAnti = true
  96. // this.checkShowIdentification()
  97. JsbSystem.ysdkInit();
  98. JsbSystem.autologin();
  99. }
  100. if (!gData.loginData.passAnti) {
  101. return
  102. }
  103. }
  104. }
  105. if (this.process < 100) {
  106. this.process += 2;
  107. this.setProgress(this.process);
  108. }
  109. if (gData.loginData.loadResCompelete) {
  110. if (mk.aliRiskManager.getRiskCode) {
  111. mk.aliRiskManager.getRiskCode = false;
  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. else {
  129. gData.loginData.loadLocalRes();
  130. }
  131. }
  132. private async loadMain() {
  133. mk.ui.openPanel("game/prefab/game");
  134. console.log("===[Jsbsystem 显示开屏广告================================")
  135. JsbSystem.showSplash();
  136. }
  137. private checkPrivacy() {
  138. if (mk.storage.getStorage("agreementTip") != 1) {
  139. mk.ui.openPanel("module/login/agreementTip");
  140. }
  141. else {
  142. gData.loginData.passPrivacy = true;
  143. }
  144. }
  145. /** 设置进度 */
  146. private setProgress(current) {
  147. this.bar.setProgress(current);
  148. this.lbl_progress.string = "加载不消耗流量 " + current + "%";
  149. }
  150. // /** 初始化 */
  151. // public async init() {
  152. // //获取appid 和 配置version
  153. // JsbSystem.getWxAppId();
  154. // //获取手机授权
  155. // JsbSystem.checkPermission();
  156. // }
  157. /** 获取设备信息后 */
  158. public async getDeviceInfoBack() {
  159. //获取登录需要的sessionkey
  160. gData.loginData.init();
  161. await this.checkForceUpate();
  162. this.checkRemoveLocalHot();
  163. mk.audio.init();
  164. }
  165. /** 获取appid 和 version */
  166. public async getAppVersion() {
  167. //获取appid 和 配置version
  168. JsbSystem.getWxAppId();
  169. //获取登录需要的sessionkey
  170. gData.loginData.init();
  171. await this.checkForceUpate();
  172. this.checkRemoveLocalHot();
  173. mk.audio.init();
  174. }
  175. //整包更新和新流程--------------------------------------------------------
  176. /** 新包覆盖安装后清除热更版本,重启 */
  177. checkRemoveLocalHot() {
  178. let currentVersion = mk.storage.getStorage('currentVersion');
  179. mk.storage.setStorage('currentVersion', gData.appData.appVersion);
  180. if (currentVersion) {
  181. let compareVer = Util.versionCompareHandle(currentVersion, gData.appData.appVersion)
  182. console.log('compareVer ', compareVer)
  183. if (compareVer < 0) {
  184. // 热更新的储存路径,如果旧版本中有多个,可能需要记录在列表中,全部清理
  185. var storagePath = localStorage.getItem('HotUpdateSearchPaths');
  186. if (storagePath) {
  187. let storageArr = JSON.parse(storagePath)
  188. let len = storageArr.length - 2
  189. for (var i = 0; i < len; i++) {
  190. let path = storageArr.shift()
  191. jsb.fileUtils.removeDirectory(path);
  192. }
  193. cc.sys.localStorage.removeItem('HotUpdateSearchPaths');
  194. jsb.fileUtils.setSearchPaths(storageArr);
  195. cc.audioEngine.stopAll();
  196. cc.game.restart();
  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. }