Пример #1
0
 /**
  * 修改密码处理.
  *
  * @access pubic
  * @return void
  */
 public function changePwdAction()
 {
     $userName = $this->getLoginedUserInfo('userName');
     $oldPwd = I('post.oldpwd', '');
     $newPwd = I('post.newpwd', '');
     $verifyNewPwd = I('post.verifyNewpwd', '');
     // 确认新密码.
     $data = ['userName' => $userName, 'userOldPasswd' => $oldPwd, 'userNewPasswd' => $newPwd, 'verifyNewPwd' => $verifyNewPwd];
     $modelUser = new UserModel();
     // 校验用户名和密码规则.
     if (!$modelUser->validate($modelUser->ruleChangePwd)->create($data)) {
         $this->setAjaxData(Message::FAILED, $modelUser->getError())->myAjaxReturn();
     }
     // 校验旧密码.
     $res = $modelUser->checkLogin($userName, $this->encryptMyPwd($oldPwd));
     if (FALSE === $res) {
         $this->setAjaxData(Message::FAILED, '旧密码错误')->myAjaxReturn();
     }
     // 旧密码验证通过, 重置新密码.
     $res = $modelUser->resetPwd($userName, $this->encryptMyPwd($newPwd));
     if ($res) {
         $result = $this->_logOff();
         // 修改密码后退出登录状态.
         $this->setAjaxData(Message::SUCCESS, '修改密码成功', $result)->myAjaxReturn();
     }
     $this->setAjaxData(Message::SUCCESS, '修改密码失败')->myAjaxReturn();
 }
Пример #2
0
 /**
  * 用户注册处理.
  * 
  * @access public
  * @return void
  */
 public function regAction()
 {
     $userName = I('post.username', '');
     $userEmail = I('post.useremail', '');
     $userPwd = I('post.userpass', '');
     $code = I('post.code', '');
     $modelUser = new UserModel();
     // 判断注册用户名是否存在.
     if (!$modelUser->validate($modelUser->ruleRegUserName)->create(['userName' => $userName])) {
         $this->setAjaxData(Message::USERNAME_OR_EMAIL_EXISTS, $modelUser->getError())->myAjaxReturn();
     }
     // 判断注册邮箱是否存在.
     if (!$modelUser->validate($modelUser->ruleRegEmail)->create(['email' => $userEmail])) {
         $this->setAjaxData(Message::USERNAME_OR_EMAIL_EXISTS, $modelUser->getError())->myAjaxReturn();
     }
     // 判断验证码是否正确.
     if (empty($code)) {
         $this->setAjaxData(Message::FAILED, '验证码不能为空')->myAjaxReturn();
     }
     $res = $this->_checkEmailCode($userEmail, $code);
     // 校验验证码.
     if (9 !== $res['status']) {
         // 验证码错误.
         $this->setAjaxData(Message::FAILED, $res['msg'])->myAjaxReturn();
     }
     if (!$modelUser->validate($modelUser->ruleRegUserInfos)->create(['userPasswd' => $userPwd])) {
         $this->setAjaxData(Message::FAILED, $modelUser->getError())->myAjaxReturn();
     }
     // 开始注册过程.
     $data = [];
     $data['groupID'] = 2;
     // 2: 表示普通用户, 参见 ybrids_group 表.
     $data['userName'] = $userName;
     $data['userPasswd'] = $this->encryptMyPwd($userPwd);
     $data['userType'] = 0;
     // 用户类型,0为普通用户,1为系统管理员(默认值为0)
     $data['email'] = $userEmail;
     $data['emailStatus'] = 1;
     // email是否经过验证, 0:未验证,1:验证,默认为0
     $data['regDate'] = time();
     $data['regIP'] = get_client_ip();
     $data['reg_source'] = $this->getAppName();
     // 注册来源.
     $uid = $modelUser->register($data);
     // 注册.
     if (FALSE !== $uid) {
         // 注册成功置为登录状态.
         $loginInfo = [];
         $loginInfo['userID'] = $uid;
         $loginInfo['lastLoginDate'] = $data['regDate'];
         $loginInfo['lastLoginIP'] = get_client_ip();
         $loginInfo['lastSendEmail'] = time();
         $loginInfo['emailToken'] = data_auth_sign($loginInfo);
         $modelLoginInfo = new UserLoginInfoModel();
         $res = $modelLoginInfo->updateLoginInfo($uid, $loginInfo);
         // 写入用户信息到 token.
         $userInfo = ['uid' => $uid, 'userName' => $userName, 'userType' => 0, 'email' => $userEmail, 'lastLoginDate' => $loginInfo['lastLoginDate'], 'lastLoginIP' => $loginInfo['lastLoginIP'], 'avatar' => ''];
         $tokenData = [];
         $tokenData = [C('var_is_logined') => TRUE, C('var_logined_user_info') => $userInfo];
         unset($data, $loginInfo, $userInfo);
         $newToken = $this->reNewToken($tokenData, $this->getRequestToken());
         // 生成新的 token.
         // 注册成功并返回新的 token 和 用户 id.
         $this->setAjaxData(Message::SUCCESS, '注册成功', [C('var_refresh_token') => $newToken, 'userId' => $uid, 'avatar' => '', 'per_sign' => '', 'nickname' => $userName])->myAjaxReturn();
     } else {
         $this->setAjaxData(Message::FAILED, '注册失败')->myAjaxReturn();
     }
 }