diff --git a/app/api/controller/UploadController.php b/app/api/controller/UploadController.php index adcb50b2..af5e9ea4 100644 --- a/app/api/controller/UploadController.php +++ b/app/api/controller/UploadController.php @@ -15,6 +15,7 @@ namespace app\api\controller; use app\common\enum\FileEnum; +use app\common\model\file\File; use app\common\service\UploadService; use Exception; use think\response\Json; @@ -26,7 +27,7 @@ use think\response\Json; */ class UploadController extends BaseApiController { - + public array $notNeedLogin=['uploadVideoCover']; /** * @notes 上传图片 * @return Json @@ -48,8 +49,36 @@ class UploadController extends BaseApiController { try { $deviceId = $this->request->param('device_id', ''); - $result = UploadService::image($deviceId, $this->userId,FileEnum::SOURCE_USER); - return $this->success('上传成功', ['url'=>env('project.project_url').'/'.$result['url']]); + // Base64编码的图片字符串 + $base64Image = $this->request->param('image'); // 这里只展示部分内容 + + // 去除前面的"data:"标识 + $base64Data = substr($base64Image, strpos($base64Image, ',') + 1); + + // 将Base64编码的图片字符串转换为二进制数据 + $binaryData = base64_decode($base64Data); + + // 创建图像对象 + $imageObject = imagecreatefromstring($binaryData); + $saveDir = 'uploads/images/' . date('Ymd'). '/'; + if (!is_dir($saveDir)) { + mkdir($saveDir, 0755, true); + } + $fileName = uniqid().'.png'; + + // 保存图片 + imagepng($imageObject, $saveDir.$fileName); + + $file = File::create([ + 'cid' => $deviceId, + 'type' => FileEnum::IMAGE_TYPE, + 'name' => $fileName, + 'uri' => $saveDir . $fileName, + 'source' => FileEnum::SOURCE_USER, + 'source_id' => $this->userId, + 'create_time' => time(), + ]); + return $this->success('上传成功', ['url'=>env('project.project_url').'/'.$saveDir.$fileName]); } catch (Exception $e) { return $this->fail($e->getMessage()); }