App.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div id="app">
  3. <RouterView />
  4. <GlobalProgress />
  5. <CommonProgress :visible.sync="visible" :title="title" :percentage="percentage" text="正在下载升级包,请等待..." />
  6. </div>
  7. </template>
  8. <script>
  9. import GlobalProgress from '@/components/GlobalProgress.vue';
  10. import CommonProgress from '@/components/CommonProgress.vue';
  11. import { GetSysVersionInfo } from '@/api/app';
  12. export default {
  13. name: 'App',
  14. components: {
  15. GlobalProgress,
  16. CommonProgress,
  17. },
  18. data() {
  19. return {
  20. savePath: '',
  21. client_download_url: '',
  22. version: '',
  23. visible: false,
  24. percentage: 0,
  25. };
  26. },
  27. computed: {
  28. title() {
  29. return `下载升级包(当前版本 ${Number(process.env.VUE_APP_VERSION.replace(/\./g, ''))},升级包版本 ${this.version})`;
  30. },
  31. },
  32. mounted() {
  33. const isDev = process.env.NODE_ENV === 'development';
  34. if (isDev) return;
  35. this.getSysVersionInfo();
  36. },
  37. methods: {
  38. /**
  39. * 获取系统版本信息,并检查是否有新版本可用
  40. */
  41. getSysVersionInfo() {
  42. GetSysVersionInfo().then(({ version, client_download_url }) => {
  43. let curVersion = Number(process.env.VUE_APP_VERSION.replace(/\./g, ''));
  44. if (curVersion < version) {
  45. this.$confirm('新版本可用。是否下载最新版本?', '更新提示', {
  46. confirmButtonText: '确定',
  47. cancelButtonText: '取消',
  48. type: 'primary',
  49. })
  50. .then(() => {
  51. this.version = version;
  52. this.client_download_url = client_download_url;
  53. this.selectDirectory();
  54. })
  55. .catch(() => {});
  56. }
  57. });
  58. },
  59. /**
  60. * 选择保存更新包的目录
  61. */
  62. async selectDirectory() {
  63. const result = await window.fileAPI.openFileDialog({
  64. title: '请选择保存更新包的文件夹',
  65. properties: ['openDirectory', 'createDirectory'],
  66. });
  67. if (result.canceled) {
  68. this.$message.warning('未选择文件夹,操作已取消');
  69. return;
  70. }
  71. this.savePath = result.filePaths[0];
  72. const downloadDir = `${this.savePath}\\智慧梧桐数字教材编辑器_${this.version}.exe`;
  73. this.visible = true;
  74. const unsubscribe = window.fileAPI.onDownloadProgress(({ percentage }) => {
  75. this.percentage = percentage;
  76. });
  77. try {
  78. await window.fileAPI.downloadFile(this.client_download_url, downloadDir);
  79. } catch (err) {
  80. this.$message.error(`下载失败:${err && err.message}`);
  81. } finally {
  82. unsubscribe();
  83. this.visible = false;
  84. }
  85. await window.updateAPI.installUpdate(downloadDir); // 调用安装更新的API
  86. this.percentage = 0;
  87. },
  88. },
  89. };
  90. </script>