123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- // +----------------------------------------------------------------------
- // | Niucloud-admin 企业快速开发的saas管理平台
- // +----------------------------------------------------------------------
- // | 官方网址:https://www.niucloud.com
- // +----------------------------------------------------------------------
- // | niucloud团队 版权所有 开源版本可自由商用
- // +----------------------------------------------------------------------
- // | Author: Niucloud Team
- // +----------------------------------------------------------------------
- namespace app\service\admin\site;
- use app\dict\addon\AddonDict;
- use app\dict\sys\AppTypeDict;
- use app\model\addon\Addon;
- use app\model\site\Site;
- use app\service\admin\addon\AddonService;
- use app\service\admin\auth\AuthService;
- use app\service\admin\sys\MenuService;
- use app\service\admin\sys\RoleService;
- use app\service\core\site\CoreSiteService;
- use core\base\BaseAdminService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * 站点服务层
- * Class Site
- * @package app\service\admin\site
- */
- class SiteService extends BaseAdminService
- {
- public static $cache_tag_name = 'site_cash';
- public function __construct()
- {
- parent::__construct();
- $this->model = new Site();
- }
- /**
- * 站点信息
- * @param int $site_id
- * @return array
- */
- public function getInfo(int $site_id)
- {
- $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
- district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app, addons, site_domain';
- $info = $this->model->where([ [ 'site_id', '=', $site_id ] ])->with([ 'groupName' ])->field($field)->append([ 'status_name' ])->findOrEmpty()->toArray();
- if (!empty($info)) {
- $site_addons = (new CoreSiteService())->getAddonKeysBySiteId($site_id);
- $info['site_addons'] = (new Addon())->where([ ['key', 'in', $site_addons]])->field('key,title,desc,icon,type')->select()->toArray();
- }
- return $info;
- }
- /**
- * 修改站点基础信息
- * @param int $site_id
- * @param array $data
- * @return bool
- */
- public function editBasicInfo(int $site_id, array $data)
- {
- return ( new CoreSiteService() )->editBasicInfo($site_id, $data);
- }
- /**
- * 站点数量
- * @return int
- * @throws DbException
- */
- public function getCount(array $where = [])
- {
- $condition = [
- ['site_id', '<>', 0]
- ];
- if(!empty($where['group_id'])){
- $condition[] = ['group_id', '=', $where['group_id']];
- }
- if(!empty($where['create_time'])){
- $condition[] = ['create_time', 'between', $where['create_time']];
- }
- return $this->model->where($condition)->count();
- }
- /**
- * 获取授权当前站点信息(用做缓存)
- * @return mixed
- */
- public function getSiteCache(int $site_id)
- {
- return (new CoreSiteService())->getSiteCache($site_id);
- }
- /**
- * 通过站点id获取菜单列表
- * @param int $site_id
- * @param $is_tree
- * @param $status
- * @param $addon - 所以应用名一般不建议叫all
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getMenuList(int $site_id, $is_tree, $status, $addon = 'all', int $is_button = 1)
- {
- $site_info = $this->getSiteCache($site_id);
- if (empty($site_info))
- return [];
- $app_type = $site_info[ 'app_type' ];
- if (AuthService::isSuperAdmin()) {
- $is_admin = 1;
- } else {
- $user_role_info = (new AuthService())->getAuthRole($this->site_id);
- if(empty($user_role_info))
- return [];
- $is_admin = $user_role_info['is_admin'];//是否是超级管理员组
- }
- if ($is_admin) {
- return ( new MenuService() )->getAllMenuList($app_type, $status, $is_tree, $is_button);
- } else {
- $user_role_ids = $user_role_info['role_ids'];
- $role_service = new RoleService();
- $menu_keys = $role_service->getMenuIdsByRoleIds($this->site_id, $user_role_ids);
- return ( new MenuService() )->getMenuListByMenuKeys($this->site_id, $menu_keys, $this->app_type, $is_tree, $addon, $is_button);
- }
- }
- /**
- * 通过站点id获取站点菜单极限
- * @param int $site_id
- * @param $status
- * @return array|mixed|string|null
- */
- public function getMenuIdsBySiteId(int $site_id, $status)
- {
- $site_info = $this->getSiteCache($site_id);
- if (empty($site_info))
- return [];
- $app_type = $site_info[ 'app_type' ];
- if ($app_type == AppTypeDict::ADMIN) {
- return ( new MenuService() )->getAllMenuIdsByAppType($app_type, $status);
- } else {
- $addons = ( new AddonService() )->getAddonKeysBySiteId($site_id);
- return ( new MenuService() )->getMenuKeysBySystem($app_type, $addons);
- }
- }
- /**
- * 通过站点id获取菜单列表
- * @param int $site_id
- * @param $status
- * @return mixed
- */
- public function getApiList(int $site_id, $status)
- {
- $site_info = $this->getSiteCache($site_id);
- if (empty($site_info))
- return [];
- $app_type = $site_info[ 'app_type' ];
- if ($app_type == AppTypeDict::ADMIN) {
- return ( new MenuService() )->getAllApiList($app_type, $status);
- } else {
- $addons = ( new AddonService() )->getAddonKeysBySiteId($site_id);
- return ( new MenuService() )->getApiListBySystem($app_type, $addons);
- }
- }
- /**
- * 获取站点的插件
- * @return array
- */
- public function getSiteAddons(array $where) {
- $site_addon = (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
- return (new Addon())->where([['type', '=', AddonDict::ADDON], ['status', '=', AddonDict::ON], ['key', 'in', $site_addon ]])->withSearch(['title'], $where)->append(['status_name'])->field('title, icon, key, desc, status, type, support_app')->select()->toArray();
- }
- /**
- * 获取站点支持的应用插件
- * @return array
- */
- public function getAddonKeysBySiteId() {
- return (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
- }
- /**
- * 获取店铺端应用列表
- * @param array $where
- * @return array
- */
- public function getSiteAppTools(array $where) {
- $site_addon = (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
- $addons = (new Addon())->where([['type', '=', AddonDict::ADDON], ['status', '=', AddonDict::ON], ['key', 'in', $site_addon ]])->withSearch(['title'], $where)->field('title, icon, key, desc, status, type, support_app')->select()->toArray();
- return (new CoreSiteService())->getShowAppTools($addons, AppTypeDict::SITE);
- }
- /**
- * 获取平台端应用列表
- * @param array $where
- * @return array
- */
- public function getAdminAppTools(array $where) {
- $addons = (new Addon())->where([['type', '=', AddonDict::ADDON], ['status', '=', AddonDict::ON]])->withSearch(['title'], $where)->field('title, icon, key, desc, status, type, support_app')->select()->toArray();
- return (new CoreSiteService())->getShowAppTools($addons, AppTypeDict::ADMIN);
- }
- }
|