This commit is contained in:
manghe 2025-03-31 06:17:21 +00:00
parent 886671baf0
commit b7b40cd120
17 changed files with 798 additions and 179 deletions

View File

@ -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 {

View File

@ -0,0 +1,180 @@
<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Request;
use think\facade\View;
use app\common\model\AdvertType as AdvertTypeModel;
use think\exception\ValidateException;
use think\facade\Db;
class AdvertType extends Base
{
/**
* 显示添加类型的表单
*/
public function add()
{
// 预留,如果将来需要在表单中传递其他数据
return View::fetch('AdvertType/add'); // 会渲染 app/admin/view/AdvertType/add.html
}
/**
* 保存新类型
*/
public function save(Request $request)
{
if (!$request->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 方法)
}

View File

@ -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管理员设置

View File

@ -9,13 +9,15 @@
<form onsubmit="return false;">
<div class="layui-form" wid100 lay-filter="">
<div class="layui-form-item">
<label class="layui-form-label">类型</label>
<div class="layui-input-inline" style="width: 600px">
<input type="radio" name="type" value="1" lay-filter="type" class="layui-input" title="首页" checked>
<input type="radio" name="type" value="3" lay-filter="type" class="layui-input" title="商城" >
<input type="radio" name="type" value="4" lay-filter="type" class="layui-input" title="排行榜" >
<input type="radio" name="type" value="5" lay-filter="type" class="layui-input" title="推荐上新" >
<!-- <input type="radio" name="type" value="2" lay-filter="type" class="layui-input" title="商城" checked>-->
<label class="layui-form-label">类型 <span class="text-red">*</span></label>
<div class="layui-input-block" style="width: 600px">
{if !empty($types)}
{volist name="types" id="type" key="k"}
<input type="radio" name="type" value="{$type.id}" title="{$type.name}" {if $k eq 1}checked{/if}>
{/volist}
{else}
<div class="layui-form-mid layui-word-aux">暂无可用类型,请先 <a href="javascript:;" onclick="parent.advert_type_add && parent.advert_type_add()" style="color: #007bff;">添加类型</a></div>
{/if}
</div>
</div>
<div class="layui-form-item">

View File

@ -9,13 +9,15 @@
<form onsubmit="return false;">
<div class="layui-form" wid100 lay-filter="">
<div class="layui-form-item">
<label class="layui-form-label">类型</label>
<div class="layui-input-inline" style="width: 600px">
<input type="radio" name="type" value="1" lay-filter="type" class="layui-input" disabled title="首页" {if $data.type eq 1} checked {/if}>
<!-- <input type="radio" name="type" value="2" lay-filter="type" class="layui-input" disabled title="商城" {if $data.type eq 2} checked {/if}>-->
<input type="radio" name="type" value="3" lay-filter="type" class="layui-input" disabled title="商城" {if $data.type eq 3} checked {/if}>
<input type="radio" name="type" value="4" lay-filter="type" class="layui-input" disabled title="排行榜" {if $data.type eq 4} checked {/if}>
<input type="radio" name="type" value="5" lay-filter="type" class="layui-input" disabled title="推荐上新" {if $data.type eq 5} checked {/if}>
<label class="layui-form-label">类型 <span class="text-red">*</span></label>
<div class="layui-input-block" style="width: 600px">
{if !empty($types)}
{volist name="types" id="type"}
<input type="radio" name="type" value="{$type.id}" title="{$type.name}" {if $data.type eq $type.id}checked{/if}>
{/volist}
{else}
<div class="layui-form-mid layui-word-aux">暂无可用类型,请先 <a href="javascript:;" onclick="parent.advert_type_add && parent.advert_type_add()" style="color: #007bff;">添加类型</a></div>
{/if}
</div>
</div>
<div class="layui-form-item">

View File

@ -1,116 +1,165 @@
{include file="Public:header2"/}
<body>
<div class="layui-fluid">
<div class="layui-card">
<div class="layui-card-body">
<xblock>
<div style="padding-bottom: 10px;">
<a class="layui-btn" onclick="advert_add()">添加轮播图</a>
<div class="layui-fluid">
<div class="layui-card">
<div class="layui-card-body">
<xblock>
<!-- 搜索表单 (设为 inline) -->
<form class="layui-form layui-inline" action="" method="get" style="margin-right: 10px;">
<div class="layui-inline">
<select name="type_id" lay-filter="type_id">
<option value="">全部类型</option>
{if !empty($types)}
{volist name="types" id="type"}
<option value="{$type.id}" {if $typeId eq $type.id}selected{/if}>{$type.name}</option>
{/volist}
{/if}
</select>
</div>
<div class="layui-inline">
<button class="layui-btn" lay-submit lay-filter="sreach"><i
class="layui-icon">&#xe615;</i></button>
</div>
</form>
<!-- 只保留 查看全部类型 和 添加轮播图 按钮 -->
<div class="layui-inline">
<button type="button" class="layui-btn layui-btn-primary" id="view-all-types-btn"
onclick="show_all_types()"><i class="layui-icon layui-icon-list"></i>查看全部类型</button>
</div>
<div class="layui-inline">
<a class="layui-btn" onclick="advert_add()">添加轮播图</a>
</div>
<!-- 统计信息 (保持浮动) -->
<span style="line-height:40px;float:right;">共有数据: {$count}条</span>
</div>
</xblock>
<table class="layui-table">
<thead>
<tr>
<th>类型</th>
<th>图片</th>
<th>排序值</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="data" id="vo"}
<tr>
<td>
{if $vo['type'] eq 1}
<button class="layui-btn layui-btn-info layui-btn-radius layui-btn-sm">首页</button>
{elseif $vo['type'] eq 2}
<button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-sm">商城</button>
{elseif $vo['type'] eq 3}
<button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-sm">商城</button>
{elseif $vo['type'] eq 4}
<button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-sm">排行榜</button>
{elseif $vo['type'] eq 5}
<button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-sm">推荐上新</button>
<!-- 清除浮动,确保下方表格布局正常 -->
<div style="clear: both;"></div>
</xblock>
<table class="layui-table">
<thead>
<tr>
<th>类型</th>
<th>图片</th>
<th>排序值</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="list" id="vo"}
<tr>
<td>
{if $vo->advertType}
<button
class="layui-btn layui-btn-info layui-btn-radius layui-btn-sm">{$vo->advertType->name}</button>
{else}
<button
class="layui-btn layui-btn-disabled layui-btn-radius layui-btn-sm">未知类型({$vo.type})</button>
{/if}
</td>
<td><img src="{PHP} echo imageUrl($vo['imgurl']){/php}" style="height:60px;"></td>
<td>{$vo['sort']}</td>
<td>
<a style="text-decoration:none" title="编辑" onclick="advert_edit({$vo.id})"
class="layui-btn layui-btn-normal layui-btn-xs">
<i class="layui-icon layui-icon-edit"></i>编辑
</a>
<a style="text-decoration:none" title="删除" onClick="del({$vo.id},2)"
class="layui-btn layui-btn-xs layui-btn-danger">
<i class="layui-icon"></i>删除
</a>
</td>
</tr>
{/volist}
{if $list->isEmpty()}
<tr>
<td colspan='4' style="text-align:center;">暂时没有数据!</td>
</tr>
{/if}
</td>
<td><img src="{PHP} echo imageUrl($vo['imgurl']){/php}" style="height:60px;"></td>
<td>{$vo['sort']}</td>
<td >
<a style="text-decoration:none" title="编辑" onclick="advert_edit({$vo.id})" class="layui-btn layui-btn-normal layui-btn-xs">
<i class="layui-icon layui-icon-edit"></i>编辑
</a>
<a style="text-decoration:none" title="删除" onClick="del({$vo.id},2)" class="layui-btn layui-btn-xs layui-btn-danger">
<i class="layui-icon"></i>删除
</a>
</td>
</tr>
{/volist}
{if condition="$count eq 0"}
<tr><td colspan='3' style="text-align:center;">暂时没有数据!</td></tr>
{/if}
</tbody>
</table>
<div class="my_page layui-box layui-laypage layui-laypage-default">
{$page|raw}
</tbody>
</table>
<div class="my_page layui-box layui-laypage layui-laypage-default">
{$list->render()|raw}
</div>
</div>
</div>
</div>
</div>
{include file="Public:footer"/}
<script type="text/javascript">
layui.use(['table'], function(){
{include file="Public:footer"/}
<script type="text/javascript">
layui.use(['table', 'form', 'layer'], function () { // 移除 form 和 layer 依赖如果不再需要
var table = layui.table;
var form = layui.form; // 保留 form 用于 select 渲染
var layer = layui.layer;
var $ = layui.$; // 获取 jQuery
});
//添加轮播图
function advert_add() {
var url = "{:url('/admin/advert_add')}";
layer.open({
type: 2,
title: '添加轮播图',
shadeClose: false,
shade: 0.3,
area: ['50%', '80%'],
content: url,
});
}
//编辑轮播图
function advert_edit(id) {
var url = "{:url('/admin/advert_edit?id="+id+"')}";
layer.open({
type: 2,
title: '编辑轮播图',
shadeClose: false,
shade: 0.3,
area: ['50%', '80%'],
content: url,
});
}
//上下架、删除
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);
});
//添加轮播图
function advert_add() {
var url = "{:url('/admin/advert_add')}";
layer.open({
type: 2,
title: '添加轮播图',
shadeClose: false,
shade: 0.3,
area: ['50%', '80%'],
content: url,
});
}
//编辑轮播图
function advert_edit(id) {
var url = "{:url('/admin/advert_edit?id=" + id + "')}";
layer.open({
type: 2,
title: '编辑轮播图',
shadeClose: false,
shade: 0.3,
area: ['50%', '80%'],
content: url,
});
}
//查看全部类型 (保留)
function show_all_types() {
var url = "{:url('/admin/advert_type/list_all')}";
layer.open({
type: 2,
title: '全部轮播图类型管理',
shadeClose: true,
shade: 0.6,
area: ['700px', '85%'], // 可适当调整弹窗大小
content: url,
end: function () {
location.reload();
}
})
});
}
</script>
});
}
//上下架、删除
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);
});
}
})
});
}
</script>
</body>
</html>
</html>

