diff --git a/app/admin/controller/BabyHealth/BabysController.php b/app/admin/controller/BabyHealth/BabysController.php index e4a04ff..be81aee 100644 --- a/app/admin/controller/BabyHealth/BabysController.php +++ b/app/admin/controller/BabyHealth/BabysController.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace app\admin\controller; +namespace app\admin\controller\BabyHealth; use app\admin\BaseController; use think\exception\ValidateException; @@ -10,7 +10,7 @@ use think\facade\Db; use think\facade\Session; use think\response\Json; use think\db\exception\DbException; -use app\model\AppsBabys; +use app\model\AppsBabyhealthBabys; class BabysController extends BaseController @@ -22,8 +22,7 @@ class BabysController extends BaseController public function getBabyList() { try { - $babylist = AppsBabys::where('delete_time', null) - ->order('sort', 'asc') + $babylist = AppsBabyhealthBabys::where('delete_time', null) ->field('id, name, nickname, sex, avatar, birth, height, weight, status, create_time, update_time') ->select() ->toArray(); @@ -69,7 +68,7 @@ class BabysController extends BaseController ]; // 创建宝贝信息 - $result = AppsBabys::insertGetId($babyData); + $result = AppsBabyhealthBabys::insertGetId($babyData); if ($result === false) { return json([ @@ -114,7 +113,7 @@ class BabysController extends BaseController $data = $this->request->param(); // 检查宝贝信息是否存在 - $baby = AppsBabys::where('id', $id) + $baby = AppsBabyhealthBabys::where('id', $id) ->where('delete_time', null) ->find(); @@ -139,10 +138,10 @@ class BabysController extends BaseController ]; // 执行更新 - AppsBabys::where('id', $id)->update($updateData); + AppsBabyhealthBabys::where('id', $id)->update($updateData); // 获取更新后的宝贝信息 - $updatedBaby = AppsBabys::where('id', $id)->find(); + $updatedBaby = AppsBabyhealthBabys::where('id', $id)->find(); // 记录操作日志 $this->logSuccess('宝贝管理', '更新宝贝信息', ['id' => $id]); @@ -177,7 +176,7 @@ class BabysController extends BaseController { try { // 检查宝贝信息是否存在 - $baby = AppsBabys::where('id', $id) + $baby = AppsBabyhealthBabys::where('id', $id) ->where('delete_time', null) ->find(); @@ -189,7 +188,7 @@ class BabysController extends BaseController } // 逻辑删除宝贝信息 - $result = AppsBabys::where('id', $id) + $result = AppsBabyhealthBabys::where('id', $id) ->where('delete_time', null) ->update([ 'delete_time' => date('Y-m-d H:i:s'), @@ -218,4 +217,43 @@ class BabysController extends BaseController ]); } } + + /** + * 预览宝贝信息 + * @param int $id Baby ID + * @return Json + */ + public function getBabyDetail(int $id) + { + try { + // 查询宝贝信息 + $baby = AppsBabyhealthBabys::where('id', $id) + ->where('delete_time', null) + ->find(); + + if (!$baby) { + return json([ + 'code' => 404, + 'msg' => '宝贝信息不存在' + ]); + } + + // 记录操作日志 + $this->logSuccess('宝贝监护-宝贝管理', '获取宝贝信息', ['id' => $id]); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $baby->toArray() + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-宝贝管理', '获取宝贝信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '获取失败:' . $e->getMessage(), + 'data' => [] + ]); + } + } } diff --git a/app/admin/controller/BabyHealth/UserController.php b/app/admin/controller/BabyHealth/UserController.php new file mode 100644 index 0000000..48ac7be --- /dev/null +++ b/app/admin/controller/BabyHealth/UserController.php @@ -0,0 +1,258 @@ +field('id, account, password, phone, email, name, sex, avatar, birth, status, create_time, update_time') + ->select() + ->toArray(); + // 记录操作日志 + $this->logSuccess('宝贝监护-用户管理', '获取用户信息', ['data' => $babylist]); + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $babylist + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '获取用户信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '获取失败:' . $e->getMessage(), + 'data' => [] + ]); + } + } + + /** + * 创建用户信息 + * @return Json + */ + public function createUser() + { + try { + // 获取请求参数 + $data = $this->request->param(); + + // 准备用户数据,密码进行 MD5 加密 + $babyData = [ + 'account' => $data['account'], + 'password' => !empty($data['password']) ? md5($data['password']) : '', + 'name' => $data['name'], + 'sex' => $data['sex'], + 'birth' => $data['birth'], + 'phone' => $data['phone'], + 'email' => $data['email'], + 'avatar' => $data['avatar'], + 'status' => $data['status'], + 'create_time' => date('Y-m-d H:i:s'), + ]; + + // 创建用户信息 + $result = AppsBabyhealthUsers::insertGetId($babyData); + + if ($result === false) { + return json([ + 'code' => 500, + 'msg' => '创建失败' + ]); + } + + // 记录操作日志 + $this->logSuccess('宝贝监护-用户管理', '创建用户信息', ['id' => $result]); + return json([ + 'code' => 200, + 'msg' => '创建成功', + 'data' => ['id' => $result] + ]); + } catch (ValidateException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '创建用户信息', $e->getMessage()); + return json([ + 'code' => 400, + 'msg' => $e->getError() + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '创建用户信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '创建失败:' . $e->getMessage() + ]); + } + } + + /** + * 编辑用户信息 + * @param int $id User ID + * @return Json + */ + public function updateUser(int $id) + { + try { + // 获取请求参数 + $data = $this->request->param(); + + // 检查用户信息是否存在 + $baby = AppsBabyhealthUsers::where('id', $id) + ->where('delete_time', null) + ->find(); + + if (!$baby) { + return json([ + 'code' => 404, + 'msg' => '用户信息不存在' + ]); + } + + // 准备更新数据 + $updateData = [ + 'name' => $data['name'], + 'sex' => $data['sex'], + 'birth' => $data['birth'], + 'phone' => $data['phone'], + 'email' => $data['email'], + 'avatar' => $data['avatar'], + 'status' => $data['status'], + 'update_time' => date('Y-m-d H:i:s'), + ]; + + // 执行更新 + AppsBabyhealthUsers::where('id', $id)->update($updateData); + + // 获取更新后的用户信息 + $updatedBaby = AppsBabyhealthUsers::where('id', $id)->find(); + + // 记录操作日志 + $this->logSuccess('宝贝监护-用户管理', '更新用户信息', ['id' => $id]); + return json([ + 'code' => 200, + 'msg' => '更新成功', + 'data' => $updatedBaby ? $updatedBaby->toArray() : [] + ]); + } catch (ValidateException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '更新用户信息', $e->getMessage()); + return json([ + 'code' => 400, + 'msg' => $e->getError() + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '更新用户信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '更新失败:' . $e->getMessage() + ]); + } + } + + /** + * 删除用户信息 + * @param int $id User ID + * @return Json + */ + public function deleteUser(int $id) + { + try { + // 检查用户信息是否存在 + $baby = AppsBabyhealthUsers::where('id', $id) + ->where('delete_time', null) + ->find(); + + if (!$baby) { + return json([ + 'code' => 404, + 'msg' => '用户信息不存在' + ]); + } + + // 逻辑删除用户信息 + $result = AppsBabyhealthUsers::where('id', $id) + ->where('delete_time', null) + ->update([ + 'delete_time' => date('Y-m-d H:i:s'), + 'update_time' => date('Y-m-d H:i:s') + ]); + + if ($result === false) { + return json([ + 'code' => 500, + 'msg' => '删除失败' + ]); + } + + // 记录操作日志 + $this->logSuccess('宝贝监护-用户管理', '删除用户信息', ['id' => $id]); + return json([ + 'code' => 200, + 'msg' => '删除成功' + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '删除用户信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '删除失败:' . $e->getMessage() + ]); + } + } + + /** + * 预览用户信息 + * @param int $id User ID + * @return Json + */ + public function getUserDetail(int $id) + { + try { + // 查询用户信息 + $baby = AppsBabyhealthUsers::where('id', $id) + ->where('delete_time', null) + ->find(); + + if (!$baby) { + return json([ + 'code' => 404, + 'msg' => '用户信息不存在' + ]); + } + + // 记录操作日志 + $this->logSuccess('宝贝监护-用户管理', '获取用户信息', ['id' => $id]); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $baby->toArray() + ]); + } catch (DbException $e) { + // 记录失败日志 + $this->logFail('宝贝监护-用户管理', '获取用户信息', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '获取失败:' . $e->getMessage(), + 'data' => [] + ]); + } + } +} diff --git a/app/admin/controller/FileController.php b/app/admin/controller/FileController.php index a6942cd..033e295 100644 --- a/app/admin/controller/FileController.php +++ b/app/admin/controller/FileController.php @@ -209,6 +209,7 @@ class FileController extends BaseController } } + // 上传文件 public function uploadFile() { try { @@ -387,4 +388,129 @@ class FileController extends BaseController return json(['code' => 500, 'msg' => '移动失败: ' . $e->getMessage()]); } } + + + + // 上传头像 + public function uploadAvatar() + { + try { + $file = Request::file('file'); + if (!$file) { + return json(['code' => 400, 'msg' => '请选择要上传的文件']); + } + + // 验证文件大小和类型 + $maxSize = 50 * 1024 * 1024; // 50MB + $fileExt = strtolower($file->getOriginalExtension()); + + if ($file->getSize() > $maxSize) { + return json(['code' => 400, 'msg' => '文件大小不能超过50MB']); + } + + // 计算文件MD5 + $fileMd5 = md5_file($file->getRealPath()); + + // 检查是否已存在相同文件 + $existFile = Files::where('md5', $fileMd5)->where('delete_time', null)->find(); + + if ($existFile) { + // 检查物理文件是否存在 + $existFilePath = public_path() . $existFile['src']; + if (!file_exists($existFilePath)) { + // 物理文件不存在,删除数据库记录,继续上传 + Files::where('id', $existFile['id'])->delete(); + } else { + return json([ + 'code' => 201, + 'msg' => '文件已存在', + 'data' => [ + 'url' => $existFile['src'], + 'id' => $existFile['id'], + 'name' => $existFile['name'] + ] + ]); + } + } + + // 确定文件类型 + $fileType = 1; // 默认为文件 + foreach ($this->allowedExtensions as $type => $extensions) { + if (in_array($fileExt, $extensions)) { + $fileType = $this->fileTypes[$type]; + break; + } + } + + $cate = Request::param('cate/d', 0); + // 生成按日期分类的目录结构 + $datePath = date('Y/m/d'); + $saveName = $datePath . '/' . uniqid() . '.' . $fileExt; + $fullPath = Filesystem::disk('public')->putFileAs('avatar', $file, $saveName); + $fileUrl = '/storage/' . str_replace('\\', '/', $fullPath); + + // 获取当前登录用户ID + $userId = Request::middleware('user_id', ''); + + // 保存文件信息到数据库 + $fileData = [ + 'name' => $file->getOriginalName(), + 'type' => $fileType, + 'cate' => $cate, + 'size' => $file->getSize(), + 'src' => $fileUrl, + 'md5' => $fileMd5, + 'uploader' => $userId, + 'create_time' => date('Y-m-d H:i:s'), + ]; + + $fileId = Files::insertGetId($fileData); + + // 记录操作日志 + $this->logSuccess('文件管理', '上传图片', ['id' => $fileId]); + return json([ + 'code' => 200, + 'msg' => '上传成功', + 'data' => [ + 'url' => $fileUrl, + 'id' => $fileId, + 'name' => $fileData['name'] + ] + ]); + } catch (\Exception $e) { + // 记录失败日志 + $this->logFail('文件管理', '上传图片', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '上传失败: ' . $e->getMessage() + ]); + } + } + + // 更新头像 + public function updateAvatar($id) + { + try { + $data = Request::only(['name', 'cate']); + if (empty($data)) { + return json(['code' => 400, 'msg' => '无更新数据']); + } + + $data['update_time'] = date('Y-m-d H:i:s'); + + $result = Files::where('id', $id)->where('delete_time', null)->update($data); + + if ($result) { + // 记录操作日志 + $this->logSuccess('文件管理', '更新头像', ['id' => $id]); + return json(['code' => 200, 'msg' => '更新成功']); + } + + return json(['code' => 404, 'msg' => '头像不存在']); + } catch (\Exception $e) { + // 记录失败日志 + $this->logFail('文件管理', '更新头像', $e->getMessage()); + return json(['code' => 500, 'msg' => '更新失败: ' . $e->getMessage()]); + } + } } diff --git a/app/admin/route/routes/babyhealth.php b/app/admin/route/routes/babyhealth.php index d38258e..83053cd 100644 --- a/app/admin/route/routes/babyhealth.php +++ b/app/admin/route/routes/babyhealth.php @@ -2,8 +2,15 @@ use think\facade\Route; // 宝贝管理路由 -Route::get('babys/list', 'app\\admin\\controller\\BabyHealth\\BabysController@getList'); -Route::get('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@getDetail'); -Route::post('babys', 'app\\admin\\controller\\BabyHealth\\BabysController@create'); -Route::put('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@update'); -Route::delete('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@delete'); +Route::get('babys/list', 'app\\admin\\controller\\BabyHealth\\BabysController@getBabyList'); +Route::get('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@getBabyDetail'); +Route::post('babys', 'app\\admin\\controller\\BabyHealth\\BabysController@createBaby'); +Route::put('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@editBaby'); +Route::delete('babys/:id', 'app\\admin\\controller\\BabyHealth\\BabysController@deleteBaby'); + +// 用户管理路由 +Route::get('babyhealthUser/list', 'app\\admin\\controller\\BabyHealth\\UserController@getUserList'); +Route::get('babyhealthUser/:id', 'app\\admin\\controller\\BabyHealth\\UserController@getUserDetail'); +Route::post('babyhealthUser', 'app\\admin\\controller\\BabyHealth\\UserController@createUser'); +Route::put('babyhealthUser/:id', 'app\\admin\\controller\\BabyHealth\\UserController@updateUser'); +Route::delete('babyhealthUser/:id', 'app\\admin\\controller\\BabyHealth\\UserController@deleteUser'); \ No newline at end of file diff --git a/app/admin/route/routes/file.php b/app/admin/route/routes/file.php index b438c60..7d6bf76 100644 --- a/app/admin/route/routes/file.php +++ b/app/admin/route/routes/file.php @@ -12,3 +12,6 @@ Route::get('movefile/:id', 'app\\admin\\controller\\FileController@moveFile'); Route::post('createfilecate', 'app\\admin\\controller\\FileController@createFileCate'); Route::post('renamefilecate/:id', 'app\\admin\\controller\\FileController@renameFileCate'); Route::delete('deletefilecate/:id', 'app\\admin\\controller\\FileController@deleteFileCate'); + +Route::post('uploadavatar', 'app\\admin\\controller\\FileController@uploadAvatar'); +Route::post('uploadavatar/:id', 'app\\admin\\controller\\FileController@updateAvatar'); \ No newline at end of file diff --git a/app/model/AppsBabys.php b/app/model/AppsBabyhealthBabys.php similarity index 93% rename from app/model/AppsBabys.php rename to app/model/AppsBabyhealthBabys.php index 422190a..928dd42 100644 --- a/app/model/AppsBabys.php +++ b/app/model/AppsBabyhealthBabys.php @@ -17,13 +17,13 @@ use think\model\concern\SoftDelete; /** * 文章分类模型 */ -class AppsBabys extends Model +class AppsBabyhealthBabys extends Model { // 启用软删除 use SoftDelete; // 数据库表名 - protected $name = 'mete_apps_babys'; + protected $name = 'mete_apps_babyhealth_babys'; // 字段类型转换 protected $type = [ diff --git a/app/model/AppsBabyhealthUsers.php b/app/model/AppsBabyhealthUsers.php new file mode 100644 index 0000000..29c7ea8 --- /dev/null +++ b/app/model/AppsBabyhealthUsers.php @@ -0,0 +1,46 @@ + +// +---------------------------------------------------------------------- + +namespace app\model; + +use think\Model; +use think\model\concern\SoftDelete; + +/** + * 文章分类模型 + */ +class AppsBabyhealthUsers extends Model +{ + // 启用软删除 + use SoftDelete; + + // 数据库表名 + protected $name = 'mete_apps_babyhealth_users'; + + // 字段类型转换 + protected $type = [ + 'id' => 'integer', + 'account' => 'string', + 'password' => 'string', + 'phone' => 'string', + 'email' => 'string', + 'name' => 'string', + 'sex' => 'integer', + 'birth' => 'date', + 'avatar' => 'string', + 'status' => 'integer', + 'create_time' => 'datetime', + 'update_time' => 'datetime', + 'delete_time' => 'datetime', + ]; + + +} diff --git a/public/storage/avatar/2026/02/04/698307efed952.jpg b/public/storage/avatar/2026/02/04/698307efed952.jpg new file mode 100644 index 0000000..6a65fbe Binary files /dev/null and b/public/storage/avatar/2026/02/04/698307efed952.jpg differ diff --git a/public/storage/avatar/2026/02/04/6983082596de8.jpg b/public/storage/avatar/2026/02/04/6983082596de8.jpg new file mode 100644 index 0000000..6a65fbe Binary files /dev/null and b/public/storage/avatar/2026/02/04/6983082596de8.jpg differ diff --git a/public/storage/avatar/2026/02/04/698308cbb3f92.jpg b/public/storage/avatar/2026/02/04/698308cbb3f92.jpg new file mode 100644 index 0000000..6a65fbe Binary files /dev/null and b/public/storage/avatar/2026/02/04/698308cbb3f92.jpg differ diff --git a/public/storage/avatar/2026/02/04/698318dfe49f5.png b/public/storage/avatar/2026/02/04/698318dfe49f5.png new file mode 100644 index 0000000..12779bf Binary files /dev/null and b/public/storage/avatar/2026/02/04/698318dfe49f5.png differ diff --git a/public/storage/uploads/2026/02/04/698305d199833.jpg b/public/storage/uploads/2026/02/04/698305d199833.jpg new file mode 100644 index 0000000..6a65fbe Binary files /dev/null and b/public/storage/uploads/2026/02/04/698305d199833.jpg differ