215 lines
5.1 KiB
PHP
Raw Normal View History

2019-12-06 08:24:07 +08:00
<?php
namespace catchAdmin\user\controller;
2020-01-08 22:02:25 +08:00
use catcher\base\CatchRequest as Request;
2020-01-07 17:27:55 +08:00
use catchAdmin\permissions\model\Permissions;
2019-12-11 20:59:59 +08:00
use catchAdmin\permissions\model\Roles;
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;
2020-01-07 17:27:55 +08:00
use catcher\CatchAuth;
2019-12-06 08:24:07 +08:00
use catcher\CatchResponse;
2019-12-11 20:59:59 +08:00
use catcher\Tree;
2019-12-26 09:03:09 +08:00
use catcher\Utils;
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
}
2020-01-07 17:27:55 +08:00
/**
* 获取用户信息
*
* @time 2020年01月07日
* @param CatchAuth $auth
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\response\Json
*/
public function info(CatchAuth $auth)
2019-12-07 17:31:38 +08:00
{
2020-01-07 19:00:04 +08:00
2020-01-07 17:27:55 +08:00
$user = $auth->user();
$roles = $user->getRoles();
$user->permissions = Permissions::whereIn('id', $user->getPermissionsBy())
->field(['permission_name as title', 'route', 'icon'])
->select();
$user->roles = $roles;
return CatchResponse::success($user);
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-24 18:21:57 +08:00
* @return \think\response\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());
2020-01-08 22:02:25 +08:00
2019-12-27 09:52:03 +08:00
$this->user->attach($request->param('roles'));
2019-12-12 09:13:53 +08:00
2019-12-24 18:21:57 +08:00
return CatchResponse::success('', '添加成功');
2019-12-06 08:24:07 +08:00
}
/**
*
* @time 2019年12月04日
* @param $id
2019-12-24 18:21:57 +08:00
* @return \think\response\Json
2019-12-06 08:24:07 +08:00
*/
public function read($id)
{
2019-12-27 09:52:03 +08:00
$user = $this->user->findBy($id);
$user->roles = $user->getRoles();
return CatchResponse::success($user);
2019-12-06 08:24:07 +08:00
}
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-24 18:21:57 +08:00
* @return \think\response\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();
2019-12-27 09:52:03 +08:00
if (!empty($request->param('roles'))) {
$user->attach($request->param('roles'));
2019-12-12 09:13:53 +08:00
}
return CatchResponse::success();
2019-12-06 08:24:07 +08:00
}
/**
*
* @time 2019年12月04日
* @param $id
2019-12-24 18:21:57 +08:00
* @return \think\response\Json
2019-12-06 08:24:07 +08:00
*/
public function delete($id)
{
2019-12-26 09:03:09 +08:00
$ids = Utils::stringToArrayBy($id);
2019-12-12 09:13:53 +08:00
2019-12-26 09:03:09 +08:00
foreach ($ids as $_id) {
// 删除角色
$this->user->findBy($_id)->detach();
$this->user->deleteBy($_id);
}
2019-12-12 09:13:53 +08:00
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-24 18:21:57 +08:00
* @return \think\response\Json
2019-12-07 17:31:38 +08:00
*/
2019-12-24 18:21:57 +08:00
public function switchStatus($id): \think\response\Json
2019-12-07 17:31:38 +08:00
{
2019-12-26 09:03:09 +08:00
$ids = Utils::stringToArrayBy($id);
foreach ($ids as $_id) {
$user = $this->user->findBy($_id);
$this->user->updateBy($_id, [
2019-12-07 17:31:38 +08:00
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
2019-12-26 09:03:09 +08:00
]);
}
return CatchResponse::success([], '操作成功');
2019-12-07 17:31:38 +08:00
}
/**
*
* @time 2019年12月07日
* @param $id
2019-12-24 18:21:57 +08:00
* @return \think\response\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-24 18:21:57 +08:00
public function recover($id): \think\response\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-24 18:21:57 +08:00
}