feat: 添加分类提案功能,包括提案表单和相关后端逻辑

This commit is contained in:
sivdead
2025-09-25 17:00:17 +08:00
parent 4fc7c861ee
commit 76962d6d1c
15 changed files with 485 additions and 19 deletions

View File

@@ -34,6 +34,7 @@ export default {
{ id: 'NORMAL', name: '普通帖子', icon: 'file-text' },
{ id: 'LOTTERY', name: '抽奖帖子', icon: 'gift' },
{ id: 'POLL', name: '投票帖子', icon: 'ranking-list' },
{ id: 'PROPOSAL', name: '分类提案', icon: 'tag-one' },
]
}

View File

@@ -0,0 +1,133 @@
<template>
<div class="proposal-section">
<div class="proposal-row">
<span class="proposal-row-title">拟议分类名称</span>
<BaseInput v-model="data.proposedName" placeholder="请输入分类名称" />
</div>
<div class="proposal-row">
<span class="proposal-row-title">拟议分类 Slug</span>
<BaseInput v-model="data.proposedSlug" placeholder="小写短横线分隔,如: tech-news" />
</div>
<div class="proposal-row">
<span class="proposal-row-title">提案描述</span>
<BaseInput v-model="data.proposalDescription" placeholder="简要说明提案目的与理由" />
</div>
<div class="proposal-row two-col">
<div class="proposal-col">
<span class="proposal-row-title">通过阈值(%)</span>
<input
class="number-input"
type="number"
v-model.number="data.approveThreshold"
min="0"
max="100"
/>
</div>
<div class="proposal-col">
<span class="proposal-row-title">法定最小参与数</span>
<input class="number-input" type="number" v-model.number="data.quorum" min="0" />
</div>
</div>
<div class="proposal-row">
<span class="proposal-row-title">投票结束时间</span>
<client-only>
<flat-pickr v-model="data.endTime" :config="dateConfig" class="time-picker" />
</client-only>
</div>
<div class="proposal-row">
<span class="proposal-row-title">投票选项</span>
<div class="poll-option-item" v-for="(opt, idx) in data.options" :key="idx">
<BaseInput v-model="data.options[idx]" placeholder="选项内容" />
<i
v-if="data.options.length > 2"
class="fa-solid fa-xmark remove-option-icon"
@click="removeOption(idx)"
></i>
</div>
<div class="add-option" @click="addOption">添加选项</div>
</div>
</div>
</template>
<script setup>
import 'flatpickr/dist/flatpickr.css'
import FlatPickr from 'vue-flatpickr-component'
import BaseInput from '~/components/BaseInput.vue'
const props = defineProps({
data: { type: Object, required: true },
})
const dateConfig = { enableTime: true, time_24hr: true, dateFormat: 'Y-m-d H:i' }
const addOption = () => {
props.data.options.push('')
}
const removeOption = (idx) => {
if (props.data.options.length > 2) {
props.data.options.splice(idx, 1)
}
}
</script>
<style scoped>
.proposal-section {
margin-top: 20px;
display: flex;
flex-direction: column;
gap: 20px;
margin-bottom: 200px;
}
.proposal-row-title {
font-size: 16px;
color: var(--text-color);
font-weight: bold;
margin-bottom: 10px;
}
.proposal-row {
display: flex;
flex-direction: column;
}
.two-col {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.proposal-col {
display: flex;
flex-direction: column;
}
.number-input {
max-width: 120px;
height: 30px;
border-radius: 5px;
border: 1px solid var(--border-color);
padding: 0 10px;
font-size: 16px;
color: var(--text-color);
background-color: var(--lottery-background-color);
}
.poll-option-item {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.remove-option-icon {
cursor: pointer;
}
.add-option {
color: var(--primary-color);
cursor: pointer;
width: fit-content;
margin-top: 5px;
}
.time-picker {
max-width: 200px;
height: 30px;
background-color: var(--lottery-background-color);
border-radius: 5px;
border: 1px solid var(--border-color);
}
</style>

View File

@@ -37,6 +37,7 @@
</div>
<LotteryForm v-if="postType === 'LOTTERY'" :data="lottery" />
<PollForm v-if="postType === 'POLL'" :data="poll" />
<ProposalForm v-if="postType === 'PROPOSAL'" :data="proposal" />
</div>
</div>
</template>
@@ -50,6 +51,7 @@ import PostTypeSelect from '~/components/PostTypeSelect.vue'
import TagSelect from '~/components/TagSelect.vue'
import LotteryForm from '~/components/LotteryForm.vue'
import PollForm from '~/components/PollForm.vue'
import ProposalForm from '~/components/ProposalForm.vue'
import { toast } from '~/main'
import { authState, getToken } from '~/utils/auth'
const config = useRuntimeConfig()
@@ -76,6 +78,15 @@ const poll = reactive({
endTime: null,
multiple: false,
})
const proposal = reactive({
proposedName: '',
proposedSlug: '',
proposalDescription: '',
approveThreshold: 60,
quorum: 10,
endTime: null,
options: ['同意', '反对'],
})
const startTime = ref(null)
const isWaitingPosting = ref(false)
const isAiLoading = ref(false)
@@ -123,6 +134,13 @@ const clearPost = async () => {
poll.options = ['', '']
poll.endTime = null
poll.multiple = false
proposal.proposedName = ''
proposal.proposedSlug = ''
proposal.proposalDescription = ''
proposal.approveThreshold = 60
proposal.quorum = 10
proposal.endTime = null
proposal.options = ['同意', '反对']
// 删除草稿
const token = getToken()
@@ -283,6 +301,32 @@ const submitPost = async () => {
return
}
}
if (postType.value === 'PROPOSAL') {
if (!proposal.proposedName.trim()) {
toast.error('请填写拟议分类名称')
return
}
if (!proposal.proposedSlug.trim()) {
toast.error('请填写拟议分类 Slug')
return
}
if (proposal.approveThreshold < 0 || proposal.approveThreshold > 100) {
toast.error('通过阈值需在0到100之间')
return
}
if (proposal.quorum < 0) {
toast.error('最小参与数需大于或等于0')
return
}
if (proposal.options.length < 2 || proposal.options.some((o) => !o.trim())) {
toast.error('请填写至少两个投票选项')
return
}
if (!proposal.endTime) {
toast.error('请选择投票结束时间')
return
}
}
try {
const token = getToken()
await ensureTags(token)
@@ -321,6 +365,18 @@ const submitPost = async () => {
prizeDescription: postType.value === 'LOTTERY' ? lottery.prizeDescription : undefined,
options: postType.value === 'POLL' ? poll.options : undefined,
multiple: postType.value === 'POLL' ? poll.multiple : undefined,
proposedName: postType.value === 'PROPOSAL' ? proposal.proposedName : undefined,
proposedSlug: postType.value === 'PROPOSAL' ? proposal.proposedSlug : undefined,
proposalDescription:
postType.value === 'PROPOSAL' ? proposal.proposalDescription : undefined,
approveThreshold: postType.value === 'PROPOSAL' ? proposal.approveThreshold : undefined,
quorum: postType.value === 'PROPOSAL' ? proposal.quorum : undefined,
options:
postType.value === 'POLL'
? poll.options
: postType.value === 'PROPOSAL'
? proposal.options
: undefined,
startTime:
postType.value === 'LOTTERY' ? new Date(startTime.value).toISOString() : undefined,
pointCost: postType.value === 'LOTTERY' ? lottery.pointCost : undefined,
@@ -330,7 +386,11 @@ const submitPost = async () => {
? new Date(new Date(lottery.endTime).getTime() + 8.02 * 60 * 60 * 1000).toISOString()
: postType.value === 'POLL'
? new Date(new Date(poll.endTime).getTime() + 8.02 * 60 * 60 * 1000).toISOString()
: undefined,
: postType.value === 'PROPOSAL'
? new Date(
new Date(proposal.endTime).getTime() + 8.02 * 60 * 60 * 1000,
).toISOString()
: undefined,
}),
})
const data = await res.json()