feat: 版本更新检测
This commit is contained in:
1 parent
8392b1b159
commit
e67bfcca52
2 files changed
+75
-27
No files matched your search
+14
-5
@@ -3,8 +3,8 @@
|
||||
<h1 class="text-2xl font-bold text-gray-900 dark:text-white mb-8">关于</h1>
|
||||
|
||||
<div class="space-y-6">
|
||||
<!-- 版本信息 -->
|
||||
<div class="card p-6">
|
||||
<!-- 版本信息(仅登录后显示) -->
|
||||
<div v-if="authStore.isAuthenticated" class="card p-6">
|
||||
<h2 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center gap-2">
|
||||
<Icon name="heroicons:information-circle" class="w-5 h-5 text-primary-500" />
|
||||
版本信息
|
||||
@@ -179,12 +179,15 @@
|
||||
|
||||
<script setup>
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { useAuthStore } from '~/stores/auth'
|
||||
|
||||
// 页面元数据
|
||||
definePageMeta({
|
||||
title: '关于'
|
||||
})
|
||||
|
||||
const authStore = useAuthStore()
|
||||
|
||||
// 版本信息
|
||||
const versionInfo = reactive({
|
||||
currentVersion: '1.0.0',
|
||||
@@ -197,11 +200,15 @@ const loading = ref(false)
|
||||
|
||||
// 检查版本更新
|
||||
async function checkVersion() {
|
||||
if (!authStore.isAuthenticated) return
|
||||
|
||||
loading.value = true
|
||||
versionInfo.error = null
|
||||
|
||||
try {
|
||||
const response = await $fetch('/api/version/check')
|
||||
const response = await $fetch('/api/version/check', {
|
||||
headers: authStore.authHeader
|
||||
})
|
||||
|
||||
if (response.success) {
|
||||
versionInfo.currentVersion = response.data.currentVersion
|
||||
@@ -218,8 +225,10 @@ async function checkVersion() {
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载时自动检测版本
|
||||
// 页面加载时自动检测版本(仅登录后)
|
||||
onMounted(() => {
|
||||
checkVersion()
|
||||
if (authStore.isAuthenticated) {
|
||||
checkVersion()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -1,8 +1,31 @@
|
||||
import { readFileSync } from 'fs'
|
||||
import { resolve } from 'path'
|
||||
import { verifyToken } from '../../utils/jwt'
|
||||
|
||||
// GitHub package.json 的 URL
|
||||
const PROXY_URL = 'https://cf.111443.xyz/https://raw.githubusercontent.com/chaos-zhu/easyimg/main/package.json'
|
||||
const DIRECT_URL = 'https://raw.githubusercontent.com/chaos-zhu/easyimg/main/package.json'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// 验证登录状态
|
||||
const authHeader = getHeader(event, 'authorization')
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return {
|
||||
success: false,
|
||||
message: '需要登录后才能检测版本更新'
|
||||
}
|
||||
}
|
||||
|
||||
const token = authHeader.substring(7)
|
||||
const decoded = verifyToken(token)
|
||||
if (!decoded) {
|
||||
return {
|
||||
success: false,
|
||||
message: '登录已过期,请重新登录'
|
||||
}
|
||||
}
|
||||
|
||||
// 读取本地 package.json 获取当前版本
|
||||
const packageJsonPath = resolve(process.cwd(), 'package.json')
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'))
|
||||
@@ -13,30 +36,20 @@ export default defineEventHandler(async (event) => {
|
||||
let hasUpdate = false
|
||||
let error = null
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
'https://cf.111443.xyz/https://raw.githubusercontent.com/chaos-zhu/easyimg/main/package.json',
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'EasyImg-Version-Check'
|
||||
}
|
||||
}
|
||||
)
|
||||
// 尝试通过代理获取
|
||||
let remotePackage = await fetchPackageJson(PROXY_URL)
|
||||
|
||||
if (response.ok) {
|
||||
const remotePackage = await response.json()
|
||||
latestVersion = remotePackage.version
|
||||
// 如果代理失败,使用源地址兜底
|
||||
if (!remotePackage) {
|
||||
remotePackage = await fetchPackageJson(DIRECT_URL)
|
||||
}
|
||||
|
||||
// 比较版本号
|
||||
if (latestVersion) {
|
||||
hasUpdate = compareVersions(latestVersion, currentVersion) > 0
|
||||
}
|
||||
} else {
|
||||
error = `GitHub 请求失败: ${response.status}`
|
||||
}
|
||||
} catch (fetchError) {
|
||||
error = `无法连接到 GitHub: ${fetchError.message}`
|
||||
if (remotePackage && remotePackage.version) {
|
||||
latestVersion = remotePackage.version
|
||||
// 比较版本号
|
||||
hasUpdate = compareVersions(latestVersion, currentVersion) > 0
|
||||
} else {
|
||||
error = '无法获取最新版本信息'
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -56,6 +69,32 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 从指定 URL 获取 package.json
|
||||
* @param {string} url - 请求地址
|
||||
* @returns {object|null} - package.json 对象或 null
|
||||
*/
|
||||
async function fetchPackageJson(url) {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'EasyImg-Version-Check'
|
||||
},
|
||||
// 设置超时时间为 10 秒
|
||||
signal: AbortSignal.timeout(10000)
|
||||
})
|
||||
|
||||
if (response.ok) {
|
||||
return await response.json()
|
||||
}
|
||||
} catch (err) {
|
||||
// 请求失败,返回 null
|
||||
console.log(`版本检测请求失败 (${url}):`, err.message)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个语义化版本号
|
||||
* @param {string} v1 - 版本号1
|
||||
|
||||
Reference in new issue
Block a user