feat(TGWindow): 精简代码,缓存可清除

This commit is contained in:
BTMuli
2023-03-11 23:27:48 +08:00
parent 54018f0a36
commit 049510c578
4 changed files with 89 additions and 71 deletions

View File

@@ -98,7 +98,7 @@
<script lang="ts" setup> <script lang="ts" setup>
// Node // Node
import { dialog, fs, window as TauriWindow } from "@tauri-apps/api"; import { dialog, fs } from "@tauri-apps/api";
import { onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
// Store // Store
import useAppStore from "../store/modules/app"; import useAppStore from "../store/modules/app";
@@ -115,6 +115,7 @@ import { Map } from "../interface/Base";
import UIAF_Oper from "../plugins/UIAF"; import UIAF_Oper from "../plugins/UIAF";
// Utils // Utils
import TGMap from "../utils/TGMap"; import TGMap from "../utils/TGMap";
import { createTGWindow } from "../utils/TGWindow";
// Store // Store
const appStore = useAppStore(); const appStore = useAppStore();
@@ -172,36 +173,7 @@ function selectSeries(index: number) {
} }
// 打开图片 // 打开图片
function openImg() { function openImg() {
// 获取窗口宽度 createTGWindow(getCardInfo.value.profile, "nameCard", getCardInfo.value.name, 840, 400, false);
const width = window.screen.width;
// 获取窗口高度
const height = window.screen.height;
// 计算窗口位置
const left = width / 2 - 480;
const top = height / 2 - 360;
if (TauriWindow.WebviewWindow.getByLabel("nameCard")) {
new TauriWindow.WindowManager("nameCard").close().then(() => {
new TauriWindow.WebviewWindow("nameCard", {
height: 400,
width: 840,
x: left,
y: top,
resizable: false,
url: getCardInfo.value.profile,
title: getCardInfo.value.name,
});
});
} else {
new TauriWindow.WebviewWindow("nameCard", {
height: 400,
width: 840,
x: left,
y: top,
resizable: false,
url: getCardInfo.value.profile,
title: getCardInfo.value.name,
});
}
} }
// 获取标题 // 获取标题
async function getTitle() { async function getTitle() {

View File

@@ -8,7 +8,10 @@
<v-list-item-title>打开用户数据目录</v-list-item-title> <v-list-item-title>打开用户数据目录</v-list-item-title>
</v-list-item> </v-list-item>
<v-list-item @click="deleteData" prepend-icon="mdi-delete"> <v-list-item @click="deleteData" prepend-icon="mdi-delete">
<v-list-item-title>删除本地数据</v-list-item-title> <v-list-item-title>删除用户数据</v-list-item-title>
</v-list-item>
<v-list-item @click="deleteTemp" prepend-icon="mdi-delete">
<v-list-item-title>删除临时数据</v-list-item-title>
</v-list-item> </v-list-item>
<v-list-item @click="setDefaultConfig" prepend-icon="mdi-cog"> <v-list-item @click="setDefaultConfig" prepend-icon="mdi-cog">
<v-list-item-title>恢复默认配置</v-list-item-title> <v-list-item-title>恢复默认配置</v-list-item-title>
@@ -73,6 +76,18 @@ async function deleteData() {
}); });
} }
} }
// 删除临时数据
async function deleteTemp() {
const res = await dialog.confirm("确定要删除临时数据吗?");
if (res) {
await fs.removeDir("tempData", {
dir: BaseDirectory.AppLocalData,
recursive: true,
});
await fs.createDir("tempData", { dir: BaseDirectory.AppLocalData });
await dialog.message("临时数据已删除!");
}
}
// 恢复默认配置 // 恢复默认配置
async function setDefaultConfig() { async function setDefaultConfig() {
const res = await dialog.confirm("确定要恢复默认配置吗?"); const res = await dialog.confirm("确定要恢复默认配置吗?");

View File

@@ -102,6 +102,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import useDevStore from "../store/modules/dev"; import useDevStore from "../store/modules/dev";
import useAppStore from "../store/modules/app";
import { import {
MysPostType, MysPostType,
ResponseNewsList, ResponseNewsList,
@@ -109,11 +110,12 @@ import {
EnumPostType, EnumPostType,
ResponsePost, ResponsePost,
} from "../interface/MysPost"; } from "../interface/MysPost";
// import { http,window as TauriWindow } from "@tauri-apps/api"; import { http, fs } from "@tauri-apps/api";
import { http } from "@tauri-apps/api"; import { createTGWindow } from "../utils/TGWindow";
// Store // Store
const devStore = useDevStore(); const devStore = useDevStore();
const appStore = useAppStore();
// 常量 // 常量
const MysNewsApi = "https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids=2&type="; const MysNewsApi = "https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids=2&type=";
@@ -189,51 +191,26 @@ async function toPost(post_id: string) {
postHtml.querySelectorAll("img").forEach(img => { postHtml.querySelectorAll("img").forEach(img => {
img.style.width = "100%"; img.style.width = "100%";
}); });
// 将 html 转为能够通过 window.open 打开的 url // 将 html 保存到 文件
const postUrl = URL.createObjectURL( await fs.writeTextFile(
new Blob([postHtml.documentElement.outerHTML], { type: "text/html;charset=utf-8" }) `${appStore.dataPath.temp}\\${post.post_id}.html`,
postHtml.documentElement.outerHTML
); );
// 打开新窗口,窗口位置居中 const postUrl = `file:\\\\\\${appStore.dataPath.temp}\\${post.post_id}.html`;
// 获取窗口宽度 createTGWindow(postUrl, "MysPost", post.subject, 960, 720, false);
const width = window.screen.width;
// 获取窗口高度
const height = window.screen.height;
// 计算窗口位置
const left = width / 2 - 480;
const top = height / 2 - 360;
// 打开窗口
window.open(postUrl, "_blank", `width=960,height=720,left=${left},top=${top}`);
// new TauriWindow.WebviewWindow("blob", {
// url: postUrl,
// title: post.subject,
// decorations: true,
// width: 960,
// x: left,
// y: top,
// height: 720,
// resizable: false,
// });
} }
async function logPost(post_id: string) { async function logPost(post_id: string) {
const post = await getPost(post_id).then(res => { const post = await getPost(post_id).then(res => {
return res.data; return res.data;
}); });
// 将 Json 内容写入 html // 将 json 保存到 文件
const postHtml = new DOMParser().parseFromString(JSON.stringify(post), "text/html"); await fs.writeTextFile(
// 将 html 转为能够通过 window.open 打开的 url `${appStore.dataPath.temp}\\${post_id}_log.json`,
const postUrl = URL.createObjectURL( JSON.stringify(post, null, 4)
new Blob([postHtml.documentElement.outerHTML], { type: "text/html;charset=utf-8" })
); );
// 打开新窗口,窗口位置居中 const postUrl = `file:\\\\\\${appStore.dataPath.temp}\\${post_id}_log.json`;
// 获取窗口宽度
const width = window.screen.width;
// 获取窗口高度
const height = window.screen.height;
// 计算窗口位置
const left = width / 2 - 480;
const top = height / 2 - 360;
// 打开窗口 // 打开窗口
window.open(postUrl, "_blank", `width=960,height=720,left=${left},top=${top}`); createTGWindow(postUrl, "MysPostLog", post_id, 960, 720, false);
} }
async function getPost(post_id: string): Promise<ResponsePost> { async function getPost(post_id: string): Promise<ResponsePost> {
return http return http

54
src/utils/TGWindow.ts Normal file
View File

@@ -0,0 +1,54 @@
/**
* @file utils TGWindow.ts
* @description 用于创建TG窗口的工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { window as TauriWindow } from "@tauri-apps/api";
/**
* @description 创建TG窗口
* @param {string} url 窗口地址
* @param {string} label 窗口标签
* @param {string} title 窗口标题
* @param {number} width 窗口宽度
* @param {number} height 窗口高度
* @param {boolean} resizable 是否可调整大小
* @returns {void}
*/
export function createTGWindow(
url: string,
label: string,
title: string,
width: number,
height: number,
resizable: boolean
): void {
// 计算窗口位置
const left = (window.screen.width - width) / 2;
const top = (window.screen.height - height) / 2;
// 判断窗口是否存在
if (TauriWindow.WebviewWindow.getByLabel(label)) {
new TauriWindow.WindowManager(label).close().then(() => {
new TauriWindow.WebviewWindow(label, {
height: height,
width: width,
x: left,
y: top,
resizable: resizable,
url: url,
title: title,
});
});
} else {
new TauriWindow.WebviewWindow(label, {
height: height,
width: width,
x: left,
y: top,
resizable: resizable,
url: url,
title: title,
});
}
}