179 lines
4.0 KiB
PHP
Raw Normal View History

2019-12-06 08:24:07 +08:00
<?php
namespace catchAdmin\user\controller;
2019-12-07 17:31:38 +08:00
use app\Request;
2019-12-11 20:59:59 +08:00
use catchAdmin\permissions\model\Roles;
2019-12-22 09:37:52 +08:00
use catchAdmin\user\Auth;
2019-12-06 08:24:07 +08:00
use catchAdmin\user\model\Users;
use catchAdmin\user\request\CreateRequest;
use catchAdmin\user\request\UpdateRequest;
2019-12-11 20:59:59 +08:00
use catcher\base\CatchController;
2019-12-06 08:24:07 +08:00
use catcher\CatchResponse;
2019-12-11 20:59:59 +08:00
use catcher\Tree;
2019-12-06 08:24:07 +08:00
2019-12-11 20:59:59 +08:00
class User extends CatchController
2019-12-06 08:24:07 +08:00
{
protected $user;
public function __construct(Users $user)
{
$this->user = $user;
}
/**
*
* @time 2019年12月04日
2019-12-22 09:37:52 +08:00
* @param Request $request
2019-12-06 08:24:07 +08:00
* @return string
2019-12-22 09:37:52 +08:00
* @throws \think\db\exception\DbException
2019-12-06 08:24:07 +08:00
*/
2019-12-22 09:37:52 +08:00
public function index(Request $request)
2019-12-07 17:31:38 +08:00
{
2019-12-22 09:37:52 +08:00
return CatchResponse::paginate($this->user->getList($request->param()));
2019-12-07 17:31:38 +08:00
}
2019-12-22 09:37:52 +08:00
public function info()
2019-12-07 17:31:38 +08:00
{
2019-12-22 09:37:52 +08:00
return CatchResponse::success(Auth::getUserInfo());
2019-12-07 17:31:38 +08:00
}
/**
*
* @time 2019年12月06日
* @throws \Exception
* @return string
*/
public function create()
2019-12-22 14:18:21 +08:00
{}
2019-12-06 08:24:07 +08:00
/**
*
* @param CreateRequest $request
2019-12-07 17:31:38 +08:00
* @time 2019年12月06日
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-06 08:24:07 +08:00
*/
2019-12-07 17:31:38 +08:00
public function save(CreateRequest $request)
2019-12-06 08:24:07 +08:00
{
2019-12-12 09:13:53 +08:00
$this->user->storeBy($request->post());
2019-12-11 20:59:59 +08:00
if (!empty($request->param('roleids'))) {
$this->user->attach($request->param('roleids'));
}
2019-12-12 09:13:53 +08:00
2019-12-11 20:59:59 +08:00
return CatchResponse::success();
2019-12-06 08:24:07 +08:00
}
/**
*
* @time 2019年12月04日
* @param $id
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-06 08:24:07 +08:00
*/
public function read($id)
{
return CatchResponse::success($this->user->findBy($id));
}
2019-12-20 22:15:45 +08:00
/**
* @param $id
* @return string
* @throws \Exception
*/
2019-12-22 14:18:21 +08:00
public function edit($id){}
2019-12-06 08:24:07 +08:00
/**
*
* @time 2019年12月04日
* @param $id
* @param UpdateRequest $request
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-06 08:24:07 +08:00
*/
public function update($id, UpdateRequest $request)
{
2019-12-12 09:13:53 +08:00
$this->user->updateBy($id, $request->post());
$user = $this->user->findBy($id);
$user->detach();
if (!empty($request->param('roleids'))) {
$user->attach($request->param('roleids'));
}
return CatchResponse::success();
2019-12-06 08:24:07 +08:00
}
/**
*
* @time 2019年12月04日
* @param $id
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-06 08:24:07 +08:00
*/
public function delete($id)
{
2019-12-12 09:13:53 +08:00
// 删除角色
$this->user->findBy($id)->detach();
$this->user->deleteBy($id);
return CatchResponse::success();
2019-12-06 08:24:07 +08:00
}
2019-12-07 17:31:38 +08:00
/**
*
* @time 2019年12月07日
* @param $id
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-07 17:31:38 +08:00
*/
2019-12-20 22:15:45 +08:00
public function switchStatus($id): Json
2019-12-07 17:31:38 +08:00
{
$user = $this->user->findBy($id);
return CatchResponse::success($this->user->updateBy($id, [
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
]));
}
/**
*
* @time 2019年12月07日
* @param $id
2019-12-20 22:15:45 +08:00
* @return Json
2019-12-07 17:31:38 +08:00
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
2019-12-20 22:15:45 +08:00
public function recover($id): Json
2019-12-07 17:31:38 +08:00
{
$trashedUser = $this->user->findBy($id, ['*'], true);
if ($this->user->where('email', $trashedUser->email)->find()) {
return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
}
return CatchResponse::success($this->user->recover($id));
}
2019-12-11 20:59:59 +08:00
/**
*
* @time 2019年12月11日
* @param Request $request
* @param Roles $roles
* @return \think\response\Json
*/
public function getRoles(Request $request, Roles $roles): \think\response\Json
{
$roles = Tree::done($roles->getList());
$roleIds = [];
if ($request->param('uid')) {
$userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
foreach ($userHasRoles as $role) {
$roleIds[] = $role->pivot->role_id;
}
}
return CatchResponse::success([
'roles' => $roles,
'hasRoles' => $roleIds,
]);
}
2019-12-06 08:24:07 +08:00
}