View File

@ -0,0 +1,72 @@
{include file="../app/admin/view/Public/header2.html"/}
<body>
<div class="layui-fluid">
<div class="layui-card">
<div class="layui-card-body">
<form class="layui-form" action="{:url('/admin/advert_type/save')}" method="post">
<div class="layui-form-item">
<label class="layui-form-label">类型名称 <span class="text-red">*</span></label>
<div class="layui-input-block">
<input type="text" name="name" lay-verify="required" placeholder="请输入类型名称" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序值</label>
<div class="layui-input-block">
<input type="number" name="sort" value="0" placeholder="请输入排序值 (数字,越小越靠前)" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
</div>
</div>
{include file="../app/admin/view/Public/footer.html"/}
<script>
layui.use('form', function(){
var form = layui.form;
// 监听表单提交事件
form.on('submit(formDemo)', function(data){
var $ = layui.$;
$.ajax({
url: data.form.action,
type: 'post',
data: data.field, // 表单数据
dataType: 'json',
success: function(res){
if(res.status == 1){
layer.msg(res.msg, {icon: 1, time: 1000}, function(){
// 添加成功后可以关闭弹窗并刷新父页面 (如果是在弹窗中打开的)
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
if(index){ // 判断是否存在父窗口
parent.layer.close(index); // 关闭当前弹窗
parent.location.reload(); // 刷新父页面
} else {
// 如果不是在弹窗中,可以跳转到列表页或刷新当前页
// window.location.href = "{:url('/admin/advert_type/index')}"; // 跳转到类型列表页 (如果创建了的话)
history.back(); // 或者简单地返回上一页
}
});
} else {
layer.msg(res.msg, {icon: 2, anim: 6, time: 1500});
}
},
error: function(xhr, status, error){
layer.msg('请求失败: ' + error, {icon: 2, anim: 6, time: 1500});
}
});
return false; // 阻止表单默认提交行为
});
});
</script>
</body>
</html>

