🌱 完善 jsBridge

This commit is contained in:
BTMuli
2023-10-24 00:38:41 +08:00
parent 7ef89c33f1
commit 35dc972841
10 changed files with 255 additions and 25 deletions

View File

@@ -7,8 +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 TGSqlite from "../plugins/Sqlite";
import { useUserStore } from "../store/modules/user";
import TGConstant from "../web/constant/TGConstant";
import TGRequest from "../web/request/TGRequest";
import TGUtils from "../web/utils/TGUtils";
// 正常 arg 参数
interface NormalArg {
@@ -72,6 +76,18 @@ class TGClient {
}
}
/**
* @func hideSideBar
* @since Beta v0.3.4
* @desc 隐藏侧边栏
* @returns {void} - 无返回值
*/
async hideSideBar(): Promise<void> {
const executeJS =
"javascript:(function() {let st = document.createElement('style');st.innerHTML = '::-webkit-scrollbar{display:none}';document.querySelector('body').appendChild(st);})();";
await invoke("execute_js", { label: "mhy_client", js: executeJS });
}
/**
* @func open
* @since Beta v0.3.4
@@ -86,6 +102,7 @@ class TGClient {
await invoke<InvokeArg>("create_mhy_client", { func });
this.window = WebviewWindow.getByLabel("mhy_client");
await this.window?.show();
await this.hideSideBar();
}
/**
@@ -98,14 +115,33 @@ class TGClient {
async handleCallback(arg: Event<string>): Promise<any> {
console.log(`[${arg.windowLabel}] ${arg.payload}`);
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
console.log(method, payload, callback);
switch (method) {
case "getStatusBarHeight":
this.getStatusBarHeight(callback);
await this.getStatusBarHeight(callback);
break;
case "getCookieInfo":
await this.getCookieInfo(callback);
break;
case "getActionTicket":
await this.getActionTicket(payload, callback);
break;
case "getHTTPRequestHeaders":
await this.getHTTPRequestHeaders(callback);
break;
case "getDS":
await this.getDS(callback);
break;
case "getDS2":
await this.getDS2(payload, callback);
break;
case "getUserInfo":
await this.getUserInfo(callback);
break;
case "configure_share":
await this.callback(callback ?? "");
break;
default:
console.warn(`[${arg.windowLabel}] ${arg.payload}`);
}
}
@@ -118,8 +154,9 @@ class TGClient {
* @returns {void} - 无返回值
*/
async callback(callback: string, payload?: string): Promise<void> {
const js = `javascript:mhyWebBridge("${callback}", ${payload ?? ""})`;
await invoke("create_mhy_client");
const js = `javascript:mhyWebBridge("${callback}", ${payload ?? ""});`;
console.log(js);
await invoke("create_mhy_client", { func: "execute_js" });
await invoke("execute_js", { label: "mhy_client", js });
}
@@ -130,9 +167,11 @@ class TGClient {
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
getStatusBarHeight(callback: string): void {
console.log("getStatusBarHeight");
console.log(callback);
async getStatusBarHeight(callback: string): Promise<void> {
const data = {
statusBarHeight: 0,
};
await this.callback(callback, JSON.stringify(data));
}
/**
@@ -143,10 +182,134 @@ class TGClient {
* @returns {void} - 无返回值
*/
async getCookieInfo(callback: string): Promise<void> {
console.log("getCookieInfo");
const cookie = await TGSqlite.getCookie();
const user = useUserStore();
// todo这边还不清楚返回结构
const cookie = {
data: {
ltuid: user.cookie.ltuid,
ltoken: user.cookie.ltoken,
stuid: user.cookie.stuid,
stoken: user.cookie.stoken,
mid: user.cookie.mid,
},
};
await this.callback(callback, JSON.stringify(cookie));
}
/**
* @func getActionTicket
* @since Beta v0.3.4
* @desc 获取米游社客户端的 action_ticket\
* @param {unknown} payload - 请求参数
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getActionTicket(payload: any, callback: string): Promise<void> {
const actionType = payload.action_type;
const user = useUserStore();
const uid = user.getCurAccount().gameUid;
const mid = user.cookie.mid;
const stoken = user.cookie.stoken;
const actionTicket = await TGRequest.User.bySToken.getActionTicket(
actionType,
stoken,
mid,
uid,
);
await this.callback(callback, JSON.stringify(actionTicket));
}
/**
* @func getHTTPRequestHeaders
* @since Beta v0.3.4
* @desc 获取米游社客户端的 HTTP 请求头
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getHTTPRequestHeaders(callback: string): Promise<void> {
console.log("getHTTPRequestHeaders");
const header = {
"x-rpc-client_type": "5",
"x-rpc-device_id": localStorage.getItem("device_id") ?? "",
"x-rpc-app_version": TGConstant.BBS.VERSION,
};
const data = {
headers: header,
};
await this.callback(callback, JSON.stringify(data));
}
/**
* @func getDS
* @since Beta v0.3.4
* @desc 获取米游社客户端的 DS 参数
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getDS(callback: string): Promise<void> {
console.log("getDS");
const salt = TGConstant.Salt.LK2;
const time = Math.floor(Date.now() / 1000).toString();
const random = TGUtils.Tools.getRandomString(6);
const ds = Md5.md5.update(`salt=${salt}&t=${time}&r=${random}`).hex();
const data = {
data: {
DS: `${time},${random},${ds}`,
},
};
await this.callback(callback, JSON.stringify(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.LK2;
const time = Math.floor(Date.now() / 1000).toString();
const random = TGUtils.Tools.getRandomNumber(100000, 200000).toString();
const dataB = TGUtils.Tools.transParams(body);
const dataQ = TGUtils.Tools.transParams(query);
const ds = Md5.md5.update(`salt=${salt}&t=${time}&r=${random}&b=${dataB}&q=${dataQ}`).hex();
const data = {
data: {
DS: `${time},${random},${ds}`,
},
};
console.log(data);
await this.callback(callback, JSON.stringify(data));
}
/**
* @func getUserInfo
* @since Beta v0.3.4
* @desc 获取米游社客户端的用户信息
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getUserInfo(callback: string): Promise<void> {
const user = useUserStore();
const cookieToken = user.cookie.cookie_token;
const accountId = user.cookie.account_id;
const userInfo = await TGRequest.User.byCookie.getUserInfo(cookieToken, accountId, true);
if ("retcode" in userInfo) {
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, JSON.stringify(data));
}
}
const mhyClient = new TGClient();