import { Injectable } from '@angular/core'; import { Platform, AlertController, LoadingController, } from '@ionic/angular'; import Parse from 'parse'; import { App } from '@capacitor/app'; import { Browser } from '@capacitor/browser'; // import { Diagnostic } from '@awesome-cordova-plugins/diagnostic/ngx'; @Injectable({ providedIn: 'root', }) export class UpdateService { versionNumber: any = ''; currentVersion: any = ''; isAndroid: boolean = false; isIOS: boolean = false; apkUrl: string = 'https://chat.gdchat.cn/release/Android/heychat.apk'; itunesUrl: string = 'https://chat.gdchat.cn/download' constructor( private platform: Platform, public alertCtrl: AlertController, public loadingCtrl: LoadingController ) { this.isAndroid = this.platform.is('android'); this.isIOS = this.platform.is('ios'); } /* 设置本地内存存储 */ // async checkPromission() { // let isAvailable = await this.diagnostic.isExternalStorageAuthorized() // console.log("permisson_STORAGE:", isAvailable) // if (!isAvailable) { // let data = this.diagnostic.requestExternalStorageAuthorization() // } // return // } updateVersion(auto?:boolean) { // console.log('updateversion', this.isIOS , this.isAndroid); if (this.platform.is('hybrid') && (this.isIOS || this.isAndroid)) { this.checkUpdate(auto); } } async checkUpdate(auto?:boolean) { let appInfo = await App.getInfo(); let appId = appInfo.id; if (!appId) { appId = 'com.heychat.app'; } console.log('update check:', appId); let versionNumber = appInfo.version; Parse.Cloud.run('app_update', { appid: appId}).then( async (res) => { this.versionNumber = Number( '0.' + res.version.toString().replace(new RegExp(/(\.)/g), '0') ); this.currentVersion = Number( '0.' + versionNumber.toString().replace(new RegExp(/(\.)/g), '0') ); if (res.apkUrl) { this.apkUrl = res.apkUrl; } if (res.downUrl) { this.itunesUrl = res.downUrl; } console.log(this.versionNumber, this.currentVersion); if (this.versionNumber > this.currentVersion) { let buttons = []; if (res.isForce !== true) { buttons.push({ text: '取消', handler: async () => { confirm.dismiss(); }, }); } buttons.push({ text: '更新', handler: async () => { if (res.isForce == true) { let loading = await this.loadingCtrl.create({ message: '请点下载更新...', duration: 0, }); loading.present(); } if (this.isAndroid) { this.openUrl(this.apkUrl); // 跳转进入下载页 } if (this.isIOS) { this.openUrl(this.itunesUrl); } return true; }, }); let confirm = await this.alertCtrl.create({ header: '新版本更新' + res.version, message: this.getUpdateMessage(res), backdropDismiss: false, buttons: buttons, }); confirm.onDidDismiss().then(() => { if (res.isForce == true) { this.checkUpdate(); } }); confirm.present(); } else { if (auto) { let alert = await this.alertCtrl.create({ header: '已是最新版本', message: `当前版本 ${res.version}`, backdropDismiss: false, buttons: [ { text: '确定', handler: async () => {}, }, ], }); alert.present(); } } }, (err) => {} ); } getUpdateMessage(res) { if (res?.changelog) { return res?.changelog; } else { return ''; } } openUrl = async (url: string) => { await Browser.open({ url: url }); }; }