更新网站架构
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\middleware;
|
||||
|
||||
use think\facade\Db;
|
||||
use think\Request;
|
||||
|
||||
/**
|
||||
* 域名解析中间件
|
||||
* 通过访问域名自动识别租户
|
||||
*/
|
||||
class DomainParse
|
||||
{
|
||||
public function handle(Request $request, \Closure $next)
|
||||
{
|
||||
$host = $request->host(true); // 获取完整域名,不带端口
|
||||
|
||||
// 排除后台域名和平台官网域名
|
||||
$adminDomains = ['admin.xxx.com']; // TODO: 配置后台域名
|
||||
$platformDomains = ['www.xxx.com', 'xxx.com']; // TODO: 配置平台官网域名
|
||||
|
||||
if (in_array($host, $adminDomains) || in_array($host, $platformDomains)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// 解析二级域名
|
||||
$domainParts = explode('.', $host);
|
||||
|
||||
// 至少需要三级域名(如 sub.domain.com)
|
||||
if (count($domainParts) < 3) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$subDomain = $domainParts[0];
|
||||
$mainDomain = implode('.', array_slice($domainParts, 1));
|
||||
|
||||
// 查询租户域名绑定记录
|
||||
$tenantDomain = Db::name('mete_tenant_domain')
|
||||
->where('full_domain', $host)
|
||||
->where('status', 1) // 只有已生效的域名才能访问
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($tenantDomain) {
|
||||
// 将租户ID写入请求对象,供后续控制器使用
|
||||
$request->tenantId = $tenantDomain['tenant_id'];
|
||||
|
||||
// 同时写入header,方便前端获取
|
||||
$request->header['X-Tenant-Id', $tenantDomain['tenant_id']);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user