商品评论:回复表单提取到单独的组件中

This commit is contained in:
owen 2023-08-26 13:34:07 +08:00
parent 15971b6feb
commit 3a15390090
3 changed files with 82 additions and 56 deletions

View File

@ -75,7 +75,6 @@ const formData = ref({
spuId: undefined, spuId: undefined,
spuName: undefined, spuName: undefined,
skuId: undefined, skuId: undefined,
scores: 5,
descriptionScores: 5, descriptionScores: 5,
benefitScores: 5, benefitScores: 5,
content: undefined, content: undefined,
@ -87,7 +86,6 @@ const formRules = reactive({
userAvatar: [{ required: true, message: '用户头像不能为空', trigger: 'blur' }], userAvatar: [{ required: true, message: '用户头像不能为空', trigger: 'blur' }],
userNickname: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }], userNickname: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }],
content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }], content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }],
scores: [{ required: true, message: '评分星级不能为空', trigger: 'blur' }],
descriptionScores: [{ required: true, message: '描述星级不能为空', trigger: 'blur' }], descriptionScores: [{ required: true, message: '描述星级不能为空', trigger: 'blur' }],
benefitScores: [{ required: true, message: '服务星级不能为空', trigger: 'blur' }] benefitScores: [{ required: true, message: '服务星级不能为空', trigger: 'blur' }]
}) })
@ -153,7 +151,6 @@ const resetForm = () => {
userAvatar: undefined, userAvatar: undefined,
spuId: undefined, spuId: undefined,
skuId: undefined, skuId: undefined,
scores: 5,
descriptionScores: 5, descriptionScores: 5,
benefitScores: 5, benefitScores: 5,
content: undefined, content: undefined,

View File

@ -0,0 +1,77 @@
<template>
<Dialog title="回复" v-model="dialogVisible">
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-width="100px"
v-loading="formLoading"
>
<el-form-item label="回复内容" prop="replyContent">
<el-input type="textarea" v-model="formData.replyContent" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitReplyForm" type="primary" :disabled="formLoading"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import * as CommentApi from '@/api/mall/product/comment'
import { ElInput } from 'element-plus'
defineOptions({ name: 'ProductComment' })
const message = useMessage() //
const { t } = useI18n() //
const dialogVisible = ref(false) //
const formLoading = ref(false) // 12
const formData = ref({
id: undefined,
replyContent: undefined
})
const formRules = reactive({
replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
})
const formRef = ref() // Ref
/** 打开弹窗 */
const open = async (id?: number) => {
resetForm()
formData.value.id = id
dialogVisible.value = true
}
defineExpose({ open }) // open
/** 提交表单 */
const emit = defineEmits(['success']) // success
const submitReplyForm = async () => {
const valid = await formRef?.value?.validate()
if (!valid) return
formLoading.value = true
try {
await CommentApi.replyComment(formData.value)
message.success(t('common.createSuccess'))
dialogVisible.value = false
//
emit('success')
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: undefined,
replyContent: undefined
}
formRef.value?.resetFields()
}
</script>

View File

@ -60,7 +60,6 @@
<ContentWrap> <ContentWrap>
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="false"> <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="false">
<el-table-column label="评论编号" align="center" prop="id" min-width="60" /> <el-table-column label="评论编号" align="center" prop="id" min-width="60" />
<!-- TODO @疯狂后端貌似没读取 -->
<el-table-column label="用户名称" align="center" prop="userNickname" width="80" /> <el-table-column label="用户名称" align="center" prop="userNickname" width="80" />
<el-table-column label="商品信息" align="center" min-width="210"> <el-table-column label="商品信息" align="center" min-width="210">
<template #default="scope"> <template #default="scope">
@ -144,33 +143,15 @@
<!-- 表单弹窗添加/修改 --> <!-- 表单弹窗添加/修改 -->
<CommentForm ref="formRef" @success="getList" /> <CommentForm ref="formRef" @success="getList" />
<!-- 回复表单弹窗 -->
<Dialog title="回复" v-model="replyDialog.visible"> <ReplyForm ref="replyFormRef" @success="getList" />
<el-form
ref="replyFormRef"
:model="replyDialog.formData"
:rules="replyDialog.formRules"
label-width="100px"
v-loading="replyDialog.loading"
>
<el-form-item label="回复内容" prop="replyContent">
<el-input type="textarea" v-model="replyDialog.formData.replyContent" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitReplyForm" type="primary" :disabled="replyDialog.loading"
>
</el-button>
<el-button @click="replyDialog.visible = false"> </el-button>
</template>
</Dialog>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime' import { dateFormatter } from '@/utils/formatTime'
import * as CommentApi from '@/api/mall/product/comment' import * as CommentApi from '@/api/mall/product/comment'
import CommentForm from './CommentForm.vue' import CommentForm from './CommentForm.vue'
import { ElInput } from 'element-plus' import ReplyForm from '@/views/mall/product/comment/ReplyForm.vue'
defineOptions({ name: 'ProductComment' }) defineOptions({ name: 'ProductComment' })
@ -242,39 +223,10 @@ const openForm = (type: string, id?: number) => {
formRef.value.open(type, id) formRef.value.open(type, id)
} }
// TODO @ /** 回复按钮操作 **/
/** 回复 **/
const replyFormRef = ref() const replyFormRef = ref()
const replyDialog = reactive({
visible: false,
loading: false,
formData: {
id: -1,
replyContent: ''
},
formRules: {
replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
}
})
const handleReply = (id: number) => { const handleReply = (id: number) => {
replyDialog.formData.id = id replyFormRef.value.open(id)
replyDialog.formData.replyContent = ''
replyDialog.visible = true
}
const submitReplyForm = async () => {
const valid = await replyFormRef?.value?.validate()
if (!valid) return
replyDialog.loading = true
try {
await CommentApi.replyComment(replyDialog.formData)
message.success(t('common.createSuccess'))
replyDialog.visible = false
await getList()
} finally {
replyDialog.loading = false
}
} }
/** 显示/隐藏 **/ /** 显示/隐藏 **/