ChannelController.php 7.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/8/12
 * Time: 9:14
 */

namespace app\admin\controller;

use app\admin\model\ChannelModel;
use cmf\controller\AdminBaseController;
use think\Config;
use think\Db;

class ChannelController extends AdminBaseController
{

    protected $table;
    protected $channel_model;
    protected $channel;
    public function _initialize()
    {
        $this->table = 'Channel';
        $this->channel_model = new ChannelModel();
    }

    /**
     * 渠道列表
     * @adminMenu(
     *     'name'   => '渠道列表',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '渠道列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [
            'delete_time'=>0
        ];
        if(!empty($param['keyword'])) {
            $where['name'] = ['like','%'.$param['keyword'].'%'];
        }
        $list = $this->singleData($this->table,$where,2,['create_time'=>'DESC']);
        $list->appends($param);
        $this->assign('list',$list->items());
        $this->assign('page',$list->render());
        $this->assign('keyword',isset($param['keyword']) ? $param['keyword'] : '');
        return $this->fetch();
    }

    /**
     * 添加渠道
     * @adminMenu(
     *     'name'   => '添加渠道',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 1,
     *     'icon'   => '',
     *     'remark' => '添加渠道',
     *     'param'  => ''
     * )
     */
    public function add() {
        return $this->fetch();
    }

    /**
     * 添加渠道提交
     * @adminMenu(
     *     'name'   => '添加渠道提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 2,
     *     'icon'   => '',
     *     'remark' => '添加渠道提交',
     *     'param'  => ''
     * )
     */
    public function addPost() {
        if($this->request->isPost()) {
            $post = $this->request->param();
            $validate = $this->validate($post,$this->table.'.add');
            if($validate !== true) {
                $this->error($validate);
            }
            $token = $post['token'] = strtoupper(sha1(md5($post['name'].Config::get('database.authcode'))));
            $url = url('user/Register/index',['name'=>$post['name'],'token'=>$token],true,true);
            $post['qrcode'] = $this->createQRcode($url,'L',5,'channel');
            $result = $this->channel_model->addChannel($post);
            if(!$result) {
                $this->error('添加失败');
            }
            $this->success('添加成功');
        }
    }

    /**
     * 编辑渠道
     * @adminMenu(
     *     'name'   => '编辑渠道',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 3,
     *     'icon'   => '',
     *     'remark' => '编辑渠道',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('参数错误');
        }
        $info = $this->singleData($this->table,['id'=>$id,'delete_time'=>0]);
        $this->assign($info);
        return $this->fetch();
    }

    /**
     * 编辑渠道提交
     * @adminMenu(
     *     'name'   => '编辑渠道提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 4,
     *     'icon'   => '',
     *     'remark' => '编辑渠道提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        if($this->request->isPost()) {
            $id = $this->request->param('id',0,'intval');
            if(!$id) {
                $this->error('参数错误');
            }
            $post = $this->request->param();
            $validate = $this->validate($post,$this->table.'.edit');
            if($validate !== true) {
                $this->error($validate);
            }
            $info = $this->channel_model->where(['id'=>$id])->find();
            if($info['name'] != $post['name']) {
                $token = $post['token'] = strtoupper(sha1(md5($post['name'].Config::get('database.authcode'))));
                $url = url('user/Register/index',['name'=>$post['name'],'token'=>$token],true,true);
                $post['qrcode'] = $this->createQRcode($url,'L',5,'channel');
                unlink('./upload/'.$info['qrcode']);
            }
            $result = $this->channel_model->editChannel($post);
            if(!$result) {
                unlink('./upload/'.$info['qrcode']);
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
    }

    /**
     * 修改渠道收集状态
     * @adminMenu(
     *     'name'   => '修改渠道收集状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 5,
     *     'icon'   => '',
     *     'remark' => '修改渠道收集状态',
     *     'param'  => ''
     * )
     */
    public function stop()
    {
        $param           = $this->request->param();
        $msg = '';

        if (isset($param['ids']) && isset($param["is_stop"])) {
            $stop = $param["is_stop"];
            $ids = $this->request->param('ids/a');

            $this->channel_model->where(['id' => ['in', $ids]])->update(['is_stop' => $stop, 'update_time' => time()]);

            if($stop == 0) {
                $msg = "开启收集成功!";
            }
            if($stop == 1) {
                $msg = "停止收集成功!";
            }
            $this->success($msg, '');

        }
        if (isset($param['id']) && isset($param["is_stop"])) {
            $stop = $param["is_stop"];
            $id = $this->request->param('id');

            $this->channel_model->where(['id' => $id])->update(['is_stop' => $stop, 'update_time' => time()]);

            if($stop == 0) {
                $msg = "开启收集成功!";
            }
            if($stop == 1) {
                $msg = "停止收集成功!";
            }
            $this->success($msg, '');

        }
    }

    /**
     * 删除渠道
     * @adminMenu(
     *     'name'   => '删除渠道',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 6,
     *     'icon'   => '',
     *     'remark' => '删除渠道',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param           = $this->request->param();

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');

            $this->channel_model->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);

            $this->success("删除成功!", '');

        }
        if (isset($param['id'])) {
            $id = $this->request->param('id');

            $this->channel_model->where(['id' => $id])->update(['delete_time' => time()]);

            $this->success("删除成功!", '');

        }
    }
}