ActivityController.php
8.0 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/8/29
* Time: 11:21
*/
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
class ActivityController extends AdminBaseController {
public function index() {
$search=[];
$search['is_del']=['eq', 0];
$post=request()->param();
$t_id=(isset($post['t_id'])) ? $post['t_id'] : 0;
$name=(isset($post['name'])) ? $post['name'] : null;
if($t_id != 0) {
$search['t_id']=['eq', $_POST['t_id']];
}
if($name != null) {
$search['name']=['like', "%$_POST[name]%"];
}
$res=Db::name('activity')->alias('a')
->field('a.*,t.type_name,q.qr_code')
->join('type t', 'a.t_id=t.id')
->join('qr q', 'q.activity_id=a.id')
->where($search)
->paginate(20, false, [
'query' => request()->param()
]);
return $this->fetch('index', [
'activity' => $res,
'page' => $res->render(),
'type' => Db::name('type')->field('id,type_name')->select(),
'search' => ['t_id' => $t_id, 'name' => $name]
]);
}
public function add() {
$request=request();
if($request->isPost()) {
$post=$request->param();
$post['is_down_payment']=(isset($post['is_down_payment'])) ? 1 : 0;
$post['is_new']=(isset($post['is_new'])) ? 1 : 0;
$post['is_hot']=(isset($post['is_hot'])) ? 1 : 0;
$post['is_on_sale']=(isset($post['is_on_sale'])) ? 1 : 0;
if(!empty($post['thumb'])) {
$post['thumb'] = cmf_get_image_url($post['thumb']);
}
if(!empty($post['content'])) {
$post['content'] = htmlspecialchars_decode($post['content']);
}
if(!empty($post['banner'])) {
$banner='';
for($j=0; $j<count($post['banner']); $j++) {
$banner.=cmf_get_image_url($post['banner'][$j]).',';
}
$post['banner']=substr($banner, 0, strlen($banner)-1);
}
Db::startTrans();
$schedule=$post['schedule'];
unset($post['schedule']);
if(Db::name('activity')->insert($post)) {
$activity_id=Db::name('activity')->getLastInsID();
$data=[];
for ($i=0; $i<count($schedule); $i++) {
$data[$i]=[
'activity_id' => $activity_id,
'start_time' => strtotime($schedule[$i]['start_time']),
'end_time' => strtotime($schedule[$i]['end_time']),
'price' => $schedule[$i]['price'],
'maximum' => $schedule[$i]['maximum'],
];
}
if(Db::name('activity_schedule')->insertAll($data)) {
//生成二维码
$qr=[
'activity_id' => $activity_id,
'qr_code' => $this->qrCode($activity_id),
];
if(Db::name('qr')->insert($qr)) {
Db::commit();
$this->success('添加成功');
}else {
Db::rollback();
$this->error('添加失败');
}
}else {
Db::rollback();
$this->error('添加失败');
}
}else {
Db::rollback();
$this->error('添加失败');
}
}else {
return $this->fetch('add', [
'type' => Db::name('type')->field('id,type_name')->select()
]);
}
}
public function edit() {
$request=request();
if($request->isPost()) {
$post=$request->param();
$post['is_down_payment']=(isset($post['is_down_payment'])) ? 1 : 0;
$post['is_new']=(isset($post['is_new'])) ? 1 : 0;
$post['is_hot']=(isset($post['is_hot'])) ? 1 : 0;
$post['is_on_sale']=(isset($post['is_on_sale'])) ? 1 : 0;
if($post['is_down_payment'] == 0) {
$post['down_price']=0;
}
if(!empty($post['thumb'])) {
$post['thumb'] = cmf_get_image_url($post['thumb']);
}
if(!empty($post['content'])) {
$post['content'] = htmlspecialchars_decode($post['content']);
}
if(!empty($post['banner'])) {
$banner='';
for($j=0; $j<count($post['banner']); $j++) {
$banner.=cmf_get_image_url($post['banner'][$j]).',';
}
$post['banner']=substr($banner, 0, strlen($banner)-1);
}
$schedule=(isset($post['schedule'])) ? $post['schedule'] : null;
if(empty($schedule)) {
$this->error('请添加批次');
}
$schedule=$post['schedule'];
unset($post['schedule']);
Db::startTrans();
if(Db::name('activity')->update($post) >= 0) {
if(Db::name('activity_schedule')->where(['activity_id'=>$request->param('id')])->delete()) {
$data=[];
for ($i=0; $i<count($schedule); $i++) {
$data[$i]=[
'activity_id' => $request->param('id'),
'start_time' => strtotime($schedule[$i]['start_time']),
'end_time' => strtotime($schedule[$i]['end_time']),
'price' => $schedule[$i]['price'],
'maximum' => $schedule[$i]['maximum'],
];
}
if(Db::name('activity_schedule')->insertAll($data)) {
Db::commit();
$this->success('编辑成功');
}else {
Db::rollback();
$this->error('编辑失败');
}
}else {
Db::rollback();
$this->error('编辑失败');
}
}else {
Db::rollback();
$this->error('编辑失败');
}
}else {
$info = Db::name('activity')->where(['id' => $request->param('id')])->find();
$banner = $info['banner'];
$arr = [];
if(!empty($banner)) {
$temp = explode(',', $banner);
for ($i = 0; $i < count($temp); $i++) {
$arr[$i]['url'] = $temp[$i];
}
}
return $this->fetch('edit', [
'info' => $info,
'type' => Db::name('type')->field('id,type_name')->select(),
'schedule' => Db::name('activity_schedule')->where(['activity_id' => $request->param('id')])->select(),
'banner' => $arr
]);
}
}
public function del() {
$id=request()->param('id');
if(Db::name('activity')->update(['id'=>$id,'is_del'=>1])) {
$this->success('删除成功');
}else {
$this->error('删除失败');
}
}
/**
* 生成活动二维码,扫码进入下单页面
* @param $id
* @return string
*/
private function qrCode($id) {
$savePath = APP_PATH . '/../Public/qrcode/';
$webPath = '/qrcode/';
$qrData = 'https://www.baidu.com';
$qrLevel = 'H';
$qrSize = '8';
$savePrefix = 'activity_' . $id . '_';
$pic = '';
if ($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)) {
$pic = $webPath . $filename;
}
$qr_code=cmf_get_image_url($pic);
return $qr_code;
}
}