diff --git a/src/views/merchant/ProductList.vue b/src/views/merchant/ProductList.vue index a1c20ba..35a9113 100644 --- a/src/views/merchant/ProductList.vue +++ b/src/views/merchant/ProductList.vue @@ -174,6 +174,103 @@ + + + + + + + + + + + +
+
+ 商品图片 +
+ 删除 +
+
+ + + + 上传图片 + + +
+
+ + + + + + + + + + + + + + + + + + + + +
+
@@ -327,10 +424,103 @@ export default defineComponent({ fetchData() } - // 修改商品 + // 修改商品相关 + const editModalVisible = ref(false) + const editLoading = ref(false) + const editFormRef = ref(null) + const currentEditId = ref(null) + + const editFormState = ref({ + name: '', + image_url: '', + product_price: null, + sale_price: null, + settlement_amount: null, + tags: '', + max_deduct_points: null + }) + + // 显示修改模态框 const handleEdit = (record) => { - // TODO: 实现商品修改功能 - console.log('修改商品:', record) + currentEditId.value = record.id + editFormState.value = { + name: record.name, + image_url: record.image_url, + product_price: record.product_price, + sale_price: record.sale_price, + settlement_amount: record.settlement_amount, + tags: record.tags, + max_deduct_points: record.max_deduct_points + } + editModalVisible.value = true + } + + // 处理修改图片上传 + const handleEditUpload = async ({ file, onSuccess, onError }) => { + const formData = new FormData() + formData.append('files', file) + + try { + const res = await request.post('/api/upload/images', formData, { + headers: { + 'Content-Type': 'multipart/form-data' + } + }) + + if (res.code === 200 && res.data && res.data.urls && res.data.urls.length > 0) { + editFormState.value.image_url = res.data.urls[0] + onSuccess() + message.success('上传成功') + } else { + throw new Error('上传失败') + } + } catch (error) { + console.error('上传图片失败:', error) + message.error('上传失败') + onError() + } + } + + // 删除修改图片 + const handleEditRemoveImage = () => { + editFormState.value.image_url = '' + } + + // 提交修改 + const handleEditSubmit = () => { + editFormRef.value.validate().then(async () => { + try { + editLoading.value = true + const res = await request.put(`/api/merchant/product/${currentEditId.value}`, editFormState.value) + if (res.code === 200) { + message.success('修改成功') + editModalVisible.value = false + fetchData() + } else { + throw new Error(res.message || '修改失败') + } + } catch (error) { + console.error('修改商品失败:', error) + message.error(error.message || '修改失败') + } finally { + editLoading.value = false + } + }) + } + + // 取消修改 + const handleEditCancel = () => { + editFormRef.value?.resetFields() + editFormState.value = { + name: '', + image_url: '', + product_price: null, + sale_price: null, + settlement_amount: null, + tags: '', + max_deduct_points: null + } + editModalVisible.value = false } // 添加商品相关 @@ -456,7 +646,15 @@ export default defineComponent({ handleUpload, handleRemoveImage, handleAdd, - handleCancel + handleCancel, + editModalVisible, + editLoading, + editFormRef, + editFormState, + handleEditUpload, + handleEditRemoveImage, + handleEditSubmit, + handleEditCancel } } })