完成参数获取&剪贴板读取 #42

This commit is contained in:
BTMuli
2023-09-19 16:23:57 +08:00
parent d45d26cafd
commit 9e6b8e9283
4 changed files with 89 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ async fn register_deep_link(app_handle: tauri::AppHandle) {
"teyvatguide", "teyvatguide",
move |request| { move |request| {
dbg!(&request); dbg!(&request);
app_handle.emit_all("test_deep_link", request).unwrap(); app_handle.emit_all("active_deep_link", request).unwrap();
}, },
) )
.unwrap(); .unwrap();
@@ -20,13 +20,13 @@ fn main() {
tauri_plugin_deep_link::prepare("teyvatguide"); tauri_plugin_deep_link::prepare("teyvatguide");
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_sql::Builder::default().build()) .plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![register_deep_link])
.setup(|_app| { .setup(|_app| {
let _window = _app.get_window("TeyvatGuide").unwrap(); let _window = _app.get_window("TeyvatGuide").unwrap();
#[cfg(debug_assertions)] // only include this code on debug builds #[cfg(debug_assertions)] // only include this code on debug builds
_window.open_devtools(); // open the devtools on startup _window.open_devtools(); // open the devtools on startup
Ok(()) Ok(())
}) })
.invoke_handler(tauri::generate_handler![register_deep_link])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@@ -13,10 +13,11 @@
<script lang="ts" setup> <script lang="ts" setup>
// vue // vue
import { onBeforeMount, onMounted, ref } from "vue"; import { onBeforeMount, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import TSidebar from "./components/app/t-sidebar.vue"; import TSidebar from "./components/app/t-sidebar.vue";
import TBackTop from "./components/app/t-backTop.vue"; import TBackTop from "./components/app/t-backTop.vue";
// tauri // tauri
import { app, event, fs, tauri, window } from "@tauri-apps/api"; import { app, event, fs, tauri, window as TauriWindow } from "@tauri-apps/api";
// store // store
import { useAppStore } from "./store/modules/app"; import { useAppStore } from "./store/modules/app";
// utils // utils
@@ -27,13 +28,16 @@ import TGSqlite from "./plugins/Sqlite";
const appStore = useAppStore(); const appStore = useAppStore();
const isMain = ref<boolean>(false); const isMain = ref<boolean>(false);
const theme = ref<string>(appStore.theme); const theme = ref<string>(appStore.theme);
const router = useRouter();
onBeforeMount(async () => { onBeforeMount(async () => {
// 获取当前窗口 // 获取当前窗口
const win = window.getCurrent(); const win = TauriWindow.getCurrent();
isMain.value = win.label === "TeyvatGuide"; isMain.value = win.label === "TeyvatGuide";
if (isMain.value) { if (isMain.value) {
const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta"; const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta";
await tauri.invoke("register_deep_link");
await getDeepLink();
await win.setTitle(title); await win.setTitle(title);
await emojiLoad(); await emojiLoad();
await checkLoad(); await checkLoad();
@@ -43,10 +47,7 @@ onBeforeMount(async () => {
onMounted(async () => { onMounted(async () => {
// 获取当前主题 // 获取当前主题
document.documentElement.className = theme.value; document.documentElement.className = theme.value;
await tauri.invoke("register_deep_link");
console.info("已注册深度链接!");
await listenOnTheme(); await listenOnTheme();
await getDeepLink();
}); });
// 监听主题变化 // 监听主题变化
@@ -107,10 +108,35 @@ async function initData(): Promise<void> {
} }
async function getDeepLink(): Promise<void> { async function getDeepLink(): Promise<void> {
console.info("正在监听深度链接!"); await event.listen("active_deep_link", (e) => {
await event.listen("test_deep_link", (e) => { new TauriWindow.WebviewWindow("TeyvatGuide")
console.log("深度链接已触发!"); .setFocus()
console.log(e.payload); .then(async () => {
// 导入格式: teyvatgiude://import_uigf?app=appName
// 跳转格式: localhost:4000/achievements/?app=appName
if ((<string>e.payload).startsWith("teyvatguide://import_uigf")) {
const param = (<string>e.payload).split("teyvatguide://import_uigf/?")[1];
let appName = "";
if (param) {
appName = param.split("app=")[1];
}
if (appName === "") {
await router.push("/achievements");
} else {
await router.push("/achievements/?app=" + appName);
}
window.location.reload();
} else {
showSnackbar({
text: "无效的 deep link",
color: "error",
timeout: 3000,
});
}
})
.catch((e) => {
console.log(e);
});
}); });
} }
</script> </script>

View File

@@ -105,8 +105,10 @@
<script lang="ts" setup> <script lang="ts" setup>
// vue // vue
import { computed, onBeforeMount, onMounted, ref } from "vue"; import { computed, onBeforeMount, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import ToLoading from "../../components/overlay/to-loading.vue"; import ToLoading from "../../components/overlay/to-loading.vue";
import showSnackbar from "../../components/func/snackbar"; import showSnackbar from "../../components/func/snackbar";
import showConfirm from "../../components/func/confirm";
// tauri // tauri
import { dialog, fs } from "@tauri-apps/api"; import { dialog, fs } from "@tauri-apps/api";
// Store // Store
@@ -153,6 +155,10 @@ const translateY = ref<string>("0px");
// render // render
const search = ref<string>(""); const search = ref<string>("");
// route
const route = useRoute();
const router = useRouter();
onBeforeMount(async () => { onBeforeMount(async () => {
const { total, fin } = await TGSqlite.getAchievementsOverview(); const { total, fin } = await TGSqlite.getAchievementsOverview();
achievementsStore.flushData(total, fin); achievementsStore.flushData(total, fin);
@@ -169,6 +175,9 @@ onMounted(async () => {
loadingTitle.value = "正在获取成就数据"; loadingTitle.value = "正在获取成就数据";
selectedAchievement.value = await TGSqlite.getAchievements(); selectedAchievement.value = await TGSqlite.getAchievements();
loading.value = false; loading.value = false;
if (route.query.app) {
await handleImportOuter(<string>route.query.app);
}
}); });
function handleScroll(e: Event): void { function handleScroll(e: Event): void {
@@ -336,6 +345,47 @@ async function exportJson(): Promise<void> {
function getIcon(series: number): string | undefined { function getIcon(series: number): string | undefined {
return AppAchievementSeriesData.find((item) => item.id === series)?.icon; return AppAchievementSeriesData.find((item) => item.id === series)?.icon;
} }
// 处理外部导入
async function handleImportOuter(app: string): Promise<void> {
const confirm = await showConfirm({
title: "是否导入祈愿数据?",
text: `来源APP${app}`,
});
if (confirm === true) {
// 读取 剪贴板
const clipboard = await window.navigator.clipboard.readText();
let data: TGApp.Plugins.UIAF.Achievement[];
// 里面是完整的 uiaf 数据
try {
data = JSON.parse(clipboard).list;
loadingTitle.value = "正在导入数据";
loading.value = true;
await TGSqlite.mergeUIAF(data);
loading.value = false;
showSnackbar({
color: "success",
text: "导入成功,即将刷新页面",
});
} catch (e) {
console.error(e);
showSnackbar({
color: "error",
text: "读取 UIAF 数据失败,请检查文件是否符合规范",
});
} finally {
setTimeout(async () => {
await router.push("/achievements");
window.location.reload();
}, 1500);
}
} else {
showSnackbar({
color: "warn",
text: "已取消导入",
});
}
}
</script> </script>
<style lang="css" scoped> <style lang="css" scoped>

View File

@@ -2,7 +2,7 @@
* @file router modules main.ts * @file router modules main.ts
* @description 主路由模块 * @description 主路由模块
* @author BTMuli<bt-muli@outlook.com> * @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.2 * @since Beta v0.3.2
*/ */
// 信息展示 // 信息展示
@@ -27,7 +27,7 @@ const mainRoutes = [
component: Test, component: Test,
}, },
{ {
path: "/achievements", path: "/achievements/:app?",
name: "成就", name: "成就",
component: Achievements, component: Achievements,
}, },