model = new SiteGroup(); } /** * 站点分组列表 * @param array $where * @return array */ public function getPage(array $where = []) { $field = 'group_id, group_name, group_desc, app, addon, create_time, update_time'; $search_model = $this->model->withSearch(['keywords'],$where)->field($field)->order('create_time desc'); $list = $this->pageQuery($search_model); return $list; } /** * 获取所有分组 * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getAll() { $field = 'group_id, group_name, group_desc, create_time, update_time, app'; return $this->model->field($field)->select()->toArray(); } /** * 分组详情 * @param int $group_id * @return array */ public function getInfo(int $group_id) { $field = 'group_id, group_name, group_desc, app, addon, create_time, update_time'; return $this->model->where([['group_id', '=', $group_id]])->field($field)->findOrEmpty()->toArray(); } /** * 添加站点分组 * @param array $data * @return mixed */ public function add(array $data) { //判断应用是否全部是有效的已安装应用 $this->checkAddon(array_merge($data['app'], $data['addon'])); $res = $this->model->create($data); return $res->group_id; } /** * 修改站点分组 * @param int $group_id * @param array $data * @return true */ public function edit(int $group_id, array $data){ //判断应用是否全部是有效的已安装应用 $this->checkAddon(array_merge($data['app'], $data['addon'])); $this->model->update($data, [['group_id', '=', $group_id]]); //删除缓存 $cache_name = self::$cache_name . $group_id; Cache::delete($cache_name); $site_list = (new Site())->field('site_id')->where([ ['group_id', '=', $group_id] ])->select()->toArray(); if (!empty($site_list)) { foreach ($site_list as $site) { Cache::tag(CoreSiteService::$cache_tag_name . $site['site_id'])->clear(); } } return true; } public function checkAddon($group_roles){ $install_addon_list = (new CoreAddonService())->getInstallAddonList(); $install_addon_keys = array_column($install_addon_list, 'key'); if(count(array_intersect($install_addon_keys, $group_roles)) != count($group_roles)) throw new AdminException('SITE_GROUP_APP_NOT_EXIST'); return true; } /** * 删除分组 * @param int $group_id * @return bool * @throws DbException */ public function del(int $group_id) { $count = (new Site())->where([['group_id', '=', $group_id]])->count(); if($count > 0) { throw new CommonException('SITE_GROUP_IS_EXIST'); } $res = $this->model->where([['group_id', '=', $group_id]])->delete(); $cache_name = self::$cache_name . $group_id; Cache::delete($cache_name); return $res; } /** * 通过站点分组获取站点包含的权限和应用 * @param $group_id * @return void */ public function getGroupAddon($group_id){ $cache_name = self::$cache_name . $group_id; return cache_remember( $cache_name, function () use ($group_id) { $group = $this->model->findOrEmpty($group_id); $addon = []; if (!$group->isEmpty()) { $addon = array_merge([ $group['app'] ], $group['addon']); } return $addon; }, [MenuService::$cache_tag_name,self::$cache_tag_name] ); } /** * 创建所有权限的菜单 */ public function addAllMenuGroup() { // $menus = (new SysMenu())->where([['app_type', '=', 'site']])->column("menu_key"); $data = [ 'group_name' => "默认套餐", 'group_desc' => '', 'app' => '', 'addon' => [] ]; return $this->add($data); } }