This commit is contained in:
weiz 2023-12-11 10:22:16 +08:00
parent 7a749f357b
commit 961fe95eb9
2 changed files with 59 additions and 87 deletions

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

@ -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;
}
}