AdminArtController.php 6.4 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <bronet@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\ArtModel;
use app\portal\model\BannerModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Loader;

/**
 * Class AdminArtController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'文章管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'文章管理'
 * )
 */
class AdminArtController extends AdminBaseController
{
    /**
     * 文章列表
     * @adminMenu(
     *     'name'   => '文章列表',
     *     'parent' => 'portal/AdminArt/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章列表',
     *     'param'  => ''
     * )
     */
    public function index(){
        $data = $this->request->param();
        $final = $this->adminIndex($data);
        $where_coo = $final['where_arr'];
        if(!empty($data['title'])){
            $arr['title'] = $data['title'];
            $where_coo['title'] = array('like',"%".$data['title']."%");
            $this->assign('title',$data['title']);
        }
        $where_coo['status'] = 1;
        $coo_list = Db::name('Art')->where($where_coo)->order('create_time desc')
            ->select()->toArray();
        if($coo_list){
            foreach ($coo_list as $cook=>$coov){
                $coo_list[$cook]['content'] = $this->changeLen($coov['content'],10,30);
            }
        }
//        ->paginate(10,false,['query'=>$final['page_arr']]);
//        $page = $coo_list->render();
//        $this->assign('page',$page);
        $this->assign('list',$coo_list);
        return $this->fetch();
    }


    /**
     * 添加文章
     * @adminMenu(
     *     'name'   => '添加文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $data = $this->request->param();
        if($data){
//            添加数据
            $model = new ArtModel();
//                查询是否已添加
            $where_isdouble['type'] = $data['type'];
            $where_isdouble['status'] = 1;
            $is_double = $model->where($where_isdouble)->find();
            if($is_double){
                $this->error("已添加该类协议,请直接修改");
            }
            $add['title'] = $data['title'];
            $add['type'] = $data['type'];
            $add['content'] = $data['content'];
            $add['pic'] = $data['pic'];
            $validate = Loader::validate('Protocol');
            if(!$validate->scene('add')->check($add)){
                $mes = $validate->getError();
                $this->error("$mes");
            }
            $final = $model->isUpdate(false)->save($add);
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            return $this->fetch();
        }
    }


    /**
     * 查看文章
     * @adminMenu(
     *     'name'   => '查看文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '查看文章',
     *     'param'  => ''
     * )
     */
    public function view()
    {
        $where_find['id'] = $this->request->param('id');
        $list = Db::name('Art')->where($where_find)->find();
        if($list){
            $list['content'] = htmlspecialchars_decode($list['content']);
            $list['pic'] = cmf_get_image_url($list['pic']);
        }
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * 编辑文章
     * @adminMenu(
     *     'name'   => '编辑文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
//            添加数据
        $data = $this->request->post();
        $change_add['title'] = $data['title'];
        $change_add['type'] = $data['type'];
        $change_add['content'] = $data['content'];
        if(!empty($data['pic'])){
            $change_add['pic'] = $data['pic'];
        }
        $validate = Loader::validate('Protocol');
        if(!$validate->scene('edit')->check($change_add)){
            $mes = $validate->getError();
            $this->error("$mes");
        }
        $change_add['id'] = $data['id'];
        $model = new ArtModel();
        $final = $model->isUpdate(true)->save($change_add);
        if($final){
            $this->success('成功',url('index'));
        }else{
            $this->error('失败');
        }
    }


    /**
     * 删除文章
     * @adminMenu(
     *     'name'   => '删除文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除文章',
     *     'param'  => ''
     * )
     */
    public function del(){
        $ids = $this->request->post();
        $id = $this->request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $id;
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new ArtModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }
}