perf: update version compare logic

This commit is contained in:
tiny-craft 2023-10-07 23:37:12 +08:00
parent d2aa9317b9
commit 477ed19d20
2 changed files with 42 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import { BrowserOpenURL } from 'wailsjs/runtime/runtime.js'
import { i18nGlobal } from '@/utils/i18n.js'
import { enUS, NButton, NSpace, useOsTheme, zhCN } from 'naive-ui'
import { h, nextTick } from 'vue'
import { compareVersion } from '@/utils/version.js'
const osTheme = useOsTheme()
const usePreferencesStore = defineStore('preferences', {
@ -244,7 +245,11 @@ const usePreferencesStore = defineStore('preferences', {
const { success, data = {} } = await CheckForUpdate()
if (success) {
const { version = 'v1.0.0', latest, page_url: pageUrl } = data
if ((manual || latest > this.general.skipVersion) && latest > version && !isEmpty(pageUrl)) {
if (
(manual || latest > this.general.skipVersion) &&
compareVersion(latest, version) > 0 &&
!isEmpty(pageUrl)
) {
const notiRef = $notification.show({
title: i18nGlobal.t('dialogue.upgrade.title'),
content: i18nGlobal.t('dialogue.upgrade.new_version_tip', { ver: latest }),

View File

@ -0,0 +1,36 @@
import { get, isEmpty, map, size, split, trimStart } from 'lodash'
const toVerArr = (ver) => {
const v = trimStart(ver, 'v')
let vParts = split(v, '.')
if (isEmpty(vParts)) {
vParts = ['0']
}
return map(vParts, (v) => {
let vNum = parseInt(v)
return isNaN(vNum) ? 0 : vNum
})
}
/**
* compare two version strings
* @param {string} v1
* @param {string} v2
* @return {number}
*/
export const compareVersion = (v1, v2) => {
if (v1 !== v2) {
const v1Nums = toVerArr(v1)
const v2Nums = toVerArr(v2)
const length = Math.max(size(v1Nums), size(v2Nums))
for (let i = 0; i < length; i++) {
const num1 = get(v1Nums, i, 0)
const num2 = get(v2Nums, i, 0)
if (num1 !== num2) {
return num1 > num2 ? 1 : -1
}
}
}
return 0
}