更新网站架构
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\Cms\Domain;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\facade\Db;
|
||||
use think\Request;
|
||||
use app\model\System\SystemDomainPool;
|
||||
|
||||
/**
|
||||
* 主域名池管理控制器
|
||||
*/
|
||||
class DomainPoolController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取域名池列表
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$page = $request->param('page', 1, 'int');
|
||||
$pageSize = $request->param('pageSize', 10, 'int');
|
||||
$mainDomain = $request->param('main_domain', '');
|
||||
$status = $request->param('status', '');
|
||||
|
||||
$where = [['delete_time', '=', null]];
|
||||
|
||||
if ($mainDomain) {
|
||||
$where[] = ['main_domain', 'like', "%$mainDomain%"];
|
||||
}
|
||||
if ($status !== '' && $status !== null) {
|
||||
$where[] = ['status', '=', $status];
|
||||
}
|
||||
|
||||
$list = SystemDomainPool::where($where)
|
||||
->page($page, $pageSize)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$total = SystemDomainPool::where($where)
|
||||
->count();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'list' => $list,
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取启用状态的主域名列表(供租户选择)
|
||||
*/
|
||||
public function getEnabledDomains()
|
||||
{
|
||||
$list = SystemDomainPool::where('status', 1)
|
||||
->where('delete_time', null)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $list
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主域名
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
$mainDomain = $request->param('main_domain', '');
|
||||
|
||||
if (empty($mainDomain)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '主域名不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查域名是否已存在
|
||||
$exists = SystemDomainPool::where('main_domain', $mainDomain)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '该域名已存在'
|
||||
]);
|
||||
}
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$id = SystemDomainPool::insertGetId([
|
||||
'main_domain' => $mainDomain,
|
||||
'status' => 1,
|
||||
'create_time' => $now,
|
||||
'update_time' => $now
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '创建成功',
|
||||
'data' => ['id' => $id]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑主域名
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$id = $request->param('id', 0, 'int');
|
||||
$mainDomain = $request->param('main_domain', '');
|
||||
$status = $request->param('status', null);
|
||||
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($mainDomain)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '主域名不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查域名是否与其他记录重复
|
||||
$exists = SystemDomainPool::where('main_domain', $mainDomain)
|
||||
->where('id', '<>', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '该域名已存在'
|
||||
]);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'main_domain' => $mainDomain,
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if ($status !== null) {
|
||||
$data['status'] = $status;
|
||||
}
|
||||
|
||||
SystemDomainPool::where('id', $id)->update($data);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除主域名(软删除)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
SystemDomainPool::where('id', $id)
|
||||
->update([
|
||||
'delete_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换主域名状态
|
||||
*/
|
||||
public function toggleStatus(Request $request)
|
||||
{
|
||||
$id = $request->param('id', 0, 'int');
|
||||
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
$domain = SystemDomainPool::where('id', $id)
|
||||
->find();
|
||||
|
||||
if (!$domain) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '域名不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
$newStatus = $domain['status'] == 1 ? 0 : 1;
|
||||
|
||||
SystemDomainPool::where('id', $id)
|
||||
->update([
|
||||
'status' => $newStatus,
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '状态更新成功',
|
||||
'data' => ['status' => $newStatus]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\Cms\Domain;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\facade\Db;
|
||||
use think\Request;
|
||||
use think\facade\Session;
|
||||
|
||||
/**
|
||||
* 租户域名绑定控制器
|
||||
*/
|
||||
class TenantDomainController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取租户域名列表(管理员查看所有)
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$page = $request->param('page', 1, 'int');
|
||||
$pageSize = $request->param('pageSize', 10, 'int');
|
||||
$tenantId = $request->param('tenant_id', 0, 'int');
|
||||
$status = $request->param('status', '');
|
||||
$subDomain = $request->param('sub_domain', '');
|
||||
|
||||
$where = [['delete_time', '=', null]];
|
||||
|
||||
if ($tenantId > 0) {
|
||||
$where[] = ['tenant_id', '=', $tenantId];
|
||||
}
|
||||
if ($status !== '' && $status !== null) {
|
||||
$where[] = ['status', '=', $status];
|
||||
}
|
||||
if ($subDomain) {
|
||||
$where[] = ['sub_domain', 'like', "%$subDomain%"];
|
||||
}
|
||||
|
||||
$list = Db::name('mete_tenant_domain')
|
||||
->where($where)
|
||||
->page($page, $pageSize)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$total = Db::name('mete_tenant_domain')
|
||||
->where($where)
|
||||
->count();
|
||||
|
||||
// 获取租户名称
|
||||
$tenantIds = array_column($list, 'tenant_id');
|
||||
$tenants = [];
|
||||
if ($tenantIds) {
|
||||
$tenantList = Db::name('mete_tenant')
|
||||
->whereIn('id', $tenantIds)
|
||||
->select()
|
||||
->toArray();
|
||||
$tenants = array_column($tenantList, null, 'id');
|
||||
}
|
||||
|
||||
// 附加租户名称
|
||||
foreach ($list as &$item) {
|
||||
$item['tenant_name'] = $tenants[$item['tenant_id']]['tenant_name'] ?? '';
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'list' => $list,
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户的域名列表(租户端)
|
||||
*/
|
||||
public function myDomains(Request $request)
|
||||
{
|
||||
$tid = $request->param('tid', 0, 'int');
|
||||
|
||||
if ($tid <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '租户ID不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
$list = Db::name('mete_tenant_domain')
|
||||
->where('tenant_id', $tid)
|
||||
->where('delete_time', null)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $list
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户申请二级域名
|
||||
*/
|
||||
public function apply(Request $request)
|
||||
{
|
||||
$tid = $request->param('tenant_id', 0, 'int');
|
||||
$subDomain = $request->param('sub_domain', '');
|
||||
$mainDomain = $request->param('main_domain', '');
|
||||
|
||||
if ($tid <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '租户ID不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($subDomain)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '二级域名前缀不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($mainDomain)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '请选择主域名'
|
||||
]);
|
||||
}
|
||||
|
||||
// 验证域名格式(只能包含字母、数字、连字符)
|
||||
if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$/', $subDomain)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '二级域名前缀格式不正确'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查主域名是否存在且启用
|
||||
$mainDomainInfo = Db::name('mete_system_domain_pool')
|
||||
->where('main_domain', $mainDomain)
|
||||
->where('status', 1)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$mainDomainInfo) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '主域名不存在或已禁用'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查二级域名是否已被使用
|
||||
$exists = Db::name('mete_tenant_domain')
|
||||
->where('sub_domain', $subDomain)
|
||||
->where('main_domain', $mainDomain)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '该二级域名已被使用'
|
||||
]);
|
||||
}
|
||||
|
||||
$fullDomain = $subDomain . '.' . $mainDomain;
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
$id = Db::name('mete_tenant_domain')->insertGetId([
|
||||
'tenant_id' => $tid,
|
||||
'sub_domain' => $subDomain,
|
||||
'main_domain' => $mainDomain,
|
||||
'full_domain' => $fullDomain,
|
||||
'status' => 0, // 审核中
|
||||
'create_time' => $now,
|
||||
'update_time' => $now
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '申请提交成功,等待审核',
|
||||
'data' => ['id' => $id]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核租户域名(通过/拒绝)
|
||||
*/
|
||||
public function audit(Request $request)
|
||||
{
|
||||
$id = $request->param('id', 0, 'int');
|
||||
$action = $request->param('action', ''); // 'approve' 或 'reject'
|
||||
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
$domain = Db::name('mete_tenant_domain')
|
||||
->where('id', $id)
|
||||
->find();
|
||||
|
||||
if (!$domain) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '域名不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
if ($domain['status'] != 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '该域名已审核过了'
|
||||
]);
|
||||
}
|
||||
|
||||
$newStatus = $action === 'approve' ? 1 : 2; // 1-已生效 2-已拒绝
|
||||
|
||||
Db::name('mete_tenant_domain')
|
||||
->where('id', $id)
|
||||
->update([
|
||||
'status' => $newStatus,
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
$msg = $action === 'approve' ? '审核通过' : '已拒绝';
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => $msg
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用/启用租户域名
|
||||
*/
|
||||
public function toggleStatus(Request $request)
|
||||
{
|
||||
$id = $request->param('id', 0, 'int');
|
||||
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
$domain = Db::name('mete_tenant_domain')
|
||||
->where('id', $id)
|
||||
->find();
|
||||
|
||||
if (!$domain) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '域名不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 只有已生效的域名才能被禁用
|
||||
if ($domain['status'] != 1) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '只有已生效的域名才能被禁用'
|
||||
]);
|
||||
}
|
||||
|
||||
// 切换为禁用状态
|
||||
Db::name('mete_tenant_domain')
|
||||
->where('id', $id)
|
||||
->update([
|
||||
'status' => 2,
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '已禁用'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除租户域名(软删除)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
Db::name('mete_tenant_domain')
|
||||
->where('id', $id)
|
||||
->update([
|
||||
'delete_time' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
}
|
||||
}
|
||||
+12
-5
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller;
|
||||
namespace app\admin\controller\Cms\Theme;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use app\service\ThemeService;
|
||||
@@ -20,14 +20,18 @@ class ThemeController extends BaseController
|
||||
$this->themeService = new ThemeService();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取模板列表(后台管理)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$tid = Request::get('tid', 0, 'int');
|
||||
|
||||
$themes = $this->themeService->getThemeList();
|
||||
$currentTheme = $this->themeService->getCurrentTheme();
|
||||
$currentTheme = $this->themeService->getCurrentTheme($tid);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
@@ -45,6 +49,7 @@ class ThemeController extends BaseController
|
||||
*/
|
||||
public function switch()
|
||||
{
|
||||
$tid = Request::post('tid', 0, 'int');
|
||||
$themeKey = Request::post('theme_key', '');
|
||||
|
||||
if (empty($themeKey)) {
|
||||
@@ -54,7 +59,7 @@ class ThemeController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
$result = $this->themeService->switchTheme($themeKey);
|
||||
$result = $this->themeService->switchTheme($tid, $themeKey);
|
||||
|
||||
if ($result) {
|
||||
return json([
|
||||
@@ -75,9 +80,10 @@ class ThemeController extends BaseController
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
$tid = Request::get('tid', 0, 'int');
|
||||
$themeKey = Request::get('theme_key', '');
|
||||
|
||||
$themeData = $this->themeService->getThemeData($themeKey ?: null);
|
||||
$themeData = $this->themeService->getThemeData($tid, $themeKey ?: null);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
@@ -92,6 +98,7 @@ class ThemeController extends BaseController
|
||||
*/
|
||||
public function saveData()
|
||||
{
|
||||
$tid = Request::post('tid', 0, 'int');
|
||||
$themeKey = Request::post('theme_key', '');
|
||||
$fieldKey = Request::post('field_key', '');
|
||||
$fieldValue = Request::post('field_value', '');
|
||||
@@ -103,7 +110,7 @@ class ThemeController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
$result = $this->themeService->saveThemeField($themeKey, $fieldKey, $fieldValue);
|
||||
$result = $this->themeService->saveThemeField($tid, $themeKey, $fieldKey, $fieldValue);
|
||||
|
||||
if ($result) {
|
||||
return json([
|
||||
@@ -13,6 +13,8 @@ use think\db\exception\DbException;
|
||||
use think\Request;
|
||||
use app\model\Tenant\Tenant;
|
||||
use app\model\AdminUser;
|
||||
use app\model\Template\TemplateSiteConfig;
|
||||
use app\model\Template\TemplateSiteConfig;
|
||||
|
||||
class TenantController extends BaseController
|
||||
{
|
||||
@@ -64,6 +66,9 @@ class TenantController extends BaseController
|
||||
$data = $this->request->post();
|
||||
$tenant = Tenant::create($data);
|
||||
if ($tenant) {
|
||||
// 创建租户默认数据
|
||||
$this->createTenantDefaultData($tenant->id);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '创建成功',
|
||||
@@ -76,6 +81,24 @@ class TenantController extends BaseController
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建租户默认数据
|
||||
* @param int $tenantId 租户ID
|
||||
*/
|
||||
private function createTenantDefaultData(int $tenantId)
|
||||
{
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
// 创建租户默认模板配置
|
||||
TemplateSiteConfig::create([
|
||||
'tid' => $tenantId,
|
||||
'key' => 'current_theme',
|
||||
'value' => 'default',
|
||||
'create_time' => $now,
|
||||
'update_time' => $now
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑租户
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
use think\facade\Route;
|
||||
|
||||
// 主域名池管理路由
|
||||
Route::group('domain/pool', function () {
|
||||
Route::get('index', 'app\admin\controller\Cms\Domain\DomainPoolController/index');
|
||||
Route::get('getEnabledDomains', 'app\admin\controller\Cms\Domain\DomainPoolController/getEnabledDomains');
|
||||
Route::post('create', 'app\admin\controller\Cms\Domain\DomainPoolController/create');
|
||||
Route::post('update', 'app\admin\controller\Cms\Domain\DomainPoolController/update');
|
||||
Route::delete('delete/:id', 'app\admin\controller\Cms\Domain\DomainPoolController/delete');
|
||||
Route::post('toggleStatus', 'app\admin\controller\Cms\Domain\DomainPoolController/toggleStatus');
|
||||
});
|
||||
|
||||
// 租户域名绑定路由
|
||||
Route::group('domain/tenant', function () {
|
||||
Route::get('index', 'app\admin\controller\Cms\Domain\TenantDomainController/index');
|
||||
Route::get('myDomains', 'app\admin\controller\Cms\Domain\TenantDomainController/myDomains');
|
||||
Route::post('apply', 'app\admin\controller\Cms\Domain\TenantDomainController/apply');
|
||||
Route::post('audit', 'app\admin\controller\Cms\Domain\TenantDomainController/audit');
|
||||
Route::post('toggleStatus', 'app\admin\controller\Cms\Domain\TenantDomainController/toggleStatus');
|
||||
Route::delete('delete/:id', 'app\admin\controller\Cms\Domain\TenantDomainController/delete');
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
use think\facade\Route;
|
||||
|
||||
// 模板管理路由
|
||||
Route::get('theme', 'app\admin\controller\ThemeController@index');
|
||||
Route::post('theme/switch', 'app\admin\controller\ThemeController@switch');
|
||||
Route::get('theme/data', 'app\admin\controller\ThemeController@getData');
|
||||
Route::post('theme/data', 'app\admin\controller\ThemeController@saveData');
|
||||
Route::get('theme', 'app\admin\controller\Cms\Theme\ThemeController@index');
|
||||
Route::post('theme/switch', 'app\admin\controller\Cms\Theme\ThemeController@switch');
|
||||
Route::get('theme/data', 'app\admin\controller\Cms\Theme\ThemeController@getData');
|
||||
Route::post('theme/data', 'app\admin\controller\Cms\Theme\ThemeController@saveData');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ use app\model\System\SystemSiteSettings;
|
||||
use app\service\ThemeService;
|
||||
use think\db\exception\DbException;
|
||||
use think\facade\Env;
|
||||
use think\facade\Request;
|
||||
use app\model\Template\TemplateSiteConfig;
|
||||
|
||||
class Index extends BaseController
|
||||
{
|
||||
@@ -33,16 +35,34 @@ class Index extends BaseController
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
// 直接返回默认模板数据
|
||||
// 获取租户ID(从请求参数)
|
||||
$tid = Request::param('tid', 0, 'int');
|
||||
|
||||
// 从TemplateSiteConfig获取配置(根据租户ID)
|
||||
$config = null;
|
||||
if ($tid > 0) {
|
||||
$config = TemplateSiteConfig::where('tid', $tid)
|
||||
->where('key', 'current_theme')
|
||||
->find();
|
||||
}
|
||||
|
||||
$themeKey = $config['value'] ?? 'default';
|
||||
|
||||
// 模板路径:/themes/{theme_key}/,优先使用index.php,其次index.html
|
||||
$themeBasePath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeKey;
|
||||
|
||||
if (is_file($themeBasePath . DIRECTORY_SEPARATOR . 'index.php')) {
|
||||
$themePath = '/themes/' . $themeKey . '/index.php';
|
||||
} else {
|
||||
$themePath = '/themes/' . $themeKey . '/index.html';
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'theme_key' => 'default',
|
||||
'theme_path' => '/themes/default/index.html',
|
||||
'data' => [
|
||||
'site_name' => '企业官网'
|
||||
]
|
||||
'theme_key' => $themeKey,
|
||||
'theme_path' => $themePath
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
return [
|
||||
\app\common\middleware\AllowCrossDomain::class,
|
||||
\app\common\middleware\DomainParse::class,
|
||||
\think\middleware\SessionInit::class,
|
||||
];
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\model\System;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 系统域名池模型
|
||||
*/
|
||||
class SystemDomainPool extends Model
|
||||
{
|
||||
|
||||
// 数据库表名
|
||||
protected $name = 'mete_system_domain_pool';
|
||||
|
||||
// 字段类型转换
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'main_domain' => 'string',
|
||||
'status' => 'integer',
|
||||
'create_time' => 'datetime',
|
||||
'update_time' => 'datetime',
|
||||
'delete_time' => 'datetime',
|
||||
];
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\model\Template;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
class TemplateSiteConfig extends Model
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $name = 'mete_template_site_config';
|
||||
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'tid' => 'integer',
|
||||
'key' => 'string',
|
||||
'value' => 'string',
|
||||
'create_time' => 'datetime',
|
||||
'update_time' => 'datetime',
|
||||
'delete_time' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\model\Template;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
class TemplateThemeData extends Model
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $name = 'mete_template_theme_data';
|
||||
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'theme_key' => 'string',
|
||||
'field_key' => 'string',
|
||||
'field_value' => 'string',
|
||||
'create_time' => 'datetime',
|
||||
'update_time' => 'datetime',
|
||||
'delete_time' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\model\Tenant;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 租户域名模型
|
||||
*/
|
||||
class TenantDomain extends Model
|
||||
{
|
||||
// 数据库表名
|
||||
protected $name = 'mete_tenant_domain';
|
||||
|
||||
// 字段类型转换
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'tenant_id' => 'integer',
|
||||
'sub_domain' => 'string',
|
||||
'main_domain' => 'string',
|
||||
'full_domain' => 'string',
|
||||
'status' => 'integer',
|
||||
'create_time' => 'datetime',
|
||||
'update_time' => 'datetime',
|
||||
'delete_time' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -67,7 +67,8 @@ class ThemeService
|
||||
}
|
||||
|
||||
$fullPath = $this->themesPath . DIRECTORY_SEPARATOR . $item;
|
||||
if (is_dir($fullPath) && is_file($fullPath . DIRECTORY_SEPARATOR . 'index.html')) {
|
||||
// 检查是否有 index.html 或 index.php
|
||||
if (is_dir($fullPath) && (is_file($fullPath . DIRECTORY_SEPARATOR . 'index.html') || is_file($fullPath . DIRECTORY_SEPARATOR . 'index.php'))) {
|
||||
$dirs[] = $item;
|
||||
}
|
||||
}
|
||||
@@ -114,14 +115,18 @@ class ThemeService
|
||||
|
||||
/**
|
||||
* 获取当前激活的模板Key
|
||||
* @param int $tid 租户ID
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrentTheme(): string
|
||||
public function getCurrentTheme(int $tid = 0): string
|
||||
{
|
||||
try {
|
||||
$where = [['key', '=', 'current_theme'], ['delete_time', '=', null]];
|
||||
if ($tid > 0) {
|
||||
$where[] = ['tid', '=', $tid];
|
||||
}
|
||||
$config = Db::name('mete_template_site_config')
|
||||
->where('key', 'current_theme')
|
||||
->where('delete_time', null)
|
||||
->where($where)
|
||||
->find();
|
||||
return $config['value'] ?? 'default';
|
||||
} catch (\Exception $e) {
|
||||
@@ -131,10 +136,11 @@ class ThemeService
|
||||
|
||||
/**
|
||||
* 切换当前模板
|
||||
* @param int $tid 租户ID
|
||||
* @param string $themeKey
|
||||
* @return bool
|
||||
*/
|
||||
public function switchTheme(string $themeKey): bool
|
||||
public function switchTheme(int $tid, string $themeKey): bool
|
||||
{
|
||||
// 验证模板是否存在
|
||||
$themes = $this->getThemeList();
|
||||
@@ -152,9 +158,12 @@ class ThemeService
|
||||
|
||||
try {
|
||||
// 查找或创建配置记录
|
||||
$where = [['key', '=', 'current_theme'], ['delete_time', '=', null]];
|
||||
if ($tid > 0) {
|
||||
$where[] = ['tid', '=', $tid];
|
||||
}
|
||||
$config = Db::name('mete_template_site_config')
|
||||
->where('key', 'current_theme')
|
||||
->where('delete_time', null)
|
||||
->where($where)
|
||||
->find();
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
@@ -181,17 +190,21 @@ class ThemeService
|
||||
|
||||
/**
|
||||
* 获取模板数据(用于前端渲染)
|
||||
* @param int $tid 租户ID
|
||||
* @param string|null $themeKey
|
||||
* @return array
|
||||
*/
|
||||
public function getThemeData(?string $themeKey = null): array
|
||||
public function getThemeData(int $tid = 0, ?string $themeKey = null): array
|
||||
{
|
||||
$themeKey = $themeKey ?? $this->getCurrentTheme();
|
||||
$themeKey = $themeKey ?? $this->getCurrentTheme($tid);
|
||||
|
||||
try {
|
||||
$where = [['theme_key', '=', $themeKey], ['delete_time', '=', null]];
|
||||
if ($tid > 0) {
|
||||
$where[] = ['tid', '=', $tid];
|
||||
}
|
||||
$themeData = Db::name('mete_template_theme_data')
|
||||
->where('theme_key', $themeKey)
|
||||
->where('delete_time', null)
|
||||
->where($where)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
@@ -216,18 +229,25 @@ class ThemeService
|
||||
|
||||
/**
|
||||
* 保存模板字段数据
|
||||
* @param int $tid 租户ID
|
||||
* @param string $themeKey
|
||||
* @param string $fieldKey
|
||||
* @param mixed $fieldValue
|
||||
* @return bool
|
||||
*/
|
||||
public function saveThemeField(string $themeKey, string $fieldKey, $fieldValue): bool
|
||||
public function saveThemeField(int $tid, string $themeKey, string $fieldKey, $fieldValue): bool
|
||||
{
|
||||
try {
|
||||
$where = [
|
||||
['theme_key', '=', $themeKey],
|
||||
['field_key', '=', $fieldKey],
|
||||
['delete_time', '=', null]
|
||||
];
|
||||
if ($tid > 0) {
|
||||
$where[] = ['tid', '=', $tid];
|
||||
}
|
||||
$existing = Db::name('mete_template_theme_data')
|
||||
->where('theme_key', $themeKey)
|
||||
->where('field_key', $fieldKey)
|
||||
->where('delete_time', null)
|
||||
->where($where)
|
||||
->find();
|
||||
|
||||
$value = is_array($fieldValue) ? json_encode($fieldValue, JSON_UNESCAPED_UNICODE) : $fieldValue;
|
||||
@@ -242,6 +262,7 @@ class ThemeService
|
||||
]);
|
||||
} else {
|
||||
Db::name('mete_template_theme_data')->insert([
|
||||
'tid' => $tid,
|
||||
'theme_key' => $themeKey,
|
||||
'field_key' => $fieldKey,
|
||||
'field_value' => $value,
|
||||
|
||||
Reference in New Issue
Block a user