/** * @Route("/authorize/callback", name = "app_authorize_callback") */ public function authorizeCallbackAction(Request $request) { if (!($code = $request->query->get('code'))) { throw $this->createNotFoundException(); } $client = $this->get('wechat.oauth.client'); try { $token = $client->getAccessToken($code); $user = $client->getUser(); } catch (\Exception $e) { exit(sprintf('获取用户信息失败(%s)', $e->getMessage())); } $repository = $this->get('doctrine.orm.entity_manager')->getRepository('AppBundle:User'); if (!($entity = $repository->getByOpenid($user['openid']))) { $entity = new \AppBundle\Entity\User(); } $entity->setOpenid($user['openid']); $entity->setNickname($user['nickname']); $entity->setProvince($user['province']); $entity->setCity($user['city']); $entity->setGender($user['sex']); $entity->setAvatar($user['headimgurl']); $entity->setAccessToken($accessToken); $repository->doPersist($entity); $token = new UsernamePasswordToken($entity, null, 'wechat', $entity->getRoles()); $tokenStorage = $this->get('security.token_storage'); $tokenStorage->setToken($token); $referer = $this->_getRefererUrl(); return $this->redirect($referer); }