fix(shopro): 修复切换独立 PHP 池后小程序登录 500(runtime 不可写导致 EasyWeChat 日志异常)

现象:小程序登录提示“未获取到登录态,请重试”。实测 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>
This commit is contained in:
liufumingren 2026-10-28 22:20:30 +08:00
parent 7b18287328
commit b676ad4883

View File

@ -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;
}
}