ZjNewsController.php 3.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2018/9/20
 * Time: 16:20
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;

class ZjNewsController extends AdminBaseController
{
    /**
     * 系统消息列表
     */
    public function index(){
        $all=Db::name('zj_news')->where('delete_time','0')->order('create_time','desc')->paginate('15');
        $this->assign('page',$all->render());
        $this->assign('all',$all->items());
        return $this->fetch();
    }

    /**
     * 添加系统消息
     */
    public function add(){
        return $this->fetch();
    }
    /**
     * 添加系统消息提交
     */
    public function addPost(){
        if ($this->request->param()){
            $arr=input('param.');
            $arr['create_time']=time();
            if($arr['is_sta']==1){
                $num=Db::name('zj_news')->where(['is_sta'=>1,'delete_time'=>0])->count();
                if ($num>=1){
                    $this->error('最多同时发布一条系统消息');
                }
            }
            $add=Db::name('zj_news')->insert($arr);
            if ($add){
                $this->success('添加成功',url('index'));
            }else{
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑系统消息
     */
    public function edit(){
        if ($this->request->param()){
            $id=input('param.id');
            $one=Db::name('zj_news')->where('id',$id)->find();
            $this->assign('one',$one);
            return $this->fetch();
        }
    }
    /**
     * 编辑系统消息提交
     */
    public function editPost(){
        if ($this->request->param()){
            $arr=input('param.');
            if($arr['is_sta']==1){
                $num=Db::name('zj_news')->where(['is_sta'=>1,'delete_time'=>0])->count();
                if ($num>=1){
                    $this->error('最多同时发布一条系统消息');
                }
            }
            $edit=Db::name('zj_news')->update($arr);
            if ($edit){
                $this->success('保存成功',url('index'));
            }else{
                $this->error('保存失败');
            }
        }
    }

    /**
     * 系统消息发布状态
     */
    public function sta(){
        if ($this->request->param()){
            $arr=input('param.');
            if($arr['is_sta']==1){
                $num=Db::name('zj_news')->where(['is_sta'=>1,'delete_time'=>0])->count();
                if ($num>=1){
                    $this->error('最多同时发布一条系统消息');
                }
            }
            $edit=Db::name('zj_news')->update($arr);
            if ($edit){
                $this->success('设置成功');
            }else{
                $this->edit('设置失败');
            }
        }
    }

    /**
     * 系统消息删除
     */
    public function delete(){
        if ($this->request->param()){
            $id=input('param.id');
            $del=Db::name('zj_news')->update(['id'=>$id,'delete_time'=>time()]);
            if ($del){
                $this->success('删除成功');
            }else{
                $this->edit('删除失败');
            }
        }
    }

}