12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- declare (strict_types = 1);
- /**
- * +----------------------------------------------------------------------
- * | 栏目控制制器
- * +----------------------------------------------------------------------
- */
- namespace app\admin\controller;
- // 引入框架内置类
- use think\facade\View;
- class Category extends Base
- {
- protected $modelName = "\app\common\model\Category";
- public function index()
- {
- $list = $this->modelName::getList();
- $list = obj_tree($list, $pk = 'id', $pid = 'parent_id');
- View::assign('list', $list);
- return View::fetch();
- }
- /**
- * 新增 or 修改
- * @param int $id info id
- * @return mix
- */
- public function save($id = 0)
- {
- if ($this->app->request->isPost()) {
- $params = $this->app->request->param();
-
- if ($params['category_name'] == '') {
- $this->error("名称不能为空");
- }
- try {
- $id = $params['id'];
- unset($params['id']);
- if ($id != 0) {
- $this->modelName::update($params, ['id' => $id]);
- } else {
- $this->modelName::create($params);
- }
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $this->error("错误提示:".$msg);
- }
- $this->success('操作成功', (String)url('index'));
- } else {
- if ($id != 0) {
- $data = $this->modelName::find($id);
- } else {
- $data = null;
- }
-
- View::assign('data', $data);
- $list = obj_tree($this->modelName::select());
- View::assign('list', $list);
- return View::fetch();
- }
- }
- // 删除
- public function delete($id)
- {
- if ($this->app->request->isPost()) {
- if ($this->modelName::where('parent_id', $id)->value('id')) {
- return ['code'=>0, 'msg'=>'子栏目不为空, 若要删除请先清空子栏目'];
- }
- return $this->modelName::del($id);
- }
- }
- }
|