新增模板,修复模板选择bug
This commit is contained in:
@@ -74,6 +74,121 @@ class Index extends BaseController
|
||||
return view('index/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用页面渲染
|
||||
* 根据页面名称(如 about, blog, portfolio 等)渲染对应模板
|
||||
* @param string $page 页面名称
|
||||
* @return \think\response|\think\response\View
|
||||
*/
|
||||
public function page(string $page = 'index')
|
||||
{
|
||||
$request = $this->request ?? Request::instance();
|
||||
|
||||
// 获取租户ID
|
||||
$tid = $request->tenantId ?? 0;
|
||||
if ($tid == 0) {
|
||||
$headerTid = $request->header('X-Tenant-Id');
|
||||
$tid = $headerTid ? (int) $headerTid : 0;
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
// 模板路径
|
||||
$themeBasePath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeKey;
|
||||
$themeUrlPath = '/themes/' . $themeKey . '/';
|
||||
|
||||
// 安全检查:只允许字母、数字、中划线
|
||||
$page = preg_replace('/[^a-zA-Z0-9\-]/', '', $page);
|
||||
if (empty($page)) {
|
||||
$page = 'index';
|
||||
}
|
||||
|
||||
// 查找 PHP 模板(优先)或 HTML 模板
|
||||
$templateFile = $themeBasePath . DIRECTORY_SEPARATOR . $page . '.php';
|
||||
$templateHtmlFile = $themeBasePath . DIRECTORY_SEPARATOR . $page . '.html';
|
||||
|
||||
if (is_file($templateFile)) {
|
||||
return $this->renderPhpTemplate($templateFile, $themeUrlPath);
|
||||
} elseif (is_file($templateHtmlFile)) {
|
||||
$content = file_get_contents($templateHtmlFile);
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
// 模板不存在
|
||||
return response('页面不存在', 404, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章详情页
|
||||
* @param int $id 文章ID
|
||||
* @return \think\response
|
||||
*/
|
||||
public function articleDetail(int $id = 0)
|
||||
{
|
||||
$request = $this->request ?? Request::instance();
|
||||
|
||||
// 获取租户ID
|
||||
$tid = $request->tenantId ?? 0;
|
||||
if ($tid == 0) {
|
||||
$headerTid = $request->header('X-Tenant-Id');
|
||||
$tid = $headerTid ? (int) $headerTid : 0;
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
// 模板路径
|
||||
$themeBasePath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeKey;
|
||||
$themeUrlPath = '/themes/' . $themeKey . '/';
|
||||
|
||||
// 查找文章详情模板
|
||||
$templateFile = $themeBasePath . DIRECTORY_SEPARATOR . 'article_detail.php';
|
||||
$templateHtmlFile = $themeBasePath . DIRECTORY_SEPARATOR . 'article_detail.html';
|
||||
$blogDetailsFile = $themeBasePath . DIRECTORY_SEPARATOR . 'blog-details.php';
|
||||
$blogDetailsHtmlFile = $themeBasePath . DIRECTORY_SEPARATOR . 'blog-details.html';
|
||||
|
||||
// 优先使用 article_detail 模板
|
||||
if (is_file($templateFile)) {
|
||||
return $this->renderPhpTemplate($templateFile, $themeUrlPath);
|
||||
} elseif (is_file($templateHtmlFile)) {
|
||||
$content = file_get_contents($templateHtmlFile);
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
} elseif (is_file($blogDetailsFile)) {
|
||||
return $this->renderPhpTemplate($blogDetailsFile, $themeUrlPath);
|
||||
} elseif (is_file($blogDetailsHtmlFile)) {
|
||||
$content = file_get_contents($blogDetailsHtmlFile);
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
return response('文章不存在', 404, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复模板中的资源路径
|
||||
* 将相对路径 (assets/, css/, js/, images/) 转换为绝对路径 (/themes/xxx/)
|
||||
|
||||
@@ -5,6 +5,11 @@ use think\facade\Route;
|
||||
Route::get('/', 'app\index\controller\Index@index');
|
||||
Route::get('index/index', 'app\index\controller\Index@index');
|
||||
|
||||
// --- 模板通用页面路由 ---
|
||||
Route::get('article_detail/:id', 'app\index\controller\Index@articleDetail');
|
||||
Route::get(':page', 'app\index\controller\Index@page')
|
||||
->pattern(['page' => 'about|blog|blog-details|contact|portfolio|portfolio-details|service-details|services|team|articles']);
|
||||
|
||||
// --- 模板初始化接口 ---
|
||||
Route::get('init', 'app\index\controller\Index@init');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user