| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import { _decorator, Component, ProgressBar } from 'cc';
- import { JSB } from 'cc/env';
- import { DataSystem } from '../core/data/DataSystem';
- import { Http } from '../core/net/Http';
- import { WindowSystem } from '../core/ui/window/WindowSystem';
- import { Utils } from '../core/utils/Utils';
- import { ConfigData } from '../data/ConfigData';
- import { g } from '../data/g';
- import { DeviceData } from '../data/jsb/DeviceData';
- import { ISJSB, platform } from '../data/jsb/platform';
- const { ccclass, property, requireComponent } = _decorator;
- /**
- * 应用更新
- * @author 袁浩
- */
- @ccclass('NativeUpdate')
- @requireComponent(Http)
- export class NativeUpdate extends Component {
- @property({ type: ProgressBar, tooltip: "加载进度条组件" })
- public progressBar: ProgressBar;
- @property({ tooltip: "游戏名" })
- public gameName: string = "td-jj";
- public async checkNativeVersion(version: string) {
- let deviceData = DataSystem.getData(DeviceData);
- let versionData = await this.getComponent(Http).send("/version", { game: this.gameName, channel: g.deviceData.channel });
- if (versionData && versionData.code == 0) {
- deviceData.versionIsNew = Utils.compareVersion(version, versionData.data.version);
- console.log("是否是最新" + deviceData.versionIsNew);
- deviceData.versionIsNew == -1 && this.downloadAPK(versionData.data.apk);//如果版本较老则下载APK
- }
- else {
- WindowSystem.showTips("服务器连接失败,请检查网络");
- }
- }
- public downloadAPK(url: string) {
- g.deviceData.apkDownloading = true;
- platform.downloadAPK(url, this.gameName + ".apk");
- }
- public setDownloadAPKProgress() {
- //根据APK下载进度设置UI
- this.progressBar.progress = g.deviceData.downloadAPKProgress;
- }
- update() {
- if (DataSystem.getData(ConfigData).loaded && ISJSB()) {
- let deviceData = DataSystem.getData(DeviceData);
- if (DataSystem.watch(DeviceData, "channel")) {//如果拿到渠道和版本号
- this.checkNativeVersion(deviceData.version);
- }
- if (deviceData.apkDownloading) {//如果apk正在下载,因为watch只会执行一次,所以这里使用属性
- this.setDownloadAPKProgress();
- }
- }
- }
- }
|