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);