update.service.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Injectable } from '@angular/core';
  2. import {
  3. Platform,
  4. AlertController,
  5. LoadingController,
  6. } from '@ionic/angular';
  7. import Parse from 'parse';
  8. import { App } from '@capacitor/app';
  9. import { Browser } from '@capacitor/browser';
  10. // import { Diagnostic } from '@awesome-cordova-plugins/diagnostic/ngx';
  11. @Injectable({
  12. providedIn: 'root',
  13. })
  14. export class UpdateService {
  15. versionNumber: any = '';
  16. currentVersion: any = '';
  17. isAndroid: boolean = false;
  18. isIOS: boolean = false;
  19. apkUrl: string = 'https://chat.gdchat.cn/release/Android/heychat.apk';
  20. itunesUrl: string = 'https://chat.gdchat.cn/download'
  21. constructor(
  22. private platform: Platform,
  23. public alertCtrl: AlertController,
  24. public loadingCtrl: LoadingController
  25. ) {
  26. this.isAndroid = this.platform.is('android');
  27. this.isIOS = this.platform.is('ios');
  28. }
  29. /* 设置本地内存存储 */
  30. // async checkPromission() {
  31. // let isAvailable = await this.diagnostic.isExternalStorageAuthorized()
  32. // console.log("permisson_STORAGE:", isAvailable)
  33. // if (!isAvailable) {
  34. // let data = this.diagnostic.requestExternalStorageAuthorization()
  35. // }
  36. // return
  37. // }
  38. updateVersion(auto?:boolean) {
  39. // console.log('updateversion', this.isIOS , this.isAndroid);
  40. if (this.platform.is('hybrid') && (this.isIOS || this.isAndroid)) {
  41. this.checkUpdate(auto);
  42. }
  43. }
  44. async checkUpdate(auto?:boolean) {
  45. let appInfo = await App.getInfo();
  46. let appId = appInfo.id;
  47. if (!appId) {
  48. appId = 'com.heychat.app';
  49. }
  50. console.log('update check:', appId);
  51. let versionNumber = appInfo.version;
  52. Parse.Cloud.run('app_update', { appid: appId}).then(
  53. async (res) => {
  54. this.versionNumber = Number(
  55. '0.' + res.version.toString().replace(new RegExp(/(\.)/g), '0')
  56. );
  57. this.currentVersion = Number(
  58. '0.' + versionNumber.toString().replace(new RegExp(/(\.)/g), '0')
  59. );
  60. if (res.apkUrl) {
  61. this.apkUrl = res.apkUrl;
  62. }
  63. if (res.downUrl) {
  64. this.itunesUrl = res.downUrl;
  65. }
  66. console.log(this.versionNumber, this.currentVersion);
  67. if (this.versionNumber > this.currentVersion) {
  68. let buttons = [];
  69. if (res.isForce !== true) {
  70. buttons.push({
  71. text: '取消',
  72. handler: async () => {
  73. confirm.dismiss();
  74. },
  75. });
  76. }
  77. buttons.push({
  78. text: '更新',
  79. handler: async () => {
  80. if (res.isForce == true) {
  81. let loading = await this.loadingCtrl.create({
  82. message: '请点下载更新...',
  83. duration: 0,
  84. });
  85. loading.present();
  86. }
  87. if (this.isAndroid) {
  88. this.openUrl(this.apkUrl); // 跳转进入下载页
  89. }
  90. if (this.isIOS) {
  91. this.openUrl(this.itunesUrl);
  92. }
  93. return true;
  94. },
  95. });
  96. let confirm = await this.alertCtrl.create({
  97. header: '新版本更新' + res.version,
  98. message: this.getUpdateMessage(res),
  99. backdropDismiss: false,
  100. buttons: buttons,
  101. });
  102. confirm.onDidDismiss().then(() => {
  103. if (res.isForce == true) {
  104. this.checkUpdate();
  105. }
  106. });
  107. confirm.present();
  108. } else {
  109. if (auto) {
  110. this.alertCtrl.create({
  111. header: '已是最新版本',
  112. message: `当前版本 ${res.version}`,
  113. backdropDismiss: false,
  114. buttons: [
  115. {
  116. text: '确定',
  117. handler: async () => {},
  118. },
  119. ],
  120. });
  121. }
  122. }
  123. },
  124. (err) => {}
  125. );
  126. }
  127. getUpdateMessage(res) {
  128. if (res?.changelog) {
  129. return res?.changelog;
  130. } else {
  131. return '';
  132. }
  133. }
  134. openUrl = async (url: string) => {
  135. await Browser.open({ url: url });
  136. };
  137. }