TenantLists.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\lists\tenant;
  15. use app\tenantapi\lists\BaseAdminDataLists;
  16. use app\common\enum\user\UserTerminalEnum;
  17. use app\common\lists\ListsExcelInterface;
  18. use app\common\model\tenant\Tenant;
  19. /**
  20. * 用户列表
  21. * Class TenantLists
  22. * @package app\tenantapi\lists\user
  23. */
  24. class TenantLists extends BaseAdminDataLists implements ListsExcelInterface
  25. {
  26. /**
  27. * @notes 搜索条件
  28. * @return array
  29. * @author 段誉
  30. * @date 2022/9/22 15:50
  31. */
  32. public function setSearch(): array
  33. {
  34. $allowSearch = ['keyword', 'create_time_start', 'create_time_end'];
  35. return array_intersect(array_keys($this->params), $allowSearch);
  36. }
  37. /**
  38. * @notes 获取用户列表
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @author 段誉
  44. * @date 2022/9/22 15:50
  45. */
  46. public function lists(): array
  47. {
  48. $field = "id,sn,name,avatar,disable,create_time,domain_alias,domain_alias_enable,notes,tel";
  49. $lists = Tenant::withCount(['users'])
  50. ->withSearch($this->setSearch(), $this->params)
  51. ->limit($this->limitOffset, $this->limitLength)
  52. ->field($field)
  53. ->order('id desc')
  54. ->select()->toArray();
  55. $domain = self::getRootDmain(request()->domain());
  56. // 遍历结果,添加 link 字段
  57. return array_map(function ($item) use ($domain) {
  58. // 拼接租户的链接 http://[sn].likeadmin-saas.localhost/admin/
  59. $http_prefix = self::checkHttp() ? 'https://' : 'http://';
  60. $item['default_domain'] = $http_prefix . $item['sn'] . '.' . $domain . '/tenant/';
  61. if ($item['domain_alias_enable'] === 0) {
  62. $item['domain'] = $http_prefix . $item['domain_alias'] . '/tenant/';
  63. } else {
  64. $item['domain'] = $item['default_domain'];
  65. }
  66. return $item;
  67. }, $lists);
  68. }
  69. /**
  70. * @notes 获取数量
  71. * @return int
  72. * @author 段誉
  73. * @date 2022/9/22 15:51
  74. */
  75. public function count(): int
  76. {
  77. return Tenant::withSearch($this->setSearch(), $this->params)->count();
  78. }
  79. /**
  80. * @notes 导出文件名
  81. * @return string
  82. * @author 段誉
  83. * @date 2022/11/24 16:17
  84. */
  85. public function setFileName(): string
  86. {
  87. return '租户列表';
  88. }
  89. /**
  90. * @notes 导出字段
  91. * @return string[]
  92. * @author 段誉
  93. * @date 2022/11/24 16:17
  94. */
  95. public function setExcelFields(): array
  96. {
  97. return [
  98. 'sn' => '租户编号',
  99. 'name' => '租户昵称',
  100. 'disable' => '租户状态',
  101. 'create_time' => '注册时间',
  102. ];
  103. }
  104. /**
  105. * @notes 检查是否为https
  106. * @return bool
  107. * @author JXDN
  108. * @date 2024/09/11 14:39
  109. */
  110. public static function checkHttp()
  111. {
  112. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * @notes 获取根域名
  120. * @param $url
  121. * @return array|int|string|null
  122. * @author JXDN
  123. * @date 2024/09/11 14:49
  124. */
  125. public static function getRootDmain($url)
  126. {
  127. // 解析 URL 获取主机名
  128. $host = parse_url($url, PHP_URL_HOST);
  129. // 如果主机名为空,返回 null
  130. if (!$host) {
  131. return null;
  132. }
  133. // 拆分域名
  134. $parts = explode('.', $host);
  135. // 检查域名的级数
  136. $numParts = count($parts);
  137. // 针对常见的两级或三级域名进行处理
  138. if ($numParts >= 2) {
  139. // 获取最后两部分,例如 qq.com 或 co.uk
  140. $rootDomain = $parts[$numParts - 2] . '.' . $parts[$numParts - 1];
  141. return $rootDomain;
  142. }
  143. return $host; // 当域名本身就是根域名时,直接返回
  144. }
  145. }