♻️ userStore 及 cookie 相关类型重构 #51

This commit is contained in:
BTMuli
2023-12-16 22:07:31 +08:00
parent 00386bc7ee
commit 4370bbaa57
16 changed files with 267 additions and 236 deletions

View File

@@ -145,11 +145,11 @@ async function checkUserLoad(): Promise<void> {
} else { } else {
console.info("cookie 数据已加载!"); console.info("cookie 数据已加载!");
} }
const infoLocal = userStore.briefInfo; const infoLocal = userStore.getBriefInfo();
const appData = await TGSqlite.getAppData(); const appData = await TGSqlite.getAppData();
const infoDB = appData.find((item) => item.key === "userInfo")?.value; const infoDB = appData.find((item) => item.key === "userInfo")?.value;
if (infoDB === undefined && JSON.stringify(infoLocal) !== "{}") { if (infoDB === undefined && JSON.stringify(infoLocal) !== "{}") {
await TGSqlite.saveAppData("userInfo", JSON.stringify(infoLocal)); await userStore.saveBriefInfo();
} else if (infoDB !== undefined && infoLocal !== JSON.parse(infoDB)) { } else if (infoDB !== undefined && infoLocal !== JSON.parse(infoDB)) {
userStore.setBriefInfo(JSON.parse(infoDB)); userStore.setBriefInfo(JSON.parse(infoDB));
console.info("briefInfo 数据已更新!"); console.info("briefInfo 数据已更新!");

View File

@@ -171,18 +171,12 @@ const userStore = useUserStore();
const isDevEnv = ref<boolean>(import.meta.env.MODE === "development"); const isDevEnv = ref<boolean>(import.meta.env.MODE === "development");
const userInfo = computed(() => { const userInfo = computed(() => {
if (appStore.isLogin) {
const info = userStore.getBriefInfo(); const info = userStore.getBriefInfo();
return { if (info && info.nickname) return info;
nickname: info.nickname,
avatar: info.avatar,
};
} else {
return { return {
nickname: "未登录", nickname: "未登录",
avatar: "/source/UI/defaultUser.webp", avatar: "/source/UI/defaultUser.webp",
}; };
}
}); });
const rail = ref(appStore.sidebar.collapse); const rail = ref(appStore.sidebar.collapse);
// theme // theme

View File

@@ -21,7 +21,6 @@ import QrcodeVue from "qrcode.vue";
import { computed, reactive, ref, watch } from "vue"; import { computed, reactive, ref, watch } from "vue";
import Mys from "../../plugins/Mys"; import Mys from "../../plugins/Mys";
import TGSqlite from "../../plugins/Sqlite";
import { useUserStore } from "../../store/modules/user"; import { useUserStore } from "../../store/modules/user";
import TGRequest from "../../web/request/TGRequest"; import TGRequest from "../../web/request/TGRequest";
import showSnackbar from "../func/snackbar"; import showSnackbar from "../func/snackbar";
@@ -48,7 +47,7 @@ const visible = computed({
const loading = ref<boolean>(false); const loading = ref<boolean>(false);
const qrCode = ref<string>(""); const qrCode = ref<string>("");
const ticket = ref<string>(""); const ticket = ref<string>("");
const cookie = reactive<Record<string, string>>({ const cookie = reactive<TGApp.User.Account.Cookie>({
account_id: "", account_id: "",
ltuid: "", ltuid: "",
stuid: "", stuid: "",
@@ -142,8 +141,7 @@ async function getTokens(): Promise<void> {
if (typeof cookieTokenRes === "string") cookie.cookie_token = cookieTokenRes; if (typeof cookieTokenRes === "string") cookie.cookie_token = cookieTokenRes;
const ltokenRes = await TGRequest.User.bySToken.getLToken(cookie.mid, cookie.stoken); const ltokenRes = await TGRequest.User.bySToken.getLToken(cookie.mid, cookie.stoken);
if (typeof ltokenRes === "string") cookie.ltoken = ltokenRes; if (typeof ltokenRes === "string") cookie.ltoken = ltokenRes;
userStore.cookie = cookie; await userStore.saveCookie(cookie);
await TGSqlite.saveAppData("cookie", JSON.stringify(cookie));
} }
</script> </script>
<style lang="css" scoped> <style lang="css" scoped>

View File

@@ -88,7 +88,7 @@ const loadingSub = ref<string>();
// data // data
const userTab = ref<number>(0); const userTab = ref<number>(0);
const user = computed<TGApp.Sqlite.Account.Game>(() => userStore.getCurAccount()); const user = ref<TGApp.Sqlite.Account.Game>(userStore.getCurAccount());
const localAbyss = ref<TGApp.Sqlite.Abyss.SingleTable[]>([]); const localAbyss = ref<TGApp.Sqlite.Abyss.SingleTable[]>([]);
const localAbyssID = ref<number[]>([]); const localAbyssID = ref<number[]>([]);
@@ -114,12 +114,19 @@ async function initAbyssData(): Promise<void> {
async function getAbyssData(): Promise<void> { async function getAbyssData(): Promise<void> {
loadingTitle.value = "正在获取深渊数据"; loadingTitle.value = "正在获取深渊数据";
loading.value = true; loading.value = true;
const abyssCookie = userStore.getCookieGroup4(); if (!userStore.cookie) {
const cookie: Record<string, string> = { showSnackbar({
account_id: abyssCookie.account_id, text: "未登录",
cookie_token: abyssCookie.cookie_token, color: "error",
ltoken: abyssCookie.ltoken, });
ltuid: abyssCookie.ltuid, loading.value = false;
return;
}
const cookie = {
account_id: userStore.cookie.account_id,
cookie_token: userStore.cookie.cookie_token,
ltoken: userStore.cookie.ltoken,
ltuid: userStore.cookie.ltuid,
}; };
loadingTitle.value = "正在获取上期深渊数据"; loadingTitle.value = "正在获取上期深渊数据";
const resP = await TGRequest.User.byCookie.getAbyss(cookie, "2", user.value); const resP = await TGRequest.User.byCookie.getAbyss(cookie, "2", user.value);

View File

@@ -54,7 +54,7 @@
/> />
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import DucDetailOverlay from "../../components/devCharacter/duc-detail-overlay.vue"; import DucDetailOverlay from "../../components/devCharacter/duc-detail-overlay.vue";
import showSnackbar from "../../components/func/snackbar"; import showSnackbar from "../../components/func/snackbar";
@@ -78,7 +78,6 @@ const loadingSub = ref<string>();
// data // data
const isEmpty = ref(true); const isEmpty = ref(true);
const roleList = ref<TGApp.Sqlite.Character.UserRole[]>([]); const roleList = ref<TGApp.Sqlite.Character.UserRole[]>([]);
const roleCookie = computed(() => userStore.getCookieGroup4());
// overlay // overlay
const visible = ref(false); const visible = ref(false);
@@ -134,7 +133,21 @@ async function loadRole(): Promise<void> {
async function refreshRoles(): Promise<void> { async function refreshRoles(): Promise<void> {
loadingTitle.value = "正在获取角色数据"; loadingTitle.value = "正在获取角色数据";
loading.value = true; loading.value = true;
const res = await TGRequest.User.byLToken.getRoleList(roleCookie.value, user); if (!userStore.cookie) {
showSnackbar({
text: "请先登录",
color: "error",
});
loading.value = false;
return;
}
const cookie = {
account_id: userStore.cookie.account_id,
cookie_token: userStore.cookie.cookie_token,
ltoken: userStore.cookie.ltoken,
ltuid: userStore.cookie.ltuid,
};
const res = await TGRequest.User.byLToken.getRoleList(cookie, user);
if (Array.isArray(res)) { if (Array.isArray(res)) {
loadingTitle.value = "正在保存角色数据"; loadingTitle.value = "正在保存角色数据";
await TGSqlite.saveUserCharacter(user.gameUid, res); await TGSqlite.saveUserCharacter(user.gameUid, res);
@@ -153,12 +166,20 @@ async function refreshRoles(): Promise<void> {
async function refreshTalent(): Promise<void> { async function refreshTalent(): Promise<void> {
loadingTitle.value = "正在获取天赋数据"; loadingTitle.value = "正在获取天赋数据";
loading.value = true; loading.value = true;
const talentCookie = userStore.getCookieGroup2(); if (!userStore.cookie) {
showSnackbar({
text: "请先登录",
color: "error",
});
loading.value = false;
return;
}
for (const role of roleList.value) { for (const role of roleList.value) {
loadingTitle.value = `正在获取${role.name}的天赋数据`; loadingTitle.value = `正在获取${role.name}的天赋数据`;
loadingSub.value = `CID${role.cid}`; loadingSub.value = `CID${role.cid}`;
const res = await TGRequest.User.calculate.getSyncAvatarDetail( const res = await TGRequest.User.calculate.getSyncAvatarDetail(
talentCookie, userStore.cookie.account_id,
userStore.cookie.cookie_token,
user.gameUid, user.gameUid,
role.cid, role.cid,
); );

View File

@@ -119,6 +119,14 @@ async function confirmRefresh(): Promise<void> {
} }
loadingTitle.value = "正在获取 authkey"; loadingTitle.value = "正在获取 authkey";
loading.value = true; loading.value = true;
if (!userStore.cookie) {
showSnackbar({
color: "error",
text: "请先登录",
});
loading.value = false;
return;
}
const cookie = { const cookie = {
stoken: userStore.cookie.stoken, stoken: userStore.cookie.stoken,
mid: userStore.cookie.mid, mid: userStore.cookie.mid,

View File

@@ -32,7 +32,7 @@
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import showSnackbar from "../../components/func/snackbar"; import showSnackbar from "../../components/func/snackbar";
import TSubLine from "../../components/main/t-subline.vue"; import TSubLine from "../../components/main/t-subline.vue";
@@ -58,7 +58,6 @@ const loadingSub = ref<string>();
// data // data
const isEmpty = ref<boolean>(true); const isEmpty = ref<boolean>(true);
const recordData = ref<TGApp.Sqlite.Record.SingleTable>(<TGApp.Sqlite.Record.SingleTable>{}); const recordData = ref<TGApp.Sqlite.Record.SingleTable>(<TGApp.Sqlite.Record.SingleTable>{});
const recordCookie = computed<TGApp.BBS.Constant.CookieGroup2>(() => userStore.getCookieGroup2());
onMounted(async () => { onMounted(async () => {
loadingTitle.value = "正在加载战绩数据"; loadingTitle.value = "正在加载战绩数据";
@@ -83,7 +82,19 @@ async function initUserRecordData(): Promise<void> {
async function refresh(): Promise<void> { async function refresh(): Promise<void> {
loadingTitle.value = "正在获取战绩数据"; loadingTitle.value = "正在获取战绩数据";
loading.value = true; loading.value = true;
const res = await TGRequest.User.getRecord(recordCookie.value, user); if (!userStore.cookie) {
showSnackbar({
text: "请先登录",
color: "error",
});
loading.value = false;
return;
}
const cookie = {
account_id: userStore.cookie.account_id,
cookie_token: userStore.cookie.cookie_token,
};
const res = await TGRequest.User.getRecord(cookie, user);
if (!("retcode" in res)) { if (!("retcode" in res)) {
console.log(res); console.log(res);
loadingTitle.value = "正在保存战绩数据"; loadingTitle.value = "正在保存战绩数据";

View File

@@ -39,14 +39,14 @@
</template> </template>
</v-list-item> </v-list-item>
<v-list-item title="登录信息"> <v-list-item title="登录信息">
<v-list-item-subtitle v-show="userInfo.nickname !== '未登录'"> <v-list-item-subtitle v-show="userInfo?.nickname !== '未登录'">
{{ userInfo.nickname }} uid:{{ userInfo.uid }} {{ userInfo?.nickname }} uid:{{ userInfo?.uid }}
</v-list-item-subtitle> </v-list-item-subtitle>
<v-list-item-subtitle v-show="userInfo.nickname === '未登录'"> <v-list-item-subtitle v-show="userInfo?.nickname === '未登录'">
未登录请扫码登录 未登录请扫码登录
</v-list-item-subtitle> </v-list-item-subtitle>
<template #prepend> <template #prepend>
<img class="config-icon" :src="userInfo.avatar" alt="Login" /> <img class="config-icon" :src="userInfo?.avatar" alt="Login" />
</template> </template>
<template #append> <template #append>
<v-btn class="config-btn" @click="confirmScanLogin">扫码登录</v-btn> <v-btn class="config-btn" @click="confirmScanLogin">扫码登录</v-btn>
@@ -202,15 +202,8 @@ const showReset = ref<boolean>(false);
// data // data
const showHome = ref<string[]>(homeStore.getShowValue()); const showHome = ref<string[]>(homeStore.getShowValue());
const userInfo = computed(() => { const userInfo = computed(() => {
if (!appStore.isLogin) {
return {
nickname: "未登录",
uid: "-1",
desc: "请扫码登录",
avatar: "/source/UI/defaultUser.webp",
};
} else {
const info = userStore.getBriefInfo(); const info = userStore.getBriefInfo();
if (info && info.nickname) {
return { return {
nickname: info.nickname, nickname: info.nickname,
uid: info.uid, uid: info.uid,
@@ -218,6 +211,12 @@ const userInfo = computed(() => {
avatar: info.avatar, avatar: info.avatar,
}; };
} }
return {
nickname: "未登录",
uid: "-1",
desc: "请扫码登录",
avatar: "/source/UI/defaultUser.webp",
};
}); });
const vuetifyTheme = computed(() => { const vuetifyTheme = computed(() => {
return appStore.theme === "dark" ? "dark" : "light"; return appStore.theme === "dark" ? "dark" : "light";
@@ -280,6 +279,13 @@ async function confirmRefreshUser(): Promise<void> {
}); });
return; return;
} }
if (!userStore.cookie) {
showSnackbar({
color: "error",
text: "请先登录",
});
return;
}
const ck = userStore.cookie; const ck = userStore.cookie;
if (JSON.stringify(ck) === "{}") { if (JSON.stringify(ck) === "{}") {
showSnackbar({ showSnackbar({
@@ -322,7 +328,7 @@ async function confirmRefreshUser(): Promise<void> {
loadingTitle.value = "刷新失败!正在获取用户头像、昵称信息"; loadingTitle.value = "刷新失败!正在获取用户头像、昵称信息";
failCount++; failCount++;
} }
await TGSqlite.saveAppData("cookie", JSON.stringify(ck)); await userStore.saveCookie(ck);
const infoRes = await TGRequest.User.byCookie.getUserInfo(ck.cookie_token, ck.account_id); const infoRes = await TGRequest.User.byCookie.getUserInfo(ck.cookie_token, ck.account_id);
if ("retcode" in infoRes) { if ("retcode" in infoRes) {
console.error(infoRes); console.error(infoRes);
@@ -335,8 +341,7 @@ async function confirmRefreshUser(): Promise<void> {
avatar: infoRes.avatar_url, avatar: infoRes.avatar_url,
desc: infoRes.introduce, desc: infoRes.introduce,
}; };
userStore.setBriefInfo(briefInfo); await userStore.saveBriefInfo(briefInfo);
await TGSqlite.saveAppData("userInfo", JSON.stringify(briefInfo));
loadingTitle.value = "获取成功!正在获取用户游戏账号信息"; loadingTitle.value = "获取成功!正在获取用户游戏账号信息";
} }
const accountRes = await TGRequest.User.byCookie.getAccounts(ck.cookie_token, ck.account_id); const accountRes = await TGRequest.User.byCookie.getAccounts(ck.cookie_token, ck.account_id);

View File

@@ -1,7 +1,7 @@
/** /**
* @file plugins/Sqlite/index.ts * @file plugins/Sqlite/index.ts
* @description Sqlite 数据库操作类 * @description Sqlite 数据库操作类
* @since Beta v0.3.6 * @since Beta v0.3.8
*/ */
import { app } from "@tauri-apps/api"; import { app } from "@tauri-apps/api";
@@ -103,10 +103,10 @@ class Sqlite {
/** /**
* @description 获取 cookie * @description 获取 cookie
* @since Beta v0.3.3 * @since Beta v0.3.8
* @returns {Promise<Record<string, string>>} * @returns {Promise<TGApp.User.Account.Cookie>}
*/ */
public async getCookie(): Promise<Record<string, string>> { public async getCookie(): Promise<TGApp.User.Account.Cookie> {
const db = await this.getDB(); const db = await this.getDB();
const sql = "SELECT value FROM AppData WHERE key='cookie';"; const sql = "SELECT value FROM AppData WHERE key='cookie';";
const res: Array<{ value: string }> = await db.select(sql); const res: Array<{ value: string }> = await db.select(sql);

View File

@@ -1,22 +1,56 @@
/** /**
* @file store modules user.ts * @file store/modules/user.ts
* @description User store module * @description 用户信息模块
* @since Beta v0.3.0 * @since Beta v0.3.8
*/ */
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { ref } from "vue"; import { ref } from "vue";
import TGSqlite from "../../plugins/Sqlite";
export const useUserStore = defineStore( export const useUserStore = defineStore(
"user", "user",
() => { () => {
const briefInfo = ref<TGApp.App.Account.BriefInfo>({ const briefInfo = ref<TGApp.App.Account.BriefInfo>(loadBriefInfo());
const account = ref<TGApp.Sqlite.Account.Game>(loadAccount());
const cookie = ref<TGApp.User.Account.Cookie>();
/**
* @description 从本地加载用户信息
* @since Beta v0.3.8
* @function loadBriefInfo
* @memberof useUserStore
* @returns {TGApp.App.Account.BriefInfo}
*/
function loadBriefInfo(): TGApp.App.Account.BriefInfo {
const info = window.localStorage.getItem("briefInfo");
if (info !== null && info !== undefined) {
console.log(JSON.parse(info).briefInfo);
return JSON.parse(info).briefInfo;
}
return {
nickname: "", nickname: "",
avatar: "", avatar: "",
uid: "", uid: "",
desc: "", desc: "",
}); };
const account = ref<TGApp.Sqlite.Account.Game>({ }
/**
* @description 从本地加载当前用户信息
* @since Beta v0.3.8
* @function loadAccount
* @memberof useUserStore
* @returns {TGApp.Sqlite.Account.Game}
*/
function loadAccount(): TGApp.Sqlite.Account.Game {
const info = window.localStorage.getItem("account");
if (info !== null && info !== undefined) {
console.log(JSON.parse(info).account);
return JSON.parse(info).account;
}
return {
gameBiz: "", gameBiz: "",
gameUid: "", gameUid: "",
isChosen: 0, isChosen: 0,
@@ -25,63 +59,95 @@ export const useUserStore = defineStore(
nickname: "", nickname: "",
region: "", region: "",
regionName: "", regionName: "",
}); };
const cookie = ref<Record<string, string>>({});
function setBriefInfo(info: TGApp.App.Account.BriefInfo): void {
briefInfo.value = info;
} }
function getBriefInfo(): Record<string, string> { /**
* @description 获取用户信息
* @since Beta v0.3.8
* @function getBriefInfo
* @memberof useUserStore
* @returns {TGApp.App.Account.BriefInfo}
*/
function getBriefInfo(): TGApp.App.Account.BriefInfo {
return briefInfo.value; return briefInfo.value;
} }
function setCurAccount(user: TGApp.Sqlite.Account.Game): void { /**
account.value = user; * @description 获取当前用户信息
} * @since Beta v0.3.8
* @function getCurAccount
* @memberof useUserStore
* @returns {TGApp.Sqlite.Account.Game}
*/
function getCurAccount(): TGApp.Sqlite.Account.Game { function getCurAccount(): TGApp.Sqlite.Account.Game {
return account.value; return account.value;
} }
function getCookieItem(key: string): string { /**
return cookie.value[key] || ""; * @description 设置用户信息
* @param info
* @since Beta v0.3.8
* @function setBriefInfo
* @memberof useUserStore
* @param {TGApp.App.Account.BriefInfo} info - 用户信息
* @returns {void}
*/
function setBriefInfo(info: TGApp.App.Account.BriefInfo): void {
briefInfo.value = info;
} }
function getCookieGroup2(): TGApp.BBS.Constant.CookieGroup2 { /**
return { * @description 设置当前用户信息
account_id: getCookieItem("account_id"), * @since Beta v0.3.8
cookie_token: getCookieItem("cookie_token"), * @function setCurAccount
}; * @memberof useUserStore
* @param {TGApp.Sqlite.Account.Game} user - 用户信息
* @returns {void}
*/
function setCurAccount(user: TGApp.Sqlite.Account.Game): void {
account.value = user;
} }
function getCookieGroup3(): TGApp.BBS.Constant.CookieGroup3 { /**
return { * @description 保存cookie到本地
ltoken: getCookieItem("ltoken"), * @since Beta v0.3.8
ltuid: getCookieItem("ltuid"), * @function saveCookie
}; * @memberof useUserStore
* @param {TGApp.User.Account.Cookie} ck - cookie对象
* @returns {Promise<void>}
*/
async function saveCookie(ck?: TGApp.User.Account.Cookie): Promise<void> {
if (ck) {
cookie.value = ck;
}
await TGSqlite.saveAppData("cookie", JSON.stringify(cookie.value));
} }
function getCookieGroup4(): TGApp.BBS.Constant.CookieGroup4 { /**
return { * @description 保存用户信息到本地
account_id: getCookieItem("account_id"), * @since Beta v0.3.8
cookie_token: getCookieItem("cookie_token"), * @function saveBriefInfo
ltoken: getCookieItem("ltoken"), * @memberof useUserStore
ltuid: getCookieItem("ltuid"), * @param {TGApp.App.Account.BriefInfo} info - 用户信息
}; * @returns {Promise<void>}
*/
async function saveBriefInfo(info?: TGApp.App.Account.BriefInfo): Promise<void> {
if (info) {
setBriefInfo(info);
localStorage.setItem("briefInfo", JSON.stringify({ briefInfo: info }));
}
await TGSqlite.saveAppData("userInfo", JSON.stringify(briefInfo.value));
} }
return { return {
briefInfo,
cookie, cookie,
account,
getBriefInfo, getBriefInfo,
setBriefInfo, setBriefInfo,
setCurAccount, setCurAccount,
getCurAccount, getCurAccount,
getCookieGroup2, saveCookie,
getCookieGroup3, saveBriefInfo,
getCookieGroup4,
}; };
}, },
{ {

View File

@@ -1,96 +0,0 @@
/**
* @file types/BBS/Constant.d.ts
* @description BBS 常量相关类型定义文件
* @since Alpha v0.1.6
*/
declare namespace TGApp.BBS.Constant {
/**
* @description 用户 Cookie 类型
* @interface Cookie
* @since Alpha v0.1.5
* @description 这边只写了需要的,其他的可以自行添加
* @description login_ticket 与 login_uid 一起使用
* @see TGRequest.User.byLoginTicket.getTokens
* @property {string} login_ticket 登录凭证
* @property {string} login_uid 登录 uid
* @description account_id 与 cookie_token 一起使用
* @see TGRequest.User.byCookie.getAccounts
* @property {string} account_id 账号 id
* @property {string} cookie_token cookie token
* @description ltoken 与 ltuid 一起使用
* @see TGRequest.User.byLToken.verify
* @property {string} ltoken ltoken
* @property {string} ltuid ltoken 对应的 uid
* @description stoken 与 stuid 一起使用,这是旧版本的 token
* @see TGRequest.User.bySToken.getCookieToken
* @property {string} stoken stoken
* @property {string} stuid stoken 对应的 uid
* @description stoken_v2 与 mid 一起使用,这是新版本的 token
* @see https://github.com/BTMuli/TeyvatGuide/issues/18
* @property {string} stoken_v2 stoken_v2
* @property {string} mid mid
* @return Cookie
*/
interface Cookie {
login_ticket: string;
login_uid: string;
account_id: string;
cookie_token: string;
ltoken: string;
ltuid: string;
mid: string;
stoken: string;
stuid: string;
stoken_v2?: string;
}
/**
* @description cookie 组合-1
* @interface CookieGroup1
* @since Alpha v0.1.6
* @property {string} login_ticket 登录凭证
* @property {string} login_uid 登录 uid
* @return CookieGroup1
*/
interface CookieGroup1 {
login_ticket: string;
login_uid: string;
}
/**
* @description cookie 组合-2
* @interface CookieGroup2
* @since Alpha v0.1.6
* @property {string} account_id 账号 id
* @property {string} cookie_token cookie token
* @return CookieGroup2
*/
interface CookieGroup2 {
account_id: string;
cookie_token: string;
}
/**
* @description cookie 组合-3
* @interface CookieGroup3
* @since Alpha v0.1.6
* @property {string} ltoken ltoken
* @property {string} ltuid ltoken 对应的 uid
* @return CookieGroup3
*/
interface CookieGroup3 {
ltoken: string;
ltuid: string;
}
/**
* @description cookie 组合-4
* @interface CookieGroup4
* @since Alpha v0.1.6
* @extends CookieGroup2
* @extends CookieGroup3
* @return CookieGroup4
*/
interface CookieGroup4 extends CookieGroup2, CookieGroup3 {}
}

View File

@@ -1,12 +1,12 @@
/** /**
* @file types/User/Account.d.ts * @file types/User/Account.d.ts
* @description 用户账号相关类型定义文件 * @description 用户账号相关类型定义文件
* @since Alpha v0.1.5 * @since Beta v0.3.8
*/ */
/** /**
* @description 用户账号相关类型定义命名空间 * @description 用户账号相关类型定义命名空间
* @since Alpha v0.1.5 * @since Beta v0.3.8
* @namespace TGApp.User.Account * @namespace TGApp.User.Account
* @memberof TGApp.User * @memberof TGApp.User
*/ */
@@ -49,4 +49,29 @@ declare namespace TGApp.User.Account {
region: string; region: string;
region_name: string; region_name: string;
} }
/**
* @description 用户 Cookie 类型
* @since Beta v0.3.8
* @interface Cookie
* @memberof TGApp.User.Account
* @property {string} account_id 账号 ID
* @property {string} cookie_token Cookie Token
* @property {string} game_token 游戏 Token
* @property {string} ltoken LToken
* @property {string} ltuid LTUID
* @property {string} mid MID
* @property {string} stoken SToken_v2
* @return Cookie
*/
interface Cookie {
account_id: string;
cookie_token: string;
game_token: string;
ltoken: string;
ltuid: string;
mid: string;
stoken: string;
stuid: string;
}
} }

View File

@@ -334,7 +334,7 @@ class TGClient {
/** /**
* @func genAuthKey * @func genAuthKey
* @since Beta v0.3.7 * @since Beta v0.3.8
* @desc 获取米游社客户端的 authkey * @desc 获取米游社客户端的 authkey
* @param {Record<string, string>} payload - 请求参数 * @param {Record<string, string>} payload - 请求参数
* @param {string} callback - 回调函数名 * @param {string} callback - 回调函数名
@@ -342,6 +342,7 @@ class TGClient {
*/ */
async genAuthKey(payload: Record<string, string>, callback: string): Promise<void> { async genAuthKey(payload: Record<string, string>, callback: string): Promise<void> {
const userStore = useUserStore(); const userStore = useUserStore();
if (!userStore.cookie) return;
const cookie = { const cookie = {
mid: userStore.cookie.mid, mid: userStore.cookie.mid,
stoken: userStore.cookie.stoken, stoken: userStore.cookie.stoken,
@@ -352,7 +353,7 @@ class TGClient {
/** /**
* @func getCookieInfo * @func getCookieInfo
* @since Beta v0.3.4 * @since Beta v0.3.8
* @desc 获取米游社客户端的 cookie * @desc 获取米游社客户端的 cookie
* @param {unknown} payload - 请求参数 * @param {unknown} payload - 请求参数
* @param {string} callback - 回调函数名 * @param {string} callback - 回调函数名
@@ -360,6 +361,7 @@ class TGClient {
*/ */
async getCookieInfo(payload: unknown, callback: string): Promise<void> { async getCookieInfo(payload: unknown, callback: string): Promise<void> {
const user = useUserStore(); const user = useUserStore();
if (!user.cookie) return;
const data = { const data = {
ltoken: user.cookie.ltoken, ltoken: user.cookie.ltoken,
ltuid: user.cookie.ltuid, ltuid: user.cookie.ltuid,
@@ -382,6 +384,7 @@ class TGClient {
} }
const ckPayload = <getCookieTokenPayload>payload; const ckPayload = <getCookieTokenPayload>payload;
const user = useUserStore(); const user = useUserStore();
if (!user.cookie) return;
if (ckPayload.forceRefresh) { if (ckPayload.forceRefresh) {
const res = await getCookieTokenBySToken(user.cookie.mid, user.cookie.stoken); const res = await getCookieTokenBySToken(user.cookie.mid, user.cookie.stoken);
if (typeof res !== "string") { if (typeof res !== "string") {
@@ -405,7 +408,7 @@ class TGClient {
/** /**
* @func getActionTicket * @func getActionTicket
* @since Beta v0.3.4 * @since Beta v0.3.8
* @desc 获取米游社客户端的 action_ticket\ * @desc 获取米游社客户端的 action_ticket\
* @param {unknown} payload - 请求参数 * @param {unknown} payload - 请求参数
* @param {string} callback - 回调函数名 * @param {string} callback - 回调函数名
@@ -414,6 +417,7 @@ class TGClient {
async getActionTicket(payload: any, callback: string): Promise<void> { async getActionTicket(payload: any, callback: string): Promise<void> {
const actionType = payload.action_type; const actionType = payload.action_type;
const user = useUserStore(); const user = useUserStore();
if (!user.cookie) return;
const uid = user.getCurAccount().gameUid; const uid = user.getCurAccount().gameUid;
const mid = user.cookie.mid; const mid = user.cookie.mid;
const stoken = user.cookie.stoken; const stoken = user.cookie.stoken;
@@ -477,13 +481,14 @@ class TGClient {
/** /**
* @func getUserInfo * @func getUserInfo
* @since Beta v0.3.4 * @since Beta v0.3.8
* @desc 获取米游社客户端的用户信息 * @desc 获取米游社客户端的用户信息
* @param {string} callback - 回调函数名 * @param {string} callback - 回调函数名
* @returns {void} - 无返回值 * @returns {void} - 无返回值
*/ */
async getUserInfo(callback: string): Promise<void> { async getUserInfo(callback: string): Promise<void> {
const user = useUserStore(); const user = useUserStore();
if (!user.cookie) return;
const cookieToken = user.cookie.cookie_token; const cookieToken = user.cookie.cookie_token;
const accountId = user.cookie.account_id; const accountId = user.cookie.account_id;
const userInfo = await TGRequest.User.byCookie.getUserInfo(cookieToken, accountId); const userInfo = await TGRequest.User.byCookie.getUserInfo(cookieToken, accountId);

View File

@@ -1,7 +1,7 @@
/** /**
* @file core/utils/getGameRecord.ts * @file core/utils/getGameRecord.ts
* @description 获取游戏数据的函数 * @description 获取游戏数据的函数
* @since Alpha v0.2.1 * @since Beta v0.3.8
*/ */
import { http } from "@tauri-apps/api"; import { http } from "@tauri-apps/api";
@@ -11,24 +11,20 @@ import TGUtils from "../utils/TGUtils";
/** /**
* @description 获取用户游戏数据 * @description 获取用户游戏数据
* @since Alpha v0.2.1 * @since Beta v0.3.8
* @description 这边的 ck 可以是 cookie_token 和 account_id * @description 这边的 ck 可以是 cookie_token 和 account_id
* @description 也可以是 ltoken 和 ltuid * @description 也可以是 ltoken 和 ltuid
* @param {TGApp.BBS.Constant.CookieGroup2} cookie cookie * @param {Record<string, string>} cookie cookie
* @param {TGApp.Sqlite.Account.Game} user 用户的基本信息 * @param {TGApp.Sqlite.Account.Game} user 用户的基本信息
* @returns {Promise<TGApp.Game.Record.FullData|TGApp.BBS.Response.Base>} 用户基本信息 * @returns {Promise<TGApp.Game.Record.FullData|TGApp.BBS.Response.Base>} 用户基本信息
*/ */
export async function getGameRecord( export async function getGameRecord(
cookie: TGApp.BBS.Constant.CookieGroup2, cookie: Record<string, string>,
user: TGApp.Sqlite.Account.Game, user: TGApp.Sqlite.Account.Game,
): Promise<TGApp.Game.Record.FullData | TGApp.BBS.Response.Base> { ): Promise<TGApp.Game.Record.FullData | TGApp.BBS.Response.Base> {
const url = TGApi.GameData.getUserBase; const url = TGApi.GameData.getUserBase;
const ck: Record<string, string> = {
account_id: cookie.account_id,
cookie_token: cookie.cookie_token,
};
const params = { role_id: user.gameUid, server: user.region }; const params = { role_id: user.gameUid, server: user.region };
const header = TGUtils.User.getHeader(ck, "GET", params, "common"); const header = TGUtils.User.getHeader(cookie, "GET", params, "common");
return await http return await http
.fetch<TGApp.Game.Record.Response | TGApp.BBS.Response.Base>(url, { .fetch<TGApp.Game.Record.Response | TGApp.BBS.Response.Base>(url, {
method: "GET", method: "GET",

View File

@@ -1,7 +1,7 @@
/** /**
* @file web/request/getRoleList.ts * @file web/request/getRoleList.ts
* @description 获取游戏角色列表的请求方法 * @description 获取游戏角色列表的请求方法
* @since Alpha v0.2.0 * @since Beta v0.3.8
*/ */
import { http } from "@tauri-apps/api"; import { http } from "@tauri-apps/api";
@@ -11,27 +11,20 @@ import TGUtils from "../utils/TGUtils";
/** /**
* @description 通过 Cookie 获取用户角色列表 * @description 通过 Cookie 获取用户角色列表
* @since Alpha v0.2.0 * @since Beta v0.3.8
* @param {TGApp.BBS.Constant.CookieGroup4} cookie Cookie * @param {Record<string, string>} cookie Cookie
* @param {TGApp.Sqlite.Account.Game} account 游戏账号 * @param {TGApp.Sqlite.Account.Game} account 游戏账号
* @returns {Promise<TGApp.Game.Character.ListItem[]|TGApp.BBS.Response.Base>} 用户角色列表 * @returns {Promise<TGApp.Game.Character.ListItem[]|TGApp.BBS.Response.Base>} 用户角色列表
*/ */
export async function getGameRoleListByLToken( export async function getGameRoleListByLToken(
cookie: TGApp.BBS.Constant.CookieGroup4, cookie: Record<string, string>,
account: TGApp.Sqlite.Account.Game, account: TGApp.Sqlite.Account.Game,
): Promise<TGApp.Game.Character.ListItem[] | TGApp.BBS.Response.Base> { ): Promise<TGApp.Game.Character.ListItem[] | TGApp.BBS.Response.Base> {
const url = TGApi.GameData.byCookie.getCharacter; const url = TGApi.GameData.byCookie.getCharacter;
const uid = account.gameUid; const uid = account.gameUid;
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
const data = { role_id: uid, server: TGUtils.Tools.getServerByUid(uid) }; const data = { role_id: uid, server: TGUtils.Tools.getServerByUid(uid) };
// 格式转换 将 cookie 对象转换 record<string, string> const header = TGUtils.User.getHeader(cookie, "POST", JSON.stringify(data), "common");
const ck: Record<string, string> = {
account_id: cookie.account_id,
cookie_token: cookie.cookie_token,
ltuid: cookie.ltuid,
ltoken: cookie.ltoken,
};
const header = TGUtils.User.getHeader(ck, "POST", JSON.stringify(data), "common");
return await http return await http
.fetch<TGApp.Game.Character.ListResponse | TGApp.BBS.Response.Base>(url, { .fetch<TGApp.Game.Character.ListResponse | TGApp.BBS.Response.Base>(url, {
method: "POST", method: "POST",

View File

@@ -1,7 +1,7 @@
/** /**
* @file web/request/getSyncAvatarDetail.ts * @file web/request/getSyncAvatarDetail.ts
* @description 获取同步角色详情相关请求函数 * @description 获取同步角色详情相关请求函数
* @since Beta v0.3.4 * @since Beta v0.3.8
*/ */
import { app, http } from "@tauri-apps/api"; import { app, http } from "@tauri-apps/api";
@@ -11,14 +11,16 @@ import TGUtils from "../utils/TGUtils";
/** /**
* @description 获取同步角色详情 * @description 获取同步角色详情
* @since Beta v0.3.4 * @since Beta v0.3.8
* @param {TGApp.BBS.Constant.CookieGroup2} cookie cookie * @param {string} accountId 账号 id
* @param {string} cookieToken cookie token
* @param {string} uid 用户 uid * @param {string} uid 用户 uid
* @param {number} avatarId 角色 id * @param {number} avatarId 角色 id
* @returns {Promise<TGApp.Game.Calculate.AvatarDetail|TGApp.BBS.Response.Base>} * @returns {Promise<TGApp.Game.Calculate.AvatarDetail|TGApp.BBS.Response.Base>}
*/ */
async function getSyncAvatarDetail( async function getSyncAvatarDetail(
cookie: TGApp.BBS.Constant.CookieGroup2, accountId: string,
cookieToken: string,
uid: string, uid: string,
avatarId: number, avatarId: number,
): Promise<TGApp.Game.Calculate.AvatarDetail | TGApp.BBS.Response.Base> { ): Promise<TGApp.Game.Calculate.AvatarDetail | TGApp.BBS.Response.Base> {
@@ -28,15 +30,11 @@ async function getSyncAvatarDetail(
region: TGUtils.Tools.getServerByUid(uid), region: TGUtils.Tools.getServerByUid(uid),
avatar_id: avatarId.toString(), avatar_id: avatarId.toString(),
}; };
const ck: Record<string, string> = {
account_id: cookie.account_id,
cookie_token: cookie.cookie_token,
};
const version = await app.getVersion(); const version = await app.getVersion();
const header = { const header = {
"User-Agent": `TeyvatGuide/${version}`, "User-Agent": `TeyvatGuide/${version}`,
Referer: "https://webstatic.mihoyo.com/", Referer: "https://webstatic.mihoyo.com/",
Cookie: TGUtils.Tools.transCookie(ck), Cookie: TGUtils.Tools.transCookie({ account_id: accountId, cookie_token: cookieToken }),
}; };
return await http return await http
.fetch<TGApp.Game.Calculate.SyncAvatarDetailResponse | TGApp.BBS.Response.Base>(url, { .fetch<TGApp.Game.Calculate.SyncAvatarDetailResponse | TGApp.BBS.Response.Base>(url, {