添加绑定小程序账号

This commit is contained in:
luofei 2023-05-19 18:22:32 +08:00
parent b3a4245781
commit 63bee8d493
2 changed files with 59 additions and 0 deletions

View File

@ -309,6 +309,63 @@ class Auth extends BaseController
return app('json')->success($userRepository->returnToken($user[1], $tokenInfo)); return app('json')->success($userRepository->returnToken($user[1], $tokenInfo));
} }
/**
* 手机号用户绑定微信账号
* @param UserRepository $repository
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function bindMp(UserRepository $repository)
{
list($code, $post_cache_key, $phone) = $this->request->params([
'code',
'cache_key',
'phone',
], true);
$userInfoCong = Cache::get('eb_api_code_' . $code);
if (!$code && !$userInfoCong)
return app('json')->fail('授权失败,参数有误');
$miniProgramService = MiniProgramService::create();
if ($code && !$userInfoCong) {
try {
$userInfoCong = $miniProgramService->getUserInfo($code);
Cache::set('eb_api_code_' . $code, $userInfoCong, 86400);
} catch (Exception $e) {
return app('json')->fail('获取session_key失败请检查您的配置', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
}
}
$data = $this->request->params([
['spread_spid', 0],
['spread_code', ''],
['iv', ''],
['encryptedData', ''],
]);
try {
//解密获取用户信息
$userInfo = $miniProgramService->encryptor($userInfoCong['session_key'], $data['iv'], $data['encryptedData']);
} catch (Exception $e) {
if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败');
throw $e;
}
if (!$userInfo) return app('json')->fail('openid获取失败');
if (!isset($userInfo['openId'])) $userInfo['openId'] = $userInfoCong['openid'] ?? '';
$userInfo['unionId'] = $userInfoCong['unionid'] ?? $userInfo['unionId'] ?? '';
if (!$userInfo['openId']) return app('json')->fail('openid获取失败');
/** @var WechatUserRepository $make */
$make = app()->make(WechatUserRepository::class);
$user = $make->syncRoutineUser($userInfo['openId'], $userInfo, false);
if (!$user) {
return app('json')->fail('授权失败');
}
$userInDb = $repository->accountByUser($phone);
if ($userInDb->save(['wechat_user_id' => $user[0]['wechat_user_id']]) === false) {
return app('json')->fail('授权失败');
}
return app('json')->success();
}
public function getCaptcha() public function getCaptcha()
{ {
$codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4)); $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));

View File

@ -595,6 +595,8 @@ Route::group('api/', function () {
Route::get('auth/wechat', 'api.Auth/auth'); Route::get('auth/wechat', 'api.Auth/auth');
//小程序授权 //小程序授权
Route::post('auth/mp', 'api.Auth/mpAuth'); Route::post('auth/mp', 'api.Auth/mpAuth');
//绑定小程序账号
Route::post('auth/bindMp', 'api.Auth/bindMp');
//app授权 //app授权
Route::post('auth/app', 'api.Auth/appAuth'); Route::post('auth/app', 'api.Auth/appAuth');
//apple授权 //apple授权