View File

@ -0,0 +1,154 @@
{include file="../app/admin/view/Public/header2.html"/}
<body style="background-color: #f2f2f2;">
<div class="layui-fluid" style="padding: 15px;">
<div class="layui-card">
<div class="layui-card-body">
<!-- 添加类型按钮 -->
<div style="padding-bottom: 15px;">
<button class="layui-btn" onclick="advert_type_add()"><i class="layui-icon layui-icon-add-1"></i> 添加类型</button>
</div>
<table class="layui-table layui-form" lay-skin="line">
<colgroup>
<col width="80">
<col>
<col width="100">
<col width="150">
</colgroup>
<thead>
<tr>
<th>ID</th>
<th>类型名称</th>
<th>排序值</th>
<th style="text-align: center;">操作</th>
</tr>
</thead>
<tbody>
{if !empty($list)}
{volist name="list" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.sort}</td>
<td>
<button class="layui-btn layui-btn-normal layui-btn-xs" onclick="advert_type_edit({$vo.id}, '{$vo.name|htmlspecialchars}', {$vo.sort})"><i class="layui-icon layui-icon-edit"></i> 编辑</button>
<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="advert_type_del({$vo.id})"><i class="layui-icon layui-icon-delete"></i> 删除</button>
</td>
</tr>
{/volist}
{else}
<tr><td colspan="4" style="text-align:center;">暂无类型数据</td></tr>
{/if}
</tbody>
</table>
</div>
</div>
</div>
{include file="../app/admin/view/Public/footer.html"/}
<script>
layui.use(['layer', 'form'], function() {
var layer = layui.layer;
var form = layui.form;
var $ = layui.$;
// 添加类型 (弹窗形式)
window.advert_type_add = function() {
var url = "{:url('/admin/advert_type_add')}";
layer.open({
type: 2,
title: '添加轮播图类型',
shadeClose: false,
shade: 0.3,
area: ['400px', '350px'],
content: url,
end: function() {
location.reload(); // 添加成功后刷新当前弹窗列表
}
});
}
// 编辑类型 (弹窗形式,包含名称和排序)
window.advert_type_edit = function(id, currentName, currentSort) {
// 使用 layer.open 打开一个自定义表单页面,或者直接用 layer.open content 嵌入表单
// 这里为了简单,使用 layer.open content 嵌入表单
layer.open({
type: 1, // 页面层
title: '编辑类型 (ID: ' + id + ')',
area: ['400px', '300px'],
content:
'<form class="layui-form" lay-filter="editTypeForm" style="padding: 20px;">' +
' <input type="hidden" name="id" value="' + id + '">' +
' <div class="layui-form-item">' +
' <label class="layui-form-label">类型名称</label>' +
' <div class="layui-input-block">' +
' <input type="text" name="name" required lay-verify="required" value="' + currentName + '" autocomplete="off" class="layui-input">' +
' </div>' +
' </div>' +
' <div class="layui-form-item">' +
' <label class="layui-form-label">排序值</label>' +
' <div class="layui-input-block">' +
' <input type="number" name="sort" required lay-verify="number" value="' + currentSort + '" autocomplete="off" class="layui-input">' +
' </div>' +
' </div>' +
' <div class="layui-form-item">' +
' <div class="layui-input-block">' +
' <button class="layui-btn" lay-submit lay-filter="submitEditType">立即提交</button>' +
' <button type="button" class="layui-btn layui-btn-primary" onclick="layer.closeAll(\'page\')">取消</button>' +
' </div>' +
' </div>' +
'</form>',
success: function(layero, index) {
form.render(null, 'editTypeForm'); // 重新渲染表单
}
});
}
// 监听编辑表单提交
form.on('submit(submitEditType)', function(data) {
var load = layer.load(2);
var url = "{:url('/admin/advert_type/update')}"; // 需要新的后端方法处理更新
$.post(url, data.field, function(res) {
layer.close(load);
if (res.status == 1) {
layer.msg(res.msg, { icon: 1, time: 1000 }, function() {
layer.closeAll('page'); // 关闭弹窗
location.reload(); // 刷新列表
});
} else {
layer.msg(res.msg, { icon: 2, anim: 6 });
}
}).fail(function() {
layer.close(load);
layer.msg('请求失败', { icon: 2, anim: 6 });
});
return false; // 阻止默认 form 跳转
});
// 删除类型
window.advert_type_del = function(id) {
layer.confirm('确认要删除这个类型吗?<br><span style="color:red;">注意:如果该类型下还有轮播图,建议先修改轮播图的类型。</span>', {
title: '删除确认',
icon: 3
}, function(index) {
layer.close(index);
var load = layer.load(2);
var url = "{:url('/admin/advert_type/delete')}"; // 需要新的后端方法处理删除
$.post(url, { id: id }, function(res) {
layer.close(load);
if (res.status == 1) {
layer.msg(res.msg, { icon: 1, time: 1000 }, function() {
location.reload(); // 刷新列表
});
} else {
layer.msg(res.msg, { icon: 2, anim: 6 });
}
}).fail(function() {
layer.close(load);
layer.msg('请求失败', { icon: 2, anim: 6 });
});
});
}
});
</script>
</body>
</html>

