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 @@ +
\ 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"} +