检测版本更新

close #231
This commit is contained in:
BTMuli
2026-03-10 12:46:46 +08:00
parent b5c7c6e8b1
commit c56b05b4f1
9 changed files with 1103 additions and 941 deletions

View File

@@ -68,6 +68,7 @@
</template>
<script lang="ts" setup>
import TGameNav from "@comp/app/t-gameNav.vue";
import showDialog from "@comp/func/dialog.js";
import showLoading from "@comp/func/loading.js";
import showSnackbar from "@comp/func/snackbar.js";
import PhCompCalendar from "@comp/pageHome/ph-comp-calendar.vue";
@@ -78,6 +79,10 @@ import TSUserAccount from "@Sqlm/userAccount.js";
import useAppStore from "@store/app.js";
import useBBSStore from "@store/bbs.js";
import useHomeStore from "@store/home.js";
import { getVersion } from "@tauri-apps/api/app";
import { invoke } from "@tauri-apps/api/core";
import { openUrl } from "@tauri-apps/plugin-opener";
import { getLatestReleaseVersion } from "@utils/Github.js";
import TGLogger from "@utils/TGLogger.js";
import { storeToRefs } from "pinia";
import { defineComponent, onMounted, ref, shallowRef, watch } from "vue";
@@ -101,7 +106,7 @@ type SelectItem = {
const homeStore = useHomeStore();
const bbsStore = useBBSStore();
const { devMode, isLogin } = storeToRefs(useAppStore());
const { devMode, isLogin, lastUcts } = storeToRefs(useAppStore());
const { gameList } = storeToRefs(bbsStore);
const curGid = ref<number>(2);
@@ -130,6 +135,7 @@ onMounted(async () => {
}
oldItems.value = showItems.value;
await loadComp();
await checkAppUpdate();
});
watch(
@@ -205,6 +211,37 @@ async function loadEnd(item: ReturnType<typeof defineComponent>): Promise<void>
else showSnackbar.warn(`${compName} 已加载`);
if (loadItems.value.length === components.value.length) await showLoading.end();
}
async function checkAppUpdate(): Promise<void> {
const nowTs = Math.floor(Date.now() / 1000);
const diffTime = nowTs - lastUcts.value;
if (diffTime < 60 * 60 * 24) return;
await TGLogger.Info("[Home][CheckAppUpdate]检测版本更新");
const versionApp = await getVersion();
const versionCheck = await getLatestReleaseVersion();
if (versionCheck === "0") return;
if (versionCheck === versionApp) {
await TGLogger.Info(`[Home][CheckAppUpdate]版本号一致:${versionCheck}`);
lastUcts.value = nowTs;
return;
}
await TGLogger.Info(`[Home][CheckAppUpdate]检测到新版本:${versionCheck}`);
const check = await showDialog.checkF({
title: "检测到新版本",
text: `${versionApp}${versionCheck}`,
otcancel: false,
confirmLabel: "前往更新",
cancelLabel: "稍后提醒",
});
lastUcts.value = nowTs;
if (!check) return;
const isMsix = await invoke<boolean>("is_msix");
if (isMsix) {
await openUrl("ms-windows-store://pdp/?ProductId=9nlbnnnbnsjn");
return;
}
await openUrl("https://github.com/BTMuli/TeyvatGuide/releases/latest");
}
</script>
<style lang="scss" scoped>
.home-top-nav {

View File

@@ -1,6 +1,6 @@
/**
* 应用状态管理
* @since Beta v0.9.1
* @since Beta v0.9.8
*/
import bbsEnum from "@enum/bbs.js";
@@ -67,6 +67,11 @@ const useAppStore = defineStore(
const closeToTray = ref<boolean>(false);
/** 展示反馈按钮 */
const showFeedback = ref<boolean>(true);
/**
* 上次检测更新时间
* @remarks LastUpdateCheckTimeStamp
*/
const lastUcts = ref<number>(0);
/**
* 初始化应用状态
@@ -91,6 +96,7 @@ const useAppStore = defineStore(
cancelLike.value = true;
closeToTray.value = false;
showFeedback.value = true;
lastUcts.value = 0;
initDevice();
}
@@ -149,6 +155,7 @@ const useAppStore = defineStore(
cancelLike,
closeToTray,
showFeedback,
lastUcts,
init,
changeTheme,
getImageUrl,
@@ -178,6 +185,7 @@ const useAppStore = defineStore(
"cancelLike",
"closeToTray",
"showFeedback",
"lastUcts",
],
},
{

64
src/types/Plugins/Github.d.ts vendored Normal file
View File

@@ -0,0 +1,64 @@
/**
* Github API 类型
* @since Beta v0.9.8
*/
declare namespace TGApp.Plugins.Github {
/**
* LatestReleaseResponse
* @since Beta v0.9.8
* @see https://api.github.com/repos/BTMuli/TeyvatGuide/releases/latest
* @remarks 省略了不需要的子数据
*/
type LastestReleaseResp = {
/** URL */
url: string;
/** Assets URL */
assets_url: string;
/** Upload URL */
upload_url: string;
/** Html URL */
html_url: string;
/** Release ID */
id: number;
/** Author */
author: unknown;
/** Node ID */
node_id: string;
/** Tag */
tag_name: string;
/** Commit Hash */
target_commitish: string;
/** Release Name */
name: string;
/** Draft */
draft: boolean;
/** Immutable */
immutable: boolean;
/** PreRelease */
prerelease: boolean;
/**
* CreateTime
* @example 2026-02-26T10:33:04Z
*/
created_at: string;
/**
* UpdateTime
* @example 2026-02-27T08:53:27Z
*/
updated_at: string;
/**
* PublishTime
* @example 2026-02-27T08:53:27Z
*/
published_at: string;
/** Assets */
assets: Array<unknown>;
/** Tarball URL */
tarball_url: string;
/** Zipball URL */
zipball_url: string;
/** Release Body */
body: string;
};
}

26
src/utils/Github.ts Normal file
View File

@@ -0,0 +1,26 @@
/**
* Github API
* @since Beta v0.9.8
*/
import TGHttp from "@utils/TGHttp.js";
/**
* 获取最新Release版本
* @since Beta v0.9.8
* @returns 最新版本
*/
export async function getLatestReleaseVersion(): Promise<string> {
const latestReleaseApi: Readonly<string> =
"https://api.github.com/repos/BTMuli/TeyvatGuide/releases/latest";
try {
const latestReleaseResp = await TGHttp<TGApp.Plugins.Github.LastestReleaseResp>(
latestReleaseApi,
{ method: "GET" },
);
return latestReleaseResp.tag_name.replace("v", "");
} catch (e) {
console.error(e);
return "0";
}
}