AuthSiteService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\home;
  12. use app\model\site\Site;
  13. use app\model\sys\SysUserRole;
  14. use app\service\admin\auth\AuthService;
  15. use core\base\BaseAdminService;
  16. use core\exception\HomeException;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * 用户服务层
  22. * Class BaseService
  23. * @package app\service
  24. */
  25. class AuthSiteService extends BaseAdminService
  26. {
  27. public static $role_cache_name = 'user_site_cache';
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->model = new Site();
  32. }
  33. /**
  34. * 获取授权当前的站点信息
  35. */
  36. public function getSiteInfo($site_id){
  37. $this->checkSite($site_id);
  38. //通过用户id获取
  39. $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  40. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app';
  41. return $this->model->where([ [ 'site_id', '=', $site_id ] ])->with(['groupName', 'addon'])->field($field)->append([ 'status_name' ])->findOrEmpty()->toArray();
  42. }
  43. /**
  44. * 获取授权账号下的站点列表
  45. * @param array $where
  46. * @return array
  47. * @throws DbException
  48. */
  49. public function getSitePage(array $where = [])
  50. {
  51. $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  52. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app';
  53. $condition = [
  54. ['site_id', '<>', request()->defaultSiteId()],
  55. ];
  56. if (!AuthService::isSuperAdmin()) $condition[] = ['site_id', 'in', $this->getSiteIds()];
  57. $search_model = $this->model->where($condition)->withSearch([ 'create_time', 'expire_time', 'keywords', 'status', 'group_id', 'app' ], $where)->with(['groupName'])->field($field)->append([ 'status_name' ])->order('create_time desc');
  58. return $this->pageQuery($search_model);
  59. }
  60. /**
  61. * 查询用户角色类表
  62. * @return mixed|string
  63. * @throws DbException
  64. * @throws DataNotFoundException
  65. * @throws ModelNotFoundException
  66. */
  67. public function getSiteIds(){
  68. $cache_name = 'user_role_list_'.$this->uid;
  69. return cache_remember(
  70. $cache_name,
  71. function(){
  72. $user_role_model = new SysUserRole();
  73. $where = [
  74. ['uid', '=', $this->uid],
  75. ['site_id', '<>', request()->defaultSiteId()],
  76. ['status', '=', 1]
  77. ];
  78. $list = $user_role_model->where($where)->select()->toArray();
  79. return array_column($list, 'site_id');
  80. },
  81. [self::$role_cache_name]
  82. );
  83. }
  84. /**
  85. * 编辑站点信息
  86. * @param int $site_id
  87. * @param array $data
  88. * @return true
  89. */
  90. public function editSite(int $site_id, array $data){
  91. $this->checkSite($site_id);
  92. $this->model->where([['site_id', '=', $site_id]])->update($data);
  93. return true;
  94. }
  95. /**
  96. * 校验是否合法
  97. * @param $site_id
  98. * @return void
  99. */
  100. public function checkSite($site_id){
  101. $site_ids = $this->getSiteIds();
  102. if(!in_array($site_id, $site_ids)) throw new HomeException('USER_ROLE_NOT_HAS_SITE');//无效的站点
  103. }
  104. }