| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | <?phpdeclare(strict_types=1);/** * +---------------------------------------------------------------------- * | 栏目控制制器 * +---------------------------------------------------------------------- */namespace app\controller\sys;// 引入框架内置类use think\facade\View;use app\model\Category as CategoryModel;use app\utils\ReUtils;class Category extends Base{    protected  $modelName = "Category";    protected $types = ['一般', '目录', '链接'];    public function index()    {        $list = CategoryModel::getList(['order' => 'id DESC']);        $list = list_tree($list, 'id', 'parent_id');        View::assign('list', $list);        View::assign('types', $this->types);        return View::fetch();    }    /**     * 新增 or 修改     * @param int $id  info id      * @return mix      */    public function save($id = 0)    {        if ($id != 0) {            $data = CategoryModel::find($id);        } else {            $data = new CategoryModel();        }        View::assign('data', $data);        $list = CategoryModel::selectOne();        View::assign('list', $list);        return View::fetch();    }    public function doSave($id)    {        if ($this->app->request->isPost()) {            $params = $this->app->request->param();            if ($params['name'] == '') {                $this->error("名称不能为空");            }            try {                $id = $params['id'];                unset($params['id']);                if ($id != 0) {                    CategoryModel::update($params, ['id' => $id]);                } else {                    CategoryModel::create($params);                }                return ReUtils::success();            } catch (\Exception $e) {                return ReUtils::error($e->getMessage());            }        }    }    // 删除    public function delete($id)    {        if ($this->app->request->isPost()) {            if (CategoryModel::where('parent_id', $id)->value('id')) {                return ReUtils::error('子栏目不为空, 若要删除请先清空子栏目');            }            if (CategoryModel::destroy($id)) {                return ReUtils::success();            } else {                return ReUtils::error();            }        }    }    // 导航状态变更    public function navStatus(int $id)    {        if ($this->app->request->isPost()) {            return CategoryModel::navStaus($id);        }    }}
 |