mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-14 09:38:13 +08:00
✨ 获取用户头像&昵称
This commit is contained in:
10
src/web/api/BBS.ts
Normal file
10
src/web/api/BBS.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @file web api BBS.ts
|
||||
* @description BBS API
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.0
|
||||
*/
|
||||
|
||||
const BBS_API = "https://bbs-api.miyoushe.com/"; // 基础 API
|
||||
|
||||
export const BBSUserInfoAPI = `${BBS_API}user/wapi/getUserFullInfo`; // 用户信息 API
|
||||
@@ -5,6 +5,7 @@
|
||||
* @since Alpha v0.2.0
|
||||
*/
|
||||
|
||||
import { BBSUserInfoApi } from "./BBS";
|
||||
import { ENKA_API } from "./ENKA";
|
||||
import { Hk4eAnnoListApi, Hk4eAnnoContentApi, Hk4eAnnoQuery } from "./Hk4e";
|
||||
import { PassportTokenApi, PassportCookieTokenApi, PassportVerifyApi } from "./Passport";
|
||||
@@ -29,6 +30,7 @@ const TGApi = {
|
||||
},
|
||||
GameData: {
|
||||
byCookie: {
|
||||
getUserInfo: BBSUserInfoApi, // 获取用户信息
|
||||
getAccounts: TakumiCookieBindingRolesApi, // 获取绑定角色
|
||||
getCharacter: TakumiRecordGenshinCharacterApi, // 获取角色信息
|
||||
},
|
||||
|
||||
@@ -13,6 +13,7 @@ import { getLTokenBySToken } from "./getLToken";
|
||||
// import * from "./getRoleList.ts";
|
||||
// import * from "./getTickets.ts";
|
||||
import { getTokensByLoginTicket } from "./getTokens";
|
||||
import { getUserInfoByCookie } from "./getUserInfo";
|
||||
// import * from "./getUserCard";
|
||||
import initCookie from "./initCookie";
|
||||
import { verifyLToken } from "./verifyLToken";
|
||||
@@ -29,6 +30,7 @@ const TGRequest = {
|
||||
},
|
||||
byCookie: {
|
||||
getAccounts: getGameAccountsByCookie,
|
||||
getUserInfo: getUserInfoByCookie,
|
||||
},
|
||||
byLToken: {
|
||||
verify: verifyLToken,
|
||||
|
||||
47
src/web/request/getUserInfo.ts
Normal file
47
src/web/request/getUserInfo.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @file web request getUserInfo.ts
|
||||
* @description 获取用户信息请求
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.0
|
||||
*/
|
||||
|
||||
// tauri
|
||||
import { http } from "@tauri-apps/api";
|
||||
// api
|
||||
import TGApi from "../api/TGApi";
|
||||
// utils
|
||||
import TGUtils from "../utils/TGUtils";
|
||||
// types
|
||||
import { type UserResponse } from "../../plugins/Mys/interface/user";
|
||||
|
||||
/**
|
||||
* @description 根据 cookie 获取用户信息
|
||||
* @since Alpha v0.2.0
|
||||
* @param {string} cookie_token cookie token
|
||||
* @param {string} account_id 用户 account_id
|
||||
* @returns {Promise<BTMuli.User.Base.BriefInfo | BTMuli.Genshin.Base.Response>}
|
||||
*/
|
||||
export async function getUserInfoByCookie (cookie_token: string, account_id: string): Promise<BTMuli.User.Base.BriefInfo | BTMuli.Genshin.Base.Response> {
|
||||
const cookie = {
|
||||
cookie_token,
|
||||
account_id,
|
||||
};
|
||||
const url = TGApi.GameData.byCookie.getUserInfo;
|
||||
const params = { gids: 2 };
|
||||
const header = TGUtils.User.getSignHeader(cookie, "GET", {}, "common");
|
||||
return await http.fetch<UserResponse>(url, {
|
||||
method: "GET",
|
||||
headers: header,
|
||||
body: http.Body.json(params),
|
||||
}).then((res) => {
|
||||
console.log(res);
|
||||
if (res.data.retcode !== 0) return res.data;
|
||||
const info = res.data.data.user_info;
|
||||
return {
|
||||
nickname: info.nickname,
|
||||
uid: info.uid,
|
||||
avatar: info.avatar,
|
||||
desc: info.introduce,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -35,17 +35,11 @@ async function initCookie (ticket: string, uid: string): Promise<void> {
|
||||
if (Array.isArray(tokenRes)) {
|
||||
const lToken = tokenRes.find((item) => item.name === "ltoken");
|
||||
const sToken = tokenRes.find((item) => item.name === "stoken");
|
||||
if (lToken) {
|
||||
await TGSqlite.saveAppData("ltoken", lToken.token);
|
||||
cookie.ltoken = lToken.token;
|
||||
}
|
||||
if (sToken) {
|
||||
await TGSqlite.saveAppData("stoken", sToken.token);
|
||||
cookie.stoken = sToken.token;
|
||||
}
|
||||
if (lToken) cookie.ltoken = lToken.token;
|
||||
if (sToken) cookie.stoken = sToken.token;
|
||||
const cookieToken = await getCookieTokenBySToken(uid, cookie.stoken);
|
||||
if (typeof cookieToken === "string") cookie.cookie_token = cookieToken;
|
||||
const mid = await verifyLToken(cookie.ltoken, cookie.ltuid, cookie.stoken);
|
||||
const mid = await verifyLToken(cookie.ltoken, cookie.ltuid);
|
||||
if (typeof mid === "string") cookie.mid = mid;
|
||||
await TGSqlite.saveAppData("cookie", JSON.stringify(cookie));
|
||||
} else {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user