11<!-- 投票组件 -->
22<template >
3- <div class =" tp-vote-box" >
3+ <div v-if = " votes " class =" tp-vote-box" >
44 <div class =" tp-vote-info" >
5- <span >{{ votes?.title }}</span >
6- <span >{{ votes?.count }}人已参与|{{ votes?.is_over ? "已截止" : "投票中" }}</span >
5+ <div class =" tp-vote-title" >
6+ {{ votes.title }}
7+ </div >
8+ <div class =" tp-vote-overview" >
9+ <span >{{ votes.count }}人已参与</span >
10+ <span >{{ votes.is_over ? "已截止" : `投票中 ${votes.endTime} 结束` }}</span >
11+ </div >
712 </div >
813 <div class =" tp-vote-list" >
9- <div v-for =" (item, index) in votes?.data" :key =" index" class =" tp-vote-item" >
10- <div class =" tp-vote-item-title" >
11- <span class =" title" >{{ item.title }}</span >
12- <span class =" val" >
13- <span >{{ item.count }}票</span >
14- <span >{{ item.percent.toFixed(2) }}%</span >
15- </span >
14+ <div v-for =" (item, index) in votes.data" :key =" index" class =" tp-vote-item" >
15+ <!-- TODO: 投票 -->
16+ <div class =" tp-vi-title" >{{ item.title }}</div >
17+ <div class =" tp-vi-stat" >
18+ <span class =" tp-vi-cnt" >{{ item.count }}票</span >
19+ <span class =" tp-vi-percent" >{{ item.percent.toFixed(2) }}%</span >
1620 </div >
17- <div class =" tp-vote -progress" >
18- <div class = " tp-vote-val " :style =" getWidth(item)" />
21+ <div class =" tp-vi -progress" >
22+ <div :style =" getWidth(item)" class = " tp-vote-val " />
1923 </div >
2024 </div >
2125 </div >
2226 </div >
2327</template >
2428<script lang="ts" setup>
2529import ApiHubReq from " @req/apiHubReq.js" ;
26- import useAppStore from " @store/app.js" ;
27- import { storeToRefs } from " pinia" ;
30+ import { timestampToDate } from " @utils/toolFunc.js" ;
2831import { onMounted , ref , shallowRef } from " vue" ;
2932
30- type TpVote = { insert: { vote: { id: string ; uid: string } } };
33+ /** 投票组件数据类型 */
34+ type TpVote = {
35+ /** 插入数据 */
36+ insert: {
37+ /** 投票数据 */
38+ vote: {
39+ /** 投票ID */
40+ id: string ;
41+ /** 投票发布者UID */
42+ uid: string ;
43+ };
44+ };
45+ };
3146type TpVoteProps = { data: TpVote };
32- type TpVoteData = { title: string ; count: number ; percent: number };
33- type TpVoteInfo = { title: string ; count: number ; is_over: boolean ; data: Array <TpVoteData > };
47+ /** 投票选项数据 */
48+ type TpVoteData = {
49+ /** 标题 */
50+ title: string ;
51+ /** 票数 */
52+ count: number ;
53+ /** 百分比 */
54+ percent: number ;
55+ };
56+ /** 投票信息 */
57+ type TpVoteInfo = {
58+ /** 标题 */
59+ title: string ;
60+ /** 参与人数 */
61+ count: number ;
62+ /** 是否结束 */
63+ is_over: boolean ;
64+ /** 选项数据 */
65+ data: Array <TpVoteData >;
66+ /** 结束时间 */
67+ endTime: string ;
68+ };
3469
3570const props = defineProps <TpVoteProps >();
3671const votes = shallowRef <TpVoteInfo >();
3772const maxCnt = ref <number >(0 );
3873
39- const { postViewWide } = storeToRefs ( useAppStore () );
74+ console . log ( " tp-vote: " , props . data );
4075
4176onMounted (async () => {
4277 const vote = props .data .insert .vote ;
4378 const voteInfo = await ApiHubReq .vote .info (vote .id , vote .uid );
79+ console .log (` [${props .data .insert .vote .id }]voteInfo: ` , voteInfo );
4480 const voteResult = await ApiHubReq .vote .result (vote .id , vote .uid );
81+ console .log (" [${props.data.insert.vote.id}]voteResult:" , voteResult );
4582 votes .value = {
4683 title: voteInfo .title ,
4784 count: voteResult .user_cnt ,
@@ -51,6 +88,7 @@ onMounted(async () => {
5188 count: voteResult .option_stats [index ] ?? 0 ,
5289 percent: ((voteResult .option_stats [index ] ?? 0 ) / voteResult .user_cnt ) * 100 ,
5390 })),
91+ endTime: timestampToDate (voteInfo .end_time * 1000 ),
5492 };
5593 maxCnt .value = Math .max (... votes .value .data .map ((item ) => item .count ));
5694});
@@ -60,71 +98,77 @@ function getWidth(item: TpVoteData): string {
6098 return ` width: ${(item .count / maxCnt .value ) * 100 }%; ` ;
6199}
62100 </script >
63- <style lang="css " scoped>
101+ <style lang="scss " scoped>
64102.tp-vote-box {
65103 display : flex ;
66104 flex-direction : column ;
67105 padding : 8px ;
68106 border : 1px solid var (--common-shadow-1 );
69107 border-radius : 4px ;
70- margin : 12 px 0 ;
108+ margin : 8 px 0 ;
71109 background : var (--box-bg-1 );
72110 row-gap : 8px ;
73111}
74112
75113.tp-vote-info {
114+ position : relative ;
76115 display : flex ;
77- flex-wrap : wrap ;
78- align-items : center ;
79- justify-content : space-between ;
116+ flex-direction : column ;
117+ align-items : flex-start ;
118+ justify-content : center ;
80119}
81120
82- .tp-vote-info :first-child {
121+ .tp-vote-title {
122+ font-family : var (--font-title );
83123 font-size : 20px ;
84- font-weight : bold ;
124+ }
125+
126+ .tp-vote-overview {
127+ position : relative ;
128+ display : flex ;
129+ align-items : center ;
130+ justify-content : center ;
131+ margin-left : auto ;
132+ column-gap : 4px ;
133+ font-size : 12px ;
134+ font-style : italic ;
85135}
86136
87137.tp-vote-list {
138+ position : relative ;
88139 display : grid ;
89- gap : 12px 16px ;
90- grid-template-columns : v-bind(" postViewWide ? '1fr 1fr' : '1fr'" );
140+ width : 100% ;
141+ gap : 8px ;
142+ grid-template-columns : repeat (auto-fill , minmax (300px , 1fr ));
91143}
92144
93145.tp-vote-item {
146+ position : relative ;
94147 display : flex ;
95148 flex-direction : column ;
96- justify-content : flex-end ;
97149 gap : 4px ;
98150}
99151
100- .tp-vote-item -title {
152+ .tp-vi -title {
101153 display : flex ;
102154 align-items : flex-end ;
103155 justify-content : space-between ;
104156 column-gap : 4px ;
157+ font-size : 16px ;
158+ font-weight : bold ;
159+ }
105160
106- .title {
107- font-size : 16px ;
108- font-weight : bold ;
109- }
110-
111- .val {
112- display : flex ;
113- flex-direction : column ;
114- gap : 4px ;
115- white-space : nowrap ;
116-
117- :first-child {
118- font-size: 12px ;
119- }
120-
121- :last-child {
122- font-size : 10px ;
123- }
124- }
161+ .tp-vi-stat {
162+ position : relative ;
163+ display : flex ;
164+ align-items : center ;
165+ justify-content : center ;
166+ margin-left : auto ;
167+ column-gap : 4px ;
168+ font-size : 12px ;
125169}
126170
127- .tp-vote -progress {
171+ .tp-vi -progress {
128172 overflow : hidden ;
129173 width : 100% ;
130174 height : 8px ;
0 commit comments