mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-09-12 16:40:40 +08:00
067f3ed98a
1. 新增mp-html、qs等生产依赖,补全项目基础库 2. 新增平台判断、缓存、工具函数等通用工具集 3. 新增标签页、网站浏览页、关于页等业务页面 4. 新增分类卡片、通知弹窗等业务组件 5. 新增uts-progressNotification、liu-poster、uhalo-upgrade等uni模块 6. 补充audio/video组件样式补件,修复uni-components路径缺失问题 7. 新增环境变量Halo个人令牌配置项 8. 重构store导出结构,新增appConfig/halo/setting三个状态模块 9. 新增tsconfig编译目标配置,适配更高版本ES语法
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
/**
|
|
* 投票工具(源自旧项目 utils/vote.js,按需命名导出)
|
|
* 投票状态计算、UID 缓存、投票周期判断
|
|
*/
|
|
import { getCache, setCache } from './storage'
|
|
|
|
/** 投票 UID 缓存 key */
|
|
const UnihaloVoteUid = 'unihalo_vote_uid'
|
|
|
|
export type VoteType = 'SINGLE' | 'MULTIPLE'
|
|
export type VoteState = 'not-voted' | 'voting' | 'voted' | 'vote-ended'
|
|
|
|
/** 投票类型常量 */
|
|
export const VOTE_TYPES: { SINGLE: VoteType, MULTIPLE: VoteType } = {
|
|
SINGLE: 'SINGLE',
|
|
MULTIPLE: 'MULTIPLE',
|
|
}
|
|
|
|
/** 投票状态常量 */
|
|
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',
|
|
}
|
|
|
|
/**
|
|
* 获取投票 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
|
|
}
|
|
|
|
/**
|
|
* 计算投票状态
|
|
* @param vote 投票对象(含 startTime/endTime/options)
|
|
* @param voteTypes 已投票项
|
|
* @param canAnonymously 是否允许匿名
|
|
*/
|
|
export function calcVoteState(
|
|
vote: { startTime?: string, endTime?: string, [key: string]: unknown },
|
|
voteTypes: string[],
|
|
canAnonymously: boolean,
|
|
): VoteState {
|
|
const now = Date.now()
|
|
const startTime = vote.startTime ? new Date(vote.startTime).getTime() : now
|
|
const endTime = vote.endTime ? new Date(vote.endTime).getTime() : now
|
|
|
|
if (endTime < now)
|
|
return VOTE_STATES.VOTE_ENDED
|
|
if (startTime > now)
|
|
return VOTE_STATES.NOT_VOTED
|
|
if (voteTypes.length !== 0)
|
|
return VOTE_STATES.VOTED
|
|
if (!canAnonymously)
|
|
return VOTE_STATES.NOT_VOTED
|
|
return VOTE_STATES.VOTING
|
|
}
|
|
|
|
/**
|
|
* 计算选项票数占比
|
|
* @param vote 投票对象(含 stats.voteCount)
|
|
* @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 Number(((count / total) * 100).toFixed(2))
|
|
}
|
|
|
|
/** 投票缓存 key 前缀 */
|
|
const VOTE_CACHE_KEY = 'unihalo_vote_'
|
|
|
|
interface IVoteCacheData {
|
|
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)
|
|
},
|
|
}
|