仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ templates/tdesign/spa/src/admin/views/UpdateView.vue 13.2 KB · 415 行 原始文件
Z zfhsh first commit 2 个月前
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-cloud-download"></i>系统更新</h3>
6 <p class="td-page-subtitle">检查新版本并执行在线更新</p>
7 </div>
8 <t-button theme="default" variant="outline" @click="recheck">
9 <i class="mdi mdi-refresh"></i> 重新检查
10 </t-button>
11 </div>
12
13 <div class="upd-wrap">
14 <!-- 错误态 -->
15 <div v-if="!info" class="td-set-card">
16 <div class="td-set-card-hd">
17 <div class="td-set-icon" style="background: #fdecee; color: #d54941">
18 <i class="mdi mdi-alert-circle-outline"></i>
19 </div>
20 <div>
21 <h4>检查更新失败</h4>
22 <p>无法连接到更新服务器,请稍后重试</p>
23 </div>
24 </div>
25 </div>
26
27 <!-- 正常态 -->
28 <template v-else>
29 <!-- 版本卡片 -->
30 <div class="stat-row">
31 <div class="stat-card">
32 <div class="stat-icon" style="background: #0052d9">
33 <i class="mdi mdi-tag"></i>
34 </div>
35 <div>
36 <div class="stat-num">{{ currentVersion }}</div>
37 <div class="stat-label">当前版本</div>
38 </div>
39 </div>
40 <div class="stat-card">
41 <div class="stat-icon" :style="{ background: hasUpdate ? '#e37318' : '#2ba471' }">
42 <i class="mdi" :class="hasUpdate ? 'mdi-arrow-up-bold-circle' : 'mdi-check-circle'"></i>
43 </div>
44 <div>
45 <div class="stat-num">{{ latestVersion }}</div>
46 <div class="stat-label">最新版本</div>
47 </div>
48 </div>
49 </div>
50
51 <!-- 更新提示 -->
52 <div v-if="hasUpdate" class="td-set-card">
53 <div class="td-set-card-hd">
54 <div class="td-set-icon" style="background: #fff3e0; color: #e37318">
55 <i class="mdi mdi-alert-circle"></i>
56 </div>
57 <div>
58 <h4>发现新版本</h4>
59 <p>{{ info.msg || '建议尽快更新以获取最新功能与安全修复' }}</p>
60 </div>
61 <t-button theme="warning" :loading="updating" @click="doUpdate" class="upd-btn">
62 <i class="mdi mdi-update"></i> 立刻更新
63 </t-button>
64 </div>
65 <div class="td-set-card-bd">
66 <h5 class="upd-section">下载来源</h5>
67 <table class="upd-kv">
68 <tr><td>更新仓库</td><td>{{ info.repo }}</td></tr>
69 <tr><td>包来源</td><td>{{ info.sourceLabel }}<template v-if="info.assetSize">({{ mb(info.assetSize) }})</template></td></tr>
70 <tr><td>下载通道</td><td>github.com 直连<template v-if="info.mirrors && info.mirrors.length">,失败后依次尝试:{{ info.mirrors.join('、') }}</template></td></tr>
71 <tr v-if="info.publishedAt"><td>发布时间</td><td>{{ String(info.publishedAt).slice(0, 10) }}</td></tr>
72 <tr v-if="info.fallback"><td>检查方式</td><td>latest 接口不可用,已改用发布列表取最高版本</td></tr>
73 </table>
74 </div>
75 <div class="td-set-card-bd" v-if="info.uplog">
76 <h5 class="upd-section">更新日志</h5>
77 <pre class="td-code-block upd-log">{{ info.uplog }}</pre>
78 </div>
79 </div>
80
81 <!-- 已是最新 -->
82 <div v-else-if="isLatest" class="td-set-card">
83 <div class="td-set-card-hd">
84 <div class="td-set-icon" style="background: #e8f8f0; color: #2ba471">
85 <i class="mdi mdi-check-circle"></i>
86 </div>
87 <div>
88 <h4>已是最新版本</h4>
89 <p>{{ info.msg || '当前版本已是最新,无需更新' }}</p>
90 </div>
91 </div>
92 <div class="td-set-card-bd">
93 <h5 class="upd-section">下载来源</h5>
94 <table class="upd-kv">
95 <tr><td>更新仓库</td><td>{{ info.repo }}</td></tr>
96 <tr><td>包来源</td><td>{{ info.sourceLabel }}</td></tr>
97 <tr><td>下载通道</td><td>github.com 直连<template v-if="info.mirrors && info.mirrors.length">,失败后依次尝试:{{ info.mirrors.join('、') }}</template></td></tr>
98 </table>
99 </div>
100 <div class="td-set-card-bd" v-if="info.uplog">
101 <h5 class="upd-section">版本日志</h5>
102 <pre class="td-code-block upd-log">{{ info.uplog }}</pre>
103 </div>
104 </div>
105
106 <!-- 检查失败 -->
107 <div v-else class="td-set-card">
108 <div class="td-set-card-hd">
109 <div class="td-set-icon" style="background: #f5f6f8; color: #8c8c8c">
110 <i class="mdi mdi-cloud-off"></i>
111 </div>
112 <div>
113 <h4>{{ info.msg || '暂时无法检查更新' }}</h4>
114 <p>更新只走 GitHub Release,请检查服务器能否访问 api.github.com 或更新设置里的仓库与镜像</p>
115 </div>
116 </div>
117 </div>
118 </template>
119
120 <!-- 更新设置 -->
121 <div class="td-set-card">
122 <div class="td-set-card-hd">
123 <div class="td-set-icon" style="background: #eff6ff; color: #2563eb">
124 <i class="mdi mdi-cog-outline"></i>
125 </div>
126 <div>
127 <h4>更新设置</h4>
128 <p>GitHub 仓库、下载镜像与可选 Token</p>
129 </div>
130 </div>
131 <div class="td-set-card-bd">
132 <div class="upd-field">
133 <label>GitHub 仓库</label>
134 <t-input v-model="form.repo" placeholder="owner/repo" :maxlength="140" />
135 <p class="upd-hint">更新包只能从这个仓库的 Release 里取。</p>
136 </div>
137 <div class="upd-field">
138 <label>下载镜像</label>
139 <div v-for="(m, i) in form.mirrors" :key="i" class="upd-mirror-row">
140 <t-input v-model="form.mirrors[i]" placeholder="https://gh-proxy.com/" :maxlength="160" />
141 <t-button theme="default" variant="outline" @click="delMirror(i)">
142 <i class="mdi mdi-close"></i>
143 </t-button>
144 </div>
145 <t-button theme="default" variant="outline" @click="addMirror">
146 <i class="mdi mdi-plus"></i> 添加镜像
147 </t-button>
148 <p class="upd-hint">先 github.com 直连,再按上面的顺序依次尝试镜像;镜像地址会在原始 GitHub 地址前面加前缀。</p>
149 </div>
150 <div class="upd-field">
151 <label>GitHub Token(可选)</label>
152 <t-input v-model="form.token" type="password" :placeholder="form.hasToken ? '已设置,留空表示不修改' : '仅用于提高 API 速率限制,可留空'" :maxlength="128" />
153 <label v-if="form.hasToken" class="upd-clear">
154 <input type="checkbox" v-model="form.clearToken" /> 清除已保存的 Token
155 </label>
156 <p class="upd-hint">Token 只用于调用 GitHub API,不会跟着下载请求发给镜像,页面也不回显明文。</p>
157 </div>
158 <div class="upd-actions">
159 <t-button theme="primary" :loading="saving" @click="saveSetting">
160 <i class="mdi mdi-content-save-outline"></i> 保存更新设置
161 </t-button>
162 </div>
163 <p class="upd-note">
164 更新会直接用 GitHub Release 的包覆盖站点文件,覆盖前会备份并还原
165 <code>config.php</code>、<code>cf_up.php</code>、<code>MPHX/SQ.php</code>、<code>install/install.lock</code>、<code>api/cookie/</code>;
166 包里 <code>update/update_v*_*.sql</code> 的版本化迁移会按版本号依次执行(仅跑游标 <code>MN_dbver</code> 之后的增量),旧式单文件 <code>update/update.sql</code> 仍兼容。请在维护时段操作。
167 </p>
168 </div>
169 </div>
170 </div>
171 </div>
172</template>
173
174<script setup>
175import { ref, reactive, computed } from 'vue'
176import { MessagePlugin } from 'tdesign-vue-next'
177import { systemUpdate, updaterCheck, saveUpdaterConfig } from '@/admin/api/dashboard'
178
179const boot = window.__TD_BOOT__ || {}
180
181// 服务器端已检查更新,直接读取 boot
182const info = ref(boot.updateInfo || null)
183const currentVersion = ref(boot.currentVersion || 'V0.00')
184
185const hasUpdate = computed(() => info.value && String(info.value.code) === '1')
186const isLatest = computed(() => info.value && String(info.value.code) === '0')
187
188const latestVersion = computed(() => {
189 if (!info.value || !info.value.ver) return '-'
190 return info.value.ver
191})
192
193const updating = ref(false)
194const saving = ref(false)
195
196const cfg = boot.updaterConfig || {}
197const form = reactive({
198 repo: cfg.repo || '',
199 mirrors: Array.isArray(cfg.mirrors) ? cfg.mirrors.slice() : [],
200 token: '',
201 hasToken: Number(cfg.hasToken) === 1,
202 clearToken: false,
203})
204
205function mb(bytes) {
206 return (Number(bytes) / 1048576).toFixed(1) + ' MB'
207}
208
209function addMirror() {
210 form.mirrors.push('')
211}
212
213function delMirror(i) {
214 if (form.mirrors.length <= 1) {
215 form.mirrors[0] = ''
216 return
217 }
218 form.mirrors.splice(i, 1)
219}
220
221async function doUpdate() {
222 updating.value = true
223 const r = await systemUpdate()
224 updating.value = false
225 if (r.ok) {
226 MessagePlugin.success('更新成功,请手动刷新页面')
227 setTimeout(() => {
228 window.location.reload()
229 }, 1200)
230 } else {
231 MessagePlugin.error(r.message || '更新失败')
232 }
233}
234
235async function recheck() {
236 const r = await updaterCheck()
237 if (r.ok && r.data && typeof r.data === 'object') {
238 info.value = normalizeInfo(r.data)
239 if (r.data.current) currentVersion.value = r.data.current
240 MessagePlugin.success('已重新检查')
241 } else {
242 // 接口不可用时退回整页重查
243 window.location.href = 'update.php?recheck=1'
244 }
245}
246
247async function saveSetting() {
248 const mirrors = form.mirrors.map((m) => String(m || '').trim()).filter((m, i, arr) => m !== '' && arr.indexOf(m) === i)
249 const payload = { repo: form.repo.trim(), mirrors }
250 if (form.token && form.token.trim() !== '') payload.github_token = form.token.trim()
251 if (form.clearToken) payload.clear_token = '1'
252 saving.value = true
253 const r = await saveUpdaterConfig(payload)
254 saving.value = false
255 if (r.ok) {
256 MessagePlugin.success('保存成功')
257 form.token = ''
258 form.clearToken = false
259 form.mirrors = mirrors.length ? mirrors.slice() : ['']
260 setTimeout(() => window.location.reload(), 1200)
261 } else {
262 MessagePlugin.error(r.message || '保存失败')
263 }
264}
265
266// 后端 upcheck 返回的是原始检查结果,转成页面用的 updateInfo 结构
267function normalizeInfo(d) {
268 const ok = Number(d.ok) === 1
269 const can = Number(d.can_update) === 1
270 return {
271 code: ok ? (can ? '1' : '0') : '-1',
272 ver: ok ? d.latest : '-',
273 msg: !ok
274 ? '暂时无法检查更新:' + (d.error || '')
275 : can
276 ? '发现新版本,可执行在线覆盖更新'
277 : '当前版本已是最新,无需更新',
278 uplog: d.body || '',
279 repo: d.repo,
280 tag: d.tag,
281 name: d.name,
282 source: d.source,
283 sourceLabel: d.source_label,
284 assetName: d.asset_name,
285 assetUrl: d.asset_url,
286 assetSize: d.asset_size,
287 publishedAt: d.published_at,
288 fallback: d.fallback,
289 mirrors: d.mirrors || [],
290 currentVer: d.current,
291 }
292}
293</script>
294
295<style scoped>
296.upd-wrap {
297 display: flex;
298 flex-direction: column;
299 gap: 14px;
300}
301.stat-row {
302 display: flex;
303 flex-wrap: wrap;
304 gap: 12px;
305}
306.stat-card {
307 flex: 1;
308 min-width: 200px;
309 background: var(--td-surface);
310 border: 1px solid var(--td-border);
311 border-radius: var(--td-radius-lg);
312 padding: 16px;
313 display: flex;
314 align-items: center;
315 gap: 12px;
316 box-shadow: var(--td-shadow);
317}
318.stat-icon {
319 width: 44px;
320 height: 44px;
321 border-radius: 8px;
322 display: grid;
323 place-items: center;
324 color: #fff;
325 font-size: 22px;
326 flex-shrink: 0;
327}
328.stat-num {
329 font-size: 20px;
330 font-weight: 700;
331 color: var(--td-text);
332 line-height: 1.2;
333}
334.stat-label {
335 font-size: 12px;
336 color: var(--td-text-secondary);
337 margin-top: 2px;
338}
339.upd-btn {
340 margin-left: auto;
341}
342.upd-section {
343 margin: 0 0 8px;
344 font-size: 13px;
345 font-weight: 600;
346 color: var(--td-text);
347}
348.upd-log {
349 white-space: pre-wrap;
350 word-break: break-word;
351 max-height: 320px;
352 overflow-y: auto;
353 margin: 0;
354}
355.upd-kv {
356 width: 100%;
357 border-collapse: collapse;
358}
359.upd-kv td {
360 font-size: 13px;
361 padding: 6px 0;
362 border-bottom: 1px solid #f1f5f9;
363 color: var(--td-text);
364 word-break: break-all;
365}
366.upd-kv td:first-child {
367 color: var(--td-text-secondary);
368 width: 96px;
369 white-space: nowrap;
370}
371.upd-field {
372 margin-bottom: 16px;
373}
374.upd-field > label {
375 display: block;
376 font-size: 13px;
377 font-weight: 500;
378 color: var(--td-text);
379 margin-bottom: 6px;
380}
381.upd-mirror-row {
382 display: flex;
383 gap: 8px;
384 margin-bottom: 8px;
385}
386.upd-clear {
387 display: block;
388 margin-top: 8px;
389 font-size: 12px;
390 color: var(--td-text-secondary);
391}
392.upd-hint {
393 margin: 6px 0 0;
394 font-size: 12px;
395 color: var(--td-text-secondary);
396 line-height: 1.7;
397}
398.upd-actions {
399 margin-top: 6px;
400}
401.upd-note {
402 margin: 16px 0 0;
403 padding: 12px 14px;
404 background: #f8fafc;
405 border: 1px solid #eef1f5;
406 font-size: 12px;
407 line-height: 1.8;
408 color: var(--td-text-secondary);
409}
410.upd-note code {
411 background: #fff;
412 border: 1px solid #e8ecf1;
413 padding: 1px 4px;
414}
415</style>