审查视图

app/admin/controller/ActivityController.php 8.7 KB
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/8/29
 * Time: 11:21
 */

namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
13 14
class ActivityController extends AdminBaseController
{
15
16 17 18 19 20 21 22 23 24
    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']];
25
        }
26 27
        if ($name != null) {
            $search['name'] = ['like', "%$_POST[name]%"];
28
        }
29
        $res = Db::name('activity')->alias('a')
30 31 32 33 34
            ->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, [
35
                'query' => request()->param()
36
            ]);
37
        return $this->fetch('index', [
38 39 40 41
            'activity' => $res,
            'page' => $res->render(),
            'type' => Db::name('type')->field('id,type_name')->select(),
            'search' => ['t_id' => $t_id, 'name' => $name]
42 43 44
        ]);
    }
45 46 47 48 49 50 51 52 53 54 55
    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;
            $post['date_type'] = 1;
            if (!empty($post['thumb'])) {
56 57
                $post['thumb'] = cmf_get_image_url($post['thumb']);
            }
58
            if (!empty($post['content'])) {
59 60
                $post['content'] = htmlspecialchars_decode($post['content']);
            }
61 62 63 64
            if (!empty($post['banner'])) {
                $banner = '';
                for ($j = 0; $j < count($post['banner']); $j++) {
                    $banner .= cmf_get_image_url($post['banner'][$j]) . ',';
65
                }
66
                $post['banner'] = substr($banner, 0, strlen($banner) - 1);
67 68
            }
            Db::startTrans();
69
            $schedule = $post['schedule'];
70
            unset($post['schedule']);
71 72 73 74 75 76 77 78 79 80 81
            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'],
                        'deadline' => strtotime($schedule[$i]['deadline'])
82 83
                    ];
                }
84
                if (Db::name('activity_schedule')->insertAll($data)) {
85
                    //生成二维码
86 87 88
                    $qr = [
                        'activity_id' => $activity_id,
                        'qr_code' => $this->qrCode($activity_id),
89
                    ];
90
                    if (Db::name('qr')->insert($qr)) {
91 92
                        Db::commit();
                        $this->success('添加成功');
93
                    } else {
94 95 96
                        Db::rollback();
                        $this->error('添加失败');
                    }
97
                } else {
98 99 100
                    Db::rollback();
                    $this->error('添加失败');
                }
101
            } else {
102 103 104
                Db::rollback();
                $this->error('添加失败');
            }
105
        } else {
106
            return $this->fetch('add', [
107
                'type' => Db::name('type')->field('id,type_name')->select()
108 109 110 111
            ]);
        }
    }
112 113 114 115 116 117 118 119 120 121 122 123
    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;
            $post['date_type'] = 1;
            if ($post['is_down_payment'] == 0) {
                $post['down_price'] = 0;
124
            }
125
            if (!empty($post['thumb'])) {
126 127
                $post['thumb'] = cmf_get_image_url($post['thumb']);
            }
128
            if (!empty($post['content'])) {
129 130
                $post['content'] = htmlspecialchars_decode($post['content']);
            }
131 132 133 134
            if (!empty($post['banner'])) {
                $banner = '';
                for ($j = 0; $j < count($post['banner']); $j++) {
                    $banner .= cmf_get_image_url($post['banner'][$j]) . ',';
135
                }
136
                $post['banner'] = substr($banner, 0, strlen($banner) - 1);
137 138
            }
            unset($post['schedule']);
139 140 141 142
            if (Db::name('activity')->update($post)) {
                $this->success('编辑成功');
            } else {
                $this->error('您为做出任何修改');
143
            }
144
        } else {
145 146 147
            $info = Db::name('activity')->where(['id' => $request->param('id')])->find();
            $banner = $info['banner'];
            $arr = [];
148
            if (!empty($banner)) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162
                $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
            ]);
        }
    }
163 164 165 166
    public function del()
    {
        $id = request()->param('id');
        if (Db::name('activity')->update(['id' => $id, 'is_del' => 1])) {
167
            $this->success('删除成功');
168
        } else {
169 170 171 172 173 174 175 176 177
            $this->error('删除失败');
        }
    }

    /**
     * 生成活动二维码,扫码进入下单页面
     * @param $id
     * @return string
     */
178 179
    private function qrCode($id)
    {
lihan authored
180
        $savePath = '/alidata/www/w/sami/public/qrcode/';
181
        $webPath = '/qrcode/';
lihan authored
182
        $qrData = 'http://catrain.net/activity/activity/detail/id/' . $id . '.html';
183 184 185 186 187 188 189
        $qrLevel = 'H';
        $qrSize = '8';
        $savePrefix = 'activity_' . $id . '_';
        $pic = '';
        if ($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)) {
            $pic = $webPath . $filename;
        }
190
        $qr_code = cmf_get_image_url($pic);
191 192 193
        return $qr_code;
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    public function addSchedule()
    {
        $request = request();
        if ($request->isPost()) {
            $post = $request->param();
            $schedule = $post['schedule'];
            $data = [];
            for ($i = 0; $i < count($schedule); $i++) {
                $data[$i] = [
                    'activity_id' => $request->param('activity_id'),
                    'start_time' => strtotime($schedule[$i]['start_time']),
                    'end_time' => strtotime($schedule[$i]['end_time']),
                    'price' => $schedule[$i]['price'],
                    'maximum' => $schedule[$i]['maximum'],
                    'deadline' => strtotime($schedule[$i]['deadline'])
                ];
            }
            if (Db::name('activity_schedule')->insertAll($data)) {
                $this->success('新增批次成功');
            } else {
                $this->error('新增批次失败');
            }
        } else {
            return $this->fetch('add_schedule', [
                'activity_id' => $request->param('id'),
                'schedule' => Db::name('activity_schedule')->where(['activity_id' => $request->param('id')])->select(),
            ]);
        }
    }

    public function delSchedule()
    {
        $schedule_id = request()->param('id');
        if (Db::name('activity_schedule')->delete($schedule_id)) {
            return $this->success('', '', true);
        }
    }

    public function editSchedule()
    {
        $post = request()->param();
        $post['start_time'] = strtotime($post['start_time']);
        $post['end_time'] = strtotime($post['end_time']);
        $post['deadline'] = strtotime($post['deadline']);
        if (Db::name('activity_schedule')->update($post)) {
            return $this->success('编辑批次成功', '', true);
        }
    }
243
}