diff --git a/application/admin/command/Install/fastadmin.sql b/application/admin/command/Install/fastadmin.sql index 1b816535fa1b9b5989da3049fd713e89fa82c5d7..d84751e5950f4aef13c203ca4f4b6a8db2a46126 100755 --- a/application/admin/command/Install/fastadmin.sql +++ b/application/admin/command/Install/fastadmin.sql @@ -267,6 +267,20 @@ INSERT INTO `fa_auth_rule` VALUES (81, 'file', 79, 'user/rule/del', 'Del', 'fa f INSERT INTO `fa_auth_rule` VALUES (82, 'file', 79, 'user/rule/add', 'Add', 'fa fa-circle-o', '', '', '', 0, NULL, '', '', '', 1491635035, 1491635035, 0, 'normal'); INSERT INTO `fa_auth_rule` VALUES (83, 'file', 79, 'user/rule/edit', 'Edit', 'fa fa-circle-o', '', '', '', 0, NULL, '', '', '', 1491635035, 1491635035, 0, 'normal'); INSERT INTO `fa_auth_rule` VALUES (84, 'file', 79, 'user/rule/multi', 'Multi', 'fa fa-circle-o', '', '', '', 0, NULL, '', '', '', 1491635035, 1491635035, 0, 'normal'); +INSERT INTO `fa_auth_rule` VALUES (85, 'file', 0, 'department', 'Organizational', 'fa fa-sitemap', '', '', '', '1', null, '','','', '1645779501', '1645781822', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (86, 'file', 85, 'department/admin', 'Employee', 'fa fa-users', '', '', '', '1', null, '', '','','1645779501', '1645779501', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (87, 'file', 86, 'department/admin/index', 'View', 'fa fa-users', '', '', '', '0', null, '','','', '1645779501', '1645867468', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (88, 'file', 86, 'department/admin/add', 'Add', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645779501', '1645867469', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (89, 'file', 86, 'department/admin/edit', 'Edit', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645779501', '1645867469', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (90, 'file', 86, 'department/admin/del', 'Del', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645779501', '1645867469', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (91, 'file', 86, 'department/admin/multi', 'Multi', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645779501', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (92, 'file', 85, 'department/index', 'Department list', 'fa fa-sliders', '', '', '', '1', null, '', '','','1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (93, 'file', 92, 'department/index/index', 'View', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (94, 'file', 92, 'department/index/add', 'Add', 'fa fa-circle-o', '', '', '', '0', null, '', '','','1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (95, 'file', 92, 'department/index/edit', 'Edit', 'fa fa-circle-o', '', '', '', '0', null, '', '','','1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (96, 'file', 92, 'department/index/del', 'Del', 'fa fa-circle-o', '', '', '', '0', null, '', '','','1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (97, 'file', 92, 'department/index/multi', 'Multi', 'fa fa-circle-o', '', '', '', '0', null, '','','', '1645780539', '1645780539', '0', 'normal'); +INSERT INTO `fa_auth_rule` VALUES (98, 'file', 86, 'department/admin/principal', 'Principal set', 'fa fa-circle-o', '', '', '', '0', null, '', '','','1646898376', '1646898376', '0', 'normal'); COMMIT; -- ---------------------------- @@ -602,3 +616,28 @@ CREATE TABLE `fa_version` ( ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT='版本表'; SET FOREIGN_KEY_CHECKS = 1; + +CREATE TABLE `fa_department` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(30) DEFAULT NULL COMMENT '名称', + `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父ID', + `weigh` int(11) DEFAULT '0' COMMENT '排序', + `create_time` varchar(20) DEFAULT NULL COMMENT '创建时间', + `update_time` int(10) DEFAULT NULL COMMENT '更新时间', + `status` enum('normal','hidden') NOT NULL DEFAULT 'normal' COMMENT '状态', + `organise_id` int(11) DEFAULT '0' COMMENT '组织id(最顶级)', + PRIMARY KEY (`id`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='组织部门表'; + + +CREATE TABLE `fa_department_admin` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `department_id` int(11) DEFAULT NULL COMMENT '部门id', + `organise_id` int(11) DEFAULT NULL COMMENT '组织id(最顶级)', + `admin_id` int(11) DEFAULT NULL COMMENT '成员id', + `create_time` int(11) NOT NULL COMMENT '加入时间', + `update_time` int(11) NOT NULL COMMENT '更新时间', + `is_principal` tinyint(1) DEFAULT NULL COMMENT '是否负责人', + `is_owner` tinyint(1) DEFAULT '0' COMMENT '拥有者', + PRIMARY KEY (`id`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='组织部门-成员表'; diff --git a/application/admin/controller/department/Admin.php b/application/admin/controller/department/Admin.php new file mode 100644 index 0000000000000000000000000000000000000000..108a381e3b679d8a49eec416f591bd3ddb3fa798 --- /dev/null +++ b/application/admin/controller/department/Admin.php @@ -0,0 +1,433 @@ +childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin()); + + $this->dadminModel = new DepartmentAdminModel; + $departmentList = []; + $this->allDepartment = collection(DepartmentModel::allDepartment())->toArray(); + $departmentdata = []; + foreach ($this->allDepartment as $k => $v) { + $state = ['opened' => true]; + $departmentList[] = [ + 'id' => $v['id'], + 'parent' => $v['parent_id'] ? $v['parent_id'] : '#', + 'text' => __($v['name']), + 'state' => $state + ]; + } + $tree = Tree::instance()->init($this->allDepartment, 'parent_id'); + $departmentOptions = $tree->getTree(0, ""); + + $this->view->assign('departmentOptions', $departmentOptions); + $this->assignconfig('departmentList', $departmentList); + + $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); + foreach ($result as $k => $v) { + $departmentdata[$v['id']] = $v['name']; + } + $this->view->assign('departmentdata', $departmentdata); + } + + /** + * 成员列表 + */ + public function index() + { + + //测试 + //设置过滤方法 + $this->request->filter(['strip_tags']); + $department_id = $this->request->request("department_id"); + if ($this->request->isAjax()) { + $this->model = new \app\admin\model\department\AuthAdmin(); + $filter = $this->request->get("filter", ''); + $filter = (array)json_decode($filter, true); + $filter_w = []; + + if (isset($filter['department_id'])) { + $department_id = $filter['department_id']; + unset($filter['department_id']); + $this->request->get(['filter' => json_encode($filter)]); + } + if ($department_id) { + $admin_ids = $this->dadminModel->where('department_id', 'in', $department_id)->column('admin_id'); + $filter_w['id'] = ['in', $admin_ids]; + } + + + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->where($filter_w) + ->order($sort, $order)->fetchSql(false) + ->count(); + $list = $this->model + ->where($where) + ->with(['dadmin.department']) + ->with(['groups.getGroup']) + ->where($filter_w) + ->order($sort, $order) + ->limit($offset, $limit)->fetchSql(false) + ->select(); + + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + + /** + * 添加 + * @return string|\think\response\Json + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function add() + { + $groupdata = DepartmentModel::getGroupdata( + $this->childrenGroupIds, + $this->auth->isSuperAdmin() ? null : $this->auth->getGroups() + ); + + if ($this->request->isPost()) { + + $adminModel = new AdminModel(); + $departmentModel = new DepartmentModel(); + + $params = $this->request->post("row/a"); + if ($params) { + Db::startTrans(); + try { + $department_id = $this->request->post("department_id/a"); + + //获取部门信息 + if (!$department_id) { + exception(__("部门不能为空")); + } + $d_list = $departmentModel->where('id', 'in', $department_id)->select(); + + if (!$d_list) { + exception(__("部门不能为空")); + } + + if (!Validate::is($params['password'], '\S{6,16}')) { + exception(__("Please input correct password")); + } + $params['salt'] = Random::alnum(); + $params['password'] = md5(md5($params['password']) . $params['salt']); + $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。 + $result = $adminModel->validate('Admin.add')->save($params); + if ($result === false) { + exception($adminModel->getError()); + } + + $admin_id = $adminModel->id; + $dadmin = array(); + //添加部门信息 + foreach ($d_list as $d_row) { + $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id? $d_row->organise_id: $d_row->id, 'admin_id' => $admin_id]; + } + $this->dadminModel->saveAll($dadmin); + + $group = $this->request->post("group/a"); + //过滤不允许的组别,避免越权 + $group = array_intersect($this->childrenGroupIds, $group); + + //添加权限默认第一个部门 + if (!$group) { + exception(__('The parent group exceeds permission limit')); + } + + $dataset = []; + foreach ($group as $value) { + $dataset[] = ['uid' => $admin_id, 'group_id' => $value]; + } + model('AuthGroupAccess')->saveAll($dataset); + + + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + $this->success(); + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->view->assign('groupdata', $groupdata); + return $this->view->fetch(); + } + + /** + * 修改 + * @param null $ids + * @return string|\think\response\Json + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public function edit($ids = null) + { + + $this->model = new AdminModel(); + $departmentModel = new DepartmentModel(); + $row = $this->model->get($ids); + if ($this->request->isPost()) { + $params = $this->request->post("row/a"); + if ($params) { + Db::startTrans(); + try { + $department_id = $this->request->post("department_id/a"); + //获取部门信息 + if (!$department_id) { + exception(__("Department can't null")); + } + $d_list = $departmentModel->where('id', 'in', $department_id)->select(); + + if (!$d_list) { + exception(__("Department can't null")); + } + + if ($params['password']) { + if (!Validate::is($params['password'], '\S{6,16}')) { + exception(__("Please input correct password")); + } + $params['salt'] = Random::alnum(); + $params['password'] = md5(md5($params['password']) . $params['salt']); + } else { + unset($params['password'], $params['salt']); + } + //这里需要针对username和email做唯一验证 + $adminValidate = \think\Loader::validate('Admin'); + $adminValidate->rule([ + 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id, + 'email' => 'require|email|unique:admin,email,' . $row->id, + 'password' => 'regex:\S{32}', + ]); + $result = $row->validate('Admin.edit')->save($params); + if ($result === false) { + exception($row->getError()); + } + $exist_departmentids=$this->dadminModel->where('admin_id', $row->id)->column('department_id'); + $dadmin = array(); + $deleteids=array_diff($exist_departmentids,$department_id); + //添加部门信息 + foreach ($d_list as $d_row) { + if (!in_array($d_row->id,$exist_departmentids)){ + $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id? $d_row->organise_id: $d_row->id, 'admin_id' => $row->id]; + } + } + if ($deleteids){ + $this->dadminModel->where('admin_id', $row->id)->where('department_id','in',$deleteids)->delete(); + } + if (count($dadmin)>0){ + $this->dadminModel->saveAll($dadmin); + } + + + + // 先移除所有权限 + model('AuthGroupAccess')->where('uid', $row->id)->delete(); + + $group = $this->request->post("group/a"); + // 过滤不允许的组别,避免越权 + $group = array_intersect($this->childrenGroupIds, $group); + if (!$group) { + exception(__('The parent group exceeds permission limit')); + } + + $dataset = []; + foreach ($group as $value) { + $dataset[] = ['uid' => $row->id, 'group_id' => $value]; + } + model('AuthGroupAccess')->saveAll($dataset); + + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + $this->success(); + } + $this->error(__('Parameter %s can not be empty', '')); + } + $groupdata = DepartmentModel::getGroupdata( + $this->childrenGroupIds, + $this->auth->isSuperAdmin() ? null : $this->auth->getGroups() + ); + $grouplist = $this->auth->getGroups($row['id']); + $groupids = []; + foreach ($grouplist as $k => $v) { + $groupids[] = $v['id']; + } + $this->view->assign("groupids", $groupids); + + $this->view->assign('row', $row); + $this->view->assign('department_ids', $this->dadminModel->getDepartmentIds($ids)); + $this->view->assign('groupdata', $groupdata); + return $this->view->fetch(); + } + + + /** + * 删除 + */ + public function del($ids = "") + { + if (!$this->request->isPost()) { + $this->error(__("Invalid parameters")); + } + $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin()); + $this->model = new AdminModel(); + + $ids = $ids ? $ids : $this->request->post("ids"); + if ($ids) { + $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids))); + // 避免越权删除管理员 + $childrenGroupIds = $this->childrenGroupIds; + $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) { + $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid'); + })->select(); + if ($adminList) { + $deleteIds = []; + foreach ($adminList as $k => $v) { + $deleteIds[] = $v->id; + } + $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id])); + if ($deleteIds) { + Db::startTrans(); + try { + $this->model->destroy($deleteIds); + model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete(); + //删除部门员工信息 + $this->dadminModel->where('admin_id', 'in', $deleteIds)->delete();; + + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + $this->success(); + } + $this->error(__('No rows were deleted')); + } + } + $this->error(__('You have no permission')); + } + + /** + * 批量更新 + * @internal + */ + public function multi($ids = "") + { + // 管理员禁止批量操作 + $this->error(); + } + + /** + * 设置部门负责人 + */ + public function principal($ids = "") + { + + $this->model = new AdminModel(); + $departmentModel = new DepartmentModel(); + $row = $this->model->get($ids); + if ($this->request->isPost()) { + $department_id = $this->request->post("department_id/a"); + if ($department_id&&$department_id[0]) { + Db::startTrans(); + try { + $d_list = $departmentModel->where('id', 'in', $department_id)->select(); + + if (!$d_list) { + exception(__("Department can't null")); + } + //先移除他所有负责的部门 + $this->dadminModel->where('admin_id', $row->id)->update(['is_principal'=>0]); + + //判断选择的部门是否存在,不存在就先把他加入 + $p_departmentids = array_column($d_list, 'id'); + $exist_departmentids=$this->dadminModel->where('admin_id', $row->id)->column('department_id'); + + $dadmin = array(); + //添加部门信息 + foreach ($d_list as $d_row) { + if (!in_array($d_row->id,$exist_departmentids)){ + $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id? $d_row->organise_id: $d_row->id, 'admin_id' => $row->id]; + } + } + if (count($dadmin)>0){ + $this->dadminModel->saveAll($dadmin); + } + //更改为负责人 + $this->dadminModel->where('admin_id', $row->id)->where('department_id','in',$p_departmentids)->update(['is_principal'=>1]); + + + + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + + }else{ + //移除他所有负责的部门 + $this->dadminModel->where('admin_id', $row->id)->update(['is_principal'=>0]); + } + $this->success(); + } + + + $this->view->assign('row', $row); + $this->view->assign('department_ids', $this->dadminModel::getPrincipalIds($ids)); + return $this->view->fetch(); + } + + +} diff --git a/application/admin/controller/department/Index.php b/application/admin/controller/department/Index.php new file mode 100644 index 0000000000000000000000000000000000000000..5aeb2812b47af3bf12b1247e6e5b77f780e18085 --- /dev/null +++ b/application/admin/controller/department/Index.php @@ -0,0 +1,193 @@ +model = new DepartmentModel; + + $this->tree = Tree::instance(); + $this->tree->init(DepartmentModel::allDepartment(), 'parent_id'); + $this->departmentList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name'); + $this->view->assign("departmentList", $this->departmentList); + + $this->view->assign("statusList", DepartmentModel::getStatusList()); + } + + /** + * 部门列表 + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'trim']); + if ($this->request->isAjax()) { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('keyField')) { + //return $this->selectpage(); + } + + $search = $this->request->request("search"); + //构造父类select列表选项数据 + $list = []; + if ($search) { + foreach ($this->departmentList as $k => $v) { + if (stripos($v['name'], $search) !== false) { + $list[] = $v; + } + } + } else { + $list = $this->departmentList; + } + + $list = array_values($list); + + foreach ($list as $k => &$v) { + $v['pid'] = $v['parent_id']; + } + $total = count($list); + $result = array("total" => $total, "rows" => $list); + + return json($result); + + } + return $this->view->fetch(); + } + + /** + * 添加 + */ + public function add() + { + if ($this->request->isPost()) { + $params = $this->request->post("row/a"); + if ($params) { + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $params[$this->dataLimitField] = $this->auth->id; + } + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; + $this->model->validate($validate); + } + $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $params['name']))); + + //获取组织最高的ID; + $params['organise_id'] = DepartmentModel::getOrganiseID(isset($params['parent_id']) ? $params['parent_id'] : 0); + + if (count($nameArr) > 1) { + foreach ($nameArr as $index => $item) { + $params['name'] = $item; + $result = $this->model->allowField(true)->isUpdate(false)->data($params)->save(); + } + } else { + $result = $this->model->allowField(true)->save($params); + } + if ($result !== false) { + //清空部门缓存 + DepartmentModel::clearCache(); + $this->success(); + } else { + $this->error($this->model->getError()); + } + } catch (\think\exception\PDOException $e) { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + return $this->view->fetch(); + } + + public function edit($ids = null) + { + $row = DepartmentModel::get($ids); + if (!$row) { + $this->error(__('No Results were found')); + } + if ($this->request->isPost()) { + $params = $this->request->post("row/a"); + if ($params) { + $params = $this->preExcludeFields($params); + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException(true)->validate($validate); + } + //最顶级的不能修改成下级 + if ($row['parent_id']==0&&$params['parent_id']){ + \exception(__("Top-level organization cannot be modified to lower level organization")); + } + + //获取修改后的组织最高的ID; + //获取组织最高的ID; + $organise_id = isset($params['parent_id']) ? DepartmentModel::getOrganiseID($params['parent_id']) : 0; + //判断修改的父类是否和之前的一样并且organise_id不一样就要修改,含子类 + if ($params['parent_id'] != $row['parent_id'] && $organise_id != $row['organise_id']) { + $departmentIds = DepartmentModel::getChildrenIds($row->id, true); + $mapwhere['id'] = ['in', $departmentIds]; + $departmentIds = DepartmentModel::update(['organise_id' => $organise_id], $mapwhere); + } + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException $e) { + Db::rollback(); + $this->error($e->getMessage()); + } catch (PDOException $e) { + Db::rollback(); + $this->error($e->getMessage()); + } catch (Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($result !== false) { + //清空部门缓存 + DepartmentModel::clearCache(); + $this->success(); + } else { + $this->error(__('No rows were updated')); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } else { + + $row = $row->toArray(); + $childrenIds = $this->tree->getChildrenIds($row['id'], true); + $this->view->assign('childrenIds', $childrenIds); + $this->view->assign("row", $row); + return $this->view->fetch(); + } + + + } + + + +} diff --git a/application/admin/lang/zh-cn.php b/application/admin/lang/zh-cn.php index 5f2df6fdc5d1095fc9337aa68c523289aac1f764..7344b1cf54df67bcd0fde29fabe19357a2aac7b3 100755 --- a/application/admin/lang/zh-cn.php +++ b/application/admin/lang/zh-cn.php @@ -114,13 +114,6 @@ return [ '%d week%s ago' => '%d周前', '%d month%s ago' => '%d月前', '%d year%s ago' => '%d年前', - '%d second%s after' => '%d秒后', - '%d minute%s after' => '%d分钟后', - '%d hour%s after' => '%d小时后', - '%d day%s after' => '%d天后', - '%d week%s after' => '%d周后', - '%d month%s after' => '%d月后', - '%d year%s after' => '%d年后', 'Set to normal' => '设为正常', 'Set to hidden' => '设为隐藏', 'Recycle bin' => '回收站', @@ -201,19 +194,25 @@ return [ 'Third group 2' => '三级管理组2', 'Dashboard tips' => '用于展示当前系统中的统计数据、统计报表及重要实时数据', 'Config tips' => '可以在此增改系统的变量和分组,也可以自定义分组和变量', - 'Category tips' => '分类类型请在常规管理->系统配置->字典配置中添加', + 'Category tips' => '用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加', 'Attachment tips' => '主要用于管理上传到服务器或第三方存储的数据', 'Addon tips' => '可在线安装、卸载、禁用、启用、配置、升级插件,插件升级前请做好备份。', 'Admin tips' => '一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成', 'Admin log tips' => '管理员可以查看自己所拥有的权限的管理员日志', 'Group tips' => '角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别的下级角色组或管理员', - 'Rule tips' => '菜单规则通常对应一个控制器的方法,同时菜单栏数据也从规则中获取', + 'Rule tips' => '规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过命令行进行生成规则节点', 'Access is allowed only to the super management group' => '仅超级管理组能访问', 'Local addon' => '本地插件', + 'Organizational' => '组织架构', + 'Employee' => '部门成员', + 'Department list' => '部门管理', + 'Principal set' => '设置负责人', + // 前台菜单 'Frontend' => '前台', 'API Interface' => 'API接口', 'User Module' => '会员模块', 'Register' => '注册', 'User Center' => '会员中心', + ]; diff --git a/application/admin/lang/zh-cn/department/admin.php b/application/admin/lang/zh-cn/department/admin.php new file mode 100644 index 0000000000000000000000000000000000000000..2515b15c9fc040ba7484e8d14a38731d46d27784 --- /dev/null +++ b/application/admin/lang/zh-cn/department/admin.php @@ -0,0 +1,10 @@ + '部门', + 'Principal' => '负责人', + 'Login time' => '最后登录', + 'You can select multiple departments to assign the person in charge. If you do not join the Department, you will join'=> '可选择多个部门赋予负责人,如果未加入该部门会加入', + 'Parent' => '上级', + 'Loginfailure' => '登录失败次数', + "Department can't null" => '部门不能为空', +]; diff --git a/application/admin/lang/zh-cn/department/index.php b/application/admin/lang/zh-cn/department/index.php new file mode 100644 index 0000000000000000000000000000000000000000..19b15b41c95aea6978996fdf77d55e3b4eee7de4 --- /dev/null +++ b/application/admin/lang/zh-cn/department/index.php @@ -0,0 +1,10 @@ + '部门', + 'Principal' => '负责人', + 'Children' => '子类', + 'Batch entry is supported, one record per line' => '支持批量录入,一行一条记录', + 'Toggle sub menu' => '切换菜单', + "Top-level organization cannot be modified to lower level organization"=>"顶级组织不能修改成下级", + +]; diff --git a/application/admin/model/department/Admin.php b/application/admin/model/department/Admin.php new file mode 100644 index 0000000000000000000000000000000000000000..db255548375ce1ad85eb35ca5239a0a33109373b --- /dev/null +++ b/application/admin/model/department/Admin.php @@ -0,0 +1,156 @@ +hasOne('app\admin\model\department\Department', 'id', 'department_id'); + } + + /** + * 获取管理者的部门ids + * @param $admin_id + * @param string $type + * @return array|bool|string + */ + public function getDepartmentIds($admin_id, $type = "self") + { + return $this->where('admin_id', $admin_id)->column('department_id'); + } + + /** + * 获取负责的部门IDs + * @param $admin_id + * @return array|bool|string + */ + public static function getPrincipalIds($admin_id){ + return self::where('admin_id', $admin_id)->where('is_principal',1)->column('department_id'); + } + /** + * 当前负责人下属ids + * @param int $admin_id 某个管理员ID + * @param boolean $withself 是否包含自身 + * @param string $department_ids 是否指定某个管理部门id,多个逗号id隔开 + * @return array + */ + public static function getChildrenAdminIds($admin_id, $withself = false, $department_ids = null) + { + $childrenAdminIds=[]; + if (self::isSuperAdmin($admin_id)) { + $childrenAdminIds = \app\admin\model\department\AuthAdmin::column('id'); + }else{ + $departmentIds = self::getChildrenDepartmentIds($admin_id); + $authDepartmentList = self::field('admin_id,department_id') + ->where('department_id', 'in', $departmentIds) + ->select(); + foreach ($authDepartmentList as $k => $v) { + $childrenAdminIds[] = $v['admin_id']; + } + } + if ($withself) { + if (!in_array($admin_id, $childrenAdminIds)) { + $childrenAdminIds[] = $admin_id; + } + } else { + $childrenAdminIds = array_diff($childrenAdminIds, [$admin_id]); + } + return $childrenAdminIds; + + + } + + /** + * 判断是否是超级管理员 + * @return bool + */ + public static function isSuperAdmin($admin_id) + { + $auth = new \app\admin\library\Auth(); + return in_array('*', $auth->getRuleIds($admin_id)) ? true : false; + } + + /** + * 取出当前负责人管理的下级部门 + * @param boolean $withself 是否包含当前所在的分组 + * @return array + */ + public static function getChildrenDepartmentIds($admin_id,$withself = false) + { + //取出当前负责人所有部门 + $departments =self::getDepartments($admin_id); + $departmenIds = []; + foreach ($departments as $k => $v) { + $departmenIds[] = $v['id']; + } + $originDepartmenId = $departmenIds; + foreach ($departments as $k => $v) { + if (in_array($v['parent_id'], $originDepartmenId)) { + $departmenIds = array_diff($departmenIds, [$v['id']]); + unset($departments[$k]); + } + } + // 取出所有部门 + $departmentList = \app\admin\model\department\Department::allDepartment(); + $objList = []; + foreach ($departments as $k => $v) { + // 取出包含自己的所有子节点 + $childrenList = Tree::instance()->init($departmentList, 'parent_id')->getChildren($v['id'], true); + $obj = Tree::instance()->init($childrenList, 'parent_id')->getTreeArray($v['parent_id']); + $objList = array_merge($objList, Tree::instance()->getTreeList($obj)); + } + $childrenDepartmenIds = []; + foreach ($objList as $k => $v) { + $childrenDepartmenIds[] = $v['id']; + } + if (!$withself) { + $childrenDepartmenIds = array_diff($childrenDepartmenIds, $departmenIds); + } + return $childrenDepartmenIds; + } + + + + /** + * 根据用户id获取部门组,返回值为数组 + * @param int $admin_id admin_id + * @return array 用户所属的部门 array( + * array('admin_id'=>'员工id','department_id'=>'部门id','name'=>'部门名称'), + * ...) + */ + public static function getDepartments($admin_id) + { + static $departments = []; + if (isset($departments[$admin_id])) { + return $departments[$admin_id]; + } + + // 执行查询 + $user_departments = Db::name('department_admin') + ->alias('da') + ->join('__'.strtoupper('department').'__ d', 'da.department_id = d.id', 'LEFT') + ->field('da.admin_id,da.department_id,d.id,d.parent_id,d.name') + ->where("da.admin_id='{$admin_id}' and is_principal=1 and d.status='normal'") + ->select(); + $departments[$admin_id] = $user_departments ?: []; + return $departments[$admin_id]; + } + +} \ No newline at end of file diff --git a/application/admin/model/department/AuthAdmin.php b/application/admin/model/department/AuthAdmin.php new file mode 100644 index 0000000000000000000000000000000000000000..7f438dacd1c846596f60d5303f620eae03d9312d --- /dev/null +++ b/application/admin/model/department/AuthAdmin.php @@ -0,0 +1,29 @@ +hasMany('\app\admin\model\department\Admin', 'admin_id', 'id'); + } + + /** + * 关联角色组 + * @return \think\model\relation\HasMany + */ + public function groups() + { + return $this->hasMany('\app\admin\model\department\AuthGroupAccess', 'uid', 'id'); + } + +} \ No newline at end of file diff --git a/application/admin/model/department/AuthGroupAccess.php b/application/admin/model/department/AuthGroupAccess.php new file mode 100644 index 0000000000000000000000000000000000000000..a5d16f6f52b06ed3d5ff0076f1bd0529f0bc143e --- /dev/null +++ b/application/admin/model/department/AuthGroupAccess.php @@ -0,0 +1,17 @@ +belongsTo('\app\admin\model\AuthGroup', 'group_id', 'id'); + } +} diff --git a/application/admin/model/department/Department.php b/application/admin/model/department/Department.php new file mode 100644 index 0000000000000000000000000000000000000000..02dfba5e9f85a39e5b454dd8964f57182c16f702 --- /dev/null +++ b/application/admin/model/department/Department.php @@ -0,0 +1,158 @@ +getPk(); + $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]); + self::clearCache(); + }); + self::afterDelete(function ($row) { + //删除时,删除子节点,同时将所有相关部门成员删除 + $childIds = self::getChildrenIds($row['id']); + if ($childIds) { + Department::destroy(function ($query) use ($childIds) { + $query->where('id', 'in', $childIds); + }); + } + $childIds[] = $row['id']; + db('department_admin')->where('department_id', 'in', $childIds)->delete(); + self::clearCache(); + }); + self::afterWrite(function ($row) use ($config) { + $changed = $row->getChangedData(); + //隐藏时判断是否有子节点,有则隐藏 + if (isset($changed['status']) && $changed['status'] == 'hidden') { + $childIds = self::getChildrenIds($row['id']); + db('department')->where('id', 'in', $childIds)->update(['status' => 'hidden']); + } + self::clearCache(); + + }); + } + + public static function getStatusList() + { + return ['normal' => __('Normal'), 'hidden' => __('Hidden')]; + } + + + /** + * 获取栏目的所有子节点ID + * @param int $id 栏目ID + * @param bool $withself 是否包含自身 + * @return array + */ + public static function getChildrenIds($id, $withself = false) + { + static $tree; + if (!$tree) { + $tree = \fast\Tree::instance(); + $tree->init(collection(self::order('weigh desc,id desc')->field('id,parent_id,name,status')->cache(self::$cachekey)->select())->toArray(), 'parent_id'); + } + $childIds = $tree->getChildrenIds($id, $withself); + return $childIds; + } + + /** + * 所有部门 + */ + public static function allDepartment() + { + return self::order('weigh desc,id desc')->where(['status' => 'normal'])->cache(self::$cachekey)->select(); + } + + /** + * 清空缓存 + * @param $name + */ + public static function clearCache() + { + Cache::rm(self::$cachekey); + } + + + /** + * 获取权限组 + * @param array $childrenGroupIds + * @param null $groups 如果不是超级管理员传auth->getGroups(),的值 + * @return array + */ + public static function getGroupdata($childrenGroupIds = [], $groups = null) + { + $groupList = collection(AuthGroup::where('id', 'in', $childrenGroupIds)->select())->toArray(); + Tree::instance()->init($groupList, 'pid'); + $groupdata = []; + if ($groups) { + $result = []; + foreach ($groups as $m => $n) { + $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id'])); + $temp = []; + foreach ($childlist as $k => $v) { + $temp[$v['id']] = $v['name']; + } + $result[__($n['name'])] = $temp; + } + $groupdata = $result; + + } else { + + $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); + foreach ($result as $k => $v) { + $groupdata[$v['id']] = $v['name']; + } + } + return $groupdata; + } + + /** + * 获取当前部门归属的最高组织id(最顶级) + */ + public static function getOrganiseID($departmentid) + { + if (!$departmentid) return 0; + $organise_id = self::where(['id' => $departmentid])->value('organise_id'); + return $organise_id ? $organise_id : $departmentid;//如果没有最高,本身自己就是最高; + + } + + +} \ No newline at end of file diff --git a/application/admin/view/department/admin/add.html b/application/admin/view/department/admin/add.html new file mode 100644 index 0000000000000000000000000000000000000000..ff0ace4ce543aae3d6bcd1e58f9af25f3616a131 --- /dev/null +++ b/application/admin/view/department/admin/add.html @@ -0,0 +1,54 @@ +
\ No newline at end of file diff --git a/application/admin/view/department/admin/edit.html b/application/admin/view/department/admin/edit.html new file mode 100644 index 0000000000000000000000000000000000000000..4e0dd1791bccf15833217e949f87e994cb103a85 --- /dev/null +++ b/application/admin/view/department/admin/edit.html @@ -0,0 +1,61 @@ + \ No newline at end of file diff --git a/application/admin/view/department/admin/index.html b/application/admin/view/department/admin/index.html new file mode 100644 index 0000000000000000000000000000000000000000..4fd8ac3bcc95e327df6e4648ec27c7d2754a640c --- /dev/null +++ b/application/admin/view/department/admin/index.html @@ -0,0 +1,117 @@ + + +