This commit is contained in:
yaooo 2023-12-11 11:31:56 +08:00
commit 5cb47248da
7 changed files with 164 additions and 87 deletions

View File

@ -134,4 +134,69 @@ class AdminController extends BaseAdminController
$result = AdminLogic::editSelf($params);
return $this->success('操作成功', [], 1, 1);
}
//获取所有人员
public function getAdminsByAll(): \think\response\Json
{
$data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->select()->each(function($item){
$job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty();
$dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty();
$org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty();
$item['job_name'] = $job->isEmpty() ? '' : $job['name'];
$item['dept_name'] = $dept->isEmpty() ? '' : $dept['name'];
$item['org_name'] = $org->isEmpty() ? '' : $org['name'];
unset($item['org_id'],$item['dept_id'],$item['job_id']);
return $item;
})->toArray();
foreach($data as $k=>$v){
unset($data[$k]['role_id']);
}
return $this->success('请求成功',$data);
}
//获取某个部门下的所有人员
public function getAdminsByDept(): \think\response\Json
{
$dept_id = $this->request->get('dept_id');
if(empty($dept_id)){
return $this->fail('参数错误');
}
$data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->where('dept_id',$dept_id)->select()->each(function($item){
$job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty();
$dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty();
$org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty();
$item['job_name'] = $job->isEmpty() ? '' : $job['name'];
$item['dept_name'] = $dept->isEmpty() ? '' : $dept['name'];
$item['org_name'] = $org->isEmpty() ? '' : $org['name'];
unset($item['org_id'],$item['dept_id'],$item['job_id']);
return $item;
})->toArray();
foreach($data as $k=>$v){
unset($data[$k]['role_id']);
}
return $this->success('请求成功',$data);
}
//获取某个岗位下的所有人员
public function getAdminsByJob(): \think\response\Json
{
$job_id = $this->request->get('job_id');
if(empty($job_id)){
return $this->fail('参数错误');
}
$data = Admin::field('id,name,avatar,org_id,dept_id,job_id')->where('job_id',$job_id)->select()->each(function($item){
$job = Jobs::field('name')->where('id',$item['job_id'])->findOrEmpty();
$dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty();
$org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty();
$item['job_name'] = $job->isEmpty() ? '' : $job['name'];
$item['dept_name'] = $dept->isEmpty() ? '' : $dept['name'];
$item['org_name'] = $org->isEmpty() ? '' : $org['name'];
unset($item['org_id'],$item['dept_id'],$item['job_id']);
return $item;
})->toArray();
foreach($data as $k=>$v){
unset($data[$k]['role_id']);
}
return $this->success('请求成功',$data);
}
}

View File

