完善用户中心功能

This commit is contained in:
2020-04-15 14:56:30 +08:00
parent 906dccda61
commit 10b40f0071
14 changed files with 370 additions and 16 deletions

View File

@@ -16,9 +16,11 @@ use app\model\Form;
class Base extends BaseC {
protected $outAuthUrl = ['user/index/login', 'user/index/logout', 'user/index/verify', 'user/index/register', 'user/index/forget', 'user/index/resetpasswd'];
protected function initialize() {
$url = str_replace(".", "/", strtolower($this->request->controller())) . '/' . $this->request->action();
if (!is_login() && !in_array($url, array('user/index/login', 'user/index/logout', 'user/index/verify', 'user/index/register'))) {
if (!is_login() && !in_array($url, $this->outAuthUrl)) {
$this->redirect('/user/index/login');
}

View File

@@ -8,6 +8,9 @@
// +----------------------------------------------------------------------
namespace app\controller\user;
use app\model\Member;
use think\facade\Session;
/**
* @title 用户中心
*/
@@ -26,7 +29,19 @@ class Index extends Base {
* @return [type] [description]
*/
public function login() {
return $this->fetch();
if ($this->request->isAjax()) {
try {
$userinfo = (new Member())->login($this->request);
if ($userinfo) {
Session::set('userInfo', $userinfo);
return $this->success('登录成功!', url('/user/index/index'));
}
} catch (Exception $e) {
return $this->error($e->getError(), '');
}
}else{
return $this->fetch();
}
}
/**
@@ -34,7 +49,8 @@ class Index extends Base {
* @return [type] [description]
*/
public function logout() {
return $this->fetch();
Session::delete('userInfo');
$this->redirect('/user/index/login');
}
/**
@@ -42,7 +58,16 @@ class Index extends Base {
* @return [type] [description]
*/
public function register() {
return $this->fetch();
if ($this->request->isAjax()) {
$result = (new Member())->register($this->request);
if (false !== $result) {
return $this->success("注册成功!", url('/user/index/login'));
}else{
return $this->error("注册失败!");
}
}else{
return $this->fetch();
}
}
/**
@@ -50,7 +75,30 @@ class Index extends Base {
* @return [type] [description]
*/
public function forget() {
return $this->fetch();
if ($this->request->isAjax()) {
$data = $this->request->post();
$map = [];
if (!$data['username'] || !$data['email']) {
return $this->error("请完整填写信息!");
}
$map[] = ['username', '=', $data['username']];
$map[] = ['email', '=', $data['email']];
$user = Member::where($map)->findOrEmpty();
if (!$user->isEmpty()) {
//发生重置密码连接电子邮件
$result = Member::sendFindPaswd($user);
if (false !== $result) {
return $this->success("已发送找回密码邮件!", url('/user/index/login'));
}else{
return $this->error("发送邮件失败!");
}
}else{
return $this->error('无此用户!');
}
}else{
return $this->fetch();
}
}
/**
@@ -58,6 +106,33 @@ class Index extends Base {
* @return [type] [description]
*/
public function resetpasswd() {
return $this->fetch();
if ($this->request->isAjax()) {
$token = $this->request->get('token');
$data = $this->request->post();
list($username, $email) = explode("|", \xin\helper\Secure::decrypt($token, \think\facade\Env::get('jwt.secret')));
if (!$username || !$email) {
return $this->error("找回密码地址错误或已过期!");
}
$map[] = ['username', '=', $username];
$map[] = ['email', '=', $email];
$user = Member::where($map)->findOrEmpty();
if (!$user->isEmpty()) {
$data['salt'] = \xin\helper\Str::random(6);
$result = Member::update($data, ['uid' => $user['uid']]);
if (false !== $result) {
return $this->success("已重置!", url('/user/index/login'));
}else{
return $this->error("发送邮件失败!");
}
}else{
return $this->error('无此用户!');
}
}else{
$token = $this->request->param('token');
return $this->fetch();
}
}
}

View File

@@ -20,8 +20,8 @@ class User extends Base {
*/
public function profile() {
if ($this->request->isPost()) {
$reuslt = (new Member())->editUser($this->request, session('userInfo.uid'));
if (false !== $reuslt) {
$result = (new Member())->editUser($this->request, session('userInfo.uid'));
if (false !== $result) {
return $this->success('修改成功!');
} else {
return $this->error('修改失败');
@@ -41,7 +41,29 @@ class User extends Base {
* @return [type] [description]
*/
public function repasswd() {
return $this->fetch();
if ($this->request->isAjax()) {
$data = $this->request->post();
$user = Member::where('uid', $data['uid'])->findOrEmpty();
if (!$user->isEmpty()) {
if (md5($data['oldpassword'] . $user['salt']) !== $user['password']) {
return $this->error('旧密码不正确!');
}
$data['salt'] = \xin\helper\Str::random(6);
$result = $user->save($data);
if (false !== $result) {
return $this->success('修改成功!');
} else {
return $this->error('修改失败');
}
}else{
return $this->error('无此用户!');
}
}else{
return $this->fetch();
}
}
/**