NewController.php 8.1 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\BankModel;
use app\portal\model\NewModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;
use think\Loader;

class NewController extends AdminBaseController
{
    /**
     * 列表(暂无用)
     * @adminMenu(
     *     'name'   => '文章管理',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章列表',
     *     'param'  => ''
     * )
     */
    public function index(Request $request)
    {
        $where_coo['status'] = 1;
        $arr = array();
        if($request->param()){
            $search = $request->param();
            $startTime = empty($search['start_time']) ? 0 : strtotime($search['start_time']);
            $endTime   = empty($search['end_time']) ? 0 : strtotime($search['end_time']);
            if ($startTime && $endTime) {
                $arr['start_time'] = $search['start_time'];
                $arr['end_time'] = $search['end_time'];
                $where_coo['create_time'] = array('between',"$startTime,$endTime");
                $this->assign('start_time', $search['start_time']);
                $this->assign('end_time', $search['end_time']);
            }else{
                if($startTime){
                    $arr['start_time'] = $search['start_time'];
                    $where_coo['create_time'] = array('egt',$startTime);
                    $this->assign('start_time', $search['start_time']);
                }
                if($endTime){
                    $arr['end_time'] = $search['end_time'];
                    $where_coo['create_time'] = array('elt',$endTime);
                    $this->assign('end_time', $search['end_time']);
                }
            }
            if(!empty($search['keyword'])){
                $arr['keyword'] = $search['keyword'];
                $where_coo['name'] = array('like',"%".$search['keyword']."%");
                $this->assign('keyword',$search['keyword']);
            }
        }
        $coo_list = Db::name('New')->where($where_coo)->order('score desc , create_time desc')
            ->paginate(10,false,['query'=>$arr]);
        $array = $coo_list->items();
        if($array){
            foreach ($array as $cok=>$cov){
                $exp = explode('.',$cov['save']);
                if($exp[1] == 0){
                    $array[$cok]['save'] = $exp[0];
                }else{
                    $else = str_split($exp[1]);
                    if($else[1] == 0){
                        $array[$cok]['save'] = $exp[0].'.'.$else[0];
                    }
                }
            }
        }
        $page = $coo_list->render();
        $this->assign('page',$page);
        $this->assign('array',$array);
        $this->assign('list',$coo_list);
        return $this->fetch();
    }


    /**
     * 添加、修改
     * @adminMenu(
     *     'name'   => '添加文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章',
     *     'param'  => ''
     * )
     */
    public function add(Request $request)
    {
        if($request->post()){
//            添加数据
            $data = $request->post();
            if($request->Post('id')){
//                修改
                $add = $data;
                $validate = Loader::validate('New');
                if(!$validate->scene('edit')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $add['id'] = $request->param('id');
                $model = new NewModel();
                $final = $model->isUpdate(true)->allowField(true)->save($add);
            }else{
//                添加
                $add = $data;
                $validate = Loader::validate('New');
                if(!$validate->scene('add')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $model = new NewModel();
                $final = $model->save($add);
            }
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $where_find['id'] = $request->param('id');
            $list = Db::name('New')->where($where_find)->find();
            $this->assign('list',$list);
            return $this->fetch('add');
        }
    }

//  删除
    public function del(Request $request){
        $ids = $this->request->post();
        $id = $request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new NewModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }

    public function code(Request $request){
        $where_coo['status'] = 1;
        if($request->post()){
            $search = $request->post();
            if($search['start_time']){
                $start = strtotime($search['start_time']);
                if($search['end_time']){
                    $end = strtotime($search['end_time']);
                }else{
                    $end = time();
                }
                $where_coo['update_time'] = array('between',"$start,$end");
                $this->assign('start_time',date('Y-m-d H:i',$start));
                $this->assign('end_time',date('Y-m-d H:i',$end));
            }
        }
        $coo_list = Db::name('Qrcode')->where($where_coo)->field('path as pic,id,create_time,type,status,update_time')->order('create_time desc')
            ->select()->toArray();
//        $page = $coo_list->render();
//        $this->assign('page',$page);
        $this->assign('list',$coo_list);
        return $this->fetch();
    }

    public function addcode(Request $request)
    {
        if($request->post()){
//            添加数据
            $data = $request->post();
            if(empty($data['pic'])){
                $this->error('图片错误');
            }
            if(empty($data['type'])){
                $this->error('类型不能为空');
            }
            $add['path'] = $data['pic'];
//            $add['score'] = $data['score'];
            $add['type'] = $data['type'];
            $add['update_time'] = time();
            if($request->Post('id')){
//                修改
                $where_add['id'] = $request->Post('id');
                $final = Db::name('Qrcode')->where($where_add)->update($add);
            }else{
//                添加
                $add['create_time'] = time();
                $final = Db::name('Qrcode')->insertGetId($add);
            }
            if($final){
                $this->success('成功',url('New/code'));
            }else{
                $this->error('失败');
            }
        }else{
            $where_find['id'] = $request->param('id');
            $list = Db::name('Qrcode')->where($where_find)->field('path as pic,id,create_time,type,status,update_time')->find();
//            $list['content'] = htmlspecialchars_decode($list['content']);
            $this->assign('list',$list);
            return $this->fetch();
        }
    }


}