ActivityController.php 8.7 KB
<?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;
            $post['date_type'] = 1;
            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'],
                        'deadline' => strtotime($schedule[$i]['deadline'])
                    ];
                }
                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;
            $post['date_type'] = 1;
            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);
            }
            unset($post['schedule']);
            if (Db::name('activity')->update($post)) {
                $this->success('编辑成功');
            } else {
                $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 = '/alidata/www/w/sami/public/qrcode/';
        $webPath = '/qrcode/';
        $qrData = 'http://catrain.net/activity/activity/detail/id/' . $id . '.html';
        $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;
    }

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

}