获取用户头像&昵称

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

@@ -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,

View 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,
};
});
}

View File

@@ -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 {