From 1ab1a3a2934bf2c3103394c8c22ae7b75e667362 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sat, 21 Jun 2025 12:56:15 +0800 Subject: [PATCH] =?UTF-8?q?[Staff]=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think-plugs-staff/src/controller/User.php | 151 ++++++++++++++++++ .../think-plugs-staff/src/model/StaffDept.php | 22 +++ .../think-plugs-staff/src/model/StaffUser.php | 17 ++ .../think-plugs-staff/src/view/user/form.html | 91 +++++++++++ .../src/view/user/index.html | 128 +++++++++++++++ .../src/view/user/index_search.html | 27 ++++ 6 files changed, 436 insertions(+) create mode 100644 plugs/think-plugs-staff/src/controller/User.php create mode 100644 plugs/think-plugs-staff/src/view/user/form.html create mode 100644 plugs/think-plugs-staff/src/view/user/index.html create mode 100644 plugs/think-plugs-staff/src/view/user/index_search.html diff --git a/plugs/think-plugs-staff/src/controller/User.php b/plugs/think-plugs-staff/src/controller/User.php new file mode 100644 index 0000000..0fbc5b7 --- /dev/null +++ b/plugs/think-plugs-staff/src/controller/User.php @@ -0,0 +1,151 @@ +layTable(function () { + $this->title = '部门员工'; + $this->dept = StaffDept::tree(); + }, static function (QueryHelper $query) { + $query->equal('dept_id,status')->like('name'); + $query->with('dept')->append(['dept_name']); + }); + } + + /** + * 添加员工 + * @auth true + * @menu true + * @return void + */ + public function add() + { + StaffUser::mForm('form'); + } + + /** + * 编辑员工 + * @auth true + * @return void + */ + public function edit() + { + StaffUser::mForm('form'); + } + + protected function _form_filter(array &$data) + { + if ($this->request->isPost()) { + // 检查资料是否完整 + $this->_vali([ + 'name.require' => '用户名称不能为空!', + 'phone.require' => '手机号码不能为空!', + 'phone.mobile' => '手机号码格式错误!', + 'email.email' => '邮箱地址格式错误!', + 'dept_id.require' => '部门名称不能为空!', + 'authorize.require' => '未配置权限!' + ]); + if (!empty($data['password'])) { + if (strlen($data['password']) < 6) { + $this->error('密码长度不能少于6位!'); + }; + } + } else { + if (!empty($data['id'])) { + $user = SystemUser::mk()->find($data['id']); + $data['authorize'] = str2arr($user['authorize'] ?? '');; + } else { + $data['authorize'] = []; + } + $data['dept_id'] = $data['dept_id'] ?? ''; + $this->auths = SystemAuth::items(); + $this->depts = StaffDept::items(); + } + } + + protected function _form_result(bool $state, $data) + { + if ($state) { + $user = []; + // 检查账号是否重复 + $map = ['username' => $data['phone'], 'is_deleted' => 0]; + $systemUser = SystemUser::mk()->where($map)->findOrEmpty(); + if ($systemUser->isExists() && $systemUser->id != $data['id']) { + $this->error("账号已经存在,请使用其它账号!"); + } + $user['id'] = $data['id']; + $user['username'] = $data['phone']; + if (!empty($data['password'])) { + $user['password'] = md5($data['password']); + } + $user['nickname'] = $data['name']; + $user['contact_mail'] = $data['email']; + // 处理上传的权限格式 + $user['authorize'] = arr2str($data['authorize'] ?? []); + SystemUser::mk()->save($user); + } + } + + /** + * 启用禁用员工 + * @return void + */ + public function state() + { + StaffUser::mSave($this->_vali([ + 'status.in:0,1' => '状态值范围异常!', + 'status.require' => '状态值不能为空!', + ])); + } + + protected function _state_save_result(bool $state, $data) + { + if ($state) { + $systemUser = SystemUser::mk()->findOrEmpty($this->request->post('id')); + if ($systemUser->isExists()) { + $systemUser->status = $this->request->post('status'); + $systemUser->save(); + } + } + } + + /** + * 删除员工 + * @auth true + * @return void + */ + public function del() + { + StaffUser::mDelete(); + } + + protected function _del_delete_result(bool $state) + { + if ($state) { + $systemUser = SystemUser::mk()->findOrEmpty($this->request->post('id')); + if ($systemUser->isExists()) { + $systemUser->is_deleted = 1; + $systemUser->save(); + } + } + } +} \ No newline at end of file diff --git a/plugs/think-plugs-staff/src/model/StaffDept.php b/plugs/think-plugs-staff/src/model/StaffDept.php index 99b3e5b..1b5a3c6 100644 --- a/plugs/think-plugs-staff/src/model/StaffDept.php +++ b/plugs/think-plugs-staff/src/model/StaffDept.php @@ -3,6 +3,7 @@ namespace jerryyan\staff\model; use think\admin\Model; +use think\db\Query; use think\model\relation\HasMany; use think\model\relation\HasOne; @@ -12,6 +13,22 @@ class StaffDept extends Model protected $updateTime = false; protected $oplogName = '部门'; protected $oplogType = '部门管理'; + protected $globalScope = ['not_deleted']; + + public function scopeDeleted(Query $query): void + { + $query->where(['is_deleted' => 1]); + } + + public function scopeNotDeleted(Query $query): void + { + $query->where(['is_deleted' => 0]); + } + + public static function items() + { + return self::mk()->where(['status' => 1])->select(); + } public function staffs(): HasMany { @@ -36,4 +53,9 @@ class StaffDept extends Model { return $this->hasOne(StaffDept::class, 'id', 'pid'); } + + public static function tree() + { + return static::mk()->where(['is_deleted' => 0, 'pid' => 0])->with('children.children')->select(); + } } \ No newline at end of file diff --git a/plugs/think-plugs-staff/src/model/StaffUser.php b/plugs/think-plugs-staff/src/model/StaffUser.php index 75329f8..f340ac3 100644 --- a/plugs/think-plugs-staff/src/model/StaffUser.php +++ b/plugs/think-plugs-staff/src/model/StaffUser.php @@ -3,6 +3,7 @@ namespace jerryyan\staff\model; use think\admin\Model; +use think\db\Query; use think\model\relation\HasOne; class StaffUser extends Model @@ -11,6 +12,17 @@ class StaffUser extends Model protected $updateTime = false; protected $oplogName = '部门员工'; protected $oplogType = '部门员工管理'; + protected $globalScope = ['notDeleted']; + + public function scopeDeleted(Query $query): void + { + $query->where('is_deleted', '=', 1); + } + + public function scopeNotDeleted(Query $query): void + { + $query->where('is_deleted', '=', 0); + } public function dept(): HasOne { @@ -18,4 +30,9 @@ class StaffUser extends Model 'status' => 1, 'is_deleted' => 0, ]); } + + public function getDeptNameAttr($value, $data) + { + return $this->dept ? $this->dept['name'] : ''; + } } \ No newline at end of file diff --git a/plugs/think-plugs-staff/src/view/user/form.html b/plugs/think-plugs-staff/src/view/user/form.html new file mode 100644 index 0000000..4336c01 --- /dev/null +++ b/plugs/think-plugs-staff/src/view/user/form.html @@ -0,0 +1,91 @@ +
+
+
+ 基础信息 + + + + + +
+ +
+ 部门权限 + {if !empty($depts)} +
+
部门
+
+ {foreach $depts as $dept} + + {/foreach} +
+
+ {/if} + {if !empty($auths)} +
+
访问权限
+
+ {foreach $auths as $authorize} + + {/foreach} +
+
+ {/if} +
+
+ +
+ {notempty name='vo.id'}{/notempty} + +
+ + +
+
\ No newline at end of file diff --git a/plugs/think-plugs-staff/src/view/user/index.html b/plugs/think-plugs-staff/src/view/user/index.html new file mode 100644 index 0000000..f669ee6 --- /dev/null +++ b/plugs/think-plugs-staff/src/view/user/index.html @@ -0,0 +1,128 @@ +{extend name='table'} + +{block name="button"} + + + +{/block} + +{block name="content"} +
+
+ +
+
+
+ {include file="user/index_search"} +
+
+
+ + + + +{/block} \ No newline at end of file diff --git a/plugs/think-plugs-staff/src/view/user/index_search.html b/plugs/think-plugs-staff/src/view/user/index_search.html new file mode 100644 index 0000000..b9b34d9 --- /dev/null +++ b/plugs/think-plugs-staff/src/view/user/index_search.html @@ -0,0 +1,27 @@ +
+ {:lang('条件搜索')} + +
\ No newline at end of file