FocusController.php 3.1 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\FocusModel;

class FocusController extends AdminBaseController
{
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

    //首页
    public function index(){
        $FocusModel = new FocusModel();
        $Focus     = $FocusModel->order('weigh desc')->select();
        $this->assign('focus', $Focus);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }

    //提交保存
    public function addPost(){
        $data      = $this->request->param();
        $FocusModel = new FocusModel();
        $result    = $this->validate($data, 'Focus');
        if ($result !== true) {
            $this->error($result);
        }
        $FocusModel->allowField(true)->save($data);

        $this->success("添加成功!", url("Focus/index"));
    }

    //编辑页面
    public function edit(){
        $id        = $this->request->param('id', 0, 'intval');
        $FocusModel = new FocusModel();
        $Focus      = $FocusModel->get($id);
        $this->assign('targets', $this->targets);
        $this->assign('focus', $Focus);
        return $this->fetch();
    }

    //编辑保存页面
    public function editPost()
    {
        $data      = $this->request->param();
        $FocusModel = new FocusModel();
        $result    = $this->validate($data, 'Focus');
        if ($result !== true) {
            $this->error($result);
        }
        $FocusModel->allowField(true)->isUpdate(true)->save($data);

        $this->success("保存成功!", url("Focus/index"));
    }

    //删除
    public function delete(){
        $id = $this->request->param('id', 0, 'intval');
        FocusModel::destroy($id);
        $this->success("删除成功!", url("Focus/index"));
    }

    //显示与隐藏
    public function toggle()
    {
        $data      = $this->request->param();
        $FocusModel = new FocusModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $FocusModel->where('id', 'in', $ids)->update(['status' => 1]);
            $this->success("更新成功!");
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $FocusModel->where('id', 'in', $ids)->update(['status' => 0]);
            $this->success("更新成功!");
        }

    }

}