diff --git a/app/admin/controller/Advert.php b/app/admin/controller/Advert.php index eaf7415..97ac90f 100755 --- a/app/admin/controller/Advert.php +++ b/app/admin/controller/Advert.php @@ -6,6 +6,9 @@ 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 { @@ -14,14 +17,34 @@ class Advert extends Base */ public function index(Request $request) { - $AdvertModel = new AdvertModel; + // 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'; - $data = $AdvertModel->getList($whe, $field, $order, $this->page); - View::assign('count', $data['count']); - View::assign('page', $data['page']); - View::assign('data', $data['list']); + $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'); } @@ -31,38 +54,52 @@ class Advert extends Base 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 = input('post.'); - if ($data['type'] != 1 && $data['type'] != 2 && $data['type'] != 3 && $data['type'] != 4 && $data['type'] != 5) { - return $this->renderError("类型选择错误"); + $data = $request->post(); + + if (empty($data['type'])) { + return $this->renderError("请选择类型"); } - if ($data['ttype'] == 1){ - if (!$data['coupon_id'] || $data['coupon_id'] < 0){ - return $this->renderError("优惠券id错误"); + $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("优惠券不存在"); + } } - $is_find = CouponReceivemodel::where('id',$data['coupon_id'])->find(); - if(!$is_find){ - return $this->renderError("优惠券不存在"); + if ($data['ttype'] == 2 || $data['ttype'] == 3) { + 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'] == 2 || $data['ttype'] == 3){ - if (!$data['goods_id'] || $data['goods_id'] < 0){ - return $this->renderError("盒子id错误"); - } - $is_find = \app\common\model\Goods::where('id',$data['goods_id'])->find(); - if(!$is_find){ - return $this->renderError("盒子不存在"); - } - } - if (RegZero($data['sort'])) { - 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(); - $dd = AdvertModel::insert($data); + $advert = new AdvertModel(); + $dd = $advert->save($data); + if ($dd) { return $this->renderSuccess("轮播图添加成功"); } else { @@ -76,54 +113,70 @@ class Advert extends Base */ public function edit(Request $request) { - $AdvertModel = new AdvertModel; if (!$request->isPost()) { $id = $request->param('id'); - $data = AdvertModel::where(['id' => $id])->find(); + $data = AdvertModel::find($id); if (!$data) { - return $this->renderError("请求参数错1"); + 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 = input('post.'); + $data = $request->post(); if (empty($data['id'])) { - return $this->renderError("请求参数错误"); + return $this->renderError("请求参数错误: 缺少ID"); } - $info = AdvertModel::where(['id' => $data['id']])->find(); + + $info = AdvertModel::find($data['id']); if (!$info) { - return $this->renderError("请求参数错1"); + return $this->renderError("请求参数错误: 轮播图不存在"); } - if ($data['ttype'] == 1){ - if (!$data['coupon_id'] || $data['coupon_id'] < 0){ - return $this->renderError("优惠券id错误"); + + 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("优惠券不存在"); + } } - $is_find = \app\common\model\Coupon::where('id',$data['coupon_id'])->find(); - if(!$is_find){ - return $this->renderError("优惠券不存在"); + if ($data['ttype'] == 2 || $data['ttype'] == 3) { + 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'] == 2 || $data['ttype'] == 3){ - if (!$data['goods_id'] || $data['goods_id'] < 0){ - return $this->renderError("盒子id错误"); - } - $is_find = \app\common\model\Goods::where('id',$data['goods_id'])->find(); - if(!$is_find){ - return $this->renderError("盒子不存在"); - } - } - if (RegZero($data['sort'])) { - 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) { + + if ($dd !== false) { return $this->renderSuccess("轮播图编辑成功"); } else { - return $this->renderError("轮播图编辑失败"); + return $this->renderError("轮播图编辑失败或无更改"); } } } @@ -134,11 +187,11 @@ class Advert extends Base public function del(Request $request) { $id = $request->param('id'); - $info = AdvertModel::where(['id' => $id])->find(); + $info = AdvertModel::find($id); if (!$info) { - return $this->renderError("请求参数错1"); + return $this->renderError("请求参数错误: 轮播图不存在"); } - $dd = AdvertModel::where('id', '=', $id)->delete(); + $dd = $info->delete(); if ($dd) { return $this->renderSuccess("轮播图删除成功"); } else { diff --git a/app/admin/controller/AdvertType.php b/app/admin/controller/AdvertType.php new file mode 100644 index 0000000..9b76733 --- /dev/null +++ b/app/admin/controller/AdvertType.php @@ -0,0 +1,180 @@ +isPost()) { + return $this->renderError('请求方式错误'); + } + + $data = $request->post(); + + // 数据验证 (可以使用 ThinkPHP 的验证器类来做得更完善) + if (empty($data['name'])) { + return $this->renderError('类型名称不能为空'); + } + // 确保 sort 是数字 + $data['sort'] = isset($data['sort']) ? intval($data['sort']) : 0; + + // 检查类型名称是否已存在 + $exists = AdvertTypeModel::where('name', $data['name'])->find(); + if ($exists) { + return $this->renderError('该类型名称已存在'); + } + + Db::startTrans(); // 开启事务 + try { + $advertType = new AdvertTypeModel(); + $result = $advertType->save($data); // 使用 save 方法插入数据 + + if (!$result) { + // 如果 save 返回 false,手动抛出异常以回滚事务 + throw new \Exception('类型添加失败'); + } + + Db::commit(); // 提交事务 + return $this->renderSuccess('类型添加成功'); + + } catch (\Exception $e) { + Db::rollback(); // 回滚事务 + // 记录日志 $e->getMessage() + return $this->renderError('类型添加失败: ' . $e->getMessage()); + } + + } + + /** + * 更新类型信息 (名称和排序) + */ + public function update(Request $request) + { + if (!$request->isPost()) { + return $this->renderError('请求方式错误'); + } + $id = $request->post('id', 0, 'intval'); + $name = $request->post('name', '', 'trim'); + $sort = $request->post('sort', 0, 'intval'); // 获取 sort 值 + + if (empty($id)) { + return $this->renderError('缺少类型ID'); + } + if (empty($name)) { + return $this->renderError('类型名称不能为空'); + } + // 允许 sort 为 0,但可以是负数吗?根据需要调整验证 + // if (!ctype_digit((string)$sort)) { + // return $this->renderError("排序值请输入非负整数"); + // } + if (!is_numeric($sort)) { // 更宽松的检查,允许负数 + return $this->renderError("排序值必须是数字"); + } + + $advertType = AdvertTypeModel::find($id); + if (!$advertType) { + return $this->renderError('类型不存在'); + } + + // 检查新名称是否已被其他类型使用 + $exists = AdvertTypeModel::where('name', $name)->where('id', '<>', $id)->find(); + if ($exists) { + return $this->renderError('该类型名称已被其他类型使用'); + } + + Db::startTrans(); + try { + $advertType->name = $name; + $advertType->sort = $sort; // 保存 sort 值 + $result = $advertType->save(); + + if ($result === false) { + throw new \Exception('类型信息更新失败'); + } + + Db::commit(); + return $this->renderSuccess('类型信息更新成功'); + } catch (\Exception $e) { + Db::rollback(); + return $this->renderError('类型信息更新失败: ' . $e->getMessage()); + } + } + + /** + * 删除类型 + */ + public function delete(Request $request) + { + if (!$request->isPost()) { + return $this->renderError('请求方式错误'); + } + $id = $request->post('id', 0, 'intval'); + + if (empty($id)) { + return $this->renderError('缺少类型ID'); + } + + $advertType = AdvertTypeModel::find($id); + if (!$advertType) { + // 即使不存在也可能返回成功,避免用户重复点击 + return $this->renderSuccess('类型删除成功'); + } + + // (可选) 检查该类型下是否还有轮播图 + $hasAdverts = \app\common\model\Advert::where('type', $id)->count(); + if ($hasAdverts > 0) { + return $this->renderError('删除失败:该类型下仍有关联的轮播图,请先处理轮播图。'); + } + + + Db::startTrans(); + try { + $result = $advertType->delete(); + + if (!$result) { + throw new \Exception('类型删除失败'); + } + + Db::commit(); + return $this->renderSuccess('类型删除成功'); + } catch (\Exception $e) { + Db::rollback(); + return $this->renderError('类型删除失败: ' . $e->getMessage()); + } + } + + /** + * 查看所有类型列表 (用于弹窗) + */ + public function list_all() + { + // 这里可以添加分页、搜索等,但作为弹窗,通常只显示简单列表 + $list = AdvertTypeModel::order('sort asc, id asc')->select(); + + View::assign('list', $list); + // 渲染一个简单的列表视图,例如 AdvertType/list_all.html + return View::fetch('AdvertType/list_all'); + } + + // (未来可以添加 index, edit, delete 方法) +} \ No newline at end of file diff --git a/app/admin/route/app.php b/app/admin/route/app.php index 5a6924b..f7dfe53 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -229,6 +229,17 @@ Route::rule('advert_add', 'Advert/add', 'GET|POST'); Route::rule('advert_edit', 'Advert/edit', 'GET|POST'); Route::rule('advert_del', 'Advert/del', 'GET|POST'); +#============================ +#AdvertType.php轮播图类型管理 +#============================ +Route::get('advert_type_add', 'AdvertType/add'); // 显示添加表单 +Route::post('advert_type/save', 'AdvertType/save'); // 处理添加表单提交 +Route::get('advert_type/list_all', 'AdvertType/list_all'); // 查看所有类型列表 (弹窗) +Route::post('advert_type/update', 'AdvertType/update'); // 更新类型信息 (AJAX) +Route::post('advert_type/delete', 'AdvertType/delete'); // 删除类型 (AJAX) +// 未来可以添加完整后台管理页面的路由 +// Route::get('advert_type', 'AdvertType/index'); + #============================ #Admin.php管理员设置 diff --git a/app/admin/view/Advert/add.html b/app/admin/view/Advert/add.html index 5f7a8d7..82b48fb 100755 --- a/app/admin/view/Advert/add.html +++ b/app/admin/view/Advert/add.html @@ -9,13 +9,15 @@
- -
- - - - - + +
+ {if !empty($types)} + {volist name="types" id="type" key="k"} + + {/volist} + {else} +
暂无可用类型,请先 添加类型
+ {/if}
diff --git a/app/admin/view/Advert/edit.html b/app/admin/view/Advert/edit.html index cc8b7b3..16a2555 100755 --- a/app/admin/view/Advert/edit.html +++ b/app/admin/view/Advert/edit.html @@ -9,13 +9,15 @@
- -
- - - - - + +
+ {if !empty($types)} + {volist name="types" id="type"} + + {/volist} + {else} +
暂无可用类型,请先 添加类型
+ {/if}
diff --git a/app/admin/view/Advert/index.html b/app/admin/view/Advert/index.html index 57541ee..87ebcf1 100755 --- a/app/admin/view/Advert/index.html +++ b/app/admin/view/Advert/index.html @@ -1,116 +1,165 @@ {include file="Public:header2"/} + -
-
-
- -
- 添加轮播图 +
+
+
+ + + +
+ +
+
+ +
+ + + +
+ +
+ + + 共有数据: {$count}条 -
- - - - - - - - - - - - {volist name="data" id="vo"} - - +
类型图片排序值操作
- {if $vo['type'] eq 1} - - {elseif $vo['type'] eq 2} - - {elseif $vo['type'] eq 3} - - {elseif $vo['type'] eq 4} - - {elseif $vo['type'] eq 5} - + + +
+ + + + + + + + + + + + {volist name="list" id="vo"} + + + + + + + {/volist} + {if $list->isEmpty()} + + + {/if} - - - - - - {/volist} - {if condition="$count eq 0"} - - {/if} - -
类型图片排序值操作
+ {if $vo->advertType} + + {else} + + {/if} + {$vo['sort']} + + 编辑 + + + 删除 + +
暂时没有数据!
{$vo['sort']} - - 编辑 - - - 删除 - -
暂时没有数据!
-
- {$page|raw} +
+
+ {$list->render()|raw} +
-
-{include file="Public:footer"/} - + }); + } + + + //上下架、删除 + function del(id, type) { + layer.confirm('确认要删除吗?', function () { + var url = "{:url('/admin/advert_del')}"; + var load = layer.load(2); + var $ = layui.$; + $.post(url, { "id": id, 'type': type }, function (data) { + if (data.status == 1) { + layer.msg(data.msg, { icon: 1, time: 1000 }, function () { + location.reload(); + }); + } else { + layer.msg(data.msg, { icon: 2, anim: 6, time: 1500 }, function () { + layer.close(load); + }); + } + }) + }); + } + - - + \ No newline at end of file diff --git a/app/admin/view/AdvertType/add.html b/app/admin/view/AdvertType/add.html new file mode 100644 index 0000000..567d6b5 --- /dev/null +++ b/app/admin/view/AdvertType/add.html @@ -0,0 +1,72 @@ +{include file="../app/admin/view/Public/header2.html"/} + +
+
+
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ + +
+
+
+
+
+
+ +{include file="../app/admin/view/Public/footer.html"/} + + + \ No newline at end of file diff --git a/app/admin/view/AdvertType/list_all.html b/app/admin/view/AdvertType/list_all.html new file mode 100644 index 0000000..913d9dd --- /dev/null +++ b/app/admin/view/AdvertType/list_all.html @@ -0,0 +1,154 @@ +{include file="../app/admin/view/Public/header2.html"/} + +
+
+
+ +
+ +
+ + + + + + + + + + + + + + + + + + {if !empty($list)} + {volist name="list" id="vo"} + + + + + + + {/volist} + {else} + + {/if} + +
ID类型名称排序值操作
{$vo.id}{$vo.name}{$vo.sort} + + +
暂无类型数据
+
+
+
+{include file="../app/admin/view/Public/footer.html"/} + + + \ No newline at end of file diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index 338dbe1..20d4705 100755 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -86,7 +86,10 @@ class Index extends Base return $this->renderSuccess("请求成功", $content); } - //首页 + /** + * 获取轮播图 + * @return \think\response\Json + */ public function getAdvert() { $type = request()->param('type_id/d', 1); diff --git a/app/common/model/Advert.php b/app/common/model/Advert.php index d3ee6ec..e91af79 100755 --- a/app/common/model/Advert.php +++ b/app/common/model/Advert.php @@ -11,6 +11,18 @@ class Advert extends Base // 设置当前模型对应的完整数据表名称 protected $table = 'advert'; + /** + * 定义与 AdvertType 模型的多对一关联 + */ + public function advertType() + { + // 第一个参数是关联的模型名 + // 第二个参数是外键 (Advert 表中的字段,默认为 advert_type_id) + // 第三个参数是关联表的主键 (AdvertType 表中的字段,默认为 id) + // 假设 Advert 表中存储类型 ID 的字段仍然是 'type' + return $this->belongsTo(AdvertType::class, 'type', 'id'); + } + /** * 获取列表 */ diff --git a/app/common/model/AdvertType.php b/app/common/model/AdvertType.php new file mode 100644 index 0000000..a2b6b21 --- /dev/null +++ b/app/common/model/AdvertType.php @@ -0,0 +1,21 @@ +=7.2", + "topthink/framework": "^6.0 || ^8.0", + "topthink/think-helper": "^3.0.3" + }, + "require-dev": { + "composer/composer": "^2.5.8", + "fzaninotto/faker": "^1.8", + "robmorgan/phinx": "^0.13.4" + }, + "suggest": { + "fzaninotto/faker": "Required to use the factory builder (^1.8)." + }, + "type": "library", + "extra": { + "think": { + "services": [ + "think\\migration\\Service" + ] + } + }, + "autoload": { + "psr-4": { + "Phinx\\": "phinx", + "think\\migration\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "yunwuxin", + "email": "448901948@qq.com" + } + ], + "support": { + "issues": "https://github.com/top-think/think-migration/issues", + "source": "https://github.com/top-think/think-migration/tree/v3.1.1" + }, + "time": "2023-09-14T05:51:31+00:00" + }, { "name": "topthink/think-multi-app", "version": "v1.1.1", diff --git a/public/baji.html b/public/baji.html index c2c5e02..a842c33 100755 --- a/public/baji.html +++ b/public/baji.html @@ -4,12 +4,12 @@ - 吧唧 + 友达
- +
diff --git a/public/storage/poster/share/6148.png b/public/storage/poster/share/6148.png new file mode 100644 index 0000000..58e12eb --- /dev/null +++ b/public/storage/poster/share/6148.png @@ -0,0 +1 @@ +{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 67e4e919-6947a685-6c79b904"} \ No newline at end of file diff --git a/public/storage/poster/share/6158.png b/public/storage/poster/share/6158.png new file mode 100644 index 0000000..dd77788 --- /dev/null +++ b/public/storage/poster/share/6158.png @@ -0,0 +1 @@ +{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 67e4ed6f-38541b62-30baef6e"} \ No newline at end of file diff --git a/view/baji.html b/view/baji.html index 3fe245d..005076b 100755 --- a/view/baji.html +++ b/view/baji.html @@ -4,13 +4,13 @@ - 吧唧 + 友达
- +