增加宝贝监护功能

This commit is contained in:
2026-02-04 18:02:36 +08:00
parent 769d595e30
commit 516c043924
12 changed files with 495 additions and 17 deletions
+126
View File
@@ -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()]);
}
}
}