Skip to content

Commit d09366b

Browse files
YunaiVgitee-org
authored andcommitted
!222 优化商品评论
Merge pull request !222 from 疯狂的世界/owen_dev
2 parents 2d8d323 + 3a15390 commit d09366b

File tree

5 files changed

+84
-64
lines changed

5 files changed

+84
-64
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
<el-form-item label="评论内容" prop="content">
3737
<el-input type="textarea" v-model="formData.content" />
3838
</el-form-item>
39-
<!-- TODO @疯狂:formData.scores 是通过后端计算的哈,不要前端传递 -->
40-
<el-form-item label="评分星级" prop="scores">
41-
<el-rate v-model="formData.scores" />
42-
</el-form-item>
4339
<el-form-item label="描述星级" prop="descriptionScores">
4440
<el-rate v-model="formData.descriptionScores" />
4541
</el-form-item>
@@ -60,9 +56,9 @@
6056
</template>
6157
<script setup lang="ts">
6258
import * as CommentApi from '@/api/mall/product/comment'
63-
import SpuTableSelect from '@/views/mall/product/comment/components/SpuTableSelect.vue'
59+
import SpuTableSelect from '@/views/mall/product/spu/components/SpuTableSelect.vue'
6460
import * as ProductSpuApi from '@/api/mall/product/spu'
65-
import SkuTableSelect from '@/views/mall/product/comment/components/SkuTableSelect.vue'
61+
import SkuTableSelect from '@/views/mall/product/spu/components/SkuTableSelect.vue'
6662
6763
const { t } = useI18n() // 国际化
6864
const message = useMessage() // 消息弹窗
@@ -79,7 +75,6 @@ const formData = ref({
7975
spuId: undefined,
8076
spuName: undefined,
8177
skuId: undefined,
82-
scores: 5,
8378
descriptionScores: 5,
8479
benefitScores: 5,
8580
content: undefined,
@@ -91,7 +86,6 @@ const formRules = reactive({
9186
userAvatar: [{ required: true, message: '用户头像不能为空', trigger: 'blur' }],
9287
userNickname: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }],
9388
content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }],
94-
scores: [{ required: true, message: '评分星级不能为空', trigger: 'blur' }],
9589
descriptionScores: [{ required: true, message: '描述星级不能为空', trigger: 'blur' }],
9690
benefitScores: [{ required: true, message: '服务星级不能为空', trigger: 'blur' }]
9791
})
@@ -157,7 +151,6 @@ const resetForm = () => {
157151
userAvatar: undefined,
158152
spuId: undefined,
159153
skuId: undefined,
160-
scores: 5,
161154
descriptionScores: 5,
162155
benefitScores: 5,
163156
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
/** 显示/隐藏 **/

src/views/mall/product/comment/components/SkuTableSelect.vue renamed to src/views/mall/product/spu/components/SkuTableSelect.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { ElTable } from 'element-plus'
3737
import * as ProductSpuApi from '@/api/mall/product/spu'
3838
import { propTypes } from '@/utils/propTypes'
3939
40-
// TODO @疯狂:是不是挪到 spu 的 components 下哈
4140
defineOptions({ name: 'SkuTableSelect' })
4241
4342
const props = defineProps({

src/views/mall/product/comment/components/SpuTableSelect.vue renamed to src/views/mall/product/spu/components/SpuTableSelect.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ import { defaultProps, handleTree } from '@/utils/tree'
8686
import * as ProductCategoryApi from '@/api/mall/product/category'
8787
import * as ProductSpuApi from '@/api/mall/product/spu'
8888
89-
// TODO @疯狂:是不是挪到 spu 的 components 下哈
9089
defineOptions({ name: 'SpuTableSelect' })
9190
9291
const message = useMessage() // 消息弹窗

0 commit comments

Comments
 (0)