Files
yusheng-php/application/adminapi/controller/Banner.php

219 lines
6.4 KiB
PHP
Raw Normal View History

2025-08-07 20:21:47 +08:00
<?php
namespace app\adminapi\controller;
use app\common\controller\adminApi;
use think\Db;
use think\Config;
use think\Session;
use think\Validate;
/**
* 广告位管理
* @internal
*/
class Banner extends adminApi
{
protected $noNeedLogin = [];
protected $noNeedRight = ['banner_position_lists'];
public function _initialize()
{
parent::_initialize();
}
/**
* 广告管理列表
*/
public function banner_lists(){
$page = input('page', 1);
$page_limit = input('page_limit', 30);
$search_name = input('search_name', '');
$show_type = input('show_type', '');
$etime = input('etime', '');
$where = [];
if($search_name){
$where['name'] = ['like', '%'.$search_name.'%'];
}
if($show_type){
$where['show_type'] = $show_type;
}
if($etime){
$where['etime'] = ['>', strtotime($etime)];
}
$where['delete_time'] = 0;
$count = db('vs_banner')->where($where)->count();
$lists = db('vs_banner')->where($where)->order('sort id desc')->page($page, $page_limit)->select();
$return_list = [];
foreach ($lists as $key => $value) {
$return_list[$key]['id'] = $value['bid'];
$return_list[$key]['image'] = $value['image'];
$return_list[$key]['name'] = $value['name'];
$return_list[$key]['show_type'] = $value['show_type'];
$return_list[$key]['show_type_str'] = model('api/Banner')->ShowType[$value['show_type']]?? '';
$return_list[$key]['stime'] = date('Y-m-d H:i:s', $value['stime']);
$return_list[$key]['etime'] = date('Y-m-d H:i:s', $value['etime']);
$return_list[$key]['show_status'] = $value['show_status'];
$return_list[$key]['clicks'] = $value['clicks'];
$return_list[$key]['type'] = $value['type'];
$return_list[$key]['type_str'] = model('api/Banner')->Type[$value['type']]?? '';
$return_list[$key]['content'] = $value['content'] ?? "";
}
$return_data = [
'page' =>$page,
'page_limit' => $page_limit,
'count' => $count,
'lists' => $return_list,
'show_type_array' => model('api/Banner')->ShowType
];
return V(1,"成功", $return_data);
}
/*
* 广告位置列表
*/
public function banner_position_lists(){
$show_type = model('api/Banner')->ShowType;
$i=0;
$data =[];
foreach ($show_type as $key => $value) {
$data[$i]['id'] = $key;
$data[$i]['name'] = $value;
$i++;
}
return V(1,"成功", $data);
}
/*
* 添加广告
*/
public function add_banner(){
$name = input('name', '');
$show_type = input('show_type', '');
$type = input('type', '');
$aid = input('aid', '');
$url = input('url', '');
$stime = input('stime', 0);
$etime = input('etime', 0);
$image = input('image', '');
$show_status = input('show_status', 1);
$remarks = input('remarks', '');
$content = input('content', '');
$stime = $stime ? strtotime($stime):0;
$etime = $stime ? strtotime($etime):0;
$data = [
'name' => $name,
'show_type' => $show_type,
'type' => $type,
'aid' => $aid,
'url' => $url,
'stime' => $stime,
'etime' => $etime,
'image' => $image,
'show_status' => $show_status,
'remarks' => $remarks,
'content' => $content,
'createtime' => time(),
];
$id = db::name('vs_banner')->insertGetId($data);
if(!$id){
return V(0,"添加失败");
}
return V(1,"成功",['id'=>$id]);
}
/*
* 广告详情
*/
public function banner_info(){
$id = input('id', '');
$info = db::name('vs_banner')->where('bid', $id)->find();
if(!$info){
return V(0,"数据不存在");
}
$info['stime'] = date('Y-m-d H:i:s', $info['stime']);
$info['etime'] = date('Y-m-d H:i:s', $info['etime']);
$info['show_type_array'] =model('api/Banner')->ShowType;
return V(1,"成功", $info);
}
/*
* 广告编辑
*/
public function banner_edit(){
$id = input('id', '');
$name = input('name', '');
$show_type = input('show_type', '');
$type = input('type', '');
$aid = input('aid', '');
$url = input('url', '');
$stime = input('stime', '');
$etime = input('etime', '');
$image = input('image', '');
$show_status = input('show_status', '');
$remarks = input('remarks', '');
$content = input('content', '');
if (!$id){
return V(0,"ID不能为空");
}
if($name!=""){
$data['name'] = $name;
}
if($show_type!=""){
$data['show_type'] = $show_type;
}
if($stime!=""){
$data['stime'] = strtotime($stime);
}
if($etime!=""){
$data['etime'] = strtotime($etime);
}
if($remarks!=""){
$data['remarks'] = $remarks;
}
if($image!=""){
$data['image'] = $image;
}
if($url!=""){
$data['url'] = $url;
}
if($type!=""){
$data['type'] = $type;
}
if($aid!=""){
//判断是数字
if(!is_numeric($aid)){
return V(0,"参数错误,对应跳转应为ID");
}
$data['aid'] = $aid;
}
if($show_status!=""){
$data['show_status'] = $show_status;
}
if($content!=""){
$data['content'] = $content;
}
$res = Db::name('vs_banner')->where('bid',$id)->update($data);
if(!$res){
return V(0,"修改失败");
}
return V(1,"成功");
}
/*
* 删除
*/
public function banner_delete(){
$id = input('id', '');
if($id == ''){
return V(0,"ID不能为空");
}
$res = db::name('vs_banner')->where(['bid'=>$id])->update(['delete_time'=>time()]);
if(!$res){
return V(0,"删除失败");
}
return V(1,"成功");
}
}