🎨 完善页面处理

This commit is contained in:
BTMuli
2023-10-25 00:20:30 +08:00
parent 16999f2e58
commit 6792c0ac0a
7 changed files with 206 additions and 74 deletions

View File

@@ -7,13 +7,12 @@
import { event, invoke } from "@tauri-apps/api";
import type { Event } from "@tauri-apps/api/event";
import { WebviewWindow } from "@tauri-apps/api/window";
import Md5 from "js-md5";
import { getDeviceID } from "./toolFunc";
import { useUserStore } from "../store/modules/user";
import TGConstant from "../web/constant/TGConstant";
import TGRequest from "../web/request/TGRequest";
import TGUtils from "../web/utils/TGUtils";
import { getDS4JS } from "../web/utils/getRequestHeader";
// 正常 arg 参数
interface NormalArg {
@@ -49,6 +48,14 @@ class TGClient {
*/
private window: WebviewWindow | null;
/**
* @private 模拟路由
* @since Beta v0.3.4
* @type {string[]}
* @memberof TGClient
*/
private route: string[] = [];
/**
* @constructor
* @since Beta v0.3.4
@@ -61,6 +68,8 @@ class TGClient {
} catch (error) {
this.window = null;
}
this.route = [];
this.listener = undefined;
}
/**
@@ -93,18 +102,44 @@ class TGClient {
await invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func getUrl
* @since Beta v0.3.4
* @desc 获取 url
* @param {string} func - 方法名
* @returns {string} - url
*/
getUrl(func: string): string {
switch (func) {
case "sign_in":
return "https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?act_id=e202009291139501";
case "game_record":
return "https://webstatic.mihoyo.com/app/community-game-records/index.html?bbs_presentation_style=fullscreen";
default:
return "";
}
}
/**
* @func open
* @since Beta v0.3.4
* @desc 打开米游社客户端
* @param {string} func - 方法名
* @param {string} url - url
* @returns {void} - 无返回值
*/
async open(func: string): Promise<void> {
async open(func: string, url?: string): Promise<void> {
if (this.window !== null) {
await this.window.close();
}
await invoke<InvokeArg>("create_mhy_client", { func });
if (url === undefined) {
url = this.getUrl(func);
this.route = [url];
} else if (func !== "closePage") {
this.route.push(url);
}
console.log(`[open] ${url}`);
await invoke<InvokeArg>("create_mhy_client", { func, url });
this.window = WebviewWindow.getByLabel("mhy_client");
await this.window?.show();
}
@@ -126,6 +161,9 @@ class TGClient {
case "getCookieInfo":
await this.getCookieInfo(payload, callback);
break;
case "getCookieToken":
await this.getCookieToken(callback);
break;
case "getActionTicket":
await this.getActionTicket(payload, callback);
await this.hideSideBar();
@@ -134,16 +172,25 @@ class TGClient {
await this.getHTTPRequestHeaders(callback);
break;
case "getDS":
await this.getDS(callback);
await this.getDS(1, callback, payload);
break;
case "getDS2":
await this.getDS2(payload, callback);
await this.getDS(2, callback, payload);
break;
case "getUserInfo":
await this.getUserInfo(callback);
break;
case "configure_share":
break;
case "pushPage":
await this.pushPage(payload);
break;
case "closePage":
await this.closePage();
break;
case "login":
await this.nullCallback(arg);
break;
// getNotificationSettings
default:
console.warn(`[${arg.windowLabel}] ${arg.payload}`);
@@ -167,7 +214,7 @@ class TGClient {
};
const js = `javascript:mhyWebBridge("${callback}", ${JSON.stringify(response)});`;
console.info(`[callback] ${js}`);
await invoke("create_mhy_client", { func: "execute_js" });
await invoke("create_mhy_client", { func: "execute_js", url: "" });
await invoke("execute_js", { label: "mhy_client", js });
}
@@ -203,6 +250,35 @@ class TGClient {
await this.callback(callback, data);
}
/**
* @func getCookieToken
* @since Beta v0.3.4
* @todo 待完善
* @desc 获取米游社客户端的 cookie_token
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getCookieToken(callback: string): Promise<void> {
const user = useUserStore();
const executeJS =
"javascript:(function(){" +
`document.cookie = "account_id=${user.cookie.account_id};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "cookie_token=${user.cookie.cookie_token};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "ltoken=${user.cookie.ltoken};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "ltuid=${user.cookie.ltuid};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "stuid=${user.cookie.stuid};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "stoken=${user.cookie.stoken};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
`document.cookie = "mid=${user.cookie.mid};domain=.mihoyo.com;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";` +
"})();";
console.info(`[getCookieToken] ${executeJS}`);
await invoke("execute_js", { label: "mhy_client", js: executeJS });
// callback
const data = {
cookie_token: user.cookie.cookie_token,
};
await this.callback(callback, data);
}
/**
* @func getActionTicket
* @since Beta v0.3.4
@@ -246,38 +322,24 @@ class TGClient {
* @func getDS
* @since Beta v0.3.4
* @desc 获取米游社客户端的 DS 参数
* @param {number} dsType - DS 类型
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getDS(callback: string): Promise<void> {
const salt = TGConstant.Salt.LK2;
const time = Math.floor(Date.now() / 1000).toString();
const random = TGUtils.Tools.getRandomString(6);
const check = Md5.md5.update(`salt=${salt}&t=${time}&r=${random}`).hex();
const data = {
DS: `${time},${random},${check}`,
};
await this.callback(callback, data);
}
/**
* @func getDS2
* @since Beta v0.3.4
* @desc 获取米游社客户端的 DS 参数
* @param {unknown} payload - 请求参数
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getDS2(payload: any, callback: string): Promise<void> {
const { query, body } = payload;
const salt = TGConstant.Salt.X4;
const time = Math.floor(Date.now() / 1000).toString();
const random = TGUtils.Tools.getRandomNumber(100001, 200000).toString();
const dataB = TGUtils.Tools.transParams(body);
const dataQ = TGUtils.Tools.transParams(query);
const check = Md5.md5.update(`salt=${salt}&t=${time}&r=${random}&b=${dataB}&q=${dataQ}`).hex();
async getDS(dsType: 1, callback: string, payload: undefined): Promise<void>;
async getDS(dsType: 2, callback: string, payload: any): Promise<void>;
async getDS(dsType: 1 | 2, callback: string, payload?: any): Promise<void> {
const saltType = dsType === 1 ? "lk2" : "common";
let ds = "";
if (dsType === 2) {
const { body, query } = payload;
ds = getDS4JS(saltType, dsType, body, query);
} else {
ds = getDS4JS(saltType, dsType, undefined, undefined);
}
const data = {
DS: `${time},${random},${check}`,
DS: ds,
};
await this.callback(callback, data);
}
@@ -298,15 +360,49 @@ class TGClient {
console.error(`[${callback}] ${userInfo.message}`);
return;
}
// const data = {
// id: userInfo.uid,
// gender: userInfo.gender,
// nickname: userInfo.nickname,
// introduce: userInfo.introduce,
// avatar_url: userInfo.avatar_url,
// };
await this.callback(callback, userInfo);
}
/**
* @func pushPage
* @since Beta v0.3.4
* @desc 打开米游社客户端的页面
* @param {unknown} payload - 请求参数
* @returns {void} - 无返回值
*/
async pushPage(payload: any): Promise<void> {
const url = payload.page;
await this.open("pushPage", url);
}
/**
* @func closePage
* @since Beta v0.3.4
* @desc 关闭米游社客户端的页面
* @returns {void} - 无返回值
*/
async closePage(): Promise<void> {
this.route.pop();
if (this.route.length === 0) {
await this.window?.hide();
return;
}
const url = this.route[this.route.length - 1];
await this.open("closePage", url);
}
/**
* @func nullCallback
* @since Beta v0.3.4
* @desc 空回调函数
* @param {Event<string>} arg - 回调参数
* @returns {void} - 无返回值
*/
async nullCallback(arg: Event<string>): Promise<void> {
console.warn(`[${arg.windowLabel}] ${arg.payload}`);
const { callback } = <NormalArg>JSON.parse(arg.payload);
await this.callback(callback, {});
}
}
const mhyClient = new TGClient();