♻️ 减少耦合

This commit is contained in:
BTMuli
2023-05-20 12:53:44 +08:00
parent 51b8d0277a
commit 4899879b02
9 changed files with 40 additions and 69 deletions

View File

@@ -7,9 +7,9 @@
// Node
import md5 from "js-md5";
import qs from "qs";
// Tauri.Genshin
import TGConstant from "../constant/TGConstant";
import { transCookie, transParams } from "./tools";
/**
* @description 获取 salt
@@ -56,26 +56,26 @@ function getDS (method: string, data: string, saltType: string): string {
const body = method === "GET" ? "" : data;
const query = method === "GET" ? data : "";
const hashStr = `salt=${salt}&t=${time}&r=${random}&b=${body}&q=${query}`;
const md5Str = md5.update(qs.stringify(hashStr)).hex();
const md5Str = md5.update(hashStr).hex();
return `${time},${random},${md5Str}`;
}
/**
* @description 获取请求头
* @since Alpha v0.2.0
* @param {string} cookie cookie
* @param {Record<string, string>} cookie cookie
* @param {string} method 请求方法
* @param {string} data 请求数据
* @param {Record<string, string|number>} data 请求数据
* @param {string} saltType salt 类型
* @returns {Record<string, string>} 请求头
*/
export function getRequestHeader (cookie: string, method: string, data: string, saltType: string): Record<string, string> {
export function getRequestHeader (cookie: Record<string, string>, method: string, data: Record<string, string | number>, 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,
DS: getDS(method, transParams(data), saltType),
Cookie: transCookie(cookie),
};
}

View File

@@ -42,20 +42,6 @@ export function getRandomString (length: number): string {
return res;
}
/**
* @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 将 cookie 对象转换为字符串
* @since Alpha v0.2.0
@@ -70,6 +56,21 @@ export function transCookie (cookie: Record<string, string>) {
return res;
}
/**
* @description ds 算法需要数据转换后的字符串是按照字典序排序的
* @since Alpha v0.2.0
* @param {Record<string, string|number>} obj object
* @returns {string} query string
*/
export function transParams (obj: Record<string, string | number>): string {
let res = "";
const keys = Object.keys(obj).sort();
for (const key of keys) {
res += `${key}=${obj[key]}&`;
}
return res.slice(0, -1);
}
/**
* @description 根据 uid 获取 server
* @since Alpha v0.2.0