From b676ad48835180a8a2ab445e07588f2d445c6cce Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 28 Oct 2026 22:20:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(shopro):=20=E4=BF=AE=E5=A4=8D=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E7=8B=AC=E7=AB=8B=20PHP=20=E6=B1=A0=E5=90=8E=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=99=BB=E5=BD=95=20500=EF=BC=88runtime=20?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E5=86=99=E5=AF=BC=E8=87=B4=20EasyWeChat=20?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=BC=82=E5=B8=B8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:小程序登录提示“未获取到登录态,请重试”。实测 POST /api/user/getWxMiniProgramSessionKey 返回 500: UnexpectedValueException: The stream or file "/www/wwwroot/ye4s.wms.gztbyc.com/runtime/easywechat/easywechat.log" could not be opened in append mode: Permission denied(StreamHandler.php:146) (旧版页面同源:Monolog 在写 /tmp 时也报过同类错误,见 f5bff78) 原因:站点改用独立 php-fpm 池(user=ye4s),但 runtime/、uploads/、public/uploads/ 仍归 www 所有且 755,ye4s 无写权限: - EasyWeChat 写请求日志抛异常 → code 换 session_key 接口 500 → 前端拿不到 session_id → 再次提交 login 时缓存查不到 → “未获取到登录态” - TP5 写 runtime/log 失败,每个请求在 shutdown 触发 fatal,应用日志自 10-27 起中断 改动: - Wechat.php:日志文件改为可写性探测,runtime/easywechat 不可写时退到 sys_temp_dir, 全部不可写时降级为 EasyWeChat 的 null 日志驱动,避免日志问题再次打断登录 - 服务器侧已执行 chown -R ye4s:ye4s runtime uploads public/uploads;部署脚本同步补上该步骤 验证:把 runtime/easywechat 降为 500 后调用 getWxMiniProgramSessionKey 仍返回 HTTP 200 JSON 并自动改写到临时目录;恢复权限后 runtime/log/202610/28.log 与 runtime/easywechat/easywechat.log 正常写入,ye4s-php-error.log 不再新增 fatal; 伪造 session 调 wxMiniProgramOauth 已能读到 redis 登录态(进入解密环节)。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- addons/shopro/library/Wechat.php | 44 +++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/addons/shopro/library/Wechat.php b/addons/shopro/library/Wechat.php index 8efee39..5469f19 100644 --- a/addons/shopro/library/Wechat.php +++ b/addons/shopro/library/Wechat.php @@ -299,11 +299,7 @@ class Wechat extends Model private function setConfig($platform,$appid) { $debug = config('app_debug'); - // open_basedir 下不能写 /tmp,日志改到站点 runtime 目录 - $logPath = defined('RUNTIME_PATH') ? RUNTIME_PATH . 'easywechat' . DIRECTORY_SEPARATOR : ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR . 'easywechat' . DIRECTORY_SEPARATOR; - if (!is_dir($logPath)) { - @mkdir($logPath, 0755, true); - } + $logFile = $this->getWritableLogFile(); $defaultConfig = [ 'log' => [ @@ -312,18 +308,25 @@ class Wechat extends Model // 测试环境 'dev' => [ 'driver' => 'single', - 'path' => $logPath . 'easywechat.log', + 'path' => $logFile, 'level' => 'debug', ], // 生产环境 'prod' => [ 'driver' => 'daily', - 'path' => $logPath . 'easywechat.log', + 'path' => $logFile, 'level' => 'info', ], ], ], ]; + if (!$logFile) { + // 日志目录无一可写时降级为 null 驱动,避免 Monolog 抛异常把登录等接口打成 500 + $defaultConfig['log']['channels'] = [ + 'dev' => ['driver' => 'null'], + 'prod' => ['driver' => 'null'], + ]; + } if($appid){ $Appsmodel = new \app\common\model\Apps; $APP = $Appsmodel->where(['app_id'=>$appid])->find(); @@ -343,4 +346,31 @@ class Wechat extends Model // 根据框架 debug 合并 log 配置 $this->config = array_merge($this->config, $defaultConfig); } + + /** + * 获取可写的 easywechat 日志文件,全部不可写时返回 null + * + * @return string|null + */ + private function getWritableLogFile() + { + $dirs = [ + // open_basedir 下不能写 /tmp,优先写站点 runtime 目录 + (defined('RUNTIME_PATH') ? RUNTIME_PATH : ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR) . 'easywechat' . DIRECTORY_SEPARATOR, + // runtime 不可写时退到 PHP 临时目录(php-fpm 池已配置 sys_temp_dir) + sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'easywechat' . DIRECTORY_SEPARATOR, + ]; + + foreach ($dirs as $dir) { + if (!is_dir($dir)) { + @mkdir($dir, 0755, true); + } + $file = $dir . 'easywechat.log'; + if (is_dir($dir) && is_writable($dir) && (!file_exists($file) || is_writable($file))) { + return $file; + } + } + + return null; + } }