diff --git a/app/Controller/LoginController.php b/app/Controller/LoginController.php index 308066b..f5999e1 100644 --- a/app/Controller/LoginController.php +++ b/app/Controller/LoginController.php @@ -7,6 +7,11 @@ use App\Services\LoginService; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; /** * 登陆 @@ -42,7 +47,17 @@ class LoginController extends AbstractController return $this->response->json($data); } - // 微信授权接口 + /** + * 微信授权接口 + * @return ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ClientExceptionInterface + * @throws DecodingExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ServerExceptionInterface + * @throws TransportExceptionInterface + */ public function wxAuthorize(): ResponseInterface { // 验证参数 diff --git a/app/Request/LoginRequest.php b/app/Request/LoginRequest.php index c51fa7e..5a11e22 100644 --- a/app/Request/LoginRequest.php +++ b/app/Request/LoginRequest.php @@ -12,7 +12,7 @@ class LoginRequest extends FormRequest protected array $scenes = [ 'wechatMobileLogin' => ['phone_code','wx_code','user_type'], 'mobileLogin' => ['code','phone','user_type'], - 'mobileLogin' => ['wx_code'], + 'wxAuthorize' => ['wx_code'], ]; /** diff --git a/app/Services/LoginService.php b/app/Services/LoginService.php index 03d5c72..a015df8 100644 --- a/app/Services/LoginService.php +++ b/app/Services/LoginService.php @@ -16,6 +16,11 @@ use Hyperf\DbConnection\Db; use Hyperf\Redis\Redis; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; class LoginService extends BaseService { @@ -409,4 +414,100 @@ class LoginService extends BaseService return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); } } + + /** + * 微信授权接口 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ClientExceptionInterface + * @throws DecodingExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ServerExceptionInterface + * @throws TransportExceptionInterface + */ + public function wxAuthorize(): array + { + $user_info = $this->request->getAttribute("userInfo") ?? []; + $wx_code = $this->request->input('wx_code'); + + if (empty($user_info)){ + return fail(HttpEnumCode::GET_WX_ERROR); + } + + // 获取用户数据 + $params = array(); + $params['user_id'] = $user_info['user_id']; + $user = UserModel::getOne($params); + if (empty($user)){ + return fail(); + } + + try { + $weChat = new Wechat($user['user_type']); + + // 获取用户openid + $wx_info_data = $weChat->codeToSession($wx_code); + if (empty($wx_info_data['session_key']) || empty($wx_info_data['openid'])) { + return fail(HttpEnumCode::GET_WX_ERROR); + } + + // 获取对应端用户信息 + $params = array(); + $params['user_id'] = $user['user_id']; + if ($user['user_type'] == 1) { + $result = UserPatientModel::getOne($params); + } elseif ($user['user_type'] == 2) { + $result = UserDoctorModel::getOne($params); + } elseif ($user['user_type'] == 3) { + $result = UserPharmacistModel::getOne($params); + } + + if (empty($result)) { + return fail(HttpEnumCode::SERVER_ERROR); + } + + // 判断用户状态 + if ($result['status'] != 1) { + return fail(HttpEnumCode::USER_STATUS_ERROR); + } + + // 更新session_key + $data = array(); + if ($wx_info_data['session_key'] != $result['session_key']) { + $data['wx_session_key'] = $wx_info_data['session_key']; + } + + if ($wx_info_data['openid'] != $result['open_id']) { + $data['open_id'] = $wx_info_data['openid']; + } + + if (!empty($data)){ + $data['updated_at'] = date('Y-m-d H:i:s', time()); + + $params = array(); + if ($user['user_type'] == 1) { + $params['patient_id'] = $result['patient_id']; + $res = UserPatientModel::editUserPatient($params, $data); + } elseif ($user['user_type'] == 2) { + $params['doctor_id'] = $result['doctor_id']; + $res = UserDoctorModel::editUserDoctor($params, $data); + } elseif ($user['user_type'] == 3) { + $params['pharmacist_id'] = $result['pharmacist_id']; + $res = UserPharmacistModel::editUserPharmacist($params, $data); + }else{ + $res = false; + } + + if (!$res) { + return fail(HttpEnumCode::SERVER_ERROR); + } + } + } catch (\Exception $e) { + // 此处不进行处理 + return fail(HttpEnumCode::GET_WX_ERROR,$e->getMessage()); + } + + return success(); + } } \ No newline at end of file