1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-11-13 22:11:42 +08:00

feat: 新增文章投票功能,优化多项页面细节

1. 新增文章详情页投票组件,支持投票插件检查、展开收起
2. 新增下划线文本、投票卡片、投票列表页组件
3. 优化自定义tabbar样式与返回逻辑
4. 更新投票工具类与投票详情页逻辑
5. 优化公告详情页样式与功能
6. 修复部分样式与类型定义问题
This commit is contained in:
小莫唐尼
2026-09-08 13:21:49 +08:00
parent e1c84ff701
commit dbe70e3b49
18 changed files with 1802 additions and 867 deletions
@@ -0,0 +1,421 @@
<script lang="ts" setup>
/**
* 文章详情投票单卡(源自旧项目 components/article-vote,新建复刻)
* 支持 single/multiple/pk 三种类型,已投票/已结束展示百分比,可直接提交
*/
import { computed, ref, watch } from 'vue'
import { getVoteDetail, submitVote } from '@/api/uni-halo'
import { calcVoteState, VOTE_TYPES, voteCacheUtil } from '@/utils/vote'
import { formatTime as formatTimeUtil } from '@/utils/formatTime'
import type { IVote, IVoteDetail, IVoteOption } from '@/api/types/uni-halo'
const props = defineProps<{
voteId: string
index?: number
}>()
const loading = ref<'loading' | 'success' | 'error'>('loading')
const loadingText = ref('加载中,请稍等...')
const isSubmit = ref(false)
const voteData = ref<IVote | null>(null)
const submitForm = ref<{ voteData: string[] }>({ voteData: [] })
/** 选项 id → 票数(来自 VoteDetail.voteDataList 或 Vote.stats.voteDataList) */
const voteCountMap = ref<Record<string, number>>({})
/** 是否已投票(本地缓存) */
const isVoted = computed(() => voteCacheUtil.has(props.voteId))
/** 投票展示状态(与旧项目一致:未开始/进行中/已结束) */
const voteState = computed(() => {
if (!voteData.value?.spec)
return null
return calcVoteState(voteData.value)
})
/** 类型中文名 */
const voteTypeLabel = computed(() => {
const type = voteData.value?.spec?.type
return type ? VOTE_TYPES[type] || type : ''
})
/** 选项票数占比(取自 voteCountMap) */
function handleCalcPercent(option: IVoteOption): number {
const total = voteData.value?.stats?.voteCount || 0
const count = voteCountMap.value[option.id || ''] || 0
if (total === 0)
return 0
return Number(((count / total) * 100).toFixed(2))
}
/** 是否展示百分比结果(已投票 或 已结束) */
const showResult = computed(() => isVoted.value || voteData.value?.spec?.hasEnded || false)
async function handleGetData() {
loading.value = 'loading'
loadingText.value = '加载中,请稍等...'
try {
const res = await getVoteDetail(props.voteId)
const detail = res.data as IVoteDetail
const vote = detail.vote || (detail as unknown as IVote)
voteData.value = vote
submitForm.value.voteData = []
// 票数映射:详情 voteDataList 优先,其次 stats.voteDataList
const countList = detail.voteDataList?.length ? detail.voteDataList : vote.stats?.voteDataList
const map: Record<string, number> = {}
;(countList || []).forEach((item) => {
if (item.id)
map[item.id] = item.voteCount || 0
})
voteCountMap.value = map
loading.value = 'success'
}
catch (err) {
console.error('获取投票失败', err)
loading.value = 'error'
loadingText.value = '投票内容加载失败,点击重试'
}
}
function handleSelectSingleOption(option: IVoteOption) {
const spec = voteData.value?.spec
if (!spec)
return
if (voteState.value?.state === '未开始') {
showToast('投票未开始')
return
}
if (spec.hasEnded || isVoted.value)
return
spec.options?.forEach((item) => {
item.checked = option.id === item.id
})
submitForm.value.voteData = (spec.options || []).filter(x => x.checked).map(item => item.id || '')
}
function handleSelectCheckboxOption(option: IVoteOption) {
const spec = voteData.value?.spec
if (!spec)
return
if (voteState.value?.state === '未开始') {
showToast('投票未开始')
return
}
if (spec.hasEnded || isVoted.value)
return
const checkedList = (spec.options || []).filter(x => x.checked && x.id !== option.id)
if (spec.type === 'multiple' && checkedList.length >= (spec.maxVotes || 0)) {
showToast(`最多选择 ${spec.maxVotes} 项`)
return
}
spec.options?.forEach((item) => {
if (option.id === item.id) {
item.checked = !item.checked
}
})
submitForm.value.voteData = (spec.options || []).filter(x => x.checked).map(item => item.id || '')
}
function handleSubmitTip(text: string) {
showToast(text)
}
async function handleSubmit() {
const spec = voteData.value?.spec
if (!spec)
return
if (submitForm.value.voteData.length === 0) {
showToast('请先选择选项')
return
}
if (!spec.canAnonymously) {
uni.showModal({
title: '提示',
content: '该投票不支持匿名,请到博主的 网站端 进行投票!',
cancelColor: '#666666',
cancelText: '关闭',
confirmText: '复制地址',
success: (res) => {
if (res.confirm) {
uni.setClipboardData({
data: import.meta.env.VITE_SERVER_BASEURL || '',
showToast: false,
success: () => showToast('复制成功'),
})
}
},
})
return
}
isSubmit.value = true
uni.showLoading({ title: '正在保存...' })
try {
await submitVote(props.voteId, submitForm.value, spec.canAnonymously)
voteCacheUtil.set(props.voteId, {
selected: [...submitForm.value.voteData],
data: voteData.value,
})
showToast('提交成功')
await handleGetData()
}
catch (err) {
console.error('提交投票失败', err)
showToast('提交失败,请重试')
}
finally {
isSubmit.value = false
uni.hideLoading()
}
}
/** 跳转投票详情 */
function handleToVoteDetail() {
uni.navigateTo({
url: `/pages-blog/vote-detail/vote-detail?name=${props.voteId}`,
})
}
function showToast(content: string) {
uni.showToast({ icon: 'none', title: content, mask: true })
}
/** 格式化时间 */
function formatTime(date?: string, fmt = 'yyyy-MM-dd HH:mm'): string {
return date ? formatTimeUtil({ d: date, f: fmt }) : ''
}
watch(() => props.voteId, () => {
handleGetData()
}, { immediate: true })
defineExpose({ refresh: handleGetData })
</script>
<template>
<view class="uh-article-vote-item uh-global-card-glass box-border w-full rounded-2xl p-4">
<!-- 加载失败:点击重试 -->
<view v-if="loading === 'error'" class="vote-error py-6 text-center text-[24rpx] text-gray-400" @click="handleGetData">
{{ loadingText }}
</view>
<!-- 加载中 -->
<view v-else-if="loading === 'loading'" class="loading py-6">
<wd-skeleton :row="3" :animated="true" />
</view>
<template v-else-if="voteData">
<!-- 头部:序号/类型/状态 + 查看详情 -->
<view class="vote-card-head">
<view class="flex items-center justify-between">
<view class="flex flex-wrap items-center gap-1">
<text v-if="props.index !== undefined" class="rounded bg-orange-500 px-2 py-0.5 text-[22rpx] text-white">
{{ props.index + 1 }}
</text>
<text v-if="voteTypeLabel" class="rounded bg-primary/15 px-2 py-0.5 text-[22rpx] text-primary">
{{ voteTypeLabel }}
</text>
<text
v-if="voteState"
class="rounded px-2 py-0.5 text-[22rpx]"
:style="{ color: voteState.color, backgroundColor: `${voteState.color}1a` }"
>
{{ voteState.state }}
</text>
</view>
<text class="shrink-0 text-[22rpx] text-gray-400" @click="handleToVoteDetail">查看投票详情 ></text>
</view>
<view class="title mt-2 text-[30rpx] font-bold text-gray-900">
{{ voteData.spec?.title }}
</view>
</view>
<view class="vote-card-body mt-3">
<view v-if="voteData.spec?.remark" class="remark mb-3 text-[24rpx] text-gray-400">
{{ voteData.spec.remark }}
</view>
<!-- 单选 -->
<view v-if="voteData.spec?.type === 'single'" class="flex flex-col gap-2">
<template v-if="showResult">
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="is-voted-item relative box-border min-h-[72rpx] overflow-hidden rounded-xl text-[24rpx]"
:class="option.checked ? 'bg-primary/40 text-[#4d7c0f] font-bold' : 'bg-[#e5e5e5]/75'"
:style="{ '--percent': `${handleCalcPercent(option)}%` }"
>
<view class="is-voted-item-content relative z-2 box-border min-h-[72rpx] px-6 py-3">
<view class="flex items-center justify-between">
<view class="flex-1 text-left">{{ option.title }}</view>
<view class="shrink-0">{{ handleCalcPercent(option) }}%</view>
</view>
</view>
</view>
</template>
<template v-else>
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="vote-select-option box-border rounded-xl bg-[#f6f3ee] px-5 py-4 text-[24rpx]"
:class="option.checked ? 'border-2 border-primary bg-primary/15 text-primary font-bold' : ''"
@click="handleSelectSingleOption(option)"
>
{{ option.title }}
</view>
</template>
</view>
<!-- 多选 -->
<view v-else-if="voteData.spec?.type === 'multiple'" class="flex flex-col gap-2">
<template v-if="showResult">
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="is-voted-item relative box-border min-h-[72rpx] overflow-hidden rounded-xl text-[24rpx]"
:class="option.checked ? 'bg-primary/40 text-[#4d7c0f] font-bold' : 'bg-[#e5e5e5]/75'"
:style="{ '--percent': `${handleCalcPercent(option)}%` }"
>
<view class="is-voted-item-content relative z-2 box-border min-h-[72rpx] px-6 py-3">
<view class="flex items-center justify-between">
<view class="flex-1 text-left">{{ option.title }}</view>
<view class="shrink-0">{{ handleCalcPercent(option) }}%</view>
</view>
</view>
</view>
</template>
<template v-else>
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="vote-select-option box-border rounded-xl bg-[#f6f3ee] px-5 py-4 text-[24rpx]"
:class="option.checked ? 'border-2 border-primary bg-primary/15 text-primary font-bold' : ''"
@click="handleSelectCheckboxOption(option)"
>
{{ option.title }}
</view>
</template>
</view>
<!-- PK -->
<view v-else-if="voteData.spec?.type === 'pk'" class="flex flex-col gap-2">
<!-- PK 对抗条 -->
<view class="pk-container box-border flex w-full">
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="radio-item"
:class="optionIndex === 0 ? 'radio-left' : 'radio-right'"
:style="{ width: `${handleCalcPercent(option)}%` }"
>
<view class="option-item box-border w-full rounded-xl p-4" :class="optionIndex === 0 ? 'option-item-left' : 'option-item-right'">
{{ handleCalcPercent(option) }}%
</view>
</view>
</view>
<!-- PK 选项列表 -->
<template v-if="showResult">
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="is-voted-item relative box-border min-h-[72rpx] overflow-hidden rounded-xl text-[24rpx]"
:class="option.checked ? 'bg-primary/40 text-[#4d7c0f] font-bold' : 'bg-[#e5e5e5]/75'"
:style="{ '--percent': `${handleCalcPercent(option)}%` }"
>
<view class="is-voted-item-content relative z-2 box-border min-h-[72rpx] px-6 py-3">
<view class="flex items-center justify-between">
<view class="flex-1 text-left">选项{{ optionIndex + 1 }}:{{ option.title }}</view>
<view class="shrink-0">{{ handleCalcPercent(option) }}%</view>
</view>
</view>
</view>
</template>
<template v-else>
<view
v-for="(option, optionIndex) in voteData.spec?.options || []"
:key="optionIndex"
class="vote-select-option box-border rounded-xl bg-[#f6f3ee] px-5 py-4 text-[24rpx]"
:class="option.checked ? 'border-2 border-primary bg-primary/15 text-primary font-bold' : ''"
@click="handleSelectSingleOption(option)"
>
选项{{ optionIndex + 1 }}:{{ option.title }}
</view>
</template>
</view>
</view>
<!-- 底部:时间 + 参与人数/已投票 -->
<view class="vote-card-foot mt-3 flex items-center justify-between border-t border-[#f7f7f7] pt-2">
<text v-if="voteData.spec?.timeLimit === 'permanent'" class="text-[22rpx] text-gray-400">
结束:永久有效
</text>
<text v-else-if="voteState?.state === '未开始'" class="text-[22rpx] text-gray-400">
开始:{{ formatTime(voteData.spec?.startDate) }}
</text>
<text v-else class="text-[22rpx] text-gray-400">
结束:{{ formatTime(voteData.spec?.endDate) }}
</text>
<view class="flex items-center gap-2">
<text class="text-[22rpx] text-gray-400">{{ voteData.stats?.voteCount || 0 }} 人已参与</text>
<text v-if="isVoted" class="rounded bg-primary/15 px-2 py-0.5 text-[22rpx] text-primary">已投票</text>
</view>
</view>
<!-- 提交按钮(与旧项目一致:选择后才出现状态机) -->
<view v-if="submitForm.voteData.length !== 0" class="mt-3">
<wd-button v-if="isVoted" disabled block>
您已参与投票
</wd-button>
<wd-button v-else-if="voteState?.state === '未开始'" plain block type="warning" @click="handleSubmitTip('投票未开始')">
投票未开始
</wd-button>
<wd-button v-else-if="voteState?.state === '已结束'" plain block type="danger" @click="handleSubmitTip('投票已结束')">
投票已结束
</wd-button>
<wd-button v-else-if="!voteData.spec?.canAnonymously" plain block type="danger" @click="handleSubmit()">
不支持匿名投票
</wd-button>
<wd-button v-else block type="primary" :loading="isSubmit" :disabled="isSubmit" @click="handleSubmit()">
提交投票
</wd-button>
</view>
</template>
</view>
</template>
<style scoped lang="scss">
.pk-container {
.radio-item {
flex-grow: 1;
min-width: 30%;
max-width: 70%;
}
.option-item-left {
background: linear-gradient(90deg, #3b82f6, #60a5fa);
color: white;
clip-path: polygon(0 0, calc(100% - 40rpx) 0, 100% 100%, 0 100%);
}
.option-item-right {
background: linear-gradient(90deg, #f87171, #ef4444);
color: white;
clip-path: polygon(0 0, 100% 0, 100% 100%, 40rpx 100%);
text-align: right;
}
}
.is-voted-item {
&::before {
content: '';
width: var(--percent);
position: absolute;
left: 0;
top: 0;
bottom: 0;
background-color: #d0d0d0;
z-index: 0;
border-radius: 6rpx;
}
}
</style>
@@ -0,0 +1,54 @@
<script lang="ts" setup>
/**
* 文章详情"相关投票"区块容器(源自旧项目 article-detail 的 vote-wrap 区块)
* 只接收 voteIds;插件可用性检查/展开收起/区块标题/卡片列表全部内置
* 插件未激活或没有投票数据时,整个组件不渲染
*/
import { onMounted, ref } from 'vue'
import { NeedPluginIds } from '@/hooks/usePluginAvailable'
const props = defineProps<{
voteIds: string[]
}>()
/** 展开/收起(与旧项目 voteIsOpen 一致) */
const voteIsOpen = ref(true)
/** 投票插件可用性(检查失败/未安装时整个区块不显示) */
const { available: votePluginAvailable, check: checkVotePluginAvailable } = usePluginAvailable({
pluginId: NeedPluginIds.PluginVote,
tips: '检测到当前插件没有安装或者启用,无法使用投票功能哦,请联系管理员',
})
onMounted(() => {
checkVotePluginAvailable()
})
</script>
<template>
<view v-if="votePluginAvailable && voteIds.length > 0" class="box-border px-1">
<view class="uh-global-card-glass uh-shadow-xs mb-3 rounded-xl p-3">
<view class="flex items-center justify-between">
<uh-section-title>相关投票</uh-section-title>
<text class="text-[24rpx] text-gray-400" @click="voteIsOpen = !voteIsOpen">
{{ voteIsOpen ? '收起' : '展开' }}
</text>
</view>
<template v-if="voteIsOpen">
<view class="mt-3 flex flex-col gap-3">
<uh-article-vote-item
v-for="(voteId, voteIdIndex) in voteIds"
:key="voteId"
:vote-id="voteId"
:index="voteIdIndex"
/>
</view>
</template>
<view v-if="!voteIsOpen" class="mt-2 text-center text-[24rpx] text-gray-400" @click="voteIsOpen = !voteIsOpen">
投票已收起,点击展开 {{ voteIds.length }} 个投票项
</view>
</view>
</view>
</template>
+225 -216
View File
@@ -1,256 +1,265 @@
<script setup lang="ts">
import { computed } from 'vue'
import type { DataLoadingStatus } from '@/hooks/useDataLoading'
import { computed } from 'vue'
import type { DataLoadingStatus } from '@/hooks/useDataLoading'
interface IProps {
/** 加载状态(取值同 useDataLoading 返回的 status) */
loadingStatus?: DataLoadingStatus
/** 占位区最小高度 */
minHeight?: string
loadingText?: string
errorText?: string
emptyText?: string
/** 各态副文案(留空则不显示副行) */
loadingSubText?: string
errorSubText?: string
emptySubText?: string
useLoadingButton?: boolean
}
interface IProps {
/** 加载状态(取值同 useDataLoading 返回的 status) */
loadingStatus ?: DataLoadingStatus
/** 占位区最小高度 */
minHeight ?: string
loadingText ?: string
errorText ?: string
emptyText ?: string
/** 各态副文案(留空则不显示副行) */
loadingSubText ?: string
errorSubText ?: string
emptySubText ?: string
useLoadingButton ?: boolean
}
const props = withDefaults(defineProps<IProps>(), {
loadingStatus: 'loading',
minHeight: '80vh',
loadingText: '稍等,正在加载中哦',
errorText: '哎呀,加载失败了呢~',
emptyText: '啊偶,暂时没有数据呢~',
loadingSubText: '',
errorSubText: '请检查网络连接,或稍后再试',
emptySubText: '稍后再来看看吧~',
useLoadingButton: true,
})
const props = withDefaults(defineProps<IProps>(), {
loadingStatus: 'loading',
minHeight: '80vh',
loadingText: '稍等,正在加载中哦',
errorText: '哎呀,加载失败了呢~',
emptyText: '啊偶,暂时没有数据呢~',
loadingSubText: '',
errorSubText: '请检查网络连接,或稍后再试',
emptySubText: '稍后再来看看吧~',
useLoadingButton: true,
})
const emit = defineEmits<{ (e: 'refresh'): void }>()
const emit = defineEmits<{ (e : 'refresh') : void }>()
const isLoading = computed(() => props.loadingStatus === 'loading')
const isLoading = computed(() => props.loadingStatus === 'loading')
const statusScene = computed(() => {
switch (props.loadingStatus) {
case 'error':
return {
icon: '-injury',
stageClass: 'stage-error',
mainTextClass: 'text-red-400',
mainText: props.errorText,
subText: props.errorSubText,
}
case 'empty':
return {
icon: '-confused',
stageClass: 'stage-empty',
mainTextClass: 'text-gray-900',
mainText: props.emptyText,
subText: props.emptySubText,
}
default:
return {
icon: '-happy-1',
stageClass: 'stage-loading',
mainTextClass: 'text-primary',
mainText: props.loadingText,
subText: props.loadingSubText,
}
}
})
const statusScene = computed(() => {
switch (props.loadingStatus) {
case 'error':
return {
icon: '-injury',
stageClass: 'stage-error',
mainTextClass: 'text-red-400',
mainText: props.errorText,
subText: props.errorSubText,
}
case 'empty':
return {
icon: '-confused',
stageClass: 'stage-empty',
mainTextClass: 'text-gray-900',
mainText: props.emptyText,
subText: props.emptySubText,
}
default:
return {
icon: '-happy-1',
stageClass: 'stage-loading',
mainTextClass: 'text-primary',
mainText: props.loadingText,
subText: props.loadingSubText,
}
}
})
</script>
<template>
<view
class="w-full flex flex-col items-center justify-center gap-y-4 text-sm"
:style="{ minHeight: props.minHeight }"
>
<view class="scene relative h-[250rpx] w-[250rpx] flex items-center justify-center" :class="statusScene.stageClass">
<view class="glow absolute inset-0 m-auto h-[220rpx] w-[220rpx] rounded-full" />
<view class="deco-dot dot-a absolute rounded-full" />
<view class="deco-dot dot-b absolute rounded-full" />
<view class="bubble relative h-[150rpx] w-[150rpx] flex items-center justify-center rounded-full">
<text class="bubble-icon">
<wd-icon class-prefix="uhemoji-icon" :name="statusScene.icon" size="120rpx" />
</text>
</view>
</view>
<view class="w-full flex flex-col items-center justify-center gap-y-4 text-sm"
:style="{ minHeight: props.minHeight }">
<view class="scene relative h-[250rpx] w-[250rpx] flex items-center justify-center"
:class="statusScene.stageClass">
<view class="glow absolute inset-0 m-auto h-[220rpx] w-[220rpx] rounded-full" />
<view class="deco-dot dot-a absolute rounded-full" />
<view class="deco-dot dot-b absolute rounded-full" />
<view class="bubble relative h-[150rpx] w-[150rpx] flex items-center justify-center rounded-full">
<text class="bubble-icon">
<wd-icon class-prefix="uhemoji-icon" :name="statusScene.icon" size="120rpx" />
</text>
</view>
</view>
<!-- 文案区 -->
<view class="flex flex-col items-center">
<view class="flex items-center justify-center text-sm font-bold" :class="statusScene.mainTextClass">
<text>{{ statusScene.mainText }}</text>
<view v-if="isLoading" class="ml-1 flex items-end gap-1">
<view v-for="n in 3" :key="n" class="typing-dot bg-primary" :style="{ animationDelay: `${(n - 1) * 0.15}s` }" />
</view>
</view>
<text v-if="statusScene.subText" class="mt-3 text-xs text-gray-500">
{{ statusScene.subText }}
</text>
<uh-button v-if="props.useLoadingButton" class="mt-5" @click="emit('refresh')">
刷新试试
</uh-button>
</view>
</view>
<!-- 文案区 -->
<view class="flex flex-col items-center">
<view class="flex items-center justify-center text-sm font-bold" :class="statusScene.mainTextClass">
<text>{{ statusScene.mainText }}</text>
<view v-if="isLoading" class="ml-1 flex items-end gap-1">
<view v-for="n in 3" :key="n" class="typing-dot bg-primary"
:style="{ animationDelay: `${(n - 1) * 0.15}s` }" />
</view>
</view>
<text v-if="statusScene.subText" class="mt-3 text-xs text-gray-500">
{{ statusScene.subText }}
</text>
<view v-if="props.useLoadingButton" class="mt-5 uh-global-card-glass uh-shadow-xs border rounded-lg">
<uh-button custom-class="!rounded-lg" @click="emit('refresh')">
刷新试试
</uh-button>
</view>
</view>
</view>
</template>
<style scoped lang="scss">
.bubble {
animation: bubble-float 2s ease-in-out infinite;
}
.bubble {
animation: bubble-float 2s ease-in-out infinite;
}
.bubble-icon {
display: inline-block;
}
.bubble-icon {
display: inline-block;
}
.glow {
animation: glow-pulse 2.4s ease-in-out infinite;
}
.glow {
animation: glow-pulse 2.4s ease-in-out infinite;
}
.stage-loading .glow {
background: rgba(185, 228, 36, 0.32);
}
.stage-loading .glow {
background: rgba(185, 228, 36, 0.32);
}
.stage-error .glow {
background: rgba(248, 113, 113, 0.24);
}
.stage-error .glow {
background: rgba(248, 113, 113, 0.24);
}
.stage-empty .glow {
background: rgba(217, 249, 157, 0.5);
}
.stage-empty .glow {
background: rgba(217, 249, 157, 0.5);
}
.deco-dot {
animation: deco-float 2s ease-in-out infinite;
}
.deco-dot {
animation: deco-float 2s ease-in-out infinite;
}
.dot-a {
top: 16rpx;
left: 10rpx;
width: 22rpx;
height: 22rpx;
background: rgba(163, 230, 53, 0.9);
}
.dot-a {
top: 16rpx;
left: 10rpx;
width: 22rpx;
height: 22rpx;
background: rgba(163, 230, 53, 0.9);
}
.dot-b {
top: 4rpx;
right: 14rpx;
width: 14rpx;
height: 14rpx;
background: rgba(217, 249, 157, 0.95);
animation-delay: -0.7s;
}
.dot-b {
top: 4rpx;
right: 14rpx;
width: 14rpx;
height: 14rpx;
background: rgba(217, 249, 157, 0.95);
animation-delay: -0.7s;
}
/* —— 三态表情差异化动效(均无限循环,柔和不抢眼) —— */
.stage-loading .bubble-icon {
animation: sway 1.6s ease-in-out infinite;
}
/* —— 三态表情差异化动效(均无限循环,柔和不抢眼) —— */
.stage-loading .bubble-icon {
animation: sway 1.6s ease-in-out infinite;
}
.stage-error .bubble-icon {
animation: head-shake 2.8s ease-in-out infinite;
transform-origin: 50% 85%;
}
.stage-error .bubble-icon {
animation: head-shake 2.8s ease-in-out infinite;
transform-origin: 50% 85%;
}
.stage-empty .bubble-icon {
animation: sigh 3s ease-in-out infinite;
transform-origin: 50% 85%;
}
.stage-empty .bubble-icon {
animation: sigh 3s ease-in-out infinite;
transform-origin: 50% 85%;
}
/* —— 加载中三点跳动(错峰延迟经模板 :style 注入,避开 WXSS 不支持的 :nth-child) —— */
.typing-dot {
width: 10rpx;
height: 10rpx;
border-radius: 50%;
animation: dot-jump 1s ease-in-out infinite;
}
/* —— 加载中三点跳动(错峰延迟经模板 :style 注入,避开 WXSS 不支持的 :nth-child) —— */
.typing-dot {
width: 10rpx;
height: 10rpx;
border-radius: 50%;
animation: dot-jump 1s ease-in-out infinite;
}
/* —— keyframes —— */
@keyframes bubble-float {
0%,
100% {
transform: translateY(0);
}
/* —— keyframes —— */
@keyframes bubble-float {
50% {
transform: translateY(-14rpx);
}
}
0%,
100% {
transform: translateY(0);
}
@keyframes glow-pulse {
0%,
100% {
transform: scale(1);
opacity: 0.55;
}
50% {
transform: translateY(-14rpx);
}
}
50% {
transform: scale(1.1);
opacity: 0.9;
}
}
@keyframes glow-pulse {
@keyframes deco-float {
0%,
100% {
transform: translateY(0);
}
0%,
100% {
transform: scale(1);
opacity: 0.55;
}
50% {
transform: translateY(-12rpx);
}
}
50% {
transform: scale(1.1);
opacity: 0.9;
}
}
@keyframes dot-jump {
0%,
100% {
transform: translateY(0);
}
@keyframes deco-float {
50% {
transform: translateY(-8rpx);
}
}
0%,
100% {
transform: translateY(0);
}
/* 歪头左右打量(loading) */
@keyframes sway {
0%,
100% {
transform: rotate(-4deg);
}
50% {
transform: translateY(-12rpx);
}
}
50% {
transform: rotate(4deg);
}
}
@keyframes dot-jump {
/* 缓慢左右摇头(error) */
@keyframes head-shake {
0%,
100% {
transform: rotate(0);
}
0%,
100% {
transform: translateY(0);
}
25% {
transform: rotate(6deg);
}
50% {
transform: translateY(-8rpx);
}
}
75% {
transform: rotate(-6deg);
}
}
/* 歪头左右打量(loading) */
@keyframes sway {
/* 轻轻叹气缩肩(empty) */
@keyframes sigh {
0%,
100% {
transform: scaleY(1);
}
0%,
100% {
transform: rotate(-4deg);
}
30%,
70% {
transform: scaleY(0.92);
}
}
</style>
50% {
transform: rotate(4deg);
}
}
/* 缓慢左右摇头(error) */
@keyframes head-shake {
0%,
100% {
transform: rotate(0);
}
25% {
transform: rotate(6deg);
}
75% {
transform: rotate(-6deg);
}
}
/* 轻轻叹气缩肩(empty) */
@keyframes sigh {
0%,
100% {
transform: scaleY(1);
}
30%,
70% {
transform: scaleY(0.92);
}
}
</style>
+19 -1
View File
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { onPageScroll } from '@dcloudio/uni-app'
import { ref, computed, useSlots, onMounted } from 'vue'
import { tabbarList } from '@/tabbar/config'
interface IProps {
useBack : boolean;
@@ -57,8 +58,25 @@
return props.scrollTitle;
})
// todo:注意:如果是从分享进来的,我们需要处理为返回 home页面
// 如果是从分享进来的,我们需要处理为返回 home页面
const homePage = 'pages/index/index'
const allEntryPages = computed<string[]>(() => {
return [
homePage,
'pages/maintenance/maintenance',
...tabbarList.map(item => item.pagePath),
] as string[];
})
function handleBack() {
const currentPage = getCurrentPages()[0]
if (!allEntryPages.value.some(pagePath => pagePath == currentPage.route)) {
uni.reLaunch({
url: `/${homePage}`
})
return;
}
uni.navigateBack({ delta: 1 })
}
@@ -0,0 +1,22 @@
<script setup lang="ts">
interface IProps {
customClass?: string
lineClass?: string
textClass?: string
}
const props = withDefaults(defineProps<IProps>(), {
customClass: '',
lineClass: '!h-2/5 -rotate-5',
textClass: 'font-bold'
})
</script>
<template>
<text class="shrink-0 relative" :class="[props.customClass]">
<text class="font-bold relative z-2" :class="[props.textClass]">
<slot></slot>
</text>
<text class="absolute z-1 -right-1 -bottom-0.5 transform rounded-xl bg-secondary h-3/5 w-4/5" :class="[props.lineClass]"></text>
</text>
</template>
+79 -155
View File
@@ -1,175 +1,99 @@
<script lang="ts" setup>
/**
* 投票卡片(源自旧项目 components/vote-card,新建复刻)
* 适配 plugin-vote 真实结构:详情接口返回 VoteDetail(嵌套 vote),选项为 {id,title}
* 纯展示卡片:列表接口一次返回,页面加工后传入,点击跳转详情
*/
import { computed, ref, watch } from 'vue'
import { getVoteDetail } from '@/api/uni-halo'
import { VOTE_STATES } from '@/utils/vote'
import type { IVote, IVoteDetail, IVoteOption } from '@/api/types/uni-halo'
import { formatTime as formatTimeUtil } from '@/utils/formatTime'
interface IVoteCardOption {
id?: string
title?: string
checked?: boolean
_uh_percent?: number
[key: string]: unknown
}
interface IVoteCardData {
metadata?: { name?: string, [key: string]: unknown }
spec?: {
title?: string
remark?: string
type?: string
timeLimit?: string
startDate?: string
endDate?: string
isVoted?: boolean
_uh_type?: string
_uh_state?: { state: string, color: string }
options?: IVoteCardOption[]
[key: string]: unknown
}
stats?: { voteCount?: number, voteDataList?: { id?: string, voteCount?: number }[] }
[key: string]: unknown
}
const props = defineProps<{
voteName: string
vote: IVoteCardData
}>()
const emit = defineEmits<{
(e: 'on-vote-success'): void
}>()
const loading = ref(true)
const isSubmit = ref(false)
const voteData = ref<IVote | null>(null)
const voteTypes = ref<string[]>([])
const canAnonymously = ref(true)
/** 选项 id → 票数(来自 VoteDetail.voteDataList 或 Vote.stats.voteDataList) */
const voteCountMap = ref<Record<string, number>>({})
/** 投票状态(基于插件字段 spec.startDate/endDate/hasEnded/canAnonymously) */
const voteState = computed(() => {
const spec = voteData.value?.spec
if (!spec)
return VOTE_STATES.NOT_VOTED
const now = Date.now()
const startTime = spec.startDate ? new Date(spec.startDate).getTime() : now
const endTime = spec.endDate ? new Date(spec.endDate).getTime() : now
if (spec.hasEnded || endTime < now)
return VOTE_STATES.VOTE_ENDED
if (startTime > now)
return VOTE_STATES.NOT_VOTED
if (voteTypes.value.length !== 0)
return VOTE_STATES.VOTED
if (!spec.canAnonymously)
return VOTE_STATES.NOT_VOTED
return VOTE_STATES.VOTING
})
const voteLabel = computed(() => {
if (voteState.value === VOTE_STATES.VOTE_ENDED)
return '投票已结束'
if (voteState.value === VOTE_STATES.VOTED)
return '已参与'
if (voteState.value === VOTE_STATES.VOTING)
return '投票中'
return '开始投票'
})
const voteResultLabel = computed(() => (voteState.value === VOTE_STATES.VOTED ? '查看结果' : ''))
const isSingle = computed(() => voteData.value?.spec?.type === 'single')
/** 选项票数占比(取自 voteCountMap) */
function handleCalcPercent(option: IVoteOption): number {
const total = voteData.value?.stats?.voteCount || 0
const count = voteCountMap.value[option.id || ''] || 0
if (total === 0)
return 0
return Number(((count / total) * 100).toFixed(2))
/** 跳转投票详情 */
function handleToDetail() {
uni.navigateTo({
url: `/pages-blog/vote-detail/vote-detail?name=${props.vote?.metadata?.name || ''}`,
})
}
async function handleGetData() {
loading.value = true
try {
const res = await getVoteDetail(props.voteName)
const detail = res.data as IVoteDetail
const vote = detail.vote || (detail as unknown as IVote)
voteData.value = vote
canAnonymously.value = !!vote.spec?.canAnonymously
voteTypes.value = []
// 票数映射:详情 voteDataList 优先,其次 stats.voteDataList
const countList = detail.voteDataList?.length ? detail.voteDataList : vote.stats?.voteDataList
const map: Record<string, number> = {}
;(countList || []).forEach((item) => {
if (item.id)
map[item.id] = item.voteCount || 0
})
voteCountMap.value = map
}
catch (err) {
console.error('获取投票失败', err)
}
finally {
loading.value = false
}
/** 格式化时间 */
function formatTime(date?: string, fmt = 'yyyy-MM-dd HH:mm'): string {
return date ? formatTimeUtil({ d: date, f: fmt }) : ''
}
function handleSelectOption(option: IVoteOption) {
if (voteState.value !== VOTE_STATES.VOTING)
return
const optionName = option.id || ''
if (isSingle.value) {
voteTypes.value = [optionName]
}
else {
const index = voteTypes.value.indexOf(optionName)
if (index === -1) {
voteTypes.value.push(optionName)
}
else {
voteTypes.value.splice(index, 1)
}
}
}
function handleSubmit() {
if (voteTypes.value.length === 0) {
uni.showToast({ icon: 'none', title: '请先选择选项' })
return
}
isSubmit.value = true
uni.showLoading({ title: '提交中...' })
setTimeout(() => {
uni.hideLoading()
isSubmit.value = false
emit('on-vote-success')
}, 500)
}
watch(() => props.voteName, () => {
handleGetData()
}, { immediate: true })
defineExpose({ refresh: handleGetData })
</script>
<template>
<view class="uh-vote-card uh-global-card-glass box-border w-full rounded-2xl p-4">
<view v-if="loading" class="loading py-6">
<wd-skeleton :row="2" :animated="true" />
<view class="uh-vote-card uh-global-card-glass box-border w-full rounded-2xl p-4" @click="handleToDetail">
<!-- 头部:类型/状态/已投票 -->
<view class="flex items-center justify-between">
<view class="flex flex-wrap items-center gap-1">
<text
v-if="vote.spec?._uh_type"
class="rounded-md bg-secondary px-1.5 py-0.5 text-[22rpx] text-[#4d7c0f]"
>
{{ vote.spec._uh_type }}
</text>
<text
v-if="vote.spec?._uh_state"
class="rounded-md px-1.5 py-0.5 text-[22rpx]"
:style="{ color: vote.spec._uh_state.color, backgroundColor: `${vote.spec._uh_state.color}1a` }"
>
{{ vote.spec._uh_state.state }}
</text>
<text v-if="vote.spec?.isVoted" class="rounded-md bg-primary/15 px-1.5 py-0.5 text-[22rpx] text-primary">
已投票
</text>
</view>
<wd-icon name="arrow-right" size="32rpx" class="text-primary" />
</view>
<view v-else-if="voteData" class="vote-body">
<view class="vote-title text-[30rpx] text-gray-900 font-bold">
{{ voteData.spec?.title }}
</view>
<view v-if="voteData.spec?.remark" class="vote-desc mt-1 text-[24rpx] text-gray-400">
{{ voteData.spec.remark }}
</view>
<!-- 标题 + 备注 -->
<view class="vote-title mt-2 text-[30rpx] font-bold text-gray-900">
{{ vote.spec?.title }}
</view>
<view v-if="vote.spec?.remark" class="vote-desc mt-1 text-[24rpx] text-gray-400">
{{ vote.spec.remark }}
</view>
<view class="options mt-5">
<view
v-for="option in voteData.spec?.options || []"
:key="option.id"
class="option mb-4 flex flex-col border-2 rounded-xl p-5"
:class="voteTypes.includes(option.id || '') ? 'border-[#b9e424] bg-[#f0f7d9]' : 'border-transparent bg-[#f6f3ee]'"
@click="handleSelectOption(option)"
>
<view class="option-label text-[28rpx] text-gray-700">
<text>{{ option.title }}</text>
</view>
<view v-if="voteState === VOTE_STATES.VOTED" class="option-bar mt-3 h-4 overflow-hidden rounded-lg bg-black/5">
<view class="option-bar-inner h-full rounded-lg" :style="{ width: `${handleCalcPercent(option)}%`, background: 'linear-gradient(90deg, #B9E424, #D7F94C)' }" />
</view>
</view>
</view>
<view v-if="voteState === VOTE_STATES.VOTING" class="submit-btn mt-3">
<wd-button type="primary" size="small" block :loading="isSubmit" @click="handleSubmit">
{{ voteLabel }}
</wd-button>
</view>
<view v-else class="vote-tip mt-4 text-center text-[24rpx] text-gray-400">
{{ voteLabel }}{{ voteResultLabel }}
</view>
<!-- 底部:时间 + 参与人数 -->
<view class="vote-card-foot mt-3 flex items-center justify-between border-t border-black/5 pt-2">
<text v-if="vote.spec?.timeLimit === 'permanent'" class="text-[22rpx] text-gray-400">
结束:永久有效
</text>
<text v-else-if="vote.spec?._uh_state?.state === '未开始'" class="text-[22rpx] text-gray-400">
开始:{{ formatTime(vote.spec?.startDate) }}
</text>
<text v-else class="text-[22rpx] text-gray-400">
结束:{{ formatTime(vote.spec?.endDate) }}
</text>
<text class="text-[22rpx] text-gray-400">{{ vote.stats?.voteCount || 0 }} 人已参与</text>
</view>
</view>
</template>