/** * 注册方法 * 完成数据的插入 * @return boolean whether register is successful */ public function register() { //将已经验证成功的数据插入数据库 if (!$this->hasErrors()) { $user = new Users(); $user->username = $this->username; $user->email = $this->email; $user->salt = Help::fetchSalt(); $user->password = md5(md5($this->password) . $user->salt); $user->reg_ip = Yii::app()->request->userHostAddress; $user->last_ip = Yii::app()->request->userHostAddress; $user->reg_time = time(); $user->last_login = time(); if (!$user->save()) { return false; } } //实行登入操作 if ($this->_identity === null) { $this->_identity = new UserIdentity($this->email, $this->password); $this->_identity->authenticate(); } if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { Yii::app()->user->login($this->_identity); return true; } else { return false; } }
/** * 更改账户信息 */ public function actionCheckAccount() { //var_dump($_POST); //exit(); if (!Yii::app()->request->isPostRequest) { $this->error('一定是你的提交方式不对'); } $data = array(); if (!isset($_POST['username']) || !isset($_POST['sex']) || !isset($_POST['email'])) { $this->error('相关字段不能为空'); } $data['username'] = $_POST['username']; $data['sex'] = $_POST['sex']; $data['mobile'] = $_POST['mobile']; //判断用户名是否已经存在 if (Users::model()->exists('uid <> :uid AND username=:username', array(':uid' => Yii::app()->user->id, ':username' => $data['username']))) { $this->error('用户名已经存在'); return; } //判断是否需要改密码 if ($_POST['old_password'] != '' && $_POST['re_password'] != '' && $_POST['password'] != '') { //密码长度不能小于4 if (strlen($_POST['password']) < 4) { $this->error('密码长度不能小于4'); } //判断两次密码输入是否正确 if ($_POST['password'] != $_POST['re_password']) { $this->error('两次密码输入不相同'); } //判断初始密码是否正确 $user = Users::model()->find('uid=? AND password=MD5(CONCAT(MD5(?),`salt`))', array(Yii::app()->user->id, $_POST['old_password'])); if (!$user) { $this->error('初始密码输入不正确!'); } //可以更新密码 $data['salt'] = Help::fetchSalt(); $data['password'] = md5(md5(trim($_POST['password'])) . $data['salt']); } //头像更新 //更新头像 $image = Help::uploadAvatar(Yii::app()->user->id, 'avatar'); if ($image) { $data['avatar_file'] = $image; //更新头像session // } if (false !== Users::model()->updateByPk(Yii::app()->user->id, $data)) { //更新 userInfo 信息 $user = Users::model()->findByPk(Yii::app()->user->id); Yii::app()->user->setState('userInfo', $user); $this->success('更新成功'); } }
/** * 处理添加用户 */ public function actionCheckAdd() { if (!Yii::app()->request->isPostRequest) { $this->error('一定是您的访问方式不对'); } //var_dump($_POST); //判断用户名或email是否已经存在 if (Users::model()->exists('username=? or email=?', array($_POST['username'], $_POST['email']))) { $this->error('用户名或密码已经存在!'); } //首先插入数据获取uid $user = new Users(); $user->username = $_POST['username']; $user->email = $_POST['email']; //生日更新 $birthday = $_POST['birthday']; $year = substr($birthday, 0, 4); $month = substr($birthday, 5, 2); $day = substr($birthday, 8, 2); $user->birthday = mktime(0, 0, 0, $month, $day, $year); $user->mobile = $_POST['mobile']; $user->sex = $_POST['sex']; $user->salt = Help::fetchSalt(); $user->password = md5(md5($_POST['password']) . $user->salt); $user->reg_time = time(); $user->reg_ip = Yii::app()->request->userHostAddress; $user->last_login = time(); $user->last_ip = Yii::app()->request->userHostAddress; if ($user->save()) { $this->success('添加成功!'); } else { $this->error(var_dump($user->errors)); } }