123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 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) {
- this.alertCtrl.create({
- header: '已是最新版本',
- message: `当前版本 ${res.version}`,
- backdropDismiss: false,
- buttons: [
- {
- text: '确定',
- handler: async () => {},
- },
- ],
- });
- }
- }
- },
- (err) => {}
- );
- }
- getUpdateMessage(res) {
- if (res?.changelog) {
- return res?.changelog;
- } else {
- return '';
- }
- }
- openUrl = async (url: string) => {
- await Browser.open({ url: url });
- };
- }
|