获取用户头像&昵称

This commit is contained in:
BTMuli
2023-05-21 16:05:23 +08:00
parent 64b4b4abcc
commit 821014c0a2
14 changed files with 215 additions and 85 deletions

View File

@@ -6,7 +6,7 @@
*/
import { getAnnoCard } from "./getAnnoCard";
import { getRequestHeader } from "./getRequestHeader";
import { getRequestHeader, getRequestSignHeader } from "./getRequestHeader";
import { parseAnnoContent } from "./parseAnno";
import { transCookie, getServerByUid } from "./tools";
@@ -17,6 +17,7 @@ const TGUtils = {
},
User: {
getHeader: getRequestHeader,
getSignHeader: getRequestSignHeader,
},
Tools: {
transCookie,

View File

@@ -40,19 +40,36 @@ function getRandomNumber (min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* @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.charAt(Math.floor(Math.random() * str.length));
}
return res;
}
/**
* @description 获取 ds
* @since Alpha v0.2.0
* @version 2.49.1
* @param {string} saltType salt 类型
* @param {string} method 请求方法
* @param {string} data 请求数据
* @param {string} saltType salt 类型
* @param {boolean} isSign 是否为签名
* @returns {string} ds
*/
function getDS (method: string, data: string, saltType: string): string {
function getDS (method: string, data: string, saltType: string, isSign: boolean = false): string {
const salt = getSalt(saltType);
const time = Math.floor(Date.now() / 1000).toString();
const random = getRandomNumber(100000, 200000).toString();
let random = getRandomNumber(100000, 200000).toString();
if (isSign) random = getRandomString(6);
const body = method === "GET" ? "" : data;
const query = method === "GET" ? data : "";
const hashStr = `salt=${salt}&t=${time}&r=${random}&b=${body}&q=${query}`;
@@ -80,8 +97,28 @@ export function getRequestHeader (cookie: Record<string, string>, method: string
"User-Agent": TGConstant.BBS.USER_AGENT,
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-client_type": "5",
Referer: "https://webstatic.mihoyo.com/",
Referer: TGConstant.BBS.REFERER,
DS: ds,
Cookie: transCookie(cookie),
};
}
/**
* @description 获取签名请求头
* @since Alpha v0.2.0
* @param {Record<string, string>} cookie cookie
* @param {string} method 请求方法
* @param {Record<string, string|number>} data 请求数据
* @param {string} saltType salt 类型
* @returns {Record<string, string>} 请求头
*/
export function getRequestSignHeader (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: TGConstant.BBS.REFERER,
DS: getDS(method, transParams(data), saltType, true),
Cookie: transCookie(cookie),
};
}

View File

@@ -27,21 +27,6 @@ export function decodeRegExp (data: string): string {
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 将 cookie 对象转换为字符串
* @since Alpha v0.2.0