View File

@ -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);

View File

@ -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');
}
/**
* 获取列表
*/

View File

@ -0,0 +1,21 @@
<?php
namespace app\common\model;
use think\Model;
/**
* 广告类型模型
*/
class AdvertType extends Model
{
// 定义表名
protected $name = 'advert_type';
// 定义主键
protected $pk = 'id';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = false; // 类型表通常不需要自动时间戳
}

View File

@ -30,7 +30,8 @@
"phpoffice/phpspreadsheet": "^1.29",
"qcloud/cos-sdk-v5": "^2.6",
"yzalis/identicon": "^2.0",
"topthink/think-image": "^1.0"
"topthink/think-image": "^1.0",
"topthink/think-migration": "^3.1"
},
"require-dev": {
"symfony/var-dumper": "^4.2",

59
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6fc796da97ffd24ae219cc1be9728619",
"content-hash": "d87efcd19adbf32a7bf40402f4f4d2e9",
"packages": [
{
"name": "bacon/bacon-qr-code",
@ -2073,6 +2073,63 @@
},
"time": "2024-08-07T10:06:35+00:00"
},
{
"name": "topthink/think-migration",
"version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/top-think/think-migration.git",
"reference": "22c44058e1454f3af1d346e7f6524fbe654de7fb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/top-think/think-migration/zipball/22c44058e1454f3af1d346e7f6524fbe654de7fb",
"reference": "22c44058e1454f3af1d346e7f6524fbe654de7fb",
"shasum": ""
},
"require": {
"php": ">=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",

View File

@ -4,12 +4,12 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>吧唧</title>
<title>友达</title>
</head>
<body>
<div style="overflow: auto;width: 100vw;height: 100vh;">
<img src="20241114164139.png" style="width:99.9vw;" />
<!-- <img src="20241114164139.png" style="width:99.9vw;" /> -->
</div>
</body>

View File

@ -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"}

View File

@ -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"}

View File

@ -4,13 +4,13 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>吧唧</title>
<title>友达</title>
</head>
<body>
<div style="overflow: auto;width: 100vw;height: 100vh;">
<!-- <a href="https://m.onelight.vip/api/generate_urllink"></a> -->
<img src="20241114164139.png" style="width:99.9vw;" />
<!-- <img src="20241114164139.png" style="width:99.9vw;" /> -->
</div>
</body>