♻️ 变更目录,参考 TGAssistant 优化一些方法

This commit is contained in:
BTMuli
2023-05-19 09:30:29 +08:00
parent 07056bfec7
commit 824ef681e2
27 changed files with 253 additions and 239 deletions

27
src/web/utils/TGUtils.ts Normal file
View File

@@ -0,0 +1,27 @@
/**
* @file web utils TGUtils.ts
* @description 应用用到的工具函数
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
import { parseAnnoContent } from "./parseAnno";
import { getAnnoCard } from "./getAnnoCard";
import { getRequestHeader } from "./getRequestHeader";
import { cookieToString, getServerByUid } from "./tools";
const TGUtils = {
Anno: {
getCard: getAnnoCard,
parseContent: parseAnnoContent,
},
User: {
getHeader: getRequestHeader,
},
Tools: {
cookieToString,
getServerByUid,
},
};
export default TGUtils;

View File

@@ -0,0 +1,34 @@
/**
* @file web utils transAnno.ts
* @description 公告数据转换工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
*/
// 默认封面图
const defaultCover = "/source/UI/defaultCover.webp";
/**
* @description 将获取到的数据转为渲染用的卡片
* @since Alpha v0.1.2
* @param {BTMuli.Genshin.Announcement.ListData} data 公告数据
* @returns {BTMuli.Genshin.Announcement.ListCard[]} 渲染用的卡片
*/
export function getAnnoCard (data: BTMuli.Genshin.Announcement.ListData): BTMuli.Genshin.Announcement.ListCard[] {
const cards: BTMuli.Genshin.Announcement.ListCard[] = [];
data.list.map((annoList: BTMuli.Genshin.Announcement) => {
return annoList.list.map((anno: BTMuli.Genshin.Announcement.ListItem) => {
return cards.push({
id: anno.ann_id,
title: anno.title,
subtitle: anno.subtitle,
banner: anno.banner || defaultCover,
typeLabel: anno.type_label,
tagIcon: anno.tag_icon,
startTime: anno.start_time,
endTime: anno.end_time,
});
});
});
return cards;
}

53
src/web/utils/getDS.ts Normal file
View File

@@ -0,0 +1,53 @@
/**
* @file web utils getDS.ts
* @description ds 算法
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
// Node
import md5 from "js-md5";
import qs from "qs";
// Tauri.Genshin
import { random } from "./tools";
import TGConstant from "../constant/TGConstant";
/**
* @description 获取 salt
* @since Alpha v0.2.0
* @version 2.49.1
* @param {string} saltType salt 类型
* @returns {string} salt
*/
function getSalt (saltType: string) {
switch (saltType) {
case "common":
return TGConstant.Salt.Other.X4;
case "prod":
return TGConstant.Salt.Other.prod;
default:
return TGConstant.Salt.Other.X4;
}
}
/**
* @description 获取 ds
* @since Alpha v0.2.0
* @version 2.49.1
* @param {string} saltType salt 类型
* @param {string} method 请求方法
* @param {string} data 请求数据
* @returns {string} ds
*/
export function getDS (method: string, data: string, saltType: string): string {
const salt = getSalt(saltType);
const params = {
salt,
t: Math.floor(Date.now() / 1000).toString(),
r: random(100000, 200000).toString(),
b: method === "GET" ? "" : data,
q: method === "GET" ? data : "",
};
const md5Str = md5.update(qs.stringify(params)).hex();
return `${params.t},${params.r},${md5Str}`;
}

View File

@@ -0,0 +1,29 @@
/**
* @file web utils getRequestHeader.ts
* @description 获取请求头
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
import TGConstant from "../constant/TGConstant";
import { getDS } from "./getDS";
/**
* @description 获取请求头
* @since Alpha v0.2.0
* @param {string} cookie cookie
* @param {string} method 请求方法
* @param {string} data 请求数据
* @param {string} saltType salt 类型
* @returns {Record<string, string>} 请求头
*/
export function getRequestHeader (cookie: string, method: string, data: string, saltType: string): Record<string, string> {
return {
"User-Agent": TGConstant.BBS.USER_AGENT,
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-client_type": "5",
Referer: "https://webstatic.mihoyo.com/",
DS: getDS(method, data, saltType),
Cookie: cookie,
};
}

View File

