Commit 0aaf4918 authored by 刘小敏's avatar 刘小敏

分享调试

parent 8da5c178
...@@ -163,6 +163,7 @@ class Wechat extends Common ...@@ -163,6 +163,7 @@ class Wechat extends Common
$this->success('jssdkApi', $data); $this->success('jssdkApi', $data);
} }
/** /**
* 微信小程序码接口 * 微信小程序码接口
*/ */
...@@ -171,54 +172,53 @@ class Wechat extends Common ...@@ -171,54 +172,53 @@ class Wechat extends Common
try { try {
$mp = $this->wechat->getApp(); $mp = $this->wechat->getApp();
$path = $this->payload['path'] ?? ''; $path = $this->payload['path'] ?? '';
list($page, $scene) = array_pad(explode('?', $path, 2), 2, '');
// 验证路径 // 根据域名或配置动态选择环境版本
if (empty($path)) { $envVersion = 'release';
$this->error('页面路径不能为空');
}
// 安全处理:使用 array_pad 确保变量存在 // 缓存键:使用 scene 参数作为唯一标识,支持缓存不同场景的二维码
$parts = explode('?', $path, 2); $cacheKey = 'wxacode_' . md5($scene . '_' . $envVersion);
list($page, $scene) = array_pad($parts, 2, ''); $cacheTtl = 7 * 24 * 60 * 60; // 7天
// 确保路径以 / 开头 // 尝试从缓存获取
if ($page[0] !== '/') { $cachedImage = cache($cacheKey);
$page = '/' . $page; if ($cachedImage) {
custom_log('[微信小程序码] 从缓存获取成功: scene=' . $scene, 'wechat');
return response($cachedImage, 200, ['Content-Length' => strlen($cachedImage)])->contentType('image/png');
} }
// 根据域名或配置动态选择环境版本
$envVersion = 'develop';
$inputParams = [ $inputParams = [
'platform' => $this->platform, 'platform' => $this->platform,
'payload' => $this->payload, 'payload' => $this->payload,
'path' => $path, 'path' => $path,
'page' => $page,
'scene' => $scene, 'scene' => $scene,
'env_version' => $envVersion, 'env_version' => $envVersion
'request_page' => substr($page, 1)
]; ];
custom_log(json_encode($inputParams, JSON_UNESCAPED_UNICODE), 'wechat'); custom_log(json_encode($inputParams, JSON_UNESCAPED_UNICODE), 'wechat');
// 去掉 page 参数,使用小程序默认页
$content = $mp->app_code->getUnlimit($scene, [ $content = $mp->app_code->getUnlimit($scene, [
'page' => substr($page, 1), // 微信接口要求不带开头的 /
'is_hyaline' => true, 'is_hyaline' => true,
'env_version' => $envVersion 'env_version' => $envVersion
]); ]);
if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) { if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
// 成功返回图片 // 成功返回图片
$imageContent = $content->getBodyContents();
$outputParams = [ $outputParams = [
'success' => true, 'success' => true,
'type' => 'StreamResponse', 'type' => 'StreamResponse',
'content_length' => strlen($content->getBodyContents()), 'content_length' => strlen($imageContent),
'content_type' => 'image/png' 'content_type' => 'image/png'
]; ];
custom_log('[微信小程序码]获取成功 : pag'.json_encode($outputParams), 'wechat'); custom_log('[微信小程序码]获取成功 : ' . json_encode($outputParams), 'wechat');
// 缓存图片
cache($cacheKey, $imageContent, $cacheTtl);
custom_log('[微信小程序码] 已缓存: ' . $cacheKey, 'wechat');
return response($content->getBody(), 200, ['Content-Length' => strlen($content->getBodyContents())])->contentType('image/png'); return response($imageContent, 200, ['Content-Length' => strlen($imageContent)])->contentType('image/png');
} else { } else {
// 记录详细日志 // 记录详细日志
$errcode = $content['errcode'] ?? '-'; $errcode = $content['errcode'] ?? '-';
...@@ -227,17 +227,16 @@ class Wechat extends Common ...@@ -227,17 +227,16 @@ class Wechat extends Common
'success' => false, 'success' => false,
'errcode' => $errcode, 'errcode' => $errcode,
'errmsg' => $errmsg, 'errmsg' => $errmsg,
'page' => $page,
'scene' => $scene, 'scene' => $scene,
'env_version' => $envVersion 'env_version' => $envVersion
]; ];
custom_log('[微信小程序码] 获取失败:'.json_encode($outputParams), 'wechat'); custom_log('[微信小程序码] 获取失败: ' . json_encode($outputParams), 'wechat');
$this->error('获取小程序码失败: [' . $errcode . '] ' . $errmsg); return response('获取小程序码失败: [' . $errcode . '] ' . $errmsg, 200, ['Content-Type' => 'application/json; charset=utf-8']);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
custom_log('[微信小程序码]异常: ' . $e->getMessage() . ' | ' . $e->getTraceAsString(), 'wechat'); format_log_error_custom_name($e, '[微信小程序码异常]', '', 'wechat');
$this->error('获取小程序码异常'); return response('获取小程序码异常', 200, ['Content-Type' => 'application/json; charset=utf-8']);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment