From 90bc0399e501bdbc02a1412c9d7391c0a5f044f7 Mon Sep 17 00:00:00 2001 From: liapples Date: Fri, 23 Jul 2021 18:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=8F=82=E6=95=B0=EF=BC=8C=E5=B9=B6=E6=94=B9=E7=94=A8?= =?UTF-8?q?EasyWeChat=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/LoginController.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/LoginController.php b/app/Http/Controllers/Api/LoginController.php index 39f87a8..0b5424e 100644 --- a/app/Http/Controllers/Api/LoginController.php +++ b/app/Http/Controllers/Api/LoginController.php @@ -5,8 +5,8 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Agent; use App\Models\User; +use EasyWeChat\Factory; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Http; /** * 小程序登录 @@ -33,14 +33,18 @@ class LoginController extends Controller $agent = Agent::select(['id', 'appid', 'appsecret'])->find($agent_id); //代理商数据 - $base_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=JSCODE&grant_type=authorization_code'; - $url = sprintf($base_url, $agent['appid'], $agent['appsecret']); + $config = config('wechat.mini_program.default'); + $config = array_merge($config, [ + 'app_id' => $agent['appid'], + 'secret' => $agent['appsecret'], + ]); + $app = Factory::miniProgram($config); + $res = $app->auth->session($code); - $res = Http::get($url)->json(); - if (!array_key_exists('errcode', $res) || $res['errcode'] != 0 || empty($res['openid'])) { + if (!isset($res['errcode']) || $res['errcode'] != 0 || empty($res['openid']) || empty($res['unionid'])) { $msg = $res['errcode'] ? $res['errcode'] . ': ' : ''; $msg .= $res['errmsg'] ?? '登录失败'; - return $this->error($msg); //TODO 测试时注释掉 + return $this->error($msg); } // TODO 登录部分待优化 @@ -54,6 +58,9 @@ class LoginController extends Controller 'avatar' => '/static/images/avatar.png' ]); + //保存session_key + Cache::put('session_key_' . $userModel->id, $res['session_key']); + // TODO 用于测试 $token_key = md5($userModel->id); Cache::put($token_key, 1);