diff --git a/src/utils/index.ts b/src/utils/index.ts index c2aff2539..e92981a50 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,6 +6,7 @@ export * from './auth' export * from './build-menus' export * from './rest' export * from './time' +export * from './version' /** * diff 两层,Object 浅层比较,引用不一致返回整个不一样的 Object @@ -81,7 +82,7 @@ export function toPascalCase(string: string) { .replaceAll(new RegExp(/[^\s\w]/, 'g'), '') .replaceAll( new RegExp(/\s+(.)(\w*)/, 'g'), - ($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`, + (_$1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`, ) .replace(new RegExp(/\w/), (s) => s.toUpperCase()) } diff --git a/src/utils/version.ts b/src/utils/version.ts new file mode 100644 index 000000000..b316d5b52 --- /dev/null +++ b/src/utils/version.ts @@ -0,0 +1,59 @@ +export function isNewerVersion(current: string, latest: string): boolean { + const cleanCurrent = current.replace(/^v/, '') + const cleanLatest = latest.replace(/^v/, '') + const [currentBase, currentPre] = splitVersion(cleanCurrent) + const [latestBase, latestPre] = splitVersion(cleanLatest) + const currentParts = currentBase.split('.').map(Number) + const latestParts = latestBase.split('.').map(Number) + + const maxLength = Math.max(currentParts.length, latestParts.length) + for (let i = 0; i < maxLength; i++) { + const currentPart = currentParts[i] || 0 + const latestPart = latestParts[i] || 0 + + if (latestPart > currentPart) return true + if (latestPart < currentPart) return false + } + + if (!latestPre && currentPre) return true + if (latestPre && !currentPre) return false + + if (latestPre && currentPre) { + return comparePrereleaseVersion(currentPre, latestPre) + } + + return false +} + +function splitVersion(version: string): [string, string] { + const hyphenIndex = version.indexOf('-') + if (hyphenIndex === -1) { + return [version, ''] + } + return [version.substring(0, hyphenIndex), version.substring(hyphenIndex + 1)] +} + +function comparePrereleaseVersion(current: string, latest: string): boolean { + const order = ['alpha', 'beta', 'rc'] + + const currentType = order.find((type) => current.startsWith(type)) + const latestType = order.find((type) => latest.startsWith(type)) + + if (currentType && latestType && currentType !== latestType) { + return order.indexOf(latestType) > order.indexOf(currentType) + } + + const currentNum = extractNumber(current) + const latestNum = extractNumber(latest) + + if (currentNum !== null && latestNum !== null) { + return latestNum > currentNum + } + + return latest > current +} + +function extractNumber(str: string): number | null { + const match = str.match(/\d+/) + return match ? parseInt(match[0], 10) : null +} diff --git a/src/views/dashboard/index.tsx b/src/views/dashboard/index.tsx index ad5261bbc..9d185aeb6 100644 --- a/src/views/dashboard/index.tsx +++ b/src/views/dashboard/index.tsx @@ -68,6 +68,7 @@ import { RouteName } from '~/router/name' import { AppStore } from '~/stores/app' import { UserStore } from '~/stores/user' import { parseDate, RESTManager } from '~/utils' +import { isNewerVersion } from '~/utils/version' import PKG from '../../../package.json' import { Card } from './card' @@ -190,12 +191,12 @@ export const DashBoardView = defineComponent({ trigger="hover" triggerEl={{userStore.user.value?.lastLoginIp}} ip={userStore.user.value?.lastLoginIp} - > + /> ) : ( 'N/A' )} -
+ 上次登录时间:{' '} {userStore.user.value?.lastLoginTime ? ( @@ -211,7 +212,7 @@ export const DashBoardView = defineComponent({ - + > )) @@ -640,7 +641,8 @@ const AppIF = defineComponent({ system: null as string | null, }) - const { openModal: openUpdateModal, Modal: UpdateDetailModal } = useUpdateDetailModal() + const { openModal: openUpdateModal, Modal: UpdateDetailModal } = + useUpdateDetailModal() const portal = usePortalElement() const handleUpdate = () => { @@ -657,7 +659,7 @@ const AppIF = defineComponent({ const { dashboard, system } = await checkUpdateFromGitHub() if ( - dashboard !== PKG.version && + isNewerVersion(PKG.version, dashboard) && closedTips.value.dashboard !== dashboard ) { const $notice = notice.info({ @@ -665,7 +667,7 @@ const AppIF = defineComponent({ content: () => ({`当前版本:${PKG.version},最新版本:${dashboard}`}
-{`当前版本:${app.value?.version || 'N/A'},最新版本:${versionMap.value.system}`}
-