245 lines
6.3 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\permissions\controller;
2020-09-08 14:12:42 +08:00
use catchAdmin\permissions\excel\UserExport;
2020-04-29 17:37:45 +08:00
use catcher\base\CatchRequest as Request;
use catchAdmin\permissions\model\Permissions;
use catchAdmin\permissions\model\Roles;
use catchAdmin\permissions\model\Users;
use catchAdmin\permissions\request\CreateRequest;
use catchAdmin\permissions\request\UpdateRequest;
use catchAdmin\permissions\request\ProfileRequest;
2020-04-29 17:37:45 +08:00
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchCacheKeys;
use catcher\CatchResponse;
2020-09-08 14:12:42 +08:00
use catcher\library\excel\Excel;
2020-04-29 17:37:45 +08:00
use catcher\Tree;
use catcher\Utils;
use think\facade\Cache;
class User extends CatchController
{
protected $user;
public function __construct(Users $user)
{
$this->user = $user;
}
/**
*
* @time 2020年04月24日
* @throws \think\db\exception\DbException
* @return \think\response\Json
*/
2020-05-22 15:21:05 +08:00
public function index()
2020-04-29 17:37:45 +08:00
{
2020-05-22 15:21:05 +08:00
return CatchResponse::paginate($this->user->getList());
2020-04-29 17:37:45 +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)
{
$user = $auth->user();
2020-09-03 18:55:25 +08:00
$roles = $user->getRoles()->column('identify');
2020-04-29 17:37:45 +08:00
$permissionIds = $user->getPermissionsBy($user->id);
// 缓存用户权限
Cache::set(CatchCacheKeys::USER_PERMISSIONS . $user->id, $permissionIds);
$user->permissions = Permissions::getCurrentUserPermissions($permissionIds);
$user->roles = $roles;
// 用户数据权限
// $user->data_range = Roles::getDepartmentUserIdsBy($roles);
return CatchResponse::success($user);
}
/**
*
* @param CreateRequest $request
* @time 2019年12月06日
* @return \think\response\Json
*/
public function save(CreateRequest $request)
{
2020-05-06 22:16:55 +08:00
$this->user->storeBy($request->param());
2020-04-29 17:37:45 +08:00
2020-07-24 21:40:26 +08:00
$this->user->attachRoles($request->param('roles'));
2020-04-29 17:37:45 +08:00
2020-09-04 10:09:26 +08:00
if ($request->param('jobs')) {
$this->user->attachJobs($request->param('jobs'));
}
2020-04-29 17:37:45 +08:00
return CatchResponse::success('', '添加成功');
}
/**
*
* @time 2019年12月04日
* @param $id
* @return \think\response\Json
*/
public function read($id)
{
$user = $this->user->findBy($id);
$user->roles = $user->getRoles();
$user->jobs = $user->getJobs();
return CatchResponse::success($user);
}
/**
*
* @time 2019年12月04日
* @param $id
* @param UpdateRequest $request
* @return \think\response\Json
*/
public function update($id, UpdateRequest $request)
{
2021-01-17 09:39:18 +08:00
$this->user->updateBy($id, $request->filterEmptyField()->param());
2020-04-29 17:37:45 +08:00
$user = $this->user->findBy($id);
2020-07-24 21:40:26 +08:00
$user->detachRoles();
2020-04-29 17:37:45 +08:00
$user->detachJobs();
if (!empty($request->param('roles'))) {
2020-07-24 21:40:26 +08:00
$user->attachRoles($request->param('roles'));
2020-04-29 17:37:45 +08:00
}
if (!empty($request->param('jobs'))) {
$user->attachJobs($request->param('jobs'));
}
return CatchResponse::success();
}
/**
*
* @time 2019年12月04日
* @param $id
* @return \think\response\Json
*/
public function delete($id)
{
$ids = Utils::stringToArrayBy($id);
foreach ($ids as $_id) {
$user = $this->user->findBy($_id);
// 删除角色
2020-07-24 21:40:26 +08:00
$user->detachRoles();
2020-04-29 17:37:45 +08:00
// 删除岗位
$user->detachJobs();
$this->user->deleteBy($_id);
}
return CatchResponse::success();
}
/**
*
* @time 2019年12月07日
* @param $id
* @return \think\response\Json
*/
public function switchStatus($id): \think\response\Json
{
$ids = Utils::stringToArrayBy($id);
foreach ($ids as $_id) {
$user = $this->user->findBy($_id);
$this->user->updateBy($_id, [
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
]);
}
return CatchResponse::success([], '操作成功');
}
/**
*
* @time 2019年12月07日
* @param $id
* @return \think\response\Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function recover($id): \think\response\Json
{
$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));
}
/**
*
* @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,
]);
}
2020-09-08 14:12:42 +08:00
/**
* 导出
*
* @time 2020年09月08日
* @param Excel $excel
* @param UserExport $userExport
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @return \think\response\Json
*/
public function export(Excel $excel, UserExport $userExport)
{
return CatchResponse::success($excel->save($userExport, Utils::publicPath('export/users')));
}
2020-09-21 08:06:24 +08:00
/**
* 更新个人信息
*
* @time 2020年09月20日
* @param ProfileRequest $request
2020-09-21 08:06:24 +08:00
* @return \think\response\Json
*/
public function profile(ProfileRequest $request)
2020-09-21 08:06:24 +08:00
{
return CatchResponse::success($this->user->updateBy($request->user()->id, $request->param()));
}
2020-04-29 17:37:45 +08:00
}