tp/app/admin/controller/Cms/Banner/BannerController.php
2026-03-12 17:53:36 +08:00

242 lines
7.3 KiB
PHP
Raw Permalink 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\admin\controller\Cms\Banner;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\response\Json;
use think\db\exception\DbException;
use app\model\Cms\Banner;
class BannerController extends BaseController
{
/**
* 获取所有Banner信息
* @return Json
*/
public function getAllBanners()
{
try {
$banners = Banner::where('delete_time', null)
->where('tid', $this->getTenantId())
->order('sort desc, id desc')
->field('id, title, desc, url, image, sort, create_time, update_time')
->select()
->toArray();
// 记录操作日志
$this->logSuccess('Banner管理', '获取所有Banner', ['data' => $banners]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $banners
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('Banner管理', '获取所有Banner', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 创建Banner
* @return Json
*/
public function createBanner()
{
try {
// 获取请求参数
$data = $this->request->param();
// 验证参数
$this->validate($data, [
'title|标题' => 'require|max:255',
'desc|简介' => 'max:65535',
'url|跳转地址' => 'max:65535',
'image|图片地址' => 'max:65535',
'sort|排序号' => 'integer',
]);
// 准备Banner数据
$bannerData = [
'title' => $data['title'],
'tid' => $this->getTenantId(),
'desc' => $data['desc'] ?? '',
'url' => $data['url'] ?? '',
'image' => $data['image'] ?? '',
'sort' => $data['sort'] ?? 0,
'create_time' => date('Y-m-d H:i:s'),
];
// 创建Banner
$result = Banner::insertGetId($bannerData);
if ($result === false) {
return json([
'code' => 500,
'msg' => '创建失败'
]);
}
// 记录操作日志
$this->logSuccess('Banner管理', '创建Banner', ['id' => $result]);
return json([
'code' => 200,
'msg' => '创建成功',
'data' => ['id' => $result]
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('Banner管理', '创建Banner', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('Banner管理', '创建Banner', $e->getMessage());
return json([
'code' => 500,
'msg' => '创建失败:' . $e->getMessage()
]);
}
}
/**
* 编辑Banner
* @param int $id Banner ID
* @return Json
*/
public function editBanner(int $id)
{
try {
// 获取请求参数
$data = $this->request->param();
// 验证参数
$this->validate($data, [
'title|标题' => 'require|max:255',
'desc|简介' => 'max:65535',
'url|跳转地址' => 'max:65535',
'image|图片地址' => 'max:65535',
'sort|排序号' => 'integer',
]);
// 检查Banner是否存在验证tid
$banner = Banner::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$banner) {
return json([
'code' => 404,
'msg' => 'Banner不存在'
]);
}
// 准备更新数据
$updateData = [
'title' => $data['title'],
'desc' => $data['desc'] ?? null,
'url' => $data['url'] ?? null,
'image' => $data['image'] ?? null,
'sort' => $data['sort'] ?? 0,
'update_time' => date('Y-m-d H:i:s'),
];
// 执行更新
Banner::where('id', $id)
->where('tid', $this->getTenantId())
->update($updateData);
// 获取更新后的Banner信息
$updatedBanner = Banner::where('id', $id)
->where('tid', $this->getTenantId())
->find();
// 记录操作日志
$this->logSuccess('Banner管理', '更新Banner', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $updatedBanner ? $updatedBanner->toArray() : []
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('Banner管理', '更新Banner', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('Banner管理', '更新Banner', $e->getMessage());
return json([
'code' => 500,
'msg' => '更新失败:' . $e->getMessage()
]);
}
}
/**
* 删除Banner
* @param int $id Banner ID
* @return Json
*/
public function deleteBanner(int $id)
{
try {
// 检查Banner是否存在验证tid
$banner = Banner::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$banner) {
return json([
'code' => 404,
'msg' => 'Banner不存在'
]);
}
// 逻辑删除Banner
$result = Banner::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->update([
'delete_time' => time(),
'update_time' => time()
]);
if ($result === false) {
return json([
'code' => 500,
'msg' => '删除失败'
]);
}
// 记录操作日志
$this->logSuccess('Banner管理', '删除Banner', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('Banner管理', '删除Banner', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}