@ -18,7 +18,9 @@ use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\dept\DeptLists;
use app\adminapi\logic\dept\DeptLogic;
use app\adminapi\validate\dept\DeptValidate;
use app\common\model\auth\Admin;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
/**
* 部门管理控制器
@ -107,4 +109,18 @@ class DeptController extends BaseAdminController
$data = Dept::field('id,name,leader,mobile,status')->where('org_id',$params['org_id'])->select()->toArray();
return $this->success('请求成功',$data);
}
//获取所有部门
public function getAllDept(): \think\response\Json
{
$data = Dept::field('id,name,org_id')->select()->each(function($item){
$org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty();
$item['org_name'] = $org->isEmpty() ? '' : $org['name'];
$item['admin_num'] = Admin::where('dept_id',$item['id'])->count();
unset($item['org_id']);
return $item;
})->toArray();
$result = group_by($data, 'org_name');
return $this->success('请求成功',$result);
}
}

View File

@ -18,6 +18,7 @@ use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\dept\JobsLists;
use app\adminapi\logic\dept\JobsLogic;
use app\adminapi\validate\dept\JobsValidate;
use app\common\model\auth\Admin;
use app\common\model\dept\Jobs;
@ -109,5 +110,15 @@ class JobsController extends BaseAdminController
$data = Jobs::field('id,name,status,sort,create_time')->where('dept_id',$params['dept_id'])->select()->toArray();
return $this->success('请求成功',$data);
}
//获取所有岗位
public function getAllJobs(): \think\response\Json
{
$data = Jobs::field('id,name')->select()->each(function($item){
$item['admin_num'] = Admin::where('job_id',$item['id'])->count();
return $item;
})->toArray();
return $this->success('请求成功',$data);
}
}

View File

@ -24,6 +24,9 @@ use app\common\model\auth\AdminOrgs;
use app\common\model\auth\AdminRole;
use app\common\model\auth\AdminSession;
use app\common\cache\AdminTokenCache;
use app\common\model\dept\Dept;
use app\common\model\dept\Jobs;
use app\common\model\dept\Orgs;
use app\common\service\FileService;
use think\facade\Config;
use think\facade\Db;
@ -50,7 +53,6 @@ class AdminLogic extends BaseLogic
$password = create_password($params['password'], $passwordSalt);
$defaultAvatar = config('project.default_image.admin_avatar');
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
$admin = Admin::create([
'name' => $params['name'],
'account' => $params['account'],
@ -59,17 +61,12 @@ class AdminLogic extends BaseLogic
'create_time' => time(),
'disable' => $params['disable'],
'multipoint_login' => $params['multipoint_login'],
'org_id' => $params['org_id'],
'dept_id' => $params['dept_id'],
'job_id' => $params['job_id'],
]);
// 角色
self::insertRole($admin['id'], $params['role_id'] ?? []);
// 组织
self::insertOrg($admin['id'], $params['org_id'] ?? 0);
// 部门
self::insertDept($admin['id'], $params['dept_id'] ?? 0);
// 岗位
self::insertJobs($admin['id'], $params['jobs_id'] ?? 0);
Db::commit();
return true;
} catch (\Exception $e) {
@ -97,7 +94,10 @@ class AdminLogic extends BaseLogic
'name' => $params['name'],
'account' => $params['account'],
'disable' => $params['disable'],
'multipoint_login' => $params['multipoint_login']
'multipoint_login' => $params['multipoint_login'],
'org_id' => $params['org_id'],
'dept_id' => $params['dept_id'],
'job_id' => $params['job_id'],
];
// 头像
@ -115,31 +115,18 @@ class AdminLogic extends BaseLogic
if (!empty(array_diff_assoc($roleId, $params['role_id']))) {
$editRole = true;
}
if ($params['disable'] == 1 || $editRole) {
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
foreach ($tokenArr as $token) {
self::expireToken($token['token']);
}
}
Admin::update($data);
(new AdminAuthCache($params['id']))->clearAuthCache();
// 删除旧的关联信息
AdminRole::delByUserId($params['id']);
AdminOrgs::delByUserId($params['id']);
AdminDept::delByUserId($params['id']);
AdminJobs::delByUserId($params['id']);
// 角色
self::insertRole($params['id'], $params['role_id']);
// 组织
self::insertOrg($params['id'], $params['org_id'] ?? 0);
// 部门
self::insertDept($params['id'], $params['dept_id'] ?? 0);
// 岗位
self::insertJobs($params['id'], $params['jobs_id'] ?? 0);
Db::commit();
return true;
} catch (\Exception $e) {
@ -176,10 +163,6 @@ class AdminLogic extends BaseLogic
// 删除旧的关联信息
AdminRole::delByUserId($params['id']);
AdminOrgs::delByUserId($params['id']);
AdminDept::delByUserId($params['id']);
AdminJobs::delByUserId($params['id']);
Db::commit();
return true;
} catch (\Exception $e) {
@ -229,9 +212,15 @@ class AdminLogic extends BaseLogic
public static function detail($params, $action = 'detail'): array
{
$admin = Admin::field([
'id', 'account', 'name', 'disable', 'root',
'id', 'account', 'name', 'disable', 'root', 'org_id', 'dept_id', 'job_id',
'multipoint_login', 'avatar',
])->findOrEmpty($params['id'])->toArray();
$job = Jobs::field('name')->where('id',$admin['job_id'])->findOrEmpty();
$dept = Dept::field('name')->where('id',$admin['dept_id'])->findOrEmpty();
$org = Orgs::field('name')->where('id',$admin['org_id'])->findOrEmpty();
$admin['job_name'] = $job->isEmpty() ? '' : $job['name'];
$admin['dept_name'] = $dept->isEmpty() ? '' : $dept['name'];
$admin['org_name'] = $org->isEmpty() ? '' : $org['name'];
if ($action == 'detail') {
return $admin;
}
@ -292,63 +281,4 @@ class AdminLogic extends BaseLogic
(new AdminRole())->saveAll($roleData);
}
}
/**
* @notes 新增组织
* @param $adminId
* @param $orgIds
* @throws \Exception
* @author 段誉
* @date 2022/11/25 14:22
*/
public static function insertOrg($adminId, $orgIds)
{
// 岗位
if (!empty($orgsIds)) {
(new AdminOrgs())->save([
'admin_id' => $adminId,
'org_id' => $orgsIds
]);
}
}
/**
* @notes 新增部门
* @param $adminId
* @param $deptIds
* @throws \Exception
* @author 段誉
* @date 2022/11/25 14:22
*/
public static function insertDept($adminId, $deptIds)
{
// 部门
if (!empty($deptIds)) {
(new AdminDept())->save([
'admin_id' => $adminId,
'dept_id' => $deptIds
]);
}
}
/**
* @notes 新增岗位
* @param $adminId
* @param $jobsIds
* @throws \Exception
* @author 段誉
* @date 2022/11/25 14:22
*/
public static function insertJobs($adminId, $jobsIds)
{
// 岗位
if (!empty($jobsIds)) {
(new AdminJobs())->save([
'admin_id' => $adminId,
'jobs_id' => $jobsIds
]);
}
}
}

