mirror of
https://github.com/easychen/pushdeer.git
synced 2025-01-10 23:15:30 +08:00
添加微信code2unionid接口,以支持self-hosted登入
This commit is contained in:
parent
ae6adf3349
commit
782acfa53a
@ -56,11 +56,11 @@ class PushDeerUserController extends Controller
|
|||||||
return send_error('id_token解析错误', ErrorCode('ARGS'));
|
return send_error('id_token解析错误', ErrorCode('ARGS'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function wechatLogin(Request $request)
|
public function wecode2unionid(Request $request)
|
||||||
{
|
{
|
||||||
$validated = $request->validate(
|
$validated = $request->validate(
|
||||||
[
|
[
|
||||||
'code' => 'string',
|
'code' => 'required|string',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -80,6 +80,47 @@ class PushDeerUserController extends Controller
|
|||||||
return send_error("错误的Code", ErrorCode('REMOTE'));
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return http_result(['unionid'=>$code_info['unionid']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return send_error('微信Code错误', ErrorCode('ARGS'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function wechatLogin(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate(
|
||||||
|
[
|
||||||
|
'code' => 'required|string',
|
||||||
|
'self_hosted' => 'integer|nullable',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isset($validated['code'])) {
|
||||||
|
if (intval(@$validated['self_hosted']) != 1) {
|
||||||
|
// 解码并进行验证
|
||||||
|
$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['unionid'])) {
|
||||||
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$url = "https://api2.pushdeer.com/login/unoinid?code=".urlencode($validated['code']);
|
||||||
|
$ret = json_decode(file_get_contents($url), true);
|
||||||
|
if (!$ret || !isset($ret['data']) || !isset($ret['data']['unionid'])) {
|
||||||
|
return send_error("错误的Code", ErrorCode('REMOTE'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$code_info = ['unionid'=>$ret['data']['unionid']];
|
||||||
|
}
|
||||||
|
|
||||||
// 现在拿到unionid了
|
// 现在拿到unionid了
|
||||||
|
|
||||||
$pd_user = PushDeerUser::where('wechat_id', $code_info['unionid'])->get()->first();
|
$pd_user = PushDeerUser::where('wechat_id', $code_info['unionid'])->get()->first();
|
||||||
@ -115,7 +156,7 @@ class PushDeerUserController extends Controller
|
|||||||
{
|
{
|
||||||
$validated = $request->validate(
|
$validated = $request->validate(
|
||||||
[
|
[
|
||||||
'idToken' => 'string',
|
'idToken' => 'required|string',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -157,8 +198,8 @@ class PushDeerUserController extends Controller
|
|||||||
{
|
{
|
||||||
$validated = $request->validate(
|
$validated = $request->validate(
|
||||||
[
|
[
|
||||||
'tokenorcode' => 'string|required',
|
'tokenorcode' => 'required|string',
|
||||||
'type' => 'string|required', // apple or wechat
|
'type' => 'required|string', // apple or wechat
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -29,6 +29,9 @@ Route::post('/login/idtoken', 'App\Http\Controllers\PushDeerUserController@login
|
|||||||
// 通过 微信客户端返回的 code 登入
|
// 通过 微信客户端返回的 code 登入
|
||||||
Route::post('/login/wecode', 'App\Http\Controllers\PushDeerUserController@wechatLogin');
|
Route::post('/login/wecode', 'App\Http\Controllers\PushDeerUserController@wechatLogin');
|
||||||
|
|
||||||
|
// 通过 微信客户端返回的 code 换取 unionid 等信息
|
||||||
|
Route::any('/login/unoinid', 'App\Http\Controllers\PushDeerUserController@wecode2unionid');
|
||||||
|
|
||||||
// 推送消息
|
// 推送消息
|
||||||
Route::middleware('json.request')->any('/message/push', 'App\Http\Controllers\PushDeerMessageController@push');
|
Route::middleware('json.request')->any('/message/push', 'App\Http\Controllers\PushDeerMessageController@push');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user