添加 TpVote

* PostID:44118649
This commit is contained in:
BTMuli
2023-12-28 00:15:08 +08:00
parent 6b4cd2a666
commit ca9c5ca931
5 changed files with 290 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import TpText from "./tp-text.vue";
import TpUnknown from "./tp-unknown.vue"; import TpUnknown from "./tp-unknown.vue";
import TpVillaCard from "./tp-villaCard.vue"; import TpVillaCard from "./tp-villaCard.vue";
import TpVod from "./tp-vod.vue"; import TpVod from "./tp-vod.vue";
import TpVote from "./tp-vote.vue";
interface TpParserProps { interface TpParserProps {
data: TGApp.Plugins.Mys.SctPost.Base[]; data: TGApp.Plugins.Mys.SctPost.Base[];
@@ -37,6 +38,8 @@ function getTpName(tp: TGApp.Plugins.Mys.SctPost.Base) {
return TpMention; return TpMention;
} else if ("villa_card" in tp.insert) { } else if ("villa_card" in tp.insert) {
return TpVillaCard; return TpVillaCard;
} else if ("vote" in tp.insert) {
return TpVote;
} }
return TpUnknown; return TpUnknown;
} }

View File

@@ -0,0 +1,143 @@
<template>
<div class="tp-vote-box">
<div class="tp-vote-info">
<span>{{ votes?.title }}</span>
<span>{{ votes?.count }}人已参与|{{ votes?.is_over ? "已截止" : "投票中" }}</span>
</div>
<div class="tp-vote-list">
<div v-for="(item, index) in votes?.data" :key="index" class="tp-vote-item">
<div class="tp-vote-item-title">
<span>{{ item.title }}</span>
<span>
<span>{{ item.count }}</span>
<span>{{ item.percent.toFixed(2) }}%</span>
</span>
</div>
<div class="tp-vote-progress">
<div class="tp-vote-val" :style="{ width: item.percent + '%' }" />
</div>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import Mys from "../../plugins/Mys";
interface TpVote {
insert: {
vote: {
id: string;
uid: string;
};
};
}
interface TpVoteProps {
data: TpVote;
}
interface TpVoteInfo {
title: string;
count: number;
is_over: boolean;
data: Array<{
title: string;
count: number;
percent: number;
}>;
}
const props = defineProps<TpVoteProps>();
const votes = ref<TpVoteInfo>();
onMounted(async () => {
const vote = props.data.insert.vote;
const voteInfo = await Mys.Vote.get(vote.id, vote.uid);
const voteResult = await Mys.Vote.result(vote.id, vote.uid);
votes.value = {
title: voteInfo.title,
count: voteResult.user_cnt,
is_over: voteResult.is_over,
data: voteInfo.vote_option_indexes.map((item, index) => ({
title: item,
count: voteResult.option_stats[index],
percent: (voteResult.option_stats[index] / voteResult.user_cnt) * 100,
})),
};
});
</script>
<style lang="css" scoped>
.tp-vote-box {
display: flex;
flex-direction: column;
padding: 10px;
border: 1px solid var(--common-shadow-1);
border-radius: 5px;
row-gap: 10px;
}
.tp-vote-info {
display: flex;
align-items: center;
justify-content: space-between;
}
.tp-vote-info :first-child {
font-size: 20px;
font-weight: bold;
}
.tp-vote-list {
display: grid;
gap: 10px 20px;
grid-template-columns: repeat(2, 1fr);
}
.tp-vote-item {
display: flex;
flex-direction: column;
gap: 5px;
}
.tp-vote-item-title {
display: flex;
align-items: center;
justify-content: space-between;
}
.tp-vote-item-title :first-child {
font-size: 16px;
font-weight: bold;
}
.tp-vote-item-title :last-child {
display: flex;
flex-direction: column;
gap: 5px;
}
.tp-vote-item-title :last-child :first-child {
font-size: 12px;
}
.tp-vote-item-title :last-child :last-child {
font-size: 10px;
}
.tp-vote-progress {
overflow: hidden;
width: 100%;
height: 10px;
border-radius: 5px;
background: var(--common-shadow-1);
}
.tp-vote-val {
height: 100%;
border-radius: 5px;
background: linear-gradient(to right, #fb7299, #00aeec);
}
</style>

View File

@@ -14,6 +14,7 @@ import getLotteryData from "./request/getLotteryData";
import getNewsList from "./request/getNewsList"; import getNewsList from "./request/getNewsList";
import getPositionData from "./request/getPositionData"; import getPositionData from "./request/getPositionData";
import getPostData from "./request/getPostData"; import getPostData from "./request/getPostData";
import { getVoteInfo, getVoteResult } from "./request/getVoteData";
import getGachaCard from "./utils/getGachaCard"; import getGachaCard from "./utils/getGachaCard";
import getLotteryCard from "./utils/getLotteryCard"; import getLotteryCard from "./utils/getLotteryCard";
import { getActivityCard, getNewsCard, getNoticeCard } from "./utils/getNewsCard"; import { getActivityCard, getNewsCard, getNoticeCard } from "./utils/getNewsCard";
@@ -58,6 +59,10 @@ const Mys = {
getQr: getLoginQr, getQr: getLoginQr,
getData: getLoginStatus, getData: getLoginStatus,
}, },
Vote: {
get: getVoteInfo,
result: getVoteResult,
},
}; };
export default Mys; export default Mys;

