更新模板功能

This commit is contained in:
2026-03-12 17:53:36 +08:00
parent 1f8d876cef
commit bf21006d4e
168 changed files with 82346 additions and 229 deletions
+43 -58
View File
@@ -6,9 +6,9 @@ namespace app\index\controller;
use app\model\Banner;
use app\index\BaseController;
use app\model\FrontMenu;
use app\model\OnePage;
use app\model\System\SystemSiteSettings;
use app\model\Cms\FrontMenu;
use app\model\Cms\OnePage;
use app\model\System\SystemSiteSetting;
use app\service\ThemeService;
use think\db\exception\DbException;
use think\facade\Env;
@@ -29,21 +29,21 @@ class Index extends BaseController
{
// 获取请求对象
$request = $this->request ?? Request::instance();
// 优先从请求对象获取中间件设置的租户ID(通过域名解析)
$tid = $request->tenantId ?? 0;
// 兼容:从header获取
if ($tid == 0) {
$headerTid = $request->header('X-Tenant-Id');
$tid = $headerTid ? (int)$headerTid : 0;
$tid = $headerTid ? (int) $headerTid : 0;
}
// 调试:检查tid
if ($tid === 0) {
die('tenantId未获取到,请检查域名解析');
}
// 获取租户选择的模板
$themeKey = 'default';
if ($tid > 0) {
@@ -54,11 +54,11 @@ class Index extends BaseController
->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);
@@ -69,11 +69,11 @@ class Index extends BaseController
$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/)
@@ -82,7 +82,7 @@ class Index extends BaseController
{
// 需要修复的资源目录
$assetsDirs = ['assets', 'css', 'js', 'images', 'img', 'fonts', 'plugins', 'libs'];
foreach ($assetsDirs as $dir) {
// 匹配 src="assets/..." 或 href="css/..." 等相对路径
// 不匹配已以 / 开头的绝对路径
@@ -98,10 +98,10 @@ class Index extends BaseController
$content
);
}
return $content;
}
/**
* 渲染PHP模板文件
*/
@@ -111,17 +111,17 @@ class Index extends BaseController
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']);
}
@@ -133,10 +133,10 @@ class Index extends BaseController
{
// 优先从请求对象获取中间件设置的租户ID(通过域名解析)
$tid = $this->request->tenantId ?? 0;
// 兼容:URL参数传递的tid作为备用
$tid = $tid > 0 ? $tid : Request::param('tid', 0, 'int');
// 从TemplateSiteConfig获取配置(根据租户ID
$config = null;
if ($tid > 0) {
@@ -144,12 +144,12 @@ class Index extends BaseController
->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 {
@@ -158,9 +158,9 @@ class Index extends BaseController
return json([
'code' => 200,
'msg' => 'success',
'msg' => 'success',
'data' => [
'theme_key' => $themeKey,
'theme_key' => $themeKey,
'theme_path' => $themePath
]
]);
@@ -324,39 +324,13 @@ class Index extends BaseController
}
/**
* 前端底部数据
* 获取首页综合数据
* @return \think\response\Json
*/
public function getFooterData()
public function getHomeData()
{
try {
$footerData = SystemSiteSettings::where('delete_time', null)
->field('label, value')
->select()
->toArray();
return json([
'code' => 200,
'msg' => 'success',
'data' => $footerData
]);
} catch (DbException $e) {
return json([
'code' => 500,
'msg' => 'fail' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 获取友情链接列表
* @return \think\response\Json
*/
public function getFriendlinkList()
{
try {
// 通过域名获取租户ID
// 1. 通过域名获取租户ID
$tid = BaseController::getTenantIdByDomain();
if (empty($tid)) {
@@ -367,23 +341,34 @@ class Index extends BaseController
]);
}
// 2. 获取站点基础信息 (normalinfos)
$normalInfos = SystemSiteSetting::where('tid', $tid)
->field('sitename,logo,logow,ico,description,copyright,companyname,icp,companyintroduction')
->find();
// 3. 获取友情链接列表
$friendlinkList = Friendlink::where('delete_time', null)
->where('tid', $tid)
->where('status', 1)
->order('sort', 'asc')
->field('id,link_name,link_url,link_logo,description')
->field('id,link_name,link_url,link_logo')
->select()
->toArray();
// 4. 合并返回
return json([
'code' => 200,
'msg' => 'success',
'data' => $friendlinkList
'data' => [
'normal' => $normalInfos ?: (object) [],
'links' => $friendlinkList
]
]);
} catch (DbException $e) {
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => 'fail' . $e->getMessage(),
'msg' => '服务器错误' . $e->getMessage(),
'data' => null
]);
}
+1 -1
View File
@@ -12,7 +12,7 @@ Route::get('init', 'app\index\controller\Index@init');
Route::get('footerdata', 'app\index\controller\Index@getFooterData');
Route::get('companyInfos', 'app\index\controller\Index@getCompanyInfos');
Route::post('requirement', 'app\index\controller\Index@requirement');
Route::get('friendlinks', 'app\index\controller\Index@getFriendlinkList');
Route::get('homeData', 'app\index\controller\Index@getHomeData');
// --- 客户需求路由 ---