1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-09-12 16:40:40 +08:00

refactor: 优化投票相关组件与工具代码格式与样式

1. 统一uh-article-vote与uh-article-vote-item组件代码缩进与格式
2. 更新投票提示文案与文章卡片样式适配
3. 规范vote.ts工具类代码格式与分号使用
4. 调整文章卡片布局与置顶样式
This commit is contained in:
小莫唐尼
2026-09-09 13:22:50 +08:00
parent 5b091d3c82
commit 004bac175e
4 changed files with 509 additions and 513 deletions
+64 -60
View File
@@ -2,46 +2,46 @@
* 投票工具(源自旧项目 utils/vote.js,按需命名导出)
* 投票状态计算、UID 缓存、投票周期判断
*/
import { getCache, setCache } from './storage'
import { getCache, setCache } from './storage';
/** 投票 UID 缓存 key */
const UnihaloVoteUid = 'unihalo_vote_uid'
const UnihaloVoteUid = 'unihalo_vote_uid';
export type VoteType = 'single' | 'multiple' | 'pk'
export type VoteState = 'not-voted' | 'voting' | 'voted' | 'vote-ended'
export type VoteType = 'single' | 'multiple' | 'pk';
export type VoteState = 'not-voted' | 'voting' | 'voted' | 'vote-ended';
/** 投票类型中文映射(与旧项目一致:key 为插件小写 type) */
export const VOTE_TYPES: Record<string, string> = {
pk: '双选PK',
multiple: '多选',
single: '单选',
}
pk: '双选PK',
multiple: '多选',
single: '单选'
};
/** 投票状态常量(内部状态机) */
export const VOTE_STATES: { NOT_VOTED: VoteState, VOTING: VoteState, VOTED: VoteState, VOTE_ENDED: VoteState } = {
NOT_VOTED: 'not-voted',
VOTING: 'voting',
VOTED: 'voted',
VOTE_ENDED: 'vote-ended',
}
export const VOTE_STATES: { NOT_VOTED: VoteState; VOTING: VoteState; VOTED: VoteState; VOTE_ENDED: VoteState } = {
NOT_VOTED: 'not-voted',
VOTING: 'voting',
VOTED: 'voted',
VOTE_ENDED: 'vote-ended'
};
/** 投票展示状态(与旧项目 VOTE_STATES 一致:中文 + 颜色) */
export const VOTE_STATE_LABELS: Record<string, { state: string, color: string }> = {
: { state: '未开始', color: 'orange' },
: { state: '进行中', color: 'green' },
: { state: '已结束', color: 'red' },
}
export const VOTE_STATE_LABELS: Record<string, { state: string; color: string }> = {
: { state: '未开始', color: 'orange' },
: { state: '进行中', color: 'green' },
: { state: '已结束', color: 'red' }
};
/**
* 获取投票 UID(不存在则生成)
*/
export function getOrCreateVoteUid(): string {
let uid = getCache<string>(UnihaloVoteUid)
if (!uid) {
uid = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
setCache(UnihaloVoteUid, uid)
}
return uid
let uid = getCache<string>(UnihaloVoteUid);
if (!uid) {
uid = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
setCache(UnihaloVoteUid, uid);
}
return uid;
}
/**
@@ -50,22 +50,25 @@ export function getOrCreateVoteUid(): string {
* @param vote 投票对象(含 spec.timeLimit/hasEnded/startDate/endDate)
* @returns { state: '未开始' | '进行中' | '已结束', color: 'orange' | 'green' | 'red' }
*/
export function calcVoteState(
vote: { spec?: { timeLimit?: string, hasEnded?: boolean, startDate?: string, endDate?: string, [key: string]: unknown } },
): { state: string, color: string } {
if (vote.spec?.timeLimit !== 'custom') {
return vote.spec?.hasEnded ? VOTE_STATE_LABELS['已结束'] : VOTE_STATE_LABELS['进行中']
}
export function calcVoteState(vote: { spec?: { timeLimit?: string; hasEnded?: boolean; startDate?: string; endDate?: string; [key: string]: unknown } }): {
state: string;
color: string;
} {
if (vote.spec?.timeLimit !== 'custom') {
return vote.spec?.hasEnded ? VOTE_STATE_LABELS['已结束'] : VOTE_STATE_LABELS['进行中'];
}
const nowTime = new Date().getTime()
const startTime = vote.spec?.startDate ? new Date(vote.spec.startDate).getTime() : nowTime
const endTime = vote.spec?.endDate ? new Date(vote.spec.endDate).getTime() : nowTime
const nowTime = new Date().getTime();
const startTime = vote.spec?.startDate ? new Date(vote.spec.startDate).getTime() : nowTime;
const endTime = vote.spec?.endDate ? new Date(vote.spec.endDate).getTime() : nowTime;
if (nowTime < startTime)
return VOTE_STATE_LABELS['未开始']
if (nowTime < endTime)
return VOTE_STATE_LABELS['进行中']
return vote.spec?.hasEnded ? VOTE_STATE_LABELS['已结束'] : VOTE_STATE_LABELS['进行中']
if (nowTime < startTime) {
return VOTE_STATE_LABELS['未开始'];
}
if (nowTime < endTime) {
return VOTE_STATE_LABELS['进行中'];
}
return vote.spec?.hasEnded ? VOTE_STATE_LABELS['已结束'] : VOTE_STATE_LABELS['进行中'];
}
/**
@@ -74,35 +77,36 @@ export function calcVoteState(
* @param option 选项(含 count)
*/
export function calcVotePercent(vote: { stats?: { voteCount?: number } }, option: { count?: number }): number {
const total = vote.stats?.voteCount || 0
const count = option.count || 0
if (total === 0)
return 0
return Math.round((count / total) * 100)
const total = vote.stats?.voteCount || 0;
const count = option.count || 0;
if (total === 0) {
return 0;
}
return Math.round((count / total) * 100);
}
/** 投票缓存 key 前缀 */
const VOTE_CACHE_KEY = 'unihalo_vote_'
const VOTE_CACHE_KEY = 'unihalo_vote_';
interface IVoteCacheData {
selected: string[]
data: unknown
selected: string[];
data: unknown;
}
/**
* 投票缓存工具(源自旧项目 utils/vote.js 的 voteCacheUtil)
*/
export const voteCacheUtil = {
/** 是否已缓存(已投票) */
has(name: string): boolean {
return !!getCache<IVoteCacheData>(VOTE_CACHE_KEY + name)
},
/** 获取缓存数据 */
get(name: string): IVoteCacheData | null {
return getCache<IVoteCacheData>(VOTE_CACHE_KEY + name) || null
},
/** 写入缓存 */
set(name: string, data: IVoteCacheData): void {
setCache(VOTE_CACHE_KEY + name, data)
},
}
/** 是否已缓存(已投票) */
has(name: string): boolean {
return !!getCache<IVoteCacheData>(VOTE_CACHE_KEY + name);
},
/** 获取缓存数据 */
get(name: string): IVoteCacheData | null {
return getCache<IVoteCacheData>(VOTE_CACHE_KEY + name) || null;
},
/** 写入缓存 */
set(name: string, data: IVoteCacheData): void {
setCache(VOTE_CACHE_KEY + name, data);
}
};