manghe/app/admin/controller/Advert.php
2025-04-08 07:07:33 +00:00

214 lines
7.3 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\Advert as AdvertModel;
use app\common\model\AdvertType as AdvertTypeModel;
use app\common\model\Goods;
use app\common\model\Coupon;
class Advert extends Base
{
/*
*轮播图列表
*/
public function index(Request $request)
{
// 1. 获取查询参数中的 type_id
$typeId = $request->param('type_id', '', 'intval'); // 获取 type_id, 默认为空, 强制整数
$whe = [];
// 2. 如果 type_id 有效,则添加到查询条件
if (!empty($typeId)) {
$whe[] = ['type', '=', $typeId];
}
$field = '*';
$order = 'type asc,id desc'; // 排序可以保持不变,或根据需要调整
$list = AdvertModel::with('advertType')
->where($whe) // 应用查询条件
->field($field)
->order($order)
->paginate(['list_rows' => $this->page, 'query' => request()->param()]); // query 会自动保持 type_id 等参数
$count = $list->total();
// 3. 查询所有可用类型,用于下拉框
$types = AdvertTypeModel::order('sort asc, id asc')->select();
View::assign('count', $count);
View::assign('list', $list);
View::assign('types', $types); // 传递类型列表
View::assign('typeId', $typeId); // 传递当前选中的 type_id
return View::fetch('Advert/index');
}
/*
* 轮播图添加
*/
public function add(Request $request)
{
if (!$request->isPost()) {
$types = AdvertTypeModel::order('sort asc, id asc')->select();
View::assign('types', $types);
return View::fetch('Advert/add');
} else {
$data = $request->post();
if (empty($data['type'])) {
return $this->renderError("请选择类型");
}
$typeExists = AdvertTypeModel::find($data['type']);
if (!$typeExists) {
return $this->renderError("选择的类型无效");
}
if (isset($data['ttype'])) {
if ($data['ttype'] == 1) {
if (empty($data['coupon_id']) || $data['coupon_id'] < 0) {
return $this->renderError("优惠券id错误");
}
$is_find = Coupon::where('id', $data['coupon_id'])->find();
if (!$is_find) {
return $this->renderError("优惠券不存在");
}
}
if ($data['ttype'] == 2 || $data['ttype'] == 3 || $data['ttype'] == 4) {
if (empty($data['goods_id']) || $data['goods_id'] < 0) {
return $this->renderError("盒子id错误");
}
$is_find = Goods::where('id', $data['goods_id'])->find();
if (!$is_find) {
return $this->renderError("盒子不存在");
}
}
if ($data['ttype'] == 5) {
if (empty($data['url_link'])) {
return $this->renderError("请输入跳转链接");
}
}
}
if (isset($data['sort']) && !ctype_digit((string)$data['sort'])) {
return $this->renderError("排序值请输入非负整数");
}
if (empty($data['imgurl'])) {
return $this->renderError("请上传图片");
}
$data['addtime'] = time();
$advert = new AdvertModel();
$dd = $advert->save($data);
if ($dd) {
return $this->renderSuccess("轮播图添加成功");
} else {
return $this->renderError("轮播图添加失败");
}
}
}
/*
* 轮播图修改
*/
public function edit(Request $request)
{
if (!$request->isPost()) {
$id = $request->param('id');
$data = AdvertModel::find($id);
if (!$data) {
return $this->renderError("请求参数错误: 轮播图不存在");
}
$types = AdvertTypeModel::order('sort asc, id asc')->select();
View::assign('data', $data);
View::assign('types', $types);
return View::fetch('Advert/edit');
} else {
$data = $request->post();
if (empty($data['id'])) {
return $this->renderError("请求参数错误: 缺少ID");
}
$info = AdvertModel::find($data['id']);
if (!$info) {
return $this->renderError("请求参数错误: 轮播图不存在");
}
if (empty($data['type'])) {
return $this->renderError("请选择类型");
}
$typeExists = AdvertTypeModel::find($data['type']);
if (!$typeExists) {
return $this->renderError("选择的类型无效");
}
if (isset($data['ttype'])) {
if ($data['ttype'] == 1) {
if (empty($data['coupon_id']) || $data['coupon_id'] < 0) {
return $this->renderError("优惠券id错误");
}
$is_find = Coupon::where('id', $data['coupon_id'])->find();
if (!$is_find) {
return $this->renderError("优惠券不存在");
}
}
if ($data['ttype'] == 2 || $data['ttype'] == 3 || $data['ttype'] == 4) {
if (empty($data['goods_id']) || $data['goods_id'] < 0) {
return $this->renderError("盒子id错误");
}
$is_find = Goods::where('id', $data['goods_id'])->find();
if (!$is_find) {
return $this->renderError("盒子不存在");
}
}
if ($data['ttype'] == 5) {
if (empty($data['url'])) {
return $this->renderError("请输入跳转链接");
}
}
}
if (isset($data['sort']) && !ctype_digit((string)$data['sort'])) {
return $this->renderError("排序值请输入非负整数");
}
if (empty($data['imgurl'])) {
return $this->renderError("请上传图片");
}
$data['update_time'] = time();
$dd = $info->save($data);
if ($dd !== false) {
return $this->renderSuccess("轮播图编辑成功");
} else {
return $this->renderError("轮播图编辑失败或无更改");
}
}
}
/*
* 轮播图 删除
*/
public function del(Request $request)
{
$id = $request->param('id');
$info = AdvertModel::find($id);
if (!$info) {
return $this->renderError("请求参数错误: 轮播图不存在");
}
$dd = $info->delete();
if ($dd) {
return $this->renderSuccess("轮播图删除成功");
} else {
return $this->renderError("轮播图删除失败");
}
}
}