litian
2024-04-25 c66e9eaa87989cd0b312fa1d8777a4bf799db0da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<template>
  <div class="updateDownloadInfo" v-if="showUpdateInfo">
    <el-alert
      :title="`检测到新版本,正在下载安装包${
        updateDownloadInfo?.percent ? ',进度:' + updateDownloadInfo?.percent + '%' : '...'
      }`"
      type="info"
      effect="dark"
    />
  </div>
  <RouterView />
</template>
 
<script setup lang="ts">
import { ref, reactive, inject } from 'vue'
import { RouterView, useRouter } from 'vue-router'
import { useDownloadTask, useExportTask } from '@/store'
import { ElMessage, ElMessageBox } from 'element-plus'
const router = useRouter()
 
const request = inject('request')
 
const downloadTask = useDownloadTask()
const ExportTask = useExportTask()
 
window.electronAPI.onLogout((data) => {
  localStorage.clear()
  router.replace({
    path: '/login'
  })
})
 
window.electronAPI.onOpenUrl((data) => {
  let taskInfo = null
  let downloadInfo = decodeURI(data)
  console.log(downloadInfo, '接受到的taskInfo')
  try {
    taskInfo = JSON.parse(downloadInfo)
  } catch (error) {
    taskInfo = null
  }
  if (taskInfo) {
    router.replace({
      path: '/transmission'
    })
    window.electronAPI.newDownloadTask(taskInfo)
  }
})
 
// 绑定消息提醒
window.electronAPI.onShowMessage((data) => {
  // 获取到消息后修改全局数据,页面监听全局数据进行变化
  if (data.showType) {
    switch (data.showType) {
      case 'DownloadTask':
        downloadTask.setMsgData(data)
        break
      case 'ExportTask':
        ExportTask.setMsgData(data)
        break
    }
  } else {
    downloadTask.setMsgData(data)
  }
})
 
// 绑定下载任务变化更新
window.electronAPI.onDownloadTaskChange((task) => {
  downloadTask.setUpdateList()
})
 
// 绑定导出任务变化更新
window.electronAPI.onExportTaskChange((task) => {
  ExportTask.setUpdateList()
})
 
const showUpdateInfo = ref(false)
const updateDownloadInfo = ref()
 
// 监听程序更新下载
window.electronAPI.onUpdateDownloadProgress((data) => {
  showUpdateInfo.value = true
  console.log(data, 'updateDownloadInfo')
  updateDownloadInfo.value = data
})
 
// 监听程序更新下载完成
window.electronAPI.onUpdateDownloadSuccess((data) => {
  showUpdateInfo.value = false
  ElMessageBox.confirm('检测到新版本,安装包已下载完成,是否立即更新?', '检查更新', {
    confirmButtonText: '更新',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      window.electronAPI.updateApp()
    })
    .catch(() => {})
})
 
const token = localStorage.getItem('token')
// if (token) {
//   request({
//     url: '/identity/User/GetCurrentUser',
//     method: 'post'
//   }).then((res) => {
//     // console.log(res)
//   })
// } else {
//   router.replace({
//     path: '/login'
//   })
// }
</script>
 
<style>
.updateDownloadInfo {
  position: fixed;
  top: 2px;
  left: 82px;
  right: 2px;
  z-index: 999;
}
</style>