更新租户端网站显示
This commit is contained in:
@@ -13,7 +13,7 @@ use app\service\ThemeService;
|
||||
use think\db\exception\DbException;
|
||||
use think\facade\Env;
|
||||
use think\facade\Request;
|
||||
use app\model\Template\TemplateSiteConfig;
|
||||
use app\model\Cms\TemplateSiteConfig;
|
||||
|
||||
class Index extends BaseController
|
||||
{
|
||||
@@ -26,8 +26,102 @@ class Index extends BaseController
|
||||
|
||||
public function index()
|
||||
{
|
||||
// 获取请求对象
|
||||
$request = $this->request ?? Request::instance();
|
||||
|
||||
// 优先从请求对象获取中间件设置的租户ID(通过域名解析)
|
||||
$tid = $request->tenantId ?? 0;
|
||||
|
||||
// 兼容:从header获取
|
||||
if ($tid == 0) {
|
||||
$tid = (int)$request->header('X-Tenant-Id', 0);
|
||||
}
|
||||
|
||||
// 调试:检查tid
|
||||
if ($tid === 0) {
|
||||
die('tenantId未获取到,请检查域名解析');
|
||||
}
|
||||
|
||||
// 获取租户选择的模板
|
||||
$themeKey = 'default';
|
||||
if ($tid > 0) {
|
||||
// 不使用软删除过滤
|
||||
$config = TemplateSiteConfig::where('tid', $tid)
|
||||
->where('key', 'current_theme')
|
||||
->withoutField('delete_time')
|
||||
->find();
|
||||
$themeKey = $config['value'] ?? 'default';
|
||||
}
|
||||
|
||||
// 模板路径:/themes/{theme_key}/,优先使用index.php,其次index.html
|
||||
$themeBasePath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeKey;
|
||||
$themeUrlPath = '/themes/' . $themeKey . '/';
|
||||
|
||||
if (is_file($themeBasePath . DIRECTORY_SEPARATOR . 'index.php')) {
|
||||
// 渲染PHP模板(直接include)
|
||||
return $this->renderPhpTemplate($themeBasePath . DIRECTORY_SEPARATOR . 'index.php', $themeUrlPath);
|
||||
} elseif (is_file($themeBasePath . DIRECTORY_SEPARATOR . 'index.html')) {
|
||||
// 读取HTML模板内容
|
||||
$content = file_get_contents($themeBasePath . DIRECTORY_SEPARATOR . 'index.html');
|
||||
// 修复资源路径:将相对路径转换为绝对路径
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
// 模板不存在,返回默认
|
||||
return view('index/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复模板中的资源路径
|
||||
* 将相对路径 (assets/, css/, js/, images/) 转换为绝对路径 (/themes/xxx/)
|
||||
*/
|
||||
private function fixTemplateAssets(string $content, string $themeUrlPath): string
|
||||
{
|
||||
// 需要修复的资源目录
|
||||
$assetsDirs = ['assets', 'css', 'js', 'images', 'img', 'fonts', 'plugins', 'libs'];
|
||||
|
||||
foreach ($assetsDirs as $dir) {
|
||||
// 匹配 src="assets/..." 或 href="css/..." 等相对路径
|
||||
// 不匹配已以 / 开头的绝对路径
|
||||
$content = preg_replace(
|
||||
'/(src|href)=["\'](' . $dir . '\/[^"\']*)["\']/',
|
||||
'$1="' . $themeUrlPath . '$2"',
|
||||
$content
|
||||
);
|
||||
// 匹配 src='assets/...'
|
||||
$content = preg_replace(
|
||||
'/(src|href)=[\']([^\/][^\'\"]*\/[^\'\"]*)[\']/',
|
||||
'$1="' . $themeUrlPath . '$2"',
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染PHP模板文件
|
||||
*/
|
||||
private function renderPhpTemplate(string $templateFile, string $themeUrlPath = '')
|
||||
{
|
||||
// 定义模板基础URL常量,供模板使用
|
||||
if (!defined('THEME_URL')) {
|
||||
define('THEME_URL', $themeUrlPath);
|
||||
}
|
||||
|
||||
ob_start();
|
||||
// 获取URL参数
|
||||
$getParams = Request::get();
|
||||
extract($getParams, EXTR_OVERWRITE);
|
||||
include $templateFile;
|
||||
$content = ob_get_clean();
|
||||
|
||||
// 修复PHP模板输出中的资源路径
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 前端初始化接口 - 返回当前模板和填充数据
|
||||
@@ -35,8 +129,11 @@ class Index extends BaseController
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
// 获取租户ID(从请求参数)
|
||||
$tid = Request::param('tid', 0, 'int');
|
||||
// 优先从请求对象获取中间件设置的租户ID(通过域名解析)
|
||||
$tid = $this->request->tenantId ?? 0;
|
||||
|
||||
// 兼容:URL参数传递的tid作为备用
|
||||
$tid = $tid > 0 ? $tid : Request::param('tid', 0, 'int');
|
||||
|
||||
// 从TemplateSiteConfig获取配置(根据租户ID)
|
||||
$config = null;
|
||||
|
||||
Reference in New Issue
Block a user