🔊 完善基础页面的 log #83

This commit is contained in:
目棃
2024-01-23 18:20:23 +08:00
parent 150755cbef
commit 8aaf18dbe2
15 changed files with 167 additions and 70 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file utils/TGClient.ts
* @desc 负责米游社客户端的 callback 处理
* @since Beta v0.4.0
* @since Beta v0.4.2
*/
import { event, invoke } from "@tauri-apps/api";
@@ -9,6 +9,7 @@ import type { Event } from "@tauri-apps/api/event";
import { appWindow, WebviewWindow } from "@tauri-apps/api/window";
import { parseLink } from "./linkParser";
import TGLogger from "./TGLogger";
import { createPost } from "./TGWindow";
import { getDeviceInfo } from "./toolFunc";
import showSnackbar from "../components/func/snackbar";
@@ -89,7 +90,7 @@ class TGClient {
/**
* @func callback
* @since Beta v0.3.4
* @since Beta v0.4.2
* @desc 回调函数
* @param {string} callback - 回调函数名
* @param {object} data - 回调数据
@@ -103,7 +104,6 @@ class TGClient {
};
const js = `javascript:mhyWebBridge("${callback}", ${JSON.stringify(response)});`;
console.info(`[callback] ${js}`);
await invoke("create_mhy_client", { func: "execute_js", url: "" });
await invoke("execute_js", { label: "mhy_client", js });
}
@@ -167,7 +167,7 @@ class TGClient {
/**
* @func handleCallback
* @since Beta v0.3.9
* @since Beta v0.4.2
* @desc 处理米游社客户端的 callback
* @param {Event<string>} arg - 事件参数
* @returns {Promise<void>} - 返回值
@@ -178,7 +178,10 @@ class TGClient {
await this.handleCustomCallback(argParse);
return;
}
console.warn(`[${argParse.method}] ${JSON.stringify(argParse.payload)}`);
await TGLogger.Warn(`[TGClient][handleCallback] ${JSON.stringify(argParse)}`, false);
await TGLogger.Info(
`[TGClient][handleCallback] 处理回调 ${argParse.method}${argParse.callback}`,
);
await this.hideSideBar();
await this.hideOverlay();
switch (argParse.method) {
@@ -275,6 +278,7 @@ class TGClient {
* @returns {Promise<void>} - 返回值
*/
async handleCustomCallback(arg: TGApp.Plugins.JSBridge.Arg<any>): Promise<void> {
await TGLogger.Info(`[TGClient][handleCustomCallback] ${JSON.stringify(arg)}`);
switch (arg.method) {
case "teyvat_open":
createPost(<string>arg.payload);
@@ -454,16 +458,13 @@ class TGClient {
await this.window.close();
} catch (e) {
console.error(e);
await invoke<InvokeArg>("create_mhy_client", {
func: "default",
url: "https://api-static.mihoyo.com/",
});
await invoke<InvokeArg>("create_mhy_client", { func: "default", url: "" });
await this.open(func, url);
}
}
if (url === undefined) url = this.getUrl(func);
this.route = [url];
console.log(`[open] ${url}`);
await TGLogger.Info(`[TGClient][open][${func}] ${url}`);
await invoke<InvokeArg>("create_mhy_client", { func, url });
this.window = WebviewWindow.getByLabel("mhy_client");
await this.window?.show();

View File

@@ -1,19 +1,20 @@
/**
* @file utils/TGShare.ts
* @description 生成分享截图并保存到本地
* @since Beta v0.3.9
* @since Beta v0.4.2
*/
import { dialog, fs, http, path } from "@tauri-apps/api";
import html2canvas from "html2canvas";
import TGLogger from "./TGLogger";
import { bytesToSize } from "./toolFunc";
import showConfirm from "../components/func/confirm";
import showSnackbar from "../components/func/snackbar";
/**
* @description 保存图片-canvas
* @since Beta v0.3.7
* @since Beta v0.4.2
* @param {Uint8Array} buffer - 图片数据
* @param {string} filename - 文件名
* @returns {Promise<void>} 无返回值
@@ -32,6 +33,7 @@ async function saveCanvasImg(buffer: Uint8Array, filename: string): Promise<void
contents: buffer,
});
});
await TGLogger.Info(`[saveCanvasImg][${filename}] 已将图像保存到本地`);
}
/**
@@ -72,7 +74,7 @@ function getShareImgBgColor(): string {
/**
* @description 生成分享截图
* @since Beta v0.3.9
* @since Beta v0.4.2
* @param {string} fileName - 文件名
* @param {HTMLElement} element - 元素
* @param {number} scale - 缩放比例
@@ -108,6 +110,7 @@ export async function generateShareImg(
);
const size = buffer.length;
const sizeStr = bytesToSize(size);
await TGLogger.Info(`[generateShareImg][${fileName}] 图像大小为 ${sizeStr}`);
if (size > 80000000) {
showSnackbar({
text: `图像大小为 ${sizeStr},过大,无法保存`,
@@ -136,6 +139,7 @@ export async function generateShareImg(
showSnackbar({
text: `已将 ${fileName} 复制到剪贴板,大小为 ${sizeStr}`,
});
await TGLogger.Info(`[generateShareImg][${fileName}] 已将图像复制到剪贴板`);
} catch (e) {
await saveCanvasImg(buffer, fileName);
}

View File

@@ -1,12 +1,14 @@
/**
* @file utils/TGWindow.ts
* @description 窗口创建相关工具函数
* @since Beta v0.3.8
* @since Beta v0.4.2
*/
import { invoke, window as TauriWindow } from "@tauri-apps/api";
import type { WindowOptions } from "@tauri-apps/api/types/window";
import TGLogger from "./TGLogger";
/**
* @description 创建TG窗口
* @since Beta v0.3.4
@@ -71,7 +73,7 @@ export function createTGWindow(
/**
* @description 打开帖子
* @since Beta v0.3.8
* @since Beta v0.4.2
* @param {TGApp.Plugins.Mys.News.RenderCard | string | number | TGApp.Plugins.Mys.Forum.RenderCard} item 帖子内容或ID
* @param {string} title 帖子标题
* @returns {void}
@@ -80,7 +82,7 @@ export function createPost(
item: TGApp.Plugins.Mys.News.RenderCard | string | number | TGApp.Plugins.Mys.Forum.RenderCard,
title?: string,
): void {
let postId, postTitle;
let postId: string, postTitle: string;
if (typeof item === "string" || typeof item === "number") {
postId = item.toString();
postTitle = title ? `Post_${postId} ${title}` : `Post_${postId}`;
@@ -90,17 +92,7 @@ export function createPost(
}
const postPath = `/post_detail/${postId}`;
createTGWindow(postPath, "Sub_window", postTitle, 960, 720, false, false);
}
/**
* @description 打开 Wiki
* @since Beta v0.3.8
* @param {string} dir 目录
* @param {string} name 文件名
* @returns {void}
*/
export function createWiki(dir: string, name: string): void {
const dirName = dir === "GCG" ? dir : dir.toLowerCase();
const wikiPath = `/wiki/detail/${dirName}/${name}`;
createTGWindow(wikiPath, "Sub_window", `Wiki_${dirName}_${name}`, 960, 720, false, false);
TGLogger.Info(`[createPost][${postId}] 打开帖子`).catch((err) => {
console.error(err);
});
}