feat: 完善协议管理、师傅资质认证、分账配置并完成测试环境全量部署与同步
这个提交包含在:
@@ -17,31 +17,46 @@ class WxLogin
|
||||
*/
|
||||
public static function jscode2session($code)
|
||||
{
|
||||
$appid = \think\Env::get('wechat.appid');
|
||||
$secret = \think\Env::get('wechat.secret');
|
||||
if (!$appid || !$secret) {
|
||||
return ['error' => '微信登录未配置'];
|
||||
$appid = trim((string)\think\Env::get('wechat.appid', ''));
|
||||
$secret = trim((string)\think\Env::get('wechat.secret', ''));
|
||||
if ($appid === '' || $secret === '') {
|
||||
return ['error' => '微信登录参数未配置完整(请检查 server/.env 的 wechat.appid 与 wechat.secret)', 'errcode' => -1001];
|
||||
}
|
||||
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid
|
||||
. '&secret=' . $secret
|
||||
$code = trim((string)$code);
|
||||
if ($code === '') {
|
||||
return ['error' => '缺少微信登录临时凭证 code,请在微信小程序内重新打开重试', 'errcode' => -1002];
|
||||
}
|
||||
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . urlencode($appid)
|
||||
. '&secret=' . urlencode($secret)
|
||||
. '&js_code=' . urlencode($code)
|
||||
. '&grant_type=authorization_code';
|
||||
$ctx = stream_context_create(['http' => ['timeout' => 8, 'ignore_errors' => true]]);
|
||||
$ctx = stream_context_create([
|
||||
'http' => ['timeout' => 8, 'ignore_errors' => true],
|
||||
'ssl' => ['verify_peer' => true, 'verify_peer_name' => true],
|
||||
]);
|
||||
$resp = @file_get_contents($url, false, $ctx);
|
||||
if ($resp === false) {
|
||||
return ['error' => '微信服务不可达,请稍后重试'];
|
||||
$lastErr = error_get_last();
|
||||
$errHint = !empty($lastErr['message']) ? $lastErr['message'] : '网络超时或 SSL 证书校验失败';
|
||||
\think\Log::error('[WxLogin::jscode2session] HTTP请求失败: ' . $errHint);
|
||||
return ['error' => '连接微信授权接口失败(请检查服务器外网HTTPS与CA根证书配置)', 'errcode' => -1003];
|
||||
}
|
||||
$wx = json_decode($resp, true);
|
||||
if (empty($wx['openid'])) {
|
||||
$ec = isset($wx['errcode']) ? (int)$wx['errcode'] : 0;
|
||||
$em = isset($wx['errmsg']) ? $wx['errmsg'] : '未知错误';
|
||||
if (!is_array($wx) || empty($wx['openid'])) {
|
||||
$ec = isset($wx['errcode']) ? (int)$wx['errcode'] : -1004;
|
||||
$em = isset($wx['errmsg']) ? (string)$wx['errmsg'] : '接口返回报文无效';
|
||||
$hint = [
|
||||
40029 => '小程序 appid 与服务端配置不一致:请确认 manifest.json 的 mp-weixin.appid 为 ' . $appid,
|
||||
40125 => 'appsecret 配置错误,请检查 .env 的 [wechat] secret',
|
||||
45011 => '登录请求过于频繁,请稍后重试',
|
||||
40226 => '该微信账号被风控限制,请稍后再试',
|
||||
-1 => '微信登录服务暂时繁忙,请稍后重试',
|
||||
40029 => '微信临时登录凭证 code 无效或小程序 AppID 与服务端配置不一致(服务端配置为:' . $appid . ')',
|
||||
40125 => '微信小程序密钥 appsecret 配置错误,请检查 server/.env 的 [wechat] secret',
|
||||
40163 => '微信登录凭证 code 已被使用过一次,请重新点击操作获取新授权',
|
||||
41002 => '请求缺少小程序 AppID 参数',
|
||||
41004 => '请求缺少小程序密钥 appsecret 参数',
|
||||
45011 => '微信登录请求过于频繁触发限流,请稍后再试',
|
||||
40226 => '该微信账号触发微信安全风控拦截,暂时无法授权登录',
|
||||
];
|
||||
$msg = isset($hint[$ec]) ? $hint[$ec] : ('微信登录失败(' . $ec . '):' . $em);
|
||||
$msg = isset($hint[$ec]) ? $hint[$ec] : ('微信授权登录校验未通过(微信错误码:' . $ec . '),请核对小程序 AppID/Secret 配置或重试');
|
||||
\think\Log::error("[WxLogin::jscode2session] errcode={$ec}, errmsg={$em}");
|
||||
return ['error' => $msg, 'errcode' => $ec];
|
||||
}
|
||||
return $wx;
|
||||
|
||||
在新工单中引用
屏蔽一个用户