Login.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. gData.loginData.loadLocalRes();
  52. }
  53. update() {
  54. if (!mk.shuzilmM.hasGot || !mk.tongdunM.hasGot) {
  55. return;
  56. }
  57. if (!this.hasDeviceBackInit) {
  58. this.hasDeviceBackInit = true;
  59. this.getAppVersion();
  60. }
  61. // if (!this.hasDeviceBackInit) {
  62. // this.hasDeviceBackInit = true;
  63. // this.getDeviceInfoBack();
  64. // }
  65. //如果更新没有结束,就不进入正常加载流程
  66. if (gData.appData.updateState == UpdateState.ForceUpdateFinish) {
  67. gData.appData.updateState = UpdateState.CheckHot;
  68. this.checkHotUpdate();
  69. }
  70. if (gData.appData.updateState == UpdateState.IsUpdate) {
  71. this.lbl_progress.string = '正在更新中,请耐心等待';
  72. this.bar.setProgress(gData.appData.updatePercent);
  73. return;
  74. }
  75. if (gData.appData.updateState != UpdateState.UpdateFinish) {
  76. return;
  77. }
  78. if (!this.hasCheckPrivacy) {
  79. this.hasCheckPrivacy = true
  80. this.checkPrivacy()
  81. }
  82. if (!gData.loginData.passPrivacy) {
  83. return;
  84. }
  85. if (!this.hasCheckPermission) {
  86. this.hasCheckPermission = true;
  87. //获取手机授权
  88. JsbSystem.checkPermission();
  89. }
  90. if (!gData.appData.getDeviceInfoCompelete) {
  91. return
  92. }
  93. if (!CC_DEBUG) {
  94. if (gData.loginData.popIdentifySwitch) {
  95. if (!this.hasCheckAnti) {
  96. this.hasCheckAnti = true
  97. // this.checkShowIdentification()
  98. JsbSystem.ysdkInit();
  99. JsbSystem.autologin();
  100. }
  101. if (!gData.loginData.passAnti) {
  102. return
  103. }
  104. }
  105. }
  106. if (this.process < 100) {
  107. this.process += 2;
  108. this.setProgress(this.process);
  109. }
  110. if (gData.loginData.loadResCompelete) {
  111. if (mk.aliRiskManager.getRiskCode) {
  112. mk.aliRiskManager.getRiskCode = false;
  113. this.login();
  114. }
  115. else if (gData.gameData.dataFinish) {//登录流程完毕
  116. if (gData.loginData.isAuth || CC_DEBUG) {
  117. if (!this.loadSceneState) {
  118. this.loadSceneState = true;
  119. this.loadMain();
  120. }
  121. }
  122. else {
  123. this.node_loadPart.active = false;
  124. this.node_btnLogin.active = true;
  125. }
  126. gData.gameData.dataFinish = false;
  127. }
  128. }
  129. else {
  130. gData.loginData.loadLocalRes();
  131. }
  132. }
  133. private async loadMain() {
  134. mk.ui.openPanel("game/prefab/game");
  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 = "1.5.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. }