230 lines
5.6 KiB
PHP
Raw Normal View History

2019-12-06 08:24:07 +08:00
<?php
2020-04-22 05:53:38 +08:00
namespace catchAdmin\permissions\controller;
2019-12-06 08:24:07 +08:00
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;
2020-01-17 11:29:17 +08:00
use catcher\CatchCacheKeys;
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;
2020-01-17 11:29:17 +08:00
use think\facade\Cache;
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 17:27:55 +08:00
$user = $auth->user();
$roles = $user->getRoles();
2020-04-21 22:19:10 +08:00
$permissionIds = $user->getPermissionsBy($user->id);
2020-01-17 11:29:17 +08:00
// 缓存用户权限
Cache::set(CatchCacheKeys::USER_PERMISSIONS . $user->id, $permissionIds);
$user->permissions = Permissions::getCurrentUserPermissions($permissionIds);
2020-01-07 17:27:55 +08:00
$user->roles = $roles;
2020-01-14 18:31:18 +08:00
// 用户数据权限
// $user->data_range = Roles::getDepartmentUserIdsBy($roles);
2020-01-14 08:23:15 +08:00
2020-01-07 17:27:55 +08:00
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-09 08:21:59 +08:00
2019-12-27 09:52:03 +08:00
$this->user->attach($request->param('roles'));
2019-12-12 09:13:53 +08:00
2020-01-12 09:30:56 +08:00
$this->user->attachJobs($request->param('jobs'));
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();
2020-01-12 09:30:56 +08:00
$user->jobs = $user->getJobs();
2019-12-27 09:52:03 +08:00
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();
2020-01-12 09:30:56 +08:00
$user->detachJobs();
2019-12-12 09:13:53 +08:00
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
}
2020-01-12 09:30:56 +08:00
if (!empty($request->param('jobs'))) {
$user->attachJobs($request->param('jobs'));
}
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) {
2020-01-12 09:30:56 +08:00
$user = $this->user->findBy($_id);
2019-12-26 09:03:09 +08:00
// 删除角色
2020-01-12 09:30:56 +08:00
$user->detach();
// 删除岗位
$user->detachJobs();
2019-12-26 09:03:09 +08:00
$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
}