tp/app/admin/controller/BannerController.php
2026-01-26 09:29:36 +08:00

233 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
namespace app\admin\controller;
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\Banner;
class BannerController extends BaseController
{
/**
* 获取所有Banner信息
* @return Json
*/
public function getAllBanners()
{
try {
$banners = Banner::where('delete_time', null)
->order('sort', 'asc')
->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'],
'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是否存在
$banner = Banner::where('id', $id)
->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)->update($updateData);
// 获取更新后的Banner信息
$updatedBanner = Banner::where('id', $id)->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是否存在
$banner = Banner::where('id', $id)
->where('delete_time', null)
->find();
if (!$banner) {
return json([
'code' => 404,
'msg' => 'Banner不存在'
]);
}
// 逻辑删除Banner
$result = Banner::where('id', $id)
->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()
]);
}
}
}