From eec987041ceb3e764f1b394dab0e44ef690fec95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=AB=E5=9C=B0=E5=83=A7?= <357099073@qq.com> Date: Wed, 4 Feb 2026 23:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9D=E8=B4=9D=E7=9B=91?= =?UTF-8?q?=E6=8A=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env development | 3 + src/api/babyhealth.js | 43 +- src/api/upload.js | 42 -- .../babys/components/bindParents.vue | 369 ++++++++++++++++++ .../apps/babyhealth/babys/components/edit.vue | 322 ++++++++------- src/views/apps/babyhealth/babys/index.vue | 60 +-- 6 files changed, 621 insertions(+), 218 deletions(-) create mode 100644 .env development delete mode 100644 src/api/upload.js create mode 100644 src/views/apps/babyhealth/babys/components/bindParents.vue diff --git a/.env development b/.env development new file mode 100644 index 0000000..4cd72e8 --- /dev/null +++ b/.env development @@ -0,0 +1,3 @@ +# 开发环境接口 +# VITE_API_BASE_URL='https://backend.yunzer.cn' +VITE_API_BASE_URL = 'http://localhost:8000' \ No newline at end of file diff --git a/src/api/babyhealth.js b/src/api/babyhealth.js index b15f6c9..7f52eb6 100644 --- a/src/api/babyhealth.js +++ b/src/api/babyhealth.js @@ -20,7 +20,7 @@ export function getBabyList() { * @param {number} id 宝贝ID * @returns {Promise} */ -export function getDetail(id) { +export function getBabyDetail(id) { return request({ url: `/admin/babys/${id}`, method: "get", @@ -32,7 +32,7 @@ export function getDetail(id) { * @param {Object} data 宝贝数据 * @returns {Promise} */ -export function create(data) { +export function createBaby(data) { return request({ url: "/admin/babys", method: "post", @@ -49,14 +49,11 @@ export function create(data) { * @param {Object} data 更新的数据 * @returns {Promise} */ -export function update(id, data) { +export function editBaby(id, data) { return request({ url: `/admin/babys/${id}`, - method: "put", - data: data, - headers: { - "Content-Type": "multipart/form-data" - } + method: "post", + data: { ...data, _method: 'PUT' } }); } @@ -65,13 +62,39 @@ export function update(id, data) { * @param {number} id 宝贝ID * @returns {Promise} */ -export function del(id) { +export function deleteBaby(id) { return request({ url: `/admin/babys/${id}`, method: "delete", }); } +/** + * 绑定父母 + * @param {number} id 宝贝ID + * @param {Object} data 绑定数据 + * @returns {Promise} + */ +export function bindParent(id, data) { + return request({ + url: `/admin/babys/bindparents/${id}`, + method: "post", + data: data, + }); +} + +/** + * 获取父母 + * @param {number} id 宝贝ID + * @returns {Promise} + */ +export function getParents(id) { + return request({ + url: `/admin/babys/getParents/${id}`, + method: "get", + }); +} + /************************************************* ****************** 用户相关接口 ****************** *************************************************/ @@ -124,7 +147,7 @@ export function createUser(data) { export function updateUser(id, data) { return request({ url: `/admin/babyhealthUser/${id}`, - method: "put", + method: "post", data: data, headers: { "Content-Type": "multipart/form-data" diff --git a/src/api/upload.js b/src/api/upload.js deleted file mode 100644 index 0f9cf52..0000000 --- a/src/api/upload.js +++ /dev/null @@ -1,42 +0,0 @@ -import request from '@/utils/request'; - -/** - * 上传头像 - * @param {FormData} data 表单数据 - * @param {Object} params 查询参数 - * @returns {Promise} Promise对象 - */ -export function uploadAvatar(data, params) { - return request({ - url: '/admin/uploadavatar', - method: 'post', - data, - params, - headers: { - 'Content-Type': 'multipart/form-data', - }, - }); -} - -/** - * 上传文件 - * @param {FormData} data 表单数据 - * @param {Object} params 查询参数 - * @returns {Promise} Promise对象 - */ -export function uploadFile(data, params) { - return request({ - url: '/admin/upload', - method: 'post', - data, - params, - headers: { - 'Content-Type': 'multipart/form-data', - }, - }); -} - -export default { - uploadAvatar, - uploadFile -}; diff --git a/src/views/apps/babyhealth/babys/components/bindParents.vue b/src/views/apps/babyhealth/babys/components/bindParents.vue new file mode 100644 index 0000000..ae861d4 --- /dev/null +++ b/src/views/apps/babyhealth/babys/components/bindParents.vue @@ -0,0 +1,369 @@ + + + + + + + + + 支持账号/手机号搜索 + + + {{ user.name || user.account }} + {{ user.phone }} + + + + + + 刷新列表 + + + + + + + + + 支持账号/手机号搜索 + + + {{ user.name || user.account }} + {{ user.phone }} + + + + + + 刷新列表 + + + + + + + 取消 + 确定 + + + + + + + diff --git a/src/views/apps/babyhealth/babys/components/edit.vue b/src/views/apps/babyhealth/babys/components/edit.vue index d451878..20f440e 100644 --- a/src/views/apps/babyhealth/babys/components/edit.vue +++ b/src/views/apps/babyhealth/babys/components/edit.vue @@ -6,90 +6,99 @@ :close-on-click-modal="false" @close="handleClose" > - - - - - - - - - - - - 男 - 女 - - - - - - - - - - cm - - - - - kg - - - + + + - - + + + + 点击上传头像 + + + + + + + + + + + + + + + + 男 + 女 + + + + + + + + + + CM + + + + + + KG + - - - 正常 - 禁用 - - - + + + 正常 + 禁用 + + + + 取消 确定 - - - - - @@ -97,8 +106,8 @@ import { ref, reactive, watch } from 'vue'; import { ElMessage, type FormInstance, type FormRules } from 'element-plus'; import { Plus } from '@element-plus/icons-vue'; -import { createBaby, updateBaby } from '@/api/babyhealth'; -import { uploadAvatar } from '@/api/upload'; +import { createBaby, editBaby } from '@/api/babyhealth'; +import { uploadAvatar } from '@/api/file'; interface BabyFormData { id: number; @@ -119,12 +128,10 @@ const props = defineProps<{ const emit = defineEmits<{ 'update:visible': [value: boolean]; - submit: [data: BabyFormData]; + success: []; }>(); const dialogVisible = ref(false); -const previewVisible = ref(false); -const previewImageUrl = ref(''); const loading = ref(false); const editFormRef = ref(); const fileList = ref([]); @@ -208,45 +215,44 @@ watch(() => props.editData, (data) => { } }, { immediate: true }); -// 上传配置 -const uploadUrl = import.meta.env.VITE_API_BASE_URL + "/admin/uploadavatar"; -const uploadHeaders = { - 'Content-Type': 'multipart/form-data' -}; - // 头像上传前校验 const beforeAvatarUpload = (file: any) => { const isImage = file.type.startsWith('image/'); + const isLt5M = file.size / 1024 / 1024 < 5; if (!isImage) { ElMessage.error('仅支持图片格式'); return false; } - return false; + if (!isLt5M) { + ElMessage.error('图片大小不能超过5MB'); + return false; + } + + return true; }; // 上传文件到服务器 const uploadFile = async (options: any) => { const { file } = options; - const formData = new FormData(); - formData.append('file', file); - + const uploadFormData = new FormData(); + uploadFormData.append('file', file); + try { // 使用uploadAvatar接口上传文件 - const response = await uploadAvatar(formData, { cate: 'baby_avatar' }); - - if (response.code === 200) { + const response = await uploadAvatar(uploadFormData, { cate: 'baby_avatar' }); + + if (response.code === 200 || response.code === 201) { // 更新宝贝头像 const avatarUrl = response.data.url || response.data.path || ''; formData.avatar = avatarUrl; - - // 如果正在编辑,则更新宝贝信息 - if (formData.id) { - await updateBaby(formData.id, { avatar: avatarUrl }); + + if (response.code === 201) { + ElMessage.info("文件已存在,使用已有图片"); + } else { + ElMessage.success("头像上传成功"); } - - ElMessage.success("头像上传成功"); } else { ElMessage.error(response.msg || "上传失败"); } @@ -256,23 +262,6 @@ const uploadFile = async (options: any) => { } }; -// 上传成功 -const handleAvatarSuccess = (response: any) => { - if (response.code === 200) { - const avatarUrl = response.data.url || response.data.path || ''; - formData.avatar = avatarUrl; - - // 如果正在编辑,则更新宝贝信息 - if (formData.id) { - updateBaby(formData.id, { avatar: avatarUrl }); - } - - ElMessage.success("头像上传成功"); - } else { - ElMessage.error(response.msg || "上传失败"); - } -}; - // 上传相关 const handleUploadChange = (file: any) => { if (file.raw) { @@ -291,11 +280,6 @@ const handleRemove = (file: any) => { formData.avatar = ''; }; -const handlePictureCardPreview = (file: any) => { - previewImageUrl.value = file.url; - previewVisible.value = true; -}; - // 提交表单 const handleSubmit = async () => { if (!editFormRef.value) return; @@ -311,7 +295,7 @@ const handleSubmit = async () => { uploadFormData.append('cate', 'baby'); const uploadRes = await uploadAvatar(uploadFormData); - if (uploadRes.code === 200 && uploadRes.data?.url) { + if ((uploadRes.code === 200 || uploadRes.code === 201) && uploadRes.data?.url) { formData.avatar = uploadRes.data.url; } else { ElMessage.error('图片上传失败'); @@ -319,10 +303,35 @@ const handleSubmit = async () => { } } - // 提交数据 - emit('submit', { ...formData }); - dialogVisible.value = false; - resetForm(); + let res; + if (formData.id) { + // 编辑 - 使用普通对象,PUT请求 + const submitData: Record = {}; + Object.keys(formData).forEach(key => { + if (key !== 'id' && formData[key as keyof BabyFormData] !== undefined && formData[key as keyof BabyFormData] !== null) { + submitData[key] = formData[key as keyof BabyFormData]; + } + }); + res = await editBaby(formData.id, submitData); + } else { + // 新增 - 使用 FormData + const submitData = new FormData(); + Object.keys(formData).forEach(key => { + if (key !== 'id' && formData[key as keyof BabyFormData] !== undefined && formData[key as keyof BabyFormData] !== null) { + submitData.append(key, String(formData[key as keyof BabyFormData])); + } + }); + res = await createBaby(submitData); + } + + if (res.code === 200) { + ElMessage.success(formData.id ? '编辑成功' : '添加成功'); + dialogVisible.value = false; + resetForm(); + emit('success'); // 通知父组件刷新列表 + } else { + ElMessage.error(res.msg || (formData.id ? '编辑失败' : '添加失败')); + } } catch (error) { console.error('提交失败:', error); ElMessage.error('提交失败'); @@ -335,39 +344,80 @@ const handleSubmit = async () => {