tp/app/index/controller/BannerController.php
2026-03-20 09:12:57 +08:00

66 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace app\index\controller;
use app\index\BaseController;
use think\response\Json;
use think\facade\Db;
/**
* Banner 控制器
*/
class BannerController extends BaseController
{
/**
* 获取 Banner 列表
* @return Json
*/
public function getBanners()
{
$baseUrl = $this->request->get('baseUrl', '');
if (!empty($baseUrl)) {
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
}
$tid = $this->getTenantId();
if (empty($tid)) {
return json([
'code' => 400,
'msg' => '无法识别租户信息',
'list' => [],
]);
}
// 查询该租户下的 Banner
$banners = Db::name('mete_apps_cms_banner')
->where('tid', $tid)
->whereNull('delete_time')
->order('sort', 'asc')
->order('id', 'desc')
->select()
->toArray();
// 处理图片路径
foreach ($banners as &$banner) {
if (!empty($banner['image'])) {
// 如果图片路径已经是完整 URL直接返回
if (!preg_match('/^https?:\/\//', $banner['image'])) {
// 拼接完整 URL
$banner['image'] = $this->request->scheme() . '://' . $this->request->host() .
(strpos($banner['image'], '/') === 0 ? '' : '/') . $banner['image'];
}
}
}
return json([
'code' => 200,
'msg' => 'success',
'list' => $banners,
]);
}
}