SiteService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\site;
  12. use app\dict\addon\AddonDict;
  13. use app\dict\sys\AppTypeDict;
  14. use app\model\addon\Addon;
  15. use app\model\site\Site;
  16. use app\service\admin\addon\AddonService;
  17. use app\service\admin\auth\AuthService;
  18. use app\service\admin\sys\MenuService;
  19. use app\service\admin\sys\RoleService;
  20. use app\service\core\site\CoreSiteService;
  21. use core\base\BaseAdminService;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. /**
  26. * 站点服务层
  27. * Class Site
  28. * @package app\service\admin\site
  29. */
  30. class SiteService extends BaseAdminService
  31. {
  32. public static $cache_tag_name = 'site_cash';
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. $this->model = new Site();
  37. }
  38. /**
  39. * 站点信息
  40. * @param int $site_id
  41. * @return array
  42. */
  43. public function getInfo(int $site_id)
  44. {
  45. $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  46. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app, addons, site_domain';
  47. $info = $this->model->where([ [ 'site_id', '=', $site_id ] ])->with([ 'groupName' ])->field($field)->append([ 'status_name' ])->findOrEmpty()->toArray();
  48. if (!empty($info)) {
  49. $site_addons = (new CoreSiteService())->getAddonKeysBySiteId($site_id);
  50. $info['site_addons'] = (new Addon())->where([ ['key', 'in', $site_addons]])->field('key,title,desc,icon,type')->select()->toArray();
  51. }
  52. return $info;
  53. }
  54. /**
  55. * 修改站点基础信息
  56. * @param int $site_id
  57. * @param array $data
  58. * @return bool
  59. */
  60. public function editBasicInfo(int $site_id, array $data)
  61. {
  62. return ( new CoreSiteService() )->editBasicInfo($site_id, $data);
  63. }
  64. /**
  65. * 站点数量
  66. * @return int
  67. * @throws DbException
  68. */
  69. public function getCount(array $where = [])
  70. {
  71. $condition = [
  72. ['site_id', '<>', 0]
  73. ];
  74. if(!empty($where['group_id'])){
  75. $condition[] = ['group_id', '=', $where['group_id']];
  76. }
  77. if(!empty($where['create_time'])){
  78. $condition[] = ['create_time', 'between', $where['create_time']];
  79. }
  80. return $this->model->where($condition)->count();
  81. }
  82. /**
  83. * 获取授权当前站点信息(用做缓存)
  84. * @return mixed
  85. */
  86. public function getSiteCache(int $site_id)
  87. {
  88. return (new CoreSiteService())->getSiteCache($site_id);
  89. }
  90. /**
  91. * 通过站点id获取菜单列表
  92. * @param int $site_id
  93. * @param $is_tree
  94. * @param $status
  95. * @param $addon - 所以应用名一般不建议叫all
  96. * @return mixed
  97. * @throws DataNotFoundException
  98. * @throws DbException
  99. * @throws ModelNotFoundException
  100. */
  101. public function getMenuList(int $site_id, $is_tree, $status, $addon = 'all', int $is_button = 1)
  102. {
  103. $site_info = $this->getSiteCache($site_id);
  104. if (empty($site_info))
  105. return [];
  106. $app_type = $site_info[ 'app_type' ];
  107. if (AuthService::isSuperAdmin()) {
  108. $is_admin = 1;
  109. } else {
  110. $user_role_info = (new AuthService())->getAuthRole($this->site_id);
  111. if(empty($user_role_info))
  112. return [];
  113. $is_admin = $user_role_info['is_admin'];//是否是超级管理员组
  114. }
  115. if ($is_admin) {
  116. return ( new MenuService() )->getAllMenuList($app_type, $status, $is_tree, $is_button);
  117. } else {
  118. $user_role_ids = $user_role_info['role_ids'];
  119. $role_service = new RoleService();
  120. $menu_keys = $role_service->getMenuIdsByRoleIds($this->site_id, $user_role_ids);
  121. return ( new MenuService() )->getMenuListByMenuKeys($this->site_id, $menu_keys, $this->app_type, $is_tree, $addon, $is_button);
  122. }
  123. }
  124. /**
  125. * 通过站点id获取站点菜单极限
  126. * @param int $site_id
  127. * @param $status
  128. * @return array|mixed|string|null
  129. */
  130. public function getMenuIdsBySiteId(int $site_id, $status)
  131. {
  132. $site_info = $this->getSiteCache($site_id);
  133. if (empty($site_info))
  134. return [];
  135. $app_type = $site_info[ 'app_type' ];
  136. if ($app_type == AppTypeDict::ADMIN) {
  137. return ( new MenuService() )->getAllMenuIdsByAppType($app_type, $status);
  138. } else {
  139. $addons = ( new AddonService() )->getAddonKeysBySiteId($site_id);
  140. return ( new MenuService() )->getMenuKeysBySystem($app_type, $addons);
  141. }
  142. }
  143. /**
  144. * 通过站点id获取菜单列表
  145. * @param int $site_id
  146. * @param $status
  147. * @return mixed
  148. */
  149. public function getApiList(int $site_id, $status)
  150. {
  151. $site_info = $this->getSiteCache($site_id);
  152. if (empty($site_info))
  153. return [];
  154. $app_type = $site_info[ 'app_type' ];
  155. if ($app_type == AppTypeDict::ADMIN) {
  156. return ( new MenuService() )->getAllApiList($app_type, $status);
  157. } else {
  158. $addons = ( new AddonService() )->getAddonKeysBySiteId($site_id);
  159. return ( new MenuService() )->getApiListBySystem($app_type, $addons);
  160. }
  161. }
  162. /**
  163. * 获取站点的插件
  164. * @return array
  165. */
  166. public function getSiteAddons(array $where) {
  167. $site_addon = (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
  168. 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();
  169. }
  170. /**
  171. * 获取站点支持的应用插件
  172. * @return array
  173. */
  174. public function getAddonKeysBySiteId() {
  175. return (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
  176. }
  177. /**
  178. * 获取店铺端应用列表
  179. * @param array $where
  180. * @return array
  181. */
  182. public function getSiteAppTools(array $where) {
  183. $site_addon = (new CoreSiteService())->getAddonKeysBySiteId($this->site_id);
  184. $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();
  185. return (new CoreSiteService())->getShowAppTools($addons, AppTypeDict::SITE);
  186. }
  187. /**
  188. * 获取平台端应用列表
  189. * @param array $where
  190. * @return array
  191. */
  192. public function getAdminAppTools(array $where) {
  193. $addons = (new Addon())->where([['type', '=', AddonDict::ADDON], ['status', '=', AddonDict::ON]])->withSearch(['title'], $where)->field('title, icon, key, desc, status, type, support_app')->select()->toArray();
  194. return (new CoreSiteService())->getShowAppTools($addons, AppTypeDict::ADMIN);
  195. }
  196. }