获取用户头像&昵称

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

@@ -33,8 +33,10 @@ import TBackTop from "./components/t-backTop.vue";
import { fs, window, app, event } from "@tauri-apps/api";
// store
import { useAppStore } from "./store/modules/app";
import { useUserStore } from "./store/modules/user";
const appStore = useAppStore();
const userStore = useUserStore();
const isMain = ref(true as boolean);
const theme = ref(appStore.theme as string);
@@ -50,6 +52,8 @@ onMounted(async () => {
await win.setTitle(title);
await checkLoad();
}
// 保存 cookie
await userStore.initCookie();
});
// 监听主题变化

View File

@@ -106,11 +106,16 @@
</v-list-group>
<v-divider />
<div class="bottom-menu">
<!-- <v-list-item v-show="isDev" :title="userInfo.nickname" value="user" link href="/user">
<v-list-item :title="userInfo.nickname" value="user" link href="/user">
<template #prepend>
<img :src="userInfo.avatar" alt="userIcon" class="side-icon">
</template>
</v-list-item> -->
<template #append>
<v-icon style="color:var(--sidebar-icon)" @click="refreshUser()">
mdi-refresh
</v-icon>
</template>
</v-list-item>
<v-list-item :title="themeTitle" value="theme" @click="switchTheme()">
<template #prepend>
<v-icon style="color:var(--sidebar-icon)">
@@ -135,17 +140,17 @@ import { computed, ref, onMounted } from "vue";
import { event } from "@tauri-apps/api";
// store
import { useAppStore } from "../store/modules/app";
import { useUserStore } from "../store/modules/user";
// utils
import TGRequest from "../web/request/TGRequest";
const appStore = useAppStore();
const userStore = useUserStore();
// 测试数据
// const userInfo = {
// nickname: "测试用户",
// avatar: "/source/UI/defaultUser.webp",
// };
const userInfo = computed(() => {
return userStore.getBriefInfo();
});
const rail = ref(appStore.sidebar.collapse);
// const isDev = ref(appStore.devMode);
// theme
const themeGet = computed({
get () {
@@ -178,6 +183,16 @@ onMounted(async () => {
await listenOnTheme();
});
async function refreshUser () {
const cookie_token = userStore.getCookieItem("cookie_token");
const account_id = userStore.getCookieItem("account_id");
const res = await TGRequest.User.byCookie.getUserInfo(cookie_token, account_id);
if (res.hasOwnProperty("nickname")) {
const info = res as BTMuli.User.Base.BriefInfo;
userStore.setBriefInfo(info);
}
}
async function listenOnTheme () {
await event.listen("readTheme", (e) => {
const theme = e.payload as string;

View File

@@ -168,6 +168,7 @@ import { fs, app, os } from "@tauri-apps/api";
import { useAppStore } from "../store/modules/app";
import { useHomeStore } from "../store/modules/home";
import { useAchievementsStore } from "../store/modules/achievements";
import { useUserStore } from "../store/modules/user";
// utils
import { backupUiafData, restoreUiafData } from "../utils/UIAF";
import TGSqlite from "../utils/TGSqlite";
@@ -175,6 +176,7 @@ import TGRequest from "../web/request/TGRequest";
// Store
const appStore = useAppStore();
const userStore = useUserStore();
const homeStore = useHomeStore();
const achievementsStore = useAchievementsStore();
@@ -429,7 +431,15 @@ async function inputCookie () {
}
try {
await TGRequest.User.init(ticket, uid);
await userStore.initCookie();
loadingTitle.value = "正在获取用户信息...";
const cookie_token = userStore.getCookieItem("cookie_token");
const res = await TGRequest.User.byCookie.getUserInfo(cookie_token, uid);
// . 判断返回是否为 BTMuli.User.Base.BriefInfo
if (res.hasOwnProperty("nickname")) {
const info = res as BTMuli.User.Base.BriefInfo;
userStore.setBriefInfo(info);
}
loading.value = false;
snackbarText.value = "Cookie 已保存!";
snackbarColor.value = "success";

View File

@@ -1,47 +0,0 @@
/**
* @file store modules hk4e.ts
* @description store modules hk4e.ts
* @author BTMuli<outlook.com>
* @since Alpha v0.1.2
*/
// pinia
import { defineStore } from "pinia";
export const useHk4eStore = defineStore(
"hk4e",
() => {
// getCookie
function getCookie (): string {
return localStorage.getItem("hk4eCookie") || "{}";
}
// 获取具体的 cookie
function getCookieItem (key: string): string {
const cookie = getCookie();
const cookieParsed = JSON.parse(cookie);
return cookieParsed.value[key];
}
// setCookie
function setCookie (value: string): void {
// 解析 cookie
const cookieObj: Record<string, string> = {};
value.split(";").forEach((item) => {
const [key, value] = item.split("=");
cookieObj[key.trim()] = value;
});
// 保存 cookie
localStorage.setItem("hk4eCookie", JSON.stringify(cookieObj));
}
return {
getCookie,
setCookie,
getCookieItem,
};
},
{
persist: true,
},
);

53
src/store/modules/user.ts Normal file
View File

@@ -0,0 +1,53 @@
/**
* @file store modules user.ts
* @description User store module
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.2.0
*/
// vue
import { ref } from "vue";
// pinia
import { defineStore } from "pinia";
// utils
import TGSqlite from "../../utils/TGSqlite";
export const useUserStore = defineStore(
"user", () => {
const briefInfo = ref({
nickname: "",
avatar: "",
uid: "",
desc: "",
} as BTMuli.User.Base.BriefInfo);
const cookie = ref({} as Record<string, string>);
function setBriefInfo (info: BTMuli.User.Base.BriefInfo): void {
briefInfo.value = info;
}
function getBriefInfo (): Record<string, string> {
return briefInfo.value;
}
function getCookieItem (key: string): string {
return cookie.value[key] || "";
}
async function initCookie (): Promise<void> {
const ck = await TGSqlite.getCookie();
if (cookie.value !== ck) {
cookie.value = ck;
}
}
return {
getBriefInfo,
setBriefInfo,
getCookieItem,
initCookie,
};
},
{
persist: true,
},
);

View File

@@ -45,4 +45,21 @@ declare namespace BTMuli.User.Base {
stoken_v2?: string
stuid: string
}
/**
* @description 用户简略信息
* @since Alpha v0.2.0
* @interface BriefInfo
* @property {string} nickname 用户昵称
* @property {string} uid 用户 uid
* @property {string} avatar 用户头像
* @property {string} desc 用户简介
* @returns BriefInfo
*/
export interface BriefInfo {
nickname: string
uid: string
avatar: string
desc: string
}
}

10
src/web/api/BBS.ts Normal file
View 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

View File

@@ -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, // 获取角色信息
},

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 {

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