SystemUpdatePanel.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <script setup lang="ts">
  2. import { computed, onBeforeUnmount, ref, watch } from 'vue'
  3. import AppIcon from '@/components/common/AppIcon.vue'
  4. import {
  5. applySystemUpdate,
  6. checkSystemUpdate,
  7. rollbackSystemUpdate,
  8. systemUpdateJob,
  9. systemUpdateStatus,
  10. type SystemUpdateStatus,
  11. type UpdateJob,
  12. } from '@/api/systemUpdate'
  13. const props = withDefaults(defineProps<{ active?: boolean }>(), { active: true })
  14. const emit = defineEmits<{ 'lock-change': [locked: boolean] }>()
  15. const status = ref<SystemUpdateStatus>()
  16. const job = ref<UpdateJob>()
  17. const busy = ref(false)
  18. const checking = ref(false)
  19. const error = ref('')
  20. const trackingTimedOut = ref(false)
  21. const operationStarting = ref(false)
  22. let pollToken = 0
  23. let lifecycleToken = 0
  24. const terminal = computed(() => job.value && ['succeeded', 'failed', 'rolled_back'].includes(job.value.state))
  25. const jobRunning = computed(() => Boolean(job.value && !terminal.value))
  26. const locked = computed(() => operationStarting.value || (jobRunning.value && !trackingTimedOut.value))
  27. const currentPlugin = computed(() => status.value?.installedPluginVersion || '未安装')
  28. const canApply = computed(() => Boolean(status.value?.supported && status.value.updateAvailable && status.value.latest && !jobRunning.value && !busy.value))
  29. function stopPolling() { pollToken += 1 }
  30. function reloadPage() { window.location.reload() }
  31. async function refresh(checkRemote = false): Promise<SystemUpdateStatus | undefined> {
  32. error.value = ''
  33. checking.value = checkRemote
  34. try {
  35. status.value = checkRemote ? await checkSystemUpdate() : await systemUpdateStatus()
  36. if (status.value.job) job.value = status.value.job
  37. return status.value
  38. } catch (cause) {
  39. error.value = cause instanceof Error ? cause.message : '无法读取系统版本'
  40. } finally {
  41. checking.value = false
  42. }
  43. return undefined
  44. }
  45. async function poll(next: UpdateJob) {
  46. job.value = next
  47. trackingTimedOut.value = false
  48. const token = ++pollToken
  49. for (let attempt = 0; attempt < 300 && token === pollToken; attempt += 1) {
  50. if (['succeeded', 'failed', 'rolled_back'].includes(job.value.state)) break
  51. await new Promise(resolvePromise => window.setTimeout(resolvePromise, 1_000))
  52. try {
  53. job.value = await systemUpdateJob(next.id)
  54. } catch {
  55. // 8800 会在原子切换期间短暂不可用;继续等待新服务读取同一任务文件。
  56. }
  57. }
  58. if (token === pollToken && job.value && !['succeeded', 'failed', 'rolled_back'].includes(job.value.state)) {
  59. error.value = '升级仍在后台执行,请稍后重新打开此页面查看'
  60. trackingTimedOut.value = true
  61. }
  62. if (token === pollToken && !trackingTimedOut.value) await refresh(false)
  63. }
  64. async function loadAndResume(token: number) {
  65. await refresh(false)
  66. if (token !== lifecycleToken || !props.active) return
  67. if (job.value && !['succeeded', 'failed', 'rolled_back'].includes(job.value.state)) {
  68. await poll(job.value)
  69. return
  70. }
  71. await refresh(true)
  72. if (token !== lifecycleToken || !props.active) return
  73. if (job.value && !['succeeded', 'failed', 'rolled_back'].includes(job.value.state)) await poll(job.value)
  74. }
  75. async function applyUpdate() {
  76. const target = status.value?.latest
  77. if (!target || !window.confirm(`升级 Web 到 ${target.webVersion}?插件 ${target.pluginVersion} 将通过 9119 更新。`)) return
  78. const token = lifecycleToken
  79. operationStarting.value = true
  80. busy.value = true
  81. error.value = ''
  82. try {
  83. const next = await applySystemUpdate(target.releaseVersion)
  84. if (token !== lifecycleToken || !props.active) return
  85. operationStarting.value = false
  86. await poll(next)
  87. } catch (cause) {
  88. if (token === lifecycleToken) error.value = cause instanceof Error ? cause.message : '无法启动系统升级'
  89. } finally {
  90. if (token === lifecycleToken) {
  91. operationStarting.value = false
  92. busy.value = false
  93. }
  94. }
  95. }
  96. async function rollback() {
  97. if (!window.confirm('回滚到上一次 Web 版本?插件继续由 9119 保持最新版本。')) return
  98. const token = lifecycleToken
  99. operationStarting.value = true
  100. busy.value = true
  101. error.value = ''
  102. try {
  103. const next = await rollbackSystemUpdate()
  104. if (token !== lifecycleToken || !props.active) return
  105. operationStarting.value = false
  106. await poll(next)
  107. } catch (cause) {
  108. if (token === lifecycleToken) error.value = cause instanceof Error ? cause.message : '无法启动系统回滚'
  109. } finally {
  110. if (token === lifecycleToken) {
  111. operationStarting.value = false
  112. busy.value = false
  113. }
  114. }
  115. }
  116. watch(locked, value => emit('lock-change', value), { immediate: true })
  117. watch(() => props.active, active => {
  118. const token = ++lifecycleToken
  119. stopPolling()
  120. error.value = ''
  121. trackingTimedOut.value = false
  122. operationStarting.value = false
  123. if (!active) {
  124. emit('lock-change', false)
  125. return
  126. }
  127. job.value = undefined
  128. busy.value = true
  129. void loadAndResume(token)
  130. .finally(() => { if (token === lifecycleToken) busy.value = false })
  131. }, { immediate: true })
  132. onBeforeUnmount(() => { lifecycleToken += 1; operationStarting.value = false; stopPolling(); emit('lock-change', false) })
  133. </script>
  134. <template>
  135. <section class="system-update-panel" aria-label="更新与回滚">
  136. <p class="system-update-intro">Web 服务独立升级;Hermes Dashboard 插件由 9119 自动更新到最新兼容版本。</p>
  137. <p v-if="error" class="system-update-error" role="alert"><AppIcon name="alert" :size="16" />{{ error }}</p>
  138. <section v-if="status" class="version-grid" aria-label="版本信息">
  139. <article><small>当前 Web</small><strong>{{ status.current.webVersion }}</strong></article>
  140. <article><small>当前插件</small><strong>{{ currentPlugin }}</strong></article>
  141. <article><small>最新 Web</small><strong>{{ status.latest?.webVersion || status.current.webVersion }}</strong></article>
  142. <article><small>配套插件</small><strong>{{ status.latest?.pluginVersion || status.current.pluginVersion }}</strong></article>
  143. </section>
  144. <p v-if="status && !status.versionsMatch" class="version-warning"><AppIcon name="alert" :size="15" />当前插件版本落后,登录后会自动通过 9119 更新。</p>
  145. <p v-if="status?.installationMode === 'source'" class="mode-note">首次升级会把运行服务迁移到可回滚的版本目录;Git 工作区不会被覆盖。</p>
  146. <p v-if="status && !status.supported" class="mode-note">{{ status.unsupportedReason }}</p>
  147. <section v-if="job" class="update-progress" :class="`update-progress--${job.state}`" aria-live="polite">
  148. <span class="progress-icon"><AppIcon :name="job.state === 'succeeded' || job.state === 'rolled_back' ? 'check' : job.state === 'failed' ? 'alert' : 'refresh'" :size="18" /></span>
  149. <span><strong>{{ job.message }}</strong><small v-if="job.error">{{ job.error }}</small></span>
  150. </section>
  151. <footer>
  152. <button v-if="job?.state === 'succeeded' || job?.state === 'rolled_back'" class="quiet-button" type="button" @click="reloadPage"><AppIcon name="refresh" :size="16" />刷新页面</button>
  153. <button v-else-if="status?.canRollback && !jobRunning" class="quiet-button danger" type="button" :disabled="busy" @click="rollback">回滚上一版本</button>
  154. <button class="quiet-button" type="button" :disabled="checking || busy || operationStarting || jobRunning" @click="refresh(true)"><AppIcon name="refresh" :size="16" />{{ checking ? '检查中…' : '检查更新' }}</button>
  155. <button class="solid-button" type="button" :disabled="!canApply" @click="applyUpdate"><AppIcon name="download" :size="16" />{{ jobRunning ? '升级中…' : status?.updateAvailable ? '升级配套版本' : '已是最新版本' }}</button>
  156. </footer>
  157. </section>
  158. </template>
  159. <style scoped>
  160. .system-update-panel { display: grid; gap: 18px; }
  161. .system-update-intro { margin: 0; max-width: 620px; color: var(--text-secondary); font-size: 14px; line-height: 1.65; }
  162. .system-update-error,.version-warning { display: flex; align-items: flex-start; gap: 8px; margin: 0; padding: 12px 14px; border-radius: 10px; font-size: 13px; line-height: 1.55; }
  163. .system-update-error { background: color-mix(in srgb, var(--danger) 9%, transparent); color: var(--danger); }
  164. .version-warning { background: color-mix(in srgb, var(--warning, #bd7611) 10%, transparent); color: var(--text-secondary); }
  165. .version-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border-block: 1px solid var(--line); }
  166. .version-grid article { display: grid; gap: 7px; padding: 18px 20px; border-bottom: 1px solid var(--line); }
  167. .version-grid article:nth-child(odd) { border-right: 1px solid var(--line); }
  168. .version-grid article:nth-last-child(-n+2) { border-bottom: 0; }
  169. .version-grid small { color: var(--text-muted); font-size: 12px; }
  170. .version-grid strong { font-size: 20px; letter-spacing: -.02em; }
  171. .mode-note { margin: -4px 0 0; color: var(--text-muted); font-size: 12px; line-height: 1.55; }
  172. .update-progress { display: flex; gap: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-soft); }
  173. .progress-icon { display: grid; width: 34px; height: 34px; flex: 0 0 34px; place-items: center; border-radius: 50%; background: var(--surface-raised); color: var(--text-secondary); }
  174. .update-progress > span:last-child { display: grid; gap: 4px; min-width: 0; }
  175. .update-progress strong { font-size: 14px; }
  176. .update-progress small { color: var(--danger); font-size: 12px; line-height: 1.5; overflow-wrap: anywhere; }
  177. .update-progress:not(.update-progress--failed, .update-progress--succeeded, .update-progress--rolled_back) .progress-icon { animation: update-spin 1s linear infinite; }
  178. .update-progress--succeeded .progress-icon,.update-progress--rolled_back .progress-icon { color: var(--success); }
  179. .update-progress--failed .progress-icon { color: var(--danger); }
  180. footer { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 6px; padding-top: 18px; border-top: 1px solid var(--line); }
  181. button { display: inline-flex; min-height: 40px; align-items: center; justify-content: center; gap: 7px; padding: 0 14px; border: 0; border-radius: 9px; cursor: pointer; font: 600 13px var(--font-ui); }
  182. .quiet-button { border: 1px solid var(--line); background: var(--surface-raised); color: var(--text-secondary); }
  183. .solid-button { background: var(--accent); color: var(--text-on-solid); }
  184. button:disabled { cursor: not-allowed; opacity: .5; }
  185. .danger { margin-right: auto; color: var(--danger); }
  186. @keyframes update-spin { to { transform: rotate(360deg); } }
  187. @media (max-width: 700px) {
  188. .version-grid { grid-template-columns: 1fr; }
  189. .version-grid article,.version-grid article:nth-child(odd),.version-grid article:nth-last-child(-n+2) { border-right: 0; border-bottom: 1px solid var(--line); }
  190. .version-grid article:last-child { border-bottom: 0; }
  191. footer .solid-button { flex: 1 1 100%; }
  192. }
  193. </style>