Skip to content

Commit 3a15390

Browse files
committed
商品评论:回复表单提取到单独的组件中
1 parent 15971b6 commit 3a15390

File tree

3 files changed

+82
-56
lines changed

3 files changed

+82
-56
lines changed

src/views/mall/product/comment/CommentForm.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const formData = ref({
7575
spuId: undefined,
7676
spuName: undefined,
7777
skuId: undefined,
78-
scores: 5,
7978
descriptionScores: 5,
8079
benefitScores: 5,
8180
content: undefined,
@@ -87,7 +86,6 @@ const formRules = reactive({
8786
userAvatar: [{ required: true, message: '用户头像不能为空', trigger: 'blur' }],
8887
userNickname: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }],
8988
content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }],
90-
scores: [{ required: true, message: '评分星级不能为空', trigger: 'blur' }],
9189
descriptionScores: [{ required: true, message: '描述星级不能为空', trigger: 'blur' }],
9290
benefitScores: [{ required: true, message: '服务星级不能为空', trigger: 'blur' }]
9391
})
@@ -153,7 +151,6 @@ const resetForm = () => {
153151
userAvatar: undefined,
154152
spuId: undefined,
155153
skuId: undefined,
156-
scores: 5,
157154
descriptionScores: 5,
158155
benefitScores: 5,
159156
content: undefined,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<template>
2+
<Dialog title="回复" v-model="dialogVisible">
3+
<el-form
4+
ref="formRef"
5+
:model="formData"
6+
:rules="formRules"
7+
label-width="100px"
8+
v-loading="formLoading"
9+
>
10+
<el-form-item label="回复内容" prop="replyContent">
11+
<el-input type="textarea" v-model="formData.replyContent" />
12+
</el-form-item>
13+
</el-form>
14+
<template #footer>
15+
<el-button @click="submitReplyForm" type="primary" :disabled="formLoading">确 定 </el-button>
16+
<el-button @click="dialogVisible = false">取 消</el-button>
17+
</template>
18+
</Dialog>
19+
</template>
20+
21+
<script setup lang="ts">
22+
import * as CommentApi from '@/api/mall/product/comment'
23+
import { ElInput } from 'element-plus'
24+
25+
defineOptions({ name: 'ProductComment' })
26+
27+
const message = useMessage() // 消息弹窗
28+
const { t } = useI18n() // 国际化
29+
30+
const dialogVisible = ref(false) // 弹窗的是否展示
31+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
32+
const formData = ref({
33+
id: undefined,
34+
replyContent: undefined
35+
})
36+
const formRules = reactive({
37+
replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
38+
})
39+
const formRef = ref() // 表单 Ref
40+
41+
/** 打开弹窗 */
42+
const open = async (id?: number) => {
43+
resetForm()
44+
formData.value.id = id
45+
dialogVisible.value = true
46+
}
47+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
48+
49+
/** 提交表单 */
50+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
51+
52+
const submitReplyForm = async () => {
53+
const valid = await formRef?.value?.validate()
54+
if (!valid) return
55+
56+
formLoading.value = true
57+
try {
58+
await CommentApi.replyComment(formData.value)
59+
message.success(t('common.createSuccess'))
60+
61+
dialogVisible.value = false
62+
// 发送操作成功的事件
63+
emit('success')
64+
} finally {
65+
formLoading.value = false
66+
}
67+
}
68+
69+
/** 重置表单 */
70+
const resetForm = () => {
71+
formData.value = {
72+
id: undefined,
73+
replyContent: undefined
74+
}
75+
formRef.value?.resetFields()
76+
}
77+
</script>

src/views/mall/product/comment/index.vue

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
<ContentWrap>
6161
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="false">
6262
<el-table-column label="评论编号" align="center" prop="id" min-width="60" />
63-
<!-- TODO @疯狂:后端貌似没读取? -->
6463
<el-table-column label="用户名称" align="center" prop="userNickname" width="80" />
6564
<el-table-column label="商品信息" align="center" min-width="210">
6665
<template #default="scope">
@@ -144,33 +143,15 @@
144143

145144
<!-- 表单弹窗:添加/修改 -->
146145
<CommentForm ref="formRef" @success="getList" />
147-
148-
<Dialog title="回复" v-model="replyDialog.visible">
149-
<el-form
150-
ref="replyFormRef"
151-
:model="replyDialog.formData"
152-
:rules="replyDialog.formRules"
153-
label-width="100px"
154-
v-loading="replyDialog.loading"
155-
>
156-
<el-form-item label="回复内容" prop="replyContent">
157-
<el-input type="textarea" v-model="replyDialog.formData.replyContent" />
158-
</el-form-item>
159-
</el-form>
160-
<template #footer>
161-
<el-button @click="submitReplyForm" type="primary" :disabled="replyDialog.loading"
162-
>确 定
163-
</el-button>
164-
<el-button @click="replyDialog.visible = false">取 消</el-button>
165-
</template>
166-
</Dialog>
146+
<!-- 回复表单弹窗 -->
147+
<ReplyForm ref="replyFormRef" @success="getList" />
167148
</template>
168149

169150
<script setup lang="ts">
170151
import { dateFormatter } from '@/utils/formatTime'
171152
import * as CommentApi from '@/api/mall/product/comment'
172153
import CommentForm from './CommentForm.vue'
173-
import { ElInput } from 'element-plus'
154+
import ReplyForm from '@/views/mall/product/comment/ReplyForm.vue'
174155
175156
defineOptions({ name: 'ProductComment' })
176157
@@ -242,39 +223,10 @@ const openForm = (type: string, id?: number) => {
242223
formRef.value.open(type, id)
243224
}
244225
245-
// TODO @疯狂:要不回复,也搞一个组件出去?
246-
/** 回复 **/
226+
/** 回复按钮操作 **/
247227
const replyFormRef = ref()
248-
const replyDialog = reactive({
249-
visible: false,
250-
loading: false,
251-
formData: {
252-
id: -1,
253-
replyContent: ''
254-
},
255-
formRules: {
256-
replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
257-
}
258-
})
259228
const handleReply = (id: number) => {
260-
replyDialog.formData.id = id
261-
replyDialog.formData.replyContent = ''
262-
replyDialog.visible = true
263-
}
264-
const submitReplyForm = async () => {
265-
const valid = await replyFormRef?.value?.validate()
266-
if (!valid) return
267-
268-
replyDialog.loading = true
269-
try {
270-
await CommentApi.replyComment(replyDialog.formData)
271-
message.success(t('common.createSuccess'))
272-
273-
replyDialog.visible = false
274-
await getList()
275-
} finally {
276-
replyDialog.loading = false
277-
}
229+
replyFormRef.value.open(id)
278230
}
279231
280232
/** 显示/隐藏 **/

0 commit comments

Comments
 (0)