mirror of
https://github.com/easychen/pushdeer.git
synced 2025-01-10 15:05:27 +08:00
添加微信登入api
This commit is contained in:
parent
622524c77a
commit
9cc6f3d322
@ -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=
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user