Exemplo n.º 1
0
 private function handleAuth()
 {
     $auth_id = $this->id;
     $user_auth = UserAuthModel::getAuth($auth_id);
     if (!$user_auth) {
         throw new \Exception('用户不存在');
     }
     $request = $this->getRequest();
     $posts = $request->request;
     $username = $posts->get('username');
     $email = $posts->get('email');
     $mobile = $posts->get('mobile');
     if (!$username) {
         throw new \Exception('用户名不能为空');
     }
     if (strlen($username) < 2) {
         throw new \Exception('用户名至少2个字符');
     }
     // 检查重复
     $user = UserAuthModel::oneUser(function (QueryBuilder $qb) use($username, $auth_id) {
         $qb->andWhere($qb->expr()->eq('username', ':username'))->setParameter(':username', $username);
         $qb->andWhere($qb->expr()->neq('id', ':id'))->setParameter(':id', $auth_id);
     });
     if ($user) {
         throw new \Exception('用户名已被占用');
     }
     if (!$email) {
         throw new \Exception('邮箱不能为空');
     }
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         throw new \Exception('邮箱地址格式错误');
     }
     // 检查重复
     $user = UserAuthModel::oneUser(function (QueryBuilder $qb) use($email, $auth_id) {
         $qb->andWhere($qb->expr()->eq('email', ':email'))->setParameter(':email', $email);
         $qb->andWhere($qb->expr()->neq('id', ':id'))->setParameter(':id', $auth_id);
     });
     if ($user) {
         throw new \Exception('邮箱已被占用');
     }
     if (!$mobile) {
         throw new \Exception('手机号不能为空');
     }
     // 检查重复
     $user = UserAuthModel::oneUser(function (QueryBuilder $qb) use($mobile, $auth_id) {
         $qb->andWhere($qb->expr()->eq('mobile', ':mobile'))->setParameter(':mobile', $mobile);
         $qb->andWhere($qb->expr()->neq('id', ':id'))->setParameter(':id', $auth_id);
     });
     if ($user) {
         throw new \Exception('手机号已被占用');
     }
     $user_auth->username = $username;
     $user_auth->email = $email;
     $user_auth->mobile = $mobile;
     // 保存
     UserAuthModel::saveAuth($user_auth);
 }