From 57a82374cc3bdfc2ac1e66337542d99a6060f2ec Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Sun, 3 Mar 2024 15:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BE=AE=E4=BF=A1=E5=A4=B4?= =?UTF-8?q?=E5=83=8F=E4=B8=8A=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Upload.php | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/app/api/controller/Upload.php b/app/api/controller/Upload.php index dca994b3..b715f494 100644 --- a/app/api/controller/Upload.php +++ b/app/api/controller/Upload.php @@ -12,6 +12,7 @@ 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\common\enum\file\FileType as FileTypeEnum; @@ -43,4 +44,79 @@ class Upload extends Controller // 图片上传成功 return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功'); } + + + public function wxUpload(): Json { + $checkLogin = $this->request->param('checkLogin'); + // 执行文件上传 + $service = new UploadService(); + if (!$service->upload(FileTypeEnum::IMAGE, boolval($checkLogin))) { + return $this->renderError('文件上传失败:' . $service->getError()); + } + // 图片上传成功 + return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功'); + } + + /** + * 微信头像上传 + */ + public function wxHeadImgUpload(): Json + { + $headImag = $this->request->param('headImg'); + $checkLogin = $this->request->param('checkLogin'); + $user_id = UserService::getCurrentLoginUserId(); + + // 创建一个新的 cURL 会话 + $ch = curl_init(); + // 设置 URL 地址 + curl_setopt($ch, CURLOPT_URL, 'https://www.royaum.com.cn/index.php?s=/api/upload/wxUpload'); //目标服务器接收文件的地址 + + $fileName = $this->getFileName($headImag, $user_id); + // 打开文件流 + $fileStream = fopen($fileName, 'r'); + // 构造请求头部信息 + $headers = array( + "Content-Type: multipart/form-data", + "storeId: ".$this->storeId, + "Access-Token: ".$this->request->header("Access-Token"), + "platform: ".$this->request->header("platform") + ); + //请求数据 + $post_data = [ + 'checkLogin' => $checkLogin, + 'file' => new \CURLFile($fileName) + ]; + // 设置其他选项 + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($ch, CURLOPT_INFILE, $fileStream); + curl_setopt($ch, CURLOPT_INFILESIZE, filesize($fileName)); + + // 执行请求并获取结果 + $result = curl_exec($ch); + + // 关闭文件流和 cURL 会话 + fclose($fileStream); + curl_close($ch); + + $result = json_decode($result, true); + return $this->renderSuccess($result['data'],$result['message']); + } + + 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); + } + file_put_contents($savePath.$fileName, $imageFile); + return $savePath.$fileName; + } + + public function setFileData($image_url) { + return file_get_contents($image_url); + } }