View File

@@ -0,0 +1,56 @@
/**
* @file plugins/Mys/request/getVoteData.ts
* @description Mys 插件投票请求
* @since Beta v0.3.9
*/
import { http } from "@tauri-apps/api";
import MysApi from "../api";
/**
* @description 获取投票信息
* @since Beta v0.3.9
* @param {string} id 投票 ID
* @param {string} uid 用户 ID
* @return {Promise<TGApp.Plugins.Mys.Vote.Info>}
*/
export async function getVoteInfo(id: string, uid: string): Promise<TGApp.Plugins.Mys.Vote.Info> {
const url = `https://bbs-api.miyoushe.com/apihub/api/getVotes?owner_uid=${uid}&vote_ids=${id}`;
return await http
.fetch<TGApp.Plugins.Mys.Vote.InfoResponse>(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Referer: MysApi.Post.Referer,
},
})
.then((res) => {
return res.data.data.data[0];
});
}
/**
* @description 获取投票结果
* @since Beta v0.3.9
* @param {string} id 投票 ID
* @param {string} uid 用户 ID
* @return {Promise<TGApp.Plugins.Mys.Vote.Result>}
*/
export async function getVoteResult(
id: string,
uid: string,
): Promise<TGApp.Plugins.Mys.Vote.Result> {
const url = `https://bbs-api.miyoushe.com/apihub/api/getVotesResult?owner_uid=${uid}&vote_ids=${id}`;
return await http
.fetch<TGApp.Plugins.Mys.Vote.ResultResponse>(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Referer: MysApi.Post.Referer,
},
})
.then((res) => {
return res.data.data.data[0];
});
}

83
src/plugins/Mys/types/Vote.d.ts vendored Normal file
View File

@@ -0,0 +1,83 @@
/**
* @file plugins/Mys/types/Vote.d.ts
* @description Mys 插件投票类型定义文件
* @since Beta v0.3.9
*/
/**
* @description Mys 插件投票类型
* @since Beta v0.3.9
* @namespace TGApp.Plugins.Mys.Vote
* @memberof TGApp.Plugins.Mys
*/
declare namespace TGApp.Plugins.Mys.Vote {
/**
* @description 投票信息返回
* @since Beta v0.3.9
* @interface InfoResponse
* @extends TGApp.BBS.Response.BaseWithData
* @property {Info[]} data.data 投票信息
* @return InfoResponse
*/
interface InfoResponse extends TGApp.BBS.Response.BaseWithData {
data: {
data: Info[];
};
}
/**
* @description 投票结果返回
* @since Beta v0.3.9
* @interface ResultResponse
* @extends TGApp.BBS.Response.BaseWithData
* @property {Result[]} data.data 投票结果
* @return ResultResponse
*/
interface ResultResponse extends TGApp.BBS.Response.BaseWithData {
data: {
data: Result[];
};
}
/**
* @description 投票信息
* @since Beta v0.3.9
* @interface Info
* @property {string} vote_id 投票 ID
* @property {string} uid 用户 ID
* @property {number} vote_limit 投票限制
* @property {number} end_time 投票结束时间(秒级时间戳)
* @property {string} title 投票标题
* @property {string[]} vote_option_indexes 投票选项索引
* @property {string} created_at 投票创建时间(秒级时间戳)
* @return Info
*/
interface Info {
vote_id: string;
uid: string;
vote_limit: number;
end_time: number;
title: string;
vote_option_indexes: string[];
created_at: string;
}
/**
* @description 投票结果
* @since Beta v0.3.9
* @interface Result
* @property {string} vote_id 投票 ID
* @property {boolean} is_over 是否已结束
* @property {Record<string, number>} option_stats 投票选项统计
* @property {number} user_cnt 投票人数
* @property {unknown[]} vote_option_indexes 投票选项索引
* @return Result
*/
interface Result {
vote_id: string;
is_over: boolean;
option_stats: Record<string, number>;
user_cnt: number;
vote_option_indexes: unknown[];
}
}