NativeUpdate.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { _decorator, Component, ProgressBar } from 'cc';
  2. import { JSB } from 'cc/env';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http } from '../core/net/Http';
  5. import { WindowSystem } from '../core/ui/window/WindowSystem';
  6. import { Utils } from '../core/utils/Utils';
  7. import { ConfigData } from '../data/ConfigData';
  8. import { g } from '../data/g';
  9. import { DeviceData } from '../data/jsb/DeviceData';
  10. import { ISJSB, platform } from '../data/jsb/platform';
  11. const { ccclass, property, requireComponent } = _decorator;
  12. /**
  13. * 应用更新
  14. * @author 袁浩
  15. */
  16. @ccclass('NativeUpdate')
  17. @requireComponent(Http)
  18. export class NativeUpdate extends Component {
  19. @property({ type: ProgressBar, tooltip: "加载进度条组件" })
  20. public progressBar: ProgressBar;
  21. @property({ tooltip: "游戏名" })
  22. public gameName: string = "td-jj";
  23. public async checkNativeVersion(version: string) {
  24. let deviceData = DataSystem.getData(DeviceData);
  25. let versionData = await this.getComponent(Http).send("/version", { game: this.gameName, channel: g.deviceData.channel });
  26. if (versionData && versionData.code == 0) {
  27. deviceData.versionIsNew = Utils.compareVersion(version, versionData.data.version);
  28. console.log("是否是最新" + deviceData.versionIsNew);
  29. deviceData.versionIsNew == -1 && this.downloadAPK(versionData.data.apk);//如果版本较老则下载APK
  30. }
  31. else {
  32. WindowSystem.showTips("服务器连接失败,请检查网络");
  33. }
  34. }
  35. public downloadAPK(url: string) {
  36. g.deviceData.apkDownloading = true;
  37. platform.downloadAPK(url, this.gameName + ".apk");
  38. }
  39. public setDownloadAPKProgress() {
  40. //根据APK下载进度设置UI
  41. this.progressBar.progress = g.deviceData.downloadAPKProgress;
  42. }
  43. update() {
  44. if (DataSystem.getData(ConfigData).loaded && ISJSB()) {
  45. let deviceData = DataSystem.getData(DeviceData);
  46. if (DataSystem.watch(DeviceData, "channel")) {//如果拿到渠道和版本号
  47. this.checkNativeVersion(deviceData.version);
  48. }
  49. if (deviceData.apkDownloading) {//如果apk正在下载,因为watch只会执行一次,所以这里使用属性
  50. this.setDownloadAPKProgress();
  51. }
  52. }
  53. }
  54. }