Skip to content

Commit

Permalink
✨ 完成参数获取&剪贴板读取 #42
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Sep 19, 2023
1 parent d45d26c commit 9e6b8e9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn register_deep_link(app_handle: tauri::AppHandle) {
"teyvatguide",
move |request| {
dbg!(&request);
app_handle.emit_all("test_deep_link", request).unwrap();
app_handle.emit_all("active_deep_link", request).unwrap();
},
)
.unwrap();
Expand All @@ -20,13 +20,13 @@ fn main() {
tauri_plugin_deep_link::prepare("teyvatguide");
tauri::Builder::default()
.plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![register_deep_link])
.setup(|_app| {
let _window = _app.get_window("TeyvatGuide").unwrap();
#[cfg(debug_assertions)] // only include this code on debug builds
_window.open_devtools(); // open the devtools on startup
Ok(())
})
.invoke_handler(tauri::generate_handler![register_deep_link])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
44 changes: 35 additions & 9 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
<script lang="ts" setup>
// vue
import { onBeforeMount, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import TSidebar from "./components/app/t-sidebar.vue";
import TBackTop from "./components/app/t-backTop.vue";
// tauri
import { app, event, fs, tauri, window } from "@tauri-apps/api";
import { app, event, fs, tauri, window as TauriWindow } from "@tauri-apps/api";
// store
import { useAppStore } from "./store/modules/app";
// utils
Expand All @@ -27,13 +28,16 @@ import TGSqlite from "./plugins/Sqlite";
const appStore = useAppStore();
const isMain = ref<boolean>(false);
const theme = ref<string>(appStore.theme);
const router = useRouter();
onBeforeMount(async () => {
// 获取当前窗口
const win = window.getCurrent();
const win = TauriWindow.getCurrent();
isMain.value = win.label === "TeyvatGuide";
if (isMain.value) {
const title = "Teyvat Guide v" + (await app.getVersion()) + " Beta";
await tauri.invoke("register_deep_link");
await getDeepLink();
await win.setTitle(title);
await emojiLoad();
await checkLoad();
Expand All @@ -43,10 +47,7 @@ onBeforeMount(async () => {
onMounted(async () => {
// 获取当前主题
document.documentElement.className = theme.value;
await tauri.invoke("register_deep_link");
console.info("已注册深度链接!");
await listenOnTheme();
await getDeepLink();
});
// 监听主题变化
Expand Down Expand Up @@ -107,10 +108,35 @@ async function initData(): Promise<void> {
}
async function getDeepLink(): Promise<void> {
console.info("正在监听深度链接!");
await event.listen("test_deep_link", (e) => {
console.log("深度链接已触发!");
console.log(e.payload);
await event.listen("active_deep_link", (e) => {
new TauriWindow.WebviewWindow("TeyvatGuide")
.setFocus()
.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>
Expand Down
50 changes: 50 additions & 0 deletions src/pages/common/Achievements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@
<script lang="ts" setup>
// vue
import { computed, onBeforeMount, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import ToLoading from "../../components/overlay/to-loading.vue";
import showSnackbar from "../../components/func/snackbar";
import showConfirm from "../../components/func/confirm";
// tauri
import { dialog, fs } from "@tauri-apps/api";
// Store
Expand Down Expand Up @@ -153,6 +155,10 @@ const translateY = ref<string>("0px");
// render
const search = ref<string>("");
// route
const route = useRoute();
const router = useRouter();
onBeforeMount(async () => {
const { total, fin } = await TGSqlite.getAchievementsOverview();
achievementsStore.flushData(total, fin);
Expand All @@ -169,6 +175,9 @@ onMounted(async () => {
loadingTitle.value = "正在获取成就数据";
selectedAchievement.value = await TGSqlite.getAchievements();
loading.value = false;
if (route.query.app) {
await handleImportOuter(<string>route.query.app);
}
});
function handleScroll(e: Event): void {
Expand Down Expand Up @@ -336,6 +345,47 @@ async function exportJson(): Promise<void> {
function getIcon(series: number): string | undefined {
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>

<style lang="css" scoped>
Expand Down
4 changes: 2 additions & 2 deletions src/router/modules/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file router modules main.ts
* @description 主路由模块
* @author BTMuli<[email protected]>
* @since Alpha v0.2.2
* @since Beta v0.3.2
*/

// 信息展示
Expand All @@ -27,7 +27,7 @@ const mainRoutes = [
component: Test,
},
{
path: "/achievements",
path: "/achievements/:app?",
name: "成就",
component: Achievements,
},
Expand Down

0 comments on commit 9e6b8e9

Please sign in to comment.