Skip to content

Commit

Permalink
UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
edenleung committed Mar 6, 2021
1 parent 2be8976 commit 7f0e6e2
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 298 deletions.
56 changes: 56 additions & 0 deletions app/admin/controller/system/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,60 @@ public function data(RoleService $role, DeptService $dept)
]
);
}

public function current()
{
return $this->sendSuccess(request()->user);
}

public function updateCurrent()
{
$data = $this->request->post();
if (empty($data)) {
return $this->sendError('数据出错');
}

if (!$this->service->updateCurrent($this->request->user, $data)) {
return $this->sendError('更新失败');
}

return $this->sendSuccess(null, '已更新个人信息');
}

/**
* 更新头像.
*
* @return \think\Response
*/
public function avatar()
{
$file = $this->request->file('file');
$savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
if (!$this->service->updateAvatar($this->request->user, $savename)) {
return $this->sendError('更新失败');
}

return $this->sendSuccess($this->request->user->avatar, '已成功更换头像');
}

/**
* 修改密码
*
* @return \think\Response
*/
public function resetPassword()
{
$oldPassword = $this->request->post('oldPassword');
$newPassword = $this->request->post('newPassword');

if (!$oldPassword || !$newPassword) {
return $this->sendError('数据出错');
}

if (!$this->service->resetPassword($this->request->user, $oldPassword, $newPassword)) {
return $this->sendError($this->service->getError());
}

return $this->sendSuccess(null, '修改成功');
}
}
5 changes: 5 additions & 0 deletions app/common/model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function can($source, $action)
return true;
}

public function super()
{
return $this->id === 1;
}

public function roles()
{
return $this->hasMany(Rule::class, 'v0', 'username')->where('ptype', 'g');
Expand Down
42 changes: 42 additions & 0 deletions app/common/service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function info()

$this->model->menus = $this->formatRoute($menus);

$this->model->is_super = $this->model->super();

return $this->model;
}

Expand Down Expand Up @@ -213,4 +215,44 @@ public function view($id)

return $user;
}

/**
* 更新个人信息.
*
* @return bool
*/
public function updateCurrent(User $user, array $data)
{
return $user->save($data);
}

/**
* 更新头像.
*
* @return bool
*/
public function updateAvatar(User $user, string $path)
{
$user->avatar = 'storage' . \DIRECTORY_SEPARATOR . $path;

return $user->save();
}

/**
* 修改密码
*
* @return bool
*/
public function resetPassword(User $user, string $oldPassword, string $newPassword)
{
if (password_verify($oldPassword, $user->password)) {
$this->error = '原密码不正确';

return false;
}

$user->password = password_hash($newPassword, PASSWORD_DEFAULT);

return $user->save();
}
}
Loading

0 comments on commit 7f0e6e2

Please sign in to comment.