From 381006ddbe6be6e5df25028900075ac474094a58 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Thu, 10 Jul 2025 17:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/PosterWeChatMiniProgram.php | 58 +++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/Server/application/cunkebao/controller/plan/PosterWeChatMiniProgram.php b/Server/application/cunkebao/controller/plan/PosterWeChatMiniProgram.php index c6550be8c..1fa0fe0f8 100644 --- a/Server/application/cunkebao/controller/plan/PosterWeChatMiniProgram.php +++ b/Server/application/cunkebao/controller/plan/PosterWeChatMiniProgram.php @@ -8,6 +8,7 @@ use EasyWeChat\Factory; // use EasyWeChat\Kernel\Exceptions\DecryptException; use EasyWeChat\Kernel\Http\StreamResponse; use think\Db; +use app\common\util\AliyunOSS; class PosterWeChatMiniProgram extends Controller { public function index() @@ -46,17 +47,23 @@ class PosterWeChatMiniProgram extends Controller // 保存小程序码到文件 if ($response instanceof StreamResponse) { - // $filename = 'minicode_' . $taskId . '.png'; - // $response->saveAs('path/to/codes', $filename); - // return 'path/to/codes/' . $filename; - - // $img = $response->getBody()->getContents();//获取图片二进制流 - // $img_base64 = 'data:image/png;base64,' .base64_encode($img);//转化base64 - // return $img_base64; - - - $filename = $response->saveAs('/www/wwwroot/mckb_quwanzhi_com/Server/runtime/img', 'appcode_'.$taskId.'.png'); - return $filename; + $savePath = ROOT_PATH . 'runtime/img'; + // 确保目录存在 + if (!is_dir($savePath)) { + mkdir($savePath, 0755, true); + } + $filename = $response->saveAs($savePath, 'appcode_'.$taskId.'.png'); + + // 上传到OSS + if ($filename) { + $ossUrl = $this->uploadToOSS($filename); + // 删除本地文件 + if (file_exists($filename)) { + unlink($filename); + } + // 如果OSS上传成功,返回OSS URL;否则返回本地文件路径 + return $ossUrl ?: $filename; + } } @@ -64,6 +71,35 @@ class PosterWeChatMiniProgram extends Controller return null; } + /** + * 上传文件到OSS + * @param string $filePath 本地文件路径 + * @return string|false 成功返回OSS URL,失败返回false + */ + private function uploadToOSS($filePath) + { + try { + // 生成OSS对象名称 + $objectName = AliyunOSS::generateObjectName('appcode_' . time() . '.png'); + + // 上传到OSS + $result = AliyunOSS::uploadFile($filePath, $objectName); + + if ($result['success']) { + // 返回完整的OSS URL + return AliyunOSS::ossUrl . '/' . $objectName; + } else { + // 记录错误日志 + \think\facade\Log::error('OSS上传失败: ' . $result['error'] . ' 文件: ' . $filePath); + return false; + } + } catch (\Exception $e) { + // 记录错误日志 + \think\facade\Log::error('OSS上传异常: ' . $e->getMessage() . ' 文件: ' . $filePath); + return false; + } + } + // getPhoneNumber public function getPhoneNumber() {