Exemplo n.º 1
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $session = $this->getSession();
         try {
             $username = $posts->get('username');
             $email = $posts->get('email');
             $mobile = $posts->get('mobile');
             $password = $posts->get('password');
             $confirm_password = $posts->get('confirm_password');
             if (!$username) {
                 throw new \Exception('用户名不能为空');
             }
             if (strlen($username) < 2) {
                 throw new \Exception('用户名至少2个字符');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByUsername($username);
             if ($user) {
                 throw new \Exception('用户名已被占用');
             }
             if (!$email) {
                 throw new \Exception('邮箱不能为空');
             }
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 throw new \Exception('邮箱地址格式错误');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByEmail($email);
             if ($user) {
                 throw new \Exception('邮箱已被占用');
             }
             if (!$mobile) {
                 throw new \Exception('手机号不能为空');
             }
             // 检查重复
             $user = UserAuthModel::getAuthByMobile($mobile);
             if ($user) {
                 throw new \Exception('手机号已被占用');
             }
             // 密码
             if (!$password) {
                 throw new \Exception('密码不能为空');
             }
             if (strlen($password) < 6) {
                 throw new \Exception('密码至少6位字符');
             }
             if (!$confirm_password) {
                 throw new \Exception('确认密码不能为空');
             }
             if ($password != $confirm_password) {
                 throw new \Exception('密码与确认密码不一致');
             }
             // 创建
             $auth = new UserAuthModel();
             $auth->username = $username;
             $auth->email = $email;
             $auth->mobile = $mobile;
             $auth->password = md5($password);
             $now = time();
             $auth->createTimestamp = $now;
             $auth->updateTimestamp = $now;
             // 后台添加的为可用状态
             $auth->status = UserAuthModel::STATUS_OK;
             // 保存
             UserAuthModel::createAuth($auth);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_user_index'));
     }
     return $this->render('user/add.html.twig');
 }