Ad.php
6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
class Ad extends Common
{
public function initialize(){
parent::initialize();
}
//广告列表
public function index(){
if(Request::isAjax()) {
$key = input('post.key');
$this->assign('testkey', $key);
$page =input('page')?input('page'):1;
$pageSize =input('limit')?input('limit'):config('pageSize');
$list = Db::table(config('database.prefix') . 'ad')->alias('a')
->join(config('database.prefix') . 'ad_type at', 'a.type_id = at.type_id', 'left')
->field('a.*,at.name as typename')
->where('a.name', 'like', "%" . $key . "%")
->order('a.sort')
->paginate(array('list_rows'=>$pageSize,'page'=>$page))
->toArray();
foreach ($list['data'] as $k=>$v){
$list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
}
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
}
return $this->fetch();
}
public function add(){
if(Request::isAjax()) {
//构建数组
$data = Request::except('file');
$data['addtime'] = time();
db('ad')->insert($data);
$result['code'] = 1;
$result['msg'] = '广告添加成功!';
cache('adList', NULL);
$result['url'] = url('index');
return $result;
}else{
$adtypeList=db('ad_type')->order('sort')->select();
$this->assign('adtypeList',json_encode($adtypeList,true));
$this->assign('title',lang('add').lang('ad'));
$this->assign('info','null');
$this->assign('selected', 'null');
return $this->fetch('form');
}
}
public function edit(){
if(Request::isAjax()) {
$data = Request::except('file');
db('ad')->update($data);
$result['code'] = 1;
$result['msg'] = '广告修改成功!';
cache('adList', NULL);
$result['url'] = url('index');
return $result;
}else{
$adtypeList=db('ad_type')->order('sort')->select();
$ad_id=input('ad_id');
$adInfo=db('ad')->where(array('ad_id'=>$ad_id))->find();
$this->assign('adtypeList',json_encode($adtypeList,true));
$selected = db('ad_type')->where('type_id',$adInfo['type_id'])->find();
$this->assign('selected',json_encode($selected,true));
$this->assign('info',json_encode($adInfo,true));
$this->assign('title',lang('edit').lang('ad'));
return $this->fetch('form');
}
}
//设置广告状态
public function editState(){
$id=input('post.id');
$open=input('post.open');
if(db('ad')->where('ad_id='.$id)->update(['open'=>$open])!==false){
return ['status'=>1,'msg'=>'设置成功!'];
}else{
return ['status'=>0,'msg'=>'设置失败!'];
}
}
public function adOrder(){
$ad=db('ad');
$data = input('post.');
if($ad->update($data)!==false){
cache('adList', NULL);
return $result = ['msg' => '操作成功!','url'=>url('index'), 'code' =>1];
}else{
return $result = ['code'=>0,'msg'=>'操作失败!'];
}
}
public function del(){
db('ad')->where(array('ad_id'=>input('ad_id')))->delete();
cache('adList', NULL);
return ['code'=>1,'msg'=>'删除成功!'];
}
public function delall(){
$map[] =array('ad_id','in',input('param.ids/a'));
db('ad')->where($map)->delete();
cache('adList', NULL);
$result['msg'] = '删除成功!';
$result['code'] = 1;
$result['url'] = url('index');
return $result;
}
/***************************位置*****************************/
//位置
public function type(){
if(Request::isAjax()) {
$key = input('key');
$this->assign('testkey', $key);
$list = db('ad_type')->where('name', 'like', "%" . $key . "%")->order('sort')->select();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
}
return $this->fetch();
}
public function typeOrder(){
$ad_type=db('ad_type');
$data = input('post.');
if($ad_type->update($data)!==false){
return $result = ['msg' => '操作成功!','url'=>url('type'), 'code' =>1];
}else{
return $result = ['code'=>0,'msg'=>'操作失败!'];
}
}
public function addType(){
if(Request::isAjax()) {
db('ad_type')->insert(input('post.'));
$result['code'] = 1;
$result['msg'] = '广告位保存成功!';
$result['url'] = url('type');
return $result;
}else{
$this->assign('title',lang('add').lang('ad').'位');
$this->assign('info','null');
return $this->fetch('typeForm');
}
}
public function editType(){
if(Request::isAjax()) {
db('ad_type')->update(input('post.'));
$result['code'] = 1;
$result['msg'] = '广告位修改成功!';
$result['url'] = url('type');
return $result;
}else{
$type_id=input('param.type_id');
$info=db('ad_type')->where('type_id',$type_id)->find();
$this->assign('title',lang('edit').lang('ad').'位');
$this->assign('info',json_encode($info,true));
return $this->fetch('typeForm');
}
}
public function delType(){
$map['type_id'] = input('param.type_id');
db('ad_type')->where($map)->delete();//删除广告位
db('ad')->where($map)->delete();//删除该广告位所有广告
return ['code'=>1,'msg'=>'删除成功!'];
}
}