update.service.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { HttpClient } from '@angular/common/http';
  2. import { EventEmitter, Injectable } from '@angular/core';
  3. import { AppVersion } from '@ionic-native/app-version/ngx';
  4. import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';
  5. import { FileOpener } from '@ionic-native/file-opener/ngx';
  6. import { File } from '@ionic-native/file/ngx';
  7. @Injectable({
  8. providedIn: 'root'
  9. })
  10. export class UpdateService {
  11. // 获取最新版本地址
  12. serviceVersionInfoUrl = 'http://192.168.137.1/';
  13. serviceVersionInfo: { elements: { versionName: string, outputFile: string }[] } = null;
  14. onDownProgress = new EventEmitter<number>();
  15. constructor(
  16. private appVersion: AppVersion,
  17. private file: File,
  18. private transfer: FileTransfer,
  19. private fileOpener: FileOpener,
  20. private http: HttpClient) {
  21. this.onDownProgress.subscribe(p => {
  22. console.log(`AppDownProgress: ${p}%`);
  23. });
  24. }
  25. getAppVersion(): Promise<string> {
  26. return this.appVersion.getVersionNumber();
  27. }
  28. getAppName(): Promise<string> {
  29. return this.appVersion.getAppName();
  30. }
  31. loadVersionInfo(): Promise<{ elements: { versionName: string, outputFile: string }[] }> {
  32. return new Promise((resolve, reject) => {
  33. this.http.get<string>(this.serviceVersionInfoUrl + 'output-metadata.json').subscribe(serviceVersion => {
  34. console.log('serv: ' + JSON.stringify(serviceVersion));
  35. resolve(serviceVersion as any);
  36. }, error => {
  37. console.log(JSON.stringify(error));
  38. })
  39. });
  40. }
  41. async getServiceVersion(): Promise<string> {
  42. if (!this.serviceVersionInfo) {
  43. this.serviceVersionInfo = await this.loadVersionInfo();
  44. }
  45. return this.serviceVersionInfo.elements[0].versionName;
  46. }
  47. async getServiceAppName(): Promise<string> {
  48. if (!this.serviceVersionInfo) {
  49. this.serviceVersionInfo = await this.loadVersionInfo();
  50. }
  51. return this.serviceVersionInfo.elements[0].outputFile;
  52. }
  53. async isNewVersion(): Promise<boolean> {
  54. const appVersion = await this.getAppVersion();
  55. console.log('appVersion: ' + appVersion);
  56. const serviceVersion = await this.getServiceVersion();
  57. console.log('serviceVersion: ' + serviceVersion);
  58. return appVersion !== serviceVersion;
  59. }
  60. async updateApp() {
  61. if (await this.isNewVersion()) {
  62. await this.downApp();
  63. }
  64. }
  65. async downApp() {
  66. const fileTransfer: FileTransferObject = this.transfer.create();
  67. const serviceAppName = await this.getServiceAppName();
  68. const apk = this.file.dataDirectory + serviceAppName;
  69. fileTransfer.download(this.serviceVersionInfoUrl + serviceAppName, apk).then((entry) => {
  70. this.fileOpener.open(apk, "application/vnd.android.package-archive")
  71. .then((e) => console.log(e))
  72. .catch(e => console.log(e));
  73. console.log('download complete: ');
  74. }, (error) => {
  75. // handle error
  76. console.log('download error :' + JSON.stringify(error))
  77. });
  78. this.onDownProgress.emit(0);
  79. fileTransfer.onProgress((event) => {
  80. let num = Math.ceil(event.loaded / event.total * 100); //转化成1-100的进度
  81. this.onDownProgress.emit(num);
  82. });
  83. }
  84. }