Example #1
0
 public function __invoke()
 {
     // 验证登录
     $session = $this->getSession();
     if (!empty($session['user'])) {
         $session->addFlash('info', '已经登录过了');
         return new RedirectResponse($this->generateUrl('forum_homepage'));
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $email = $posts->get('email');
             $username = $posts->get('username');
             $password = $posts->get('password');
             if (empty($email)) {
                 throw new \Exception("邮箱地址不能为空");
             }
             if (empty($username)) {
                 throw new \Exception('用户名不能为空');
             }
             if (empty($password)) {
                 throw new \Exception("密码不能为空");
             }
             // 查询重复
             $user = UserModel::getUserByUsername($username);
             if ($user) {
                 throw new \Exception("用户名已被占用");
             }
             $user = UserModel::getUserByEmail($email);
             if ($user) {
                 throw new \Exception("邮箱已被注册过");
             }
             $user = new UserModel();
             $user->username = $username;
             $user->password = $password;
             $user->email = $email;
             $now = time();
             $user->createTimestamp = $now;
             $user->loginTimestamp = $now;
             UserModel::createUser($user);
             $session['user'] = $user->toArray();
             if ($request->isXmlHttpRequest()) {
                 $modal_content = $this->render('modal.html.twig', array('title' => '注册成功', 'msg' => '<p class="text-success">恭喜您,注册成功</p>', 'jump_link' => $this->generateUrl('forum_homepage')));
                 return new JsonResponse(array('status' => 1, 'modal' => $modal_content->getContent()));
             } else {
                 $session->addFlash('success', '注册成功');
                 return new RedirectResponse($this->generateUrl('forum_homepage'));
             }
         } catch (\Exception $e) {
             if ($request->isXmlHttpRequest()) {
                 $modal_content = $this->render('modal.html.twig', array('title' => '注册失败', 'msg' => '<p class="text-danger">注册失败:' . $e->getMessage() . '</p>'));
                 return new JsonResponse(array('status' => 0, 'modal' => $modal_content->getContent()));
             } else {
                 $session->addFlash('error', "注册失败:{$e->getMessage()}");
             }
         }
     }
     return $this->render('user/register.html.twig');
 }
Example #2
0
 public function __invoke()
 {
     $request = $this->getRequest();
     $session = $this->getSession();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $email = $posts->get('email');
             if (!$email) {
                 throw new \Exception("邮箱不能为空");
             }
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 throw new \Exception("错误的邮箱地址");
             }
             $captcha = $posts->get('captcha');
             if (!$captcha) {
                 throw new \Exception("验证码不能为空");
             }
             if ($captcha != $session['captcha']) {
                 throw new \Exception("验证码错误");
             }
             // 检测邮箱是否注册
             $user = UserModel::getUserByEmail($email);
             if (!$user) {
                 throw new \Exception("邮箱没有注册");
             }
             // 发送邮件
             $transport = \Swift_SmtpTransport::newInstance('smtp.163.com')->setUsername('*****@*****.**')->setPassword('llw132255');
             $mailer = \Swift_Mailer::newInstance($transport);
             $message = \Swift_Message::newInstance();
             $message->setSubject('重置密码')->setFrom('*****@*****.**')->setTo($email)->setBody('这是一封测试邮件', 'text/html');
             $mailer->send($message);
             $modal_content = $this->render('modal.html.twig', array('title' => '邮件发送成功', 'msg' => '<p class="text-success">邮件发送成功</p>', 'jump_link' => $this->generateUrl('forum_homepage')));
             return new JsonResponse(array('status' => 1, 'modal' => $modal_content->getContent()));
         } catch (\Exception $e) {
             $modal_content = $this->render('modal.html.twig', array('title' => '邮件发送失败', 'msg' => '<p class="text-danger">发生错误:' . $e->getMessage() . '</p>'));
             return new JsonResponse(array('status' => 0, 'modal' => $modal_content->getContent()));
         }
     }
     return $this->render('user/password.html.twig');
 }