From bb51c273beae59c88aebb3e64b109906fa9b3637 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Wed, 29 Apr 2026 18:14:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Mobile/UserController.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Mobile/UserController.php b/app/Http/Controllers/Mobile/UserController.php index 2153026..d07eb67 100755 --- a/app/Http/Controllers/Mobile/UserController.php +++ b/app/Http/Controllers/Mobile/UserController.php @@ -9,6 +9,8 @@ use App\Models\User; use App\Models\VipCustomer; use App\Models\Visit; use EasyWeChat\Factory; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; class UserController extends CommonController @@ -41,10 +43,32 @@ class UserController extends CommonController // 获取配置信息 $appid = \config('app.wechat_appid'); $appSecret = \config('app.wechat_appsecret'); - $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $appSecret . "&js_code={$all['code']}&grant_type=authorization_code"; - $userInfo = json_decode(file_get_contents($url), true); + $url = "https://api.weixin.qq.com/sns/jscode2session"; + $userInfo = null; + try { + $response = Http::timeout(10)->get($url, [ + 'appid' => $appid, + 'secret' => $appSecret, + 'js_code' => $all['code'], + 'grant_type' => 'authorization_code', + ]); + $userInfo = $response->json(); + } catch (\Throwable $e) { + Log::error('miniapp login jscode2session request failed', [ + 'message' => $e->getMessage(), + 'appid' => $appid, + ]); + return $this->fail([ResponseCode::ERROR_BUSINESS, '微信登录服务不可用']); + } if (!isset($userInfo['openid'])) { - return $this->fail([ResponseCode::ERROR_BUSINESS, 'code异常']); + Log::warning('miniapp login jscode2session invalid response', [ + 'appid' => $appid, + 'wx_errcode' => $userInfo['errcode'] ?? null, + 'wx_errmsg' => $userInfo['errmsg'] ?? null, + 'response' => $userInfo, + ]); + $msg = $userInfo['errmsg'] ?? 'code异常'; + return $this->fail([ResponseCode::ERROR_BUSINESS, "code异常:{$msg}"]); } $openid = $userInfo['openid']; //判断该用户是否在数据库