diff --git a/app/api/controller/Upload.php b/app/api/controller/Upload.php index b715f494..2065ac84 100644 --- a/app/api/controller/Upload.php +++ b/app/api/controller/Upload.php @@ -12,10 +12,10 @@ declare (strict_types=1); namespace app\api\controller; -use app\api\service\User as UserService; -use think\response\Json; use app\api\service\Upload as UploadService; +use app\api\service\User as UserService; use app\common\enum\file\FileType as FileTypeEnum; +use think\response\Json; /** * 文件上传管理 @@ -45,8 +45,29 @@ class Upload extends Controller return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功'); } + public function uploadBase64Image(): Json + { + $base64_file = $this->request->post('file'); // 获取Base64编码的字符串 + $file_info = explode(';', $base64_file); + $file_type_info = explode('/', $file_info[0]); + $file_extension = $file_type_info[1]; // 获取文件扩展名 + $file_data = explode(',', $base64_file)[1]; // 获取Base64编码后的数据 + $file_data = base64_decode($file_data); // 解码Base64数据 + $file_name = uniqid((string)rand(), true) . '.' . $file_extension; // 创建唯一文件名 + $file_path = base_url() . 'temp/' . $file_name; + if (file_put_contents($file_path, $file_data)) { + $service = new UploadService(); + if (!$service->uploadBase64(FileTypeEnum::IMAGE, false, $file_path)) { + return $this->renderError('文件上传失败:' . $service->getError()); + } + // 图片上传成功 + return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成'); + } + return $this->renderError('文件上传失败'); + } - public function wxUpload(): Json { + public function wxUpload(): Json + { $checkLogin = $this->request->param('checkLogin'); // 执行文件上传 $service = new UploadService(); @@ -77,14 +98,14 @@ class Upload extends Controller // 构造请求头部信息 $headers = array( "Content-Type: multipart/form-data", - "storeId: ".$this->storeId, - "Access-Token: ".$this->request->header("Access-Token"), - "platform: ".$this->request->header("platform") + "storeId: " . $this->storeId, + "Access-Token: " . $this->request->header("Access-Token"), + "platform: " . $this->request->header("platform") ); //请求数据 $post_data = [ 'checkLogin' => $checkLogin, - 'file' => new \CURLFile($fileName) + 'file' => new \CURLFile($fileName) ]; // 设置其他选项 curl_setopt($ch, CURLOPT_POST, true); @@ -102,21 +123,23 @@ class Upload extends Controller curl_close($ch); $result = json_decode($result, true); - return $this->renderSuccess($result['data'],$result['message']); + return $this->renderSuccess($result['data'], $result['message']); } - public function getFileName($imageUrl,$user_id) { + public function getFileName($imageUrl, $user_id) + { $imageFile = $this->setFileData($imageUrl); - $savePath = "./uploads/wxImage/".date("Y-m-d").'/'; - $fileName = $user_id.'.jpg'; - if(!file_exists($savePath)) { - mkdir($savePath, 0777,true); + $savePath = "./uploads/wxImage/" . date("Y-m-d") . '/'; + $fileName = $user_id . '.jpg'; + if (!file_exists($savePath)) { + mkdir($savePath, 0777, true); } - file_put_contents($savePath.$fileName, $imageFile); - return $savePath.$fileName; + file_put_contents($savePath . $fileName, $imageFile); + return $savePath . $fileName; } - public function setFileData($image_url) { + public function setFileData($image_url) + { return file_get_contents($image_url); } } diff --git a/app/api/service/Upload.php b/app/api/service/Upload.php index 112bc4a9..51f0c463 100644 --- a/app/api/service/Upload.php +++ b/app/api/service/Upload.php @@ -15,8 +15,8 @@ namespace app\api\service; use app\api\model\Setting as SettingModel; use app\api\model\UploadFile as UploadFileModel; use app\api\service\User as UserService; -use app\common\enum\Setting as SettingEnum; use app\common\enum\file\FileType as FileTypeEnum; +use app\common\enum\Setting as SettingEnum; use app\common\library\storage\Driver as StorageDriver; use app\common\service\BaseService; use cores\exception\BaseException; @@ -69,6 +69,24 @@ class Upload extends BaseService return $this->record($fileInfo, $fileType, $userId); } + public function uploadBase64(int $fileType, bool $checkLogin = true, $filePath): bool + { + $userId = $checkLogin && UserService::isLogin(true) ? UserService::getCurrentLoginUserId() : 0; + $storage = $this->getDriver(self::UPLOAD_SCENE_ENUM[$fileType]); + $storage->setUploadFileByReal($filePath); + // 执行文件上传 + if (!$storage->upload()) { + $this->error = $storage->getError(); + return false; + } + // 文件信息 + $fileInfo = $storage->getSaveFileInfo(); + @unlink($filePath); + // 添加文件库记录 + return $this->record($fileInfo, $fileType, $userId); + + } + /** * 实例化存储驱动 * @param string $scene 上传场景 image和video