请求测试通过

This commit is contained in:
目棃
2024-08-15 18:43:05 +08:00
parent 32fb6df2ac
commit 6e0bb42c3b
5 changed files with 87 additions and 15 deletions

View File

@@ -13,9 +13,56 @@
</div>
</div>
</div>
<h1>数据请求测试</h1>
<div class="btn-list">
<v-btn class="test-btn" @click="tryGetList">获取角色列表</v-btn>
<v-btn class="test-btn" @click="tryGetDetail">获取角色详情</v-btn>
</div>
</div>
</template>
<script lang="ts" setup></script>
<script lang="ts" setup>
import showSnackbar from "../../components/func/snackbar.js";
import TSUserAvatar from "../../plugins/Sqlite/modules/userAvatar.js";
import { useUserStore } from "../../store/modules/user.js";
import TGRequest from "../../web/request/TGRequest.js";
const userStore = useUserStore();
async function tryGetList(): Promise<void> {
const ck = userStore.cookie;
if (!userStore.cookie) {
showSnackbar({ text: "请先登录!", color: "warn" });
return;
}
const cookie = { account_id: ck!.account_id, cookie_token: ck!.cookie_token };
const account = userStore.account;
const res = await TGRequest.User.byCookie.getAvatarList(cookie, account.gameUid);
if (!Array.isArray(res)) {
showSnackbar({ text: `[${res.retcode}] ${res.message}`, color: "error" });
return;
}
console.log(res);
showSnackbar({ text: "获取成功!", color: "success" });
}
async function tryGetDetail(): Promise<void> {
const ck = userStore.cookie;
if (!userStore.cookie) {
showSnackbar({ text: "请先登录!", color: "warn" });
return;
}
const cookie = { account_id: ck!.account_id, cookie_token: ck!.cookie_token };
const account = userStore.account;
const idList = await TSUserAvatar.getAllAvatarId();
const res = await TGRequest.User.byCookie.getAvatarDetail(cookie, account.gameUid, idList);
if ("retcode" in res) {
showSnackbar({ text: `[${res.retcode}] ${res.message}`, color: "error" });
return;
}
console.log(res);
showSnackbar({ text: "获取成功!", color: "success" });
}
</script>
<style lang="css" scoped>
.test-box {
display: flex;

View File

@@ -0,0 +1,25 @@
/**
* @file plugins/Sqlite/modules/userAvatar.ts
* @description 用户角色模块
* @since Beta v0.5.3
*/
import TGSqlite from "../index.js";
/**
* @description 获取用户角色id列表
* @since Beta v0.5.3
* @returns {Promise<string[]>} 角色id列表
*/
async function getAllAvatarId(): Promise<string[]> {
const db = await TGSqlite.getDB();
type resType = Array<{ cid: string }>;
const res = await db.select<resType>("SELECT DISTINCT cid FROM UserCharacters;");
return res.map((i) => i.cid);
}
const TSUserAvatar = {
getAllAvatarId,
};
export default TSUserAvatar;

View File

@@ -53,7 +53,7 @@ declare namespace TGApp.Game.Avatar {
* @property {number} rarity - 角色稀有度
* @property {string} side_icon - 角色侧边头像
* @property {Weapon} weapon - 角色武器
* @property {number} weapon_type - 角色武器类型 // todo: 需要查明枚举类型
* @property {number} weapon_type - 角色武器类型
* @return Avatar
*/
interface Avatar {
@@ -81,7 +81,7 @@ declare namespace TGApp.Game.Avatar {
* @property {number} id - 武器 ID
* @property {number} level - 武器等级
* @property {number} rarity - 武器稀有度
* @property {number} type - 武器类型,与上面的 weapon_type 一致 // todo: 需要查明枚举类型
* @property {number} type - 武器类型,与上面的 weapon_type 一致
* @return Weapon
*/
interface Weapon {

View File

@@ -61,7 +61,7 @@ async function TGHttp<T>(
url += `?${query}`;
}
console.log("fetch url: ", url);
console.log("fetch options: ", fetchOptions);
console.log("fetch options: ", options);
return await fetch(url, fetchOptions)
.then((res) => {
if (res.ok) {

View File

@@ -12,15 +12,15 @@ import TGUtils from "../utils/TGUtils.js";
* @description 获取角色列表
* @since Beta v0.5.3
* @param {Record<string, string>} cookie Cookie
* @param {string} uid 用户 uid
* @param {string} roleId 用户 uid
* @return {Promise<TGApp.Game.Avatar.Avatar[]|TGApp.BBS.Response.Base>}
*/
export async function getAvatarList(
cookie: Record<string, string>,
uid: string,
roleId: string,
): Promise<TGApp.Game.Avatar.Avatar[] | TGApp.BBS.Response.Base> {
const url = TGApi.GameData.byCookie.getAvatarList;
const data = { uid, region: TGUtils.Tools.getServerByUid(uid) };
const data = { role_id: roleId, server: TGUtils.Tools.getServerByUid(roleId) };
const header = TGUtils.User.getHeader(cookie, "POST", data, "common");
const resp = await TGHttp<TGApp.Game.Avatar.ListResponse | TGApp.BBS.Response.Base>(url, {
method: "POST",
@@ -35,25 +35,25 @@ export async function getAvatarList(
* @description 获取角色详情
* @since Beta v0.5.3
* @param {Record<string, string>} cookie Cookie
* @param {string} uid 用户 uid
* @param {string} roleId 用户 uid
* @param {string[]} avatarIds 角色 id 列表
* @return {Promise<TGApp.Game.Avatar.AvatarDetail|TGApp.BBS.Response.Base>}
*/
export async function getAvatarDetail(
cookie: Record<string, string>,
uid: string,
roleId: string,
avatarIds: string[],
): Promise<TGApp.Game.Avatar.AvatarDetail | TGApp.BBS.Response.Base> {
const url = TGApi.GameData.byCookie.getAvatarDetail;
const params = {
role_id: uid,
server: TGUtils.Tools.getServerByUid(uid),
const data = {
role_id: roleId,
server: TGUtils.Tools.getServerByUid(roleId),
character_ids: avatarIds,
};
const header = TGUtils.User.getHeader(cookie, "GET", params, "common");
const header = TGUtils.User.getHeader(cookie, "POST", data, "common");
const resp = await TGHttp<TGApp.Game.Avatar.DetailResponse | TGApp.BBS.Response.Base>(url, {
method: "GET",
query: params,
method: "POST",
body: JSON.stringify(data),
headers: header,
});
if (resp.retcode !== 0) return <TGApp.BBS.Response.Base>resp;