@@ -0,0 +1,61 @@
/**
* @file web utils parseAnno.ts
* @description 解析游戏内公告数据
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
*/
import { decodeRegExp } from "./tools";
/**
* @description 解析游戏内公告数据
* @since Alpha v0.1.2
* @param {string} data 游戏内公告数据
* @returns {string} 解析后的数据
*/
export function parseAnnoContent (data: string): string {
const htmlBase = new DOMParser().parseFromString(data, "text/html");
htmlBase.querySelectorAll("span").forEach((span) => {
if (span.style.fontSize) {
span.style.fontSize = "";
}
if (span.children.length === 0) {
return (span.innerHTML = decodeRegExp(span.innerHTML));
} else {
span.querySelectorAll("*").forEach((child) => {
if (child.children.length === 0) {
return (child.innerHTML = decodeRegExp(child.innerHTML));
}
});
}
});
htmlBase.querySelectorAll("p").forEach((p) => {
if (p.children.length === 0) {
return (p.innerHTML = decodeRegExp(p.innerHTML));
} else {
p.querySelectorAll("*").forEach((child) => {
if (child.children.length === 0) {
return (child.innerHTML = decodeRegExp(child.innerHTML));
}
});
}
});
htmlBase.querySelectorAll("img").forEach((img) => {
img.style.maxWidth = "100%";
img.style.borderRadius = "10px";
img.style.margin = "10px 0";
});
htmlBase.querySelectorAll("a").forEach((a) => {
const span = htmlBase.createElement("i");
span.classList.add("mdi", "mdi-link-variant", "anno-link-icon");
a.prepend(span);
if (a.href.startsWith("javascript:miHoYoGameJSSDK.openInBrowser")) {
a.href = a.href.replace("javascript:miHoYoGameJSSDK.openInBrowser('", "").replace("');", "");
a.target = "_blank";
} else if (a.href.startsWith("javascript:miHoYoGameJSSDK.openInWebview")) {
a.href = a.href.replace("javascript:miHoYoGameJSSDK.openInWebview('", "").replace("');", "");
a.target = "_blank";
}
});
return htmlBase.body.innerHTML;
}

107
src/web/utils/tools.ts Normal file
View File

@@ -0,0 +1,107 @@
/**
* @file web utils tools.ts
* @description 应用用到的工具函数
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
// Node
import { stringify } from "qs";
// TauriGenshin
import TGConstant from "../constant/TGConstant";
/**
* @description 转义正则表达式
* @since Alpha v0.1.2
* @param {string} data 内容
* @returns {string} 转义后的内容
*/
export function decodeRegExp (data: string): string {
let res = data;
if (res.length === 0) return res;
res = res.replace(/&lt;/g, "<");
res = res.replace(/&gt;/g, ">");
res = res.replace(/&nbsp;/g, " ");
res = res.replace(/&#39;/g, "'");
res = res.replace(/&quot;/g, "\"");
res = res.replace(/&apos;/g, "'");
res = res.replace(/&amp;/g, "&");
return res;
}
/**
* @description 获取随机字符串
* @since Alpha v0.2.0
* @param {number} length 字符串长度
* @returns {string} 随机字符串
*/
export function getRandomString (length: number): string {
const str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let res = "";
for (let i = 0; i < length; i++) {
res += str[Math.floor(Math.random() * str.length)];
}
return res;
}
/**
* @description 获取随机数
* @since Alpha v0.2.0
* @param {number} min 最小值
* @param {number} max 最大值
* @returns {number} 随机数
*/
export function random (min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* @description object 转换为 query string
* @since Alpha v0.2.0
* @param {Record<string, string>} obj object
* @param {boolean} encode 是否编码
* @returns {string} query string
*/
export function qs (obj: Record<string, string>, encode: boolean = false): string {
let res = "";
for (const [k, v] of Object.entries(obj)) res += `${k}=${encode ? encodeURIComponent(v) : v}&`;
res = res.slice(0, res.length - 1);
return res;
}
/**
* @description 将 ck JSON 对象转换为字符串
* @since Alpha v0.2.0
* @param {object} ck ck JSON 对象
* @returns {string} ck 字符串
*/
export function cookieToString (ck: object): string {
let res = stringify(ck);
res = res.replace(/&/g, ";");
return res;
}
/**
* @description 根据 uid 获取 server
* @since Alpha v0.2.0
* @param {string} uid uid
* @returns {string} server
*/
export function getServerByUid (uid: string): string {
// 获取第一个字符
const first = uid[0];
// 1-4 为国服-天空岛
if (first >= "1" && first <= "4") return TGConstant.Server.CN_ISLAND;
// 5 为国服-世界树
if (first === "5") return TGConstant.Server.CN_TREE;
// 6 为美服
if (first === "6") return TGConstant.Server.OS_USA;
// 7 为欧服
if (first === "7") return TGConstant.Server.OS_EURO;
// 8 为亚服
if (first === "8") return TGConstant.Server.OS_ASIA;
// 9 为台服
if (first === "9") return TGConstant.Server.OS_CHT;
// 其他情况返回未知
return TGConstant.Server.UNKNOWN;
}