From 2904adf3e99a0fdcec145b01470e88cc32b622b9 Mon Sep 17 00:00:00 2001 From: weiz <736250432@qq.com> Date: Wed, 22 May 2024 17:32:53 +0800 Subject: [PATCH] update --- .../works/rlzy/OaAdminController.php | 111 ++++++++ .../lists/works/rlzy/OaAdminLists.php | 90 +++++++ app/adminapi/logic/auth/AdminLogic.php | 4 +- .../logic/works/rlzy/OaAdminLogic.php | 247 ++++++++++++++++++ .../validate/works/rlzy/OaAdminValidate.php | 178 +++++++++++++ app/common/model/works/rlzy/OaAdmin.php | 50 ++++ 6 files changed, 679 insertions(+), 1 deletion(-) create mode 100644 app/adminapi/controller/works/rlzy/OaAdminController.php create mode 100644 app/adminapi/lists/works/rlzy/OaAdminLists.php create mode 100644 app/adminapi/logic/works/rlzy/OaAdminLogic.php create mode 100644 app/adminapi/validate/works/rlzy/OaAdminValidate.php create mode 100644 app/common/model/works/rlzy/OaAdmin.php diff --git a/app/adminapi/controller/works/rlzy/OaAdminController.php b/app/adminapi/controller/works/rlzy/OaAdminController.php new file mode 100644 index 000000000..6dfecdfd5 --- /dev/null +++ b/app/adminapi/controller/works/rlzy/OaAdminController.php @@ -0,0 +1,111 @@ +dataLists(new OaAdminLists()); + } + + + /** + * @notes 添加企业员工 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function add() + { + $params = (new OaAdminValidate())->post()->goCheck('add'); + $result = OaAdminLogic::add($params); + if (true === $result) { + return $this->success('添加成功', [], 1, 1); + } + return $this->fail(OaAdminLogic::getError()); + } + + + /** + * @notes 编辑企业员工 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function edit() + { + $params = (new OaAdminValidate())->post()->goCheck('edit'); + $result = OaAdminLogic::edit($params); + if (true === $result) { + return $this->success('编辑成功', [], 1, 1); + } + return $this->fail(OaAdminLogic::getError()); + } + + + /** + * @notes 删除企业员工 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function delete() + { + $params = (new OaAdminValidate())->post()->goCheck('delete'); + $result = OaAdminLogic::delete($params); + if (true === $result) { + return $this->success('删除成功', [], 1, 1); + } + return $this->fail(OaAdminLogic::getError()); + } + + + /** + * @notes 获取企业员工详情 + * @return \think\response\Json + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function detail() + { + $params = (new OaAdminValidate())->goCheck('detail'); + $result = OaAdminLogic::detail($params); + return $this->data($result); + } + + +} \ No newline at end of file diff --git a/app/adminapi/lists/works/rlzy/OaAdminLists.php b/app/adminapi/lists/works/rlzy/OaAdminLists.php new file mode 100644 index 000000000..2be22e9ff --- /dev/null +++ b/app/adminapi/lists/works/rlzy/OaAdminLists.php @@ -0,0 +1,90 @@ + ['sex', 'org_id', 'dept_id', 'job_id', 'type', 'status'], + '%like%' => ['name', 'mobile'], + ]; + } + + + /** + * @notes 获取企业员工列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function lists(): array + { + return OaAdmin::where($this->searchWhere) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($data){ + $data['sex_text'] = $data->sex_text; + $data['type_text'] = $data->type_text; + $data['status_text'] = $data->status_text; + $org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty(); + $job = Jobs::field('name')->where('id',$data['job_id'])->findOrEmpty(); + $data['org_name'] = !$org->isEmpty() ? $org['name'] : ''; + $data['dept_name'] = !$dept->isEmpty() ? $dept['name'] : ''; + $data['job_name'] = !$job->isEmpty() ? $job['name'] : ''; + }) + ->toArray(); + } + + + /** + * @notes 获取企业员工数量 + * @return int + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function count(): int + { + return OaAdmin::where($this->searchWhere)->count(); + } + +} \ No newline at end of file diff --git a/app/adminapi/logic/auth/AdminLogic.php b/app/adminapi/logic/auth/AdminLogic.php index 31425188d..023373de1 100755 --- a/app/adminapi/logic/auth/AdminLogic.php +++ b/app/adminapi/logic/auth/AdminLogic.php @@ -27,6 +27,7 @@ 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\model\works\rlzy\OaAdmin; use app\common\service\FileService; use think\facade\Config; use think\facade\Db; @@ -146,6 +147,7 @@ class AdminLogic extends BaseLogic */ public static function delete(array $params): bool { + $oa_admin = OaAdmin::where('admin_id',$params['id'])->findOrEmpty(); Db::startTrans(); try { $admin = Admin::findOrEmpty($params['id']); @@ -153,7 +155,7 @@ class AdminLogic extends BaseLogic throw new \Exception("超级管理员不允许被删除"); } Admin::destroy($params['id']); - + OaAdmin::destroy($oa_admin['id']); //设置token过期 $tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray(); foreach ($tokenArr as $token) { diff --git a/app/adminapi/logic/works/rlzy/OaAdminLogic.php b/app/adminapi/logic/works/rlzy/OaAdminLogic.php new file mode 100644 index 000000000..0e801de5b --- /dev/null +++ b/app/adminapi/logic/works/rlzy/OaAdminLogic.php @@ -0,0 +1,247 @@ + $params['name'], + 'account' => $params['mobile'], + 'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'), + 'password' => create_password($params['password'], Config::get('project.unique_identification')), + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], + 'disable' => $params['status'] == 2 ? 1 : 0, + ]); + self::insertRole($admin['id'], $params['role_id'] ?? []); + OaAdmin::create([ + 'name' => $params['name'], + 'email' => $params['email'] ?? '', + 'mobile' => $params['mobile'] ?? '', + 'sex' => $params['sex'] ?? 0, + 'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'), + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], + 'type' => $params['type'], + 'age' => $params['age'] ?? 0, + 'native_place' => $params['native_place'] ?? '', + 'idcard' => $params['idcard'] ?? '', + 'education' => $params['education'] ?? '', + 'bank_account' => $params['bank_account'] ?? '', + 'bank_info' => $params['bank_info'] ?? '', + 'desc' => $params['desc'] ?? null, + 'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0, + 'status' => $params['status'], + 'admin_id' => $admin['id'] + ]); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 编辑企业员工 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public static function edit(array $params): bool + { + $data = OaAdmin::where('id', $params['id'])->findOrEmpty(); + Db::startTrans(); + try { + OaAdmin::where('id', $params['id'])->update([ + 'name' => $params['name'], + 'email' => $params['email'] ?? '', + 'mobile' => $params['mobile'] ?? '', + 'sex' => $params['sex'] ?? 0, + 'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'), + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], + 'type' => $params['type'], + 'age' => $params['age'] ?? 0, + 'native_place' => $params['native_place'] ?? '', + 'idcard' => $params['idcard'] ?? '', + 'education' => $params['education'] ?? '', + 'bank_account' => $params['bank_account'] ?? '', + 'bank_info' => $params['bank_info'] ?? '', + 'desc' => $params['desc'] ?? null, + 'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0, + 'status' => $params['status'], + ]); + Admin::where('id',$data['admin_id'])->update([ + 'name' => $params['name'], + 'account' => $params['name'], + 'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'), + 'password' => create_password($params['password'], Config::get('project.unique_identification')), + 'org_id' => $params['org_id'], + 'dept_id' => $params['dept_id'], + 'job_id' => $params['job_id'], + 'disable' => $params['status'] == 2 ? 1 : 0, + ]); + // 更换角色后.设置token过期 + $roleId = AdminRole::where('admin_id', $data['admin_id'])->column('role_id'); + if (!empty(array_diff_assoc($roleId, $params['role_id'])) || $params['status'] == 2) { + $tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray(); + foreach ($tokenArr as $token) { + self::expireToken($token['token']); + } + } + (new AdminAuthCache($data['admin_id']))->clearAuthCache(); + // 删除旧的关联信息 + AdminRole::delByUserId($data['admin_id']); + // 角色 + self::insertRole($data['admin_id'], $params['role_id']); + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 删除企业员工 + * @param array $params + * @return bool + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public static function delete(array $params): bool + { + $data = OaAdmin::where('id', $params['id'])->findOrEmpty(); + Db::startTrans(); + try { + OaAdmin::destroy($params['id']); + Admin::destroy($data['admin_id']); + //设置token过期 + $tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray(); + foreach ($tokenArr as $token) { + self::expireToken($token['token']); + } + (new AdminAuthCache($data['admin_id']))->clearAuthCache(); + // 删除旧的关联信息 + AdminRole::delByUserId($data['admin_id']); + Db::commit(); + return true; + }catch (\Exception $e){ + Db::rollback(); + self::setError($e->getMessage()); + return false; + } + } + + + /** + * @notes 获取企业员工详情 + * @param $params + * @return array + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public static function detail($params): array + { + $data = OaAdmin::findOrEmpty($params['id']); + $data['sex_text'] = $data->sex_text; + $data['type_text'] = $data->type_text; + $data['status_text'] = $data->status_text; + $org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty(); + $job = Jobs::field('name')->where('id',$data['job_id'])->findOrEmpty(); + $data['org_name'] = !$org->isEmpty() ? $org['name'] : ''; + $data['dept_name'] = !$dept->isEmpty() ? $dept['name'] : ''; + $data['job_name'] = !$job->isEmpty() ? $job['name'] : ''; + $data['role_id'] = AdminRole::where('admin_id', $data['admin_id'])->column('role_id'); + return $data->toArray(); + } + + public static function insertRole($adminId, $roleIds) + { + if (!empty($roleIds)) { + // 角色 + $roleData = []; + foreach ($roleIds as $roleId) { + $roleData[] = [ + 'admin_id' => $adminId, + 'role_id' => $roleId, + ]; + } + (new AdminRole())->saveAll($roleData); + } + } + + public static function expireToken($token): bool + { + $adminSession = AdminSession::where('token', '=', $token) + ->with('admin') + ->find(); + + if (empty($adminSession)) { + return false; + } + + $time = time(); + $adminSession->expire_time = $time; + $adminSession->update_time = $time; + $adminSession->save(); + + return (new AdminTokenCache())->deleteAdminInfo($token); + } +} \ No newline at end of file diff --git a/app/adminapi/validate/works/rlzy/OaAdminValidate.php b/app/adminapi/validate/works/rlzy/OaAdminValidate.php new file mode 100644 index 000000000..43e21cd0d --- /dev/null +++ b/app/adminapi/validate/works/rlzy/OaAdminValidate.php @@ -0,0 +1,178 @@ + 'require|checkData', + 'name' => 'require', + 'email' => 'email', + 'mobile' => 'require|mobile|unique:oa_admin', + 'sex' => 'integer|in:0,1,2', + 'org_id' => 'require|checkOrg', + 'dept_id' => 'require|checkDept', + 'job_id' => 'require|checkJob', + 'type' => 'require|integer|in:0,1,2', + 'age' => 'integer|gt:0', + 'idcard' => 'idCard', + 'entry_time' => 'require|dateFormat:Y-m-d', + 'status' => 'require|integer|in:0,1,2', + 'password' => 'require|length:6,32', + 'role_id' => 'require|checkRole', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $field = [ + 'id' => 'id', + 'name' => '员工姓名', + 'email' => '电子邮箱', + 'mobile' => '手机号码', + 'sex' => '性别', + 'org_id' => '组织', + 'dept_id' => '部门', + 'job_id' => '职位', + 'type' => '员工类型', + 'age' => '年龄', + 'idcard' => '身份证', + 'entry_time' => '员工入职日期', + 'status' => '状态', + 'password' => '登录密码', + 'role_id' => '角色权限', + ]; + + + /** + * @notes 添加场景 + * @return OaAdminValidate + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function sceneAdd() + { + return $this->remove('id',true); + } + + + /** + * @notes 编辑场景 + * @return OaAdminValidate + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function sceneEdit() + {} + + + /** + * @notes 删除场景 + * @return OaAdminValidate + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return OaAdminValidate + * @author likeadmin + * @date 2024/05/22 15:49 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + + public function checkData($value): bool|string + { + $data = OaAdmin::field('id')->where('id',$value)->findOrEmpty(); + return $data->isEmpty() ? '数据不存在' : 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; + } + + public function checkRole($value): bool|string + { + if(empty($value) || !is_array($value)){ + return '角色数据格式错误'; + } + foreach ($value as $item) { + $res = SystemRole::where('id',$item)->findOrEmpty(); + if($res->isEmpty()){ + return '角色数据不存在'; + } + } + return true; + } + +} \ No newline at end of file diff --git a/app/common/model/works/rlzy/OaAdmin.php b/app/common/model/works/rlzy/OaAdmin.php new file mode 100644 index 000000000..4d5cc4b53 --- /dev/null +++ b/app/common/model/works/rlzy/OaAdmin.php @@ -0,0 +1,50 @@ +'未设置', 1=>'男', 2=>'女']; + return $arr[$data['sex']]; + } + + public function getTypeTextAttr($value,$data): string + { + $arr = [0=>'正式', 1=>'试用', 2=>'实习']; + return $arr[$data['type']]; + } + + public function getStatusTextAttr($value,$data): string + { + $arr = [0=>'禁用', 1=>'正常', 2=>'离职']; + return $arr[$data['status']]; + } +} \ No newline at end of file