Procházet zdrojové kódy

Merge branch 'master' of http://gcls-git.helxsoft.cn/GCLS/eep_page

zq před 2 týdny
rodič
revize
4f156108eb
4 změnil soubory, kde provedl 76 přidání a 56 odebrání
  1. 0 56
      main.js
  2. 14 0
      preload.js
  3. 55 0
      src/App.vue
  4. 7 0
      src/api/app.js

+ 0 - 56
main.js

@@ -62,62 +62,6 @@ app.whenReady().then(() => {
   });
 });
 
-// 检查更新
-ipcMain.handle('check-update', async () => {
-  const apiUrl = 'https://your-api.com/api/app/latest'; // <-- 替换为真实接口
-  const currentVersion = app.getVersion();
-
-  return new Promise((resolve, reject) => {
-    const req = net.request(apiUrl);
-    let body = '';
-    req.on('response', (res) => {
-      res.on('data', (chunk) => (body += chunk));
-      res.on('end', () => {
-        try {
-          const latest = JSON.parse(body);
-          const update = latest.version && latest.version !== currentVersion;
-          resolve({ update, latest, currentVersion });
-        } catch (e) {
-          reject(e);
-        }
-      });
-    });
-    req.on('error', (err) => reject(err));
-    req.end();
-  });
-});
-
-// 下载更新(渲染进程发起),主进程负责流式保存并推送进度
-ipcMain.on('download-update', (event, downloadUrl) => {
-  const win = BrowserWindow.getAllWindows()[0];
-  const filename = path.basename(downloadUrl).split('?')[0] || `update-${Date.now()}.exe`;
-  const tmpPath = path.join(app.getPath('temp'), filename);
-  const fileStream = fs.createWriteStream(tmpPath);
-  const req = net.request(downloadUrl);
-
-  let received = 0;
-  let total = 0;
-
-  req.on('response', (res) => {
-    total = parseInt(res.headers['content-length'] || res.headers['Content-Length'] || '0');
-    res.on('data', (chunk) => {
-      received += chunk.length;
-      fileStream.write(chunk);
-      win.webContents.send('update-download-progress', { received, total });
-    });
-    res.on('end', () => {
-      fileStream.end();
-      win.webContents.send('update-downloaded', { path: tmpPath });
-    });
-  });
-
-  req.on('error', (err) => {
-    win.webContents.send('update-error', { message: err.message || String(err) });
-  });
-
-  req.end();
-});
-
 // 安装更新(渲染进程确认安装时调用)
 ipcMain.on('install-update', (event, filePath) => {
   if (!fs.existsSync(filePath)) {

+ 14 - 0
preload.js

@@ -106,3 +106,17 @@ contextBridge.exposeInMainWorld('fileAPI', {
     }
   },
 });
+
+/**
+ * 应用更新相关的预加载脚本
+ */
+contextBridge.exposeInMainWorld('updateAPI', {
+  /**
+   * 安装更新
+   * @param {String} filePath 更新文件路径
+   * @returns {Promise} 安装结果
+   */
+  installUpdate: (filePath) => {
+    return ipcRenderer.send('install-update', filePath);
+  },
+});

+ 55 - 0
src/App.vue

@@ -8,10 +8,65 @@
 <script>
 import GlobalProgress from '@/components/GlobalProgress.vue';
 
+import { GetSysVersionInfo } from '@/api/app';
+
 export default {
   name: 'App',
   components: {
     GlobalProgress,
   },
+  data() {
+    return {
+      savePath: '',
+      client_download_url: '',
+      version: '',
+    };
+  },
+  mounted() {
+    GetSysVersionInfo().then(({ version, client_download_url }) => {
+      let curVersion = process.env.VUE_APP_VERSION.replace(/\./g, '');
+
+      if (Number(curVersion) < version) {
+        this.$confirm('新版本可用。是否下载最新版本?', '更新提示', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'primary',
+        })
+          .then(() => {
+            this.version = version;
+            this.client_download_url = client_download_url;
+            this.selectDirectory();
+          })
+          .catch(() => {});
+      }
+    });
+  },
+  methods: {
+    /**
+     * 选择保存更新包的目录
+     */
+    async selectDirectory() {
+      const result = await window.fileAPI.openFileDialog({
+        title: '请选择保存更新包的文件夹',
+        properties: ['openDirectory', 'createDirectory'],
+      });
+      if (result.canceled) {
+        this.$message.warning('未选择文件夹,操作已取消');
+        return;
+      }
+      this.savePath = result.filePaths[0];
+      const downloadDir = `${this.savePath}\\智慧梧桐数字教材编辑器_${this.version}.exe`;
+      this.loadingInstance = this.$loading({
+        lock: true,
+        text: '正在下载更新包,请稍候...',
+        spinner: 'el-icon-loading',
+        background: 'rgba(255, 255, 255, 0.7)',
+      });
+      await window.fileAPI.downloadFile(this.client_download_url, downloadDir);
+      this.loadingInstance.text = '下载完成,正在安装更新...';
+      await window.updateAPI.installUpdate(downloadDir);
+      this.loadingInstance.close();
+    },
+  },
 };
 </script>

+ 7 - 0
src/api/app.js

@@ -119,3 +119,10 @@ export function TextToAudioFile(data) {
 export function GetTextToAudioConfParamList() {
   return http.post(`${process.env.VUE_APP_EepServer}?MethodName=tool-GetTextToAudioConfParamList`);
 }
+
+/**
+ * 得到系统版本信息
+ */
+export function GetSysVersionInfo() {
+  return http.post(`${process.env.VUE_APP_EepServer}?MethodName=login_control-GetSysVersionInfo`);
+}