From 9cc6f3d322f8c3671fbb35ecbcb79f0eedb00f18 Mon Sep 17 00:00:00 2001 From: EasyChen Date: Mon, 24 Jan 2022 12:00:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E5=85=A5api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.env.example | 4 ++ .../Controllers/PushDeerUserController.php | 54 +++++++++++++++++++ api/routes/api.php | 3 ++ 3 files changed, 61 insertions(+) diff --git a/api/.env.example b/api/.env.example index 466092f..e6e5b2b 100644 --- a/api/.env.example +++ b/api/.env.example @@ -59,3 +59,7 @@ GO_PUSH_IOS_CLIP_TOPIC=com.pushdeer.app.ios.Clip ANDROID_PACKAGE=com.pushdeer.app.os MIPUSH_SECRET=NONE + + +WECHAT_APPID= +WECHAT_APPSECRET= diff --git a/api/app/Http/Controllers/PushDeerUserController.php b/api/app/Http/Controllers/PushDeerUserController.php index 75fb9b9..ea08f80 100644 --- a/api/app/Http/Controllers/PushDeerUserController.php +++ b/api/app/Http/Controllers/PushDeerUserController.php @@ -53,6 +53,60 @@ class PushDeerUserController extends Controller return send_error('id_token解析错误', ErrorCode('ARGS')); } + public function wechatLogin(Request $request) + { + $validated = $request->validate( + [ + 'code' => 'string', + ] + ); + + if (isset($validated['code'])) { + // 解码并进行验证 + $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + .urlencode(env("WECHAT_APPID")) + ."&secret=" + .urlencode(env("WECHAT_APPSECRET")) + ."&code=" + .urlencode($validated['code']) + ."&grant_type=authorization_code"; + + $code_info = json_decode(file_get_contents($url), true); + + if (!$code_info || !isset($code_info['access_token']) || !isset($code_info['openid'])) { + return send_error("错误的Code", ErrorCode('REMOTE')); + } + + // 现在拿到openid了 + + $pd_user = PushDeerUser::where('wechat_id', $code_info['openid'])->get()->first(); + if (!$pd_user) { + // 用户不存在,创建用户 + $the_user = []; + $the_user['wechat_id'] = $code_info['openid']; + $the_user['email'] = $code_info['openid'].'@'.'fake.pushdeer.com'; + $the_user['name'] = '微信用户'.substr($code_info['openid'], 0, 6); + $the_user['level'] = 1; + + $pd_user = PushDeerUser::create($the_user); + } + + // 将数据写到session + session_start(); + $_SESSION['uid'] = $pd_user['id']; + $_SESSION['name'] = $pd_user['name']; + $_SESSION['email'] = $pd_user['email']; + $_SESSION['level'] = $pd_user['level']; + + session_regenerate_id(true); + $token = session_id(); + return http_result(['token'=>$token]); + } + + + return send_error('微信Code错误', ErrorCode('ARGS')); + } + // public function login(Request $request) { diff --git a/api/routes/api.php b/api/routes/api.php index ecc7e33..0746002 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -26,6 +26,9 @@ Route::any('/login/fake', 'App\Http\Controllers\PushDeerUserController@fakeLogin // 通过 apple 返回的 idtoken 登入 Route::post('/login/idtoken', 'App\Http\Controllers\PushDeerUserController@login'); +// 通过 微信客户端返回的 code 登入 +Route::post('/login/wecode', 'App\Http\Controllers\PushDeerUserController@wechatLogin'); + // 推送消息 Route::any('/message/push', 'App\Http\Controllers\PushDeerMessageController@push');