import {Component, OnInit, Input, Inject} from '@angular/core'; import {Router} from '@angular/router'; import {AlertController, ModalController} from '@ionic/angular'; import {NavParams} from '@ionic/angular'; import {FileTransfer, FileTransferObject} from '@ionic-native/file-transfer/ngx'; import {FileOpener} from '@ionic-native/file-opener/ngx'; import {M2_DEVICE_INFO_PROVIDER} from '../../../core/tokens/m2.tokens'; import {DeviceInfoInterface} from '../../../core/interfaces/device-info.interface'; import {File} from '@ionic-native/file/ngx'; import {InAppBrowser} from '@ionic-native/in-app-browser/ngx'; import {AppData} from '../../../../df-ext/app-data'; @Component({ selector: 'app-modal-update', templateUrl: 'modal-update.page.html', styleUrls: ['modal-update.page.scss'] }) export class ModalUpdatePage implements OnInit { @Input() title: string; @Input() remoteVersion: string; @Input() upgradeContent: string; @Input() ifForce; alertCount = 0; progressNum = null; constructor(public router: Router, private modalCtrl: ModalController, private alertCtrl: AlertController, private fileOpener: FileOpener, private transfer: FileTransfer, @Inject(M2_DEVICE_INFO_PROVIDER) private deviceInfoInterface: DeviceInfoInterface, private file: File, private iab: InAppBrowser, private navParams: NavParams) { navParams.get('firstName'); } ngOnInit() { } cancelFun() { this.dismiss(); } async dismiss() { // alertCount是为了避免点击下载后不安装,返回本应用会进入dismiss的死循环 if (this.alertCount !== 0) { (navigator as any).app.exitApp(); // work in ionic 4 } // work in ionic 4 } todo ionic5测试 if (this.ifForce) { const alert = await this.alertCtrl.create({ subHeader: '新版本需要强制升级,点击确定会关闭应用!', message: this.upgradeContent, backdropDismiss: false, buttons: [{ text: '取消', handler: () => { this.alertCount = 0; } }, { text: '确定', handler: () => { (navigator as any).app.exitApp(); // work in ionic 4 } }] }); await alert.present(); this.alertCount++; } else { this.modalCtrl.dismiss({ dismissed: true }); } } async downloadApp() { this.progressNum = 0; this.alertCount = 1; const fileTransfer: FileTransferObject = this.transfer.create(); const apk = this.file.externalDataDirectory + 'm2app.apk'; // apk保存的目录 fileTransfer.download(AppData.config.APK_DOWNLOAD, apk).then(() => { this.fileOpener.open(apk, 'application/vnd.android.package-archive').then(() => { console.log('File is opened'); }).catch(async e => { console.log('Error openening file', e); this.downloadErrorHandler(); }); }).catch(async (error) => { this.downloadErrorHandler(); }); let num = 0; fileTransfer.onProgress((event: ProgressEvent) => { num = Math.floor(event.loaded / event.total * 100); }); setInterval(async () => { if (num === 100) { this.dismiss(); } else { this.progressNum = num; } }, 300); } async downloadErrorHandler() { const alert1 = await this.alertCtrl.create({ header: '自动升级失败,将转入下载页面!', backdropDismiss: false, buttons: [{ text: '确定', handler: () => { this.iab.create(AppData.config.APP_DOWNLOAD, '_system'); (navigator as any).app.exitApp(); // work in ionic 4 } }] }); await alert1.present(); } }