View File

@ -19,6 +19,7 @@ use app\common\logic\BaseLogic;
use app\common\model\article\Article;
use app\common\model\dept\Dept;
use app\common\model\dept\Jobs;
use app\common\model\dept\Orgs;
use app\common\service\FileService;
@ -99,7 +100,10 @@ class JobsLogic extends BaseLogic
return [];
}
$dept = Dept::where('id',$jobs['dept_id'])->findOrEmpty();
$org = Orgs::where('id',$dept['org_id'])->findOrEmpty();
$jobs['dept_name'] = $dept->isEmpty() ? '' : $dept['name'];
$jobs['org_id'] = $org->isEmpty() ? 0 : $org['id'];
$jobs['org_name'] = $org->isEmpty() ? '' : $org['name'];
$jobs['status_text'] = $jobs->status_text;
return $jobs->toArray();
}

View File

@ -14,6 +14,9 @@
namespace app\adminapi\validate\auth;
use app\common\model\dept\Dept;
use app\common\model\dept\Jobs;
use app\common\model\dept\Orgs;
use app\common\validate\BaseValidate;
use app\common\model\auth\Admin;
@ -33,6 +36,9 @@ class AdminValidate extends BaseValidate
'role_id' => 'require',
'disable' => 'require|in:0,1|checkAbleDisable',
'multipoint_login' => 'require|in:0,1',
'org_id' => 'checkOrg',
'dept_id' => 'requireWith:org_id|checkDept',
'job_id' => 'requireWith:dept_id|checkJob'
];
protected $message = [
@ -52,6 +58,8 @@ class AdminValidate extends BaseValidate
'disable.in' => '状态值错误',
'multipoint_login.require' => '请选择是否支持多处登录',
'multipoint_login.in' => '多处登录状态值为误',
'dept_id.requireWith' => '请选择部门',
'job_id.requireWith' => '请选择岗位',
];
/**
@ -164,5 +172,39 @@ class AdminValidate extends BaseValidate
}
return true;
}
//验证组织
public function checkOrg($value): bool|string
{
$org = Orgs::where('id',$value)->findOrEmpty();
if($org->isEmpty()){
return '组织不存在';
}
return true;
}
public function checkDept($value, $rule, $data): bool|string
{
$dept = Dept::where('id',$value)->findOrEmpty();
if($dept->isEmpty()){
return '部门不存在';
}
if($dept['org_id'] != $data['org_id']){
return '该部门不属于当前选择的组织';
}
return true;
}
public function checkJob($value, $rule, $data): bool|string
{
$job = Jobs::where('id',$value)->findOrEmpty();
if($job->isEmpty()){
return '岗位不存在';
}
if($job['dept_id'] != $data['dept_id']){
return '该岗位不属于当前选择的部门';
}
return true;
}
}

View File

@ -303,3 +303,12 @@ function project_code(): string
{
return 'P'.date('Ymd',time()).'-'.mt_rand(100000, 999999);
}
function group_by($array, $key): array
{
$result = [];
foreach ($array as $item) {
$result[$item[$key]][] = $item;
}
return $result;
}