diff --git a/app/controller/admin/Category.php b/app/controller/admin/Category.php index 0676c081..61187103 100644 --- a/app/controller/admin/Category.php +++ b/app/controller/admin/Category.php @@ -34,7 +34,7 @@ class Category extends Admin { $map[] = ['status', '>', -1]; if (isset($param['model_id']) && $param['model_id']) { - $map[] = ['model_id', '=', $model_id]; + $map[] = ['model_id', '=', $param['model_id']]; } $list = $category->where($map)->order('sort asc,id asc')->select(); @@ -42,13 +42,14 @@ class Category extends Admin { $tree = new \sent\tree\Tree(); $list = $tree->toFormatTree($list->toArray()); } - $subsql = $attr->where('name', 'category_id')->fetchSql(true)->column('model_id'); - $model_list = $model->where('id IN ('. $subsql.')')->select(); + $model_list = Model::where('id', 'IN', function($query){ + $query->name('Attribute')->where('name', 'category_id')->field('model_id'); + })->select(); $this->data = [ 'tree' => $list, - 'module_id' => isset($param['module_id']) ? $param['module_id'] : 0, - 'model_list' => [] + 'model_id' => isset($param['model_id']) ? $param['model_id'] : 0, + 'model_list' => $model_list ]; return $this->fetch(); } @@ -72,29 +73,20 @@ class Category extends Admin { */ public function edit($id = null, $pid = 0) { if ($this->request->isPost()) { - $category = model('Category'); - //提交表单 - $result = $category->change(); + $data = $this->request->post(); + + $result = CategoryM::update($data, ['id' => $data['id']]); if (false !== $result) { - //记录行为 - action_log('update_category', 'category', $id, session('user_auth.uid')); - return $this->success('编辑成功!', url('index')); + return $this->success('修改成功!', url('/admin/category/index')); } else { - $error = $category->getError(); - return $this->error(empty($error) ? '未知错误!' : $error); + return $this->error('修改失败!'); } } else { - $cate = []; - // if ($pid) { - // /* 获取上级分类信息 */ - // $cate = db('Category')->find($pid); - // if (!($cate && 1 == $cate['status'])) { - // return $this->error('指定的上级分类不存在或被禁用!'); - // } - // } - // $subsql = db('Attribute')->where('name', 'category_id')->fetchSql(true)->column('model_id'); - // $model_list = model('Model')->where('id IN ('. $subsql.')')->select(); - $model_list = []; + $category = CategoryM::getCategoryTree(); + + $model_list = Model::where('id', 'IN', function($query){ + $query->name('Attribute')->where('name', 'category_id')->field('model_id'); + })->select(); /* 获取分类信息 */ $info = $id ? CategoryM::find($id) : []; @@ -102,7 +94,7 @@ class Category extends Admin { $this->data = [ 'info' => $info, 'model_list' => $model_list, - 'category' => $cate + 'category' => $category ]; return $this->fetch(); } @@ -111,36 +103,32 @@ class Category extends Admin { /** * @title 添加分类 */ - public function add($pid = 0) { - $Category = model('Category'); - + public function add() { if ($this->request->isPost()) { //提交表单 - $id = $Category->change(); - if (false !== $id) { - action_log('update_category', 'category', $id, session('user_auth.uid')); - return $this->success('新增成功!', url('index')); + $data = $this->request->post(); + + $result = CategoryM::create($data); + if (false !== $result) { + return $this->success('新增成功!', url('/admin/category/index')); } else { - $error = $Category->getError(); - return $this->error(empty($error) ? '未知错误!' : $error); + return $this->error('添加失败!'); } } else { - $cate = array(); - if ($pid) { - /* 获取上级分类信息 */ - $cate = $Category->info($pid, 'id,name,title,status'); - if (!($cate && 1 == $cate['status'])) { - return $this->error('指定的上级分类不存在或被禁用!'); - } - } - $subsql = db('Attribute')->where('name', 'category_id')->fetchSql(true)->column('model_id'); - $model_list = model('Model')->where('id IN ('. $subsql.')')->select(); + $pid = $this->request->param('pid', 0); + + $category = CategoryM::getCategoryTree(); + + $model_list = Model::where('id', 'IN', function($query){ + $query->name('Attribute')->where('name', 'category_id')->field('model_id'); + })->select(); /* 获取分类信息 */ - $this->assign('info', null); - $this->assign('model_list', $model_list); - $this->assign('category', $cate); - $this->setMeta('新增分类'); + $this->data = [ + 'info' => ['pid' => $pid], + 'model_list' => $model_list, + 'category' => $category + ]; return $this->fetch('edit'); } } @@ -153,8 +141,9 @@ class Category extends Admin { return $this->error('参数错误!'); } //判断该分类下有没有子分类,有则不允许删除 - $child = db('Category')->where(array('pid' => $id))->field('id')->select(); - if (!empty($child)) { + $child = CategoryM::where('pid', $id)->field('id')->select(); + + if (!$child->isEmpty()) { return $this->error('请先删除该分类下的子分类'); } //判断该分类下有没有内容 @@ -163,10 +152,8 @@ class Category extends Admin { // return $this->error('请先删除该分类下的文章(包含回收站)'); // } //删除该分类信息 - $res = db('Category')->where(array('id' => $id))->delete(); - if ($res !== false) { - //记录行为 - action_log('update_category', 'category', $id, session('user_auth.uid')); + $result = CategoryM::where('id', $id)->delete(); + if ($result !== false) { return $this->success('删除分类成功!'); } else { return $this->error('删除分类失败!'); @@ -268,19 +255,20 @@ class Category extends Admin { * @author huajie */ public function status() { - $id = $this->getArrayParam('id'); - $status = input('status', '0', 'trim,intval'); - - if (!$id) { - return $this->error("非法操作!"); + $id = $this->request->param('id', 0); + $status = $this->request->param('status', 0); + $map = []; + if (is_array($id)) { + $map[] = ['id', 'IN', $id]; + }else{ + $map[] = ['id', '=', $id]; } - $map['id'] = array('IN', $id); - $result = db('Category')->where($map)->setField('status', $status); - if ($result) { - return $this->success("设置成功!"); + $result = CategoryM::update(['status'=> $status], $map); + if ($result !== false) { + return $this->success('操作成功!'); } else { - return $this->error("设置失败!"); + return $this->error('操作失败!'); } } diff --git a/app/http/validate/admin/Category.php b/app/http/validate/admin/Category.php new file mode 100644 index 00000000..3a3aa1c2 --- /dev/null +++ b/app/http/validate/admin/Category.php @@ -0,0 +1,29 @@ + +// +---------------------------------------------------------------------- +namespace app\http\validate\admin; + +use think\Validate; + +/** + * 菜单验证 + */ +class Category extends Validate{ + protected $rule = [ + 'title' => 'require', + ]; + + protected $message = [ + 'title.require' => '栏目名称必须', + ]; + + protected $scene = [ + 'add' => ['title'], + 'edit' => ['title'], + ]; +} \ No newline at end of file diff --git a/app/model/Category.php b/app/model/Category.php index 8b7c5d50..40bd4e1f 100644 --- a/app/model/Category.php +++ b/app/model/Category.php @@ -21,6 +21,16 @@ class Category extends \think\Model{ 'icon' => 'integer', ); + public static function getCategoryTree($map = []){ + $list = self::where($map)->select(); + + if (!empty($list)) { + $tree = new \sent\tree\Tree(); + $list = $tree->toFormatTree($list->toArray()); + } + return $list; + } + public function change() { $data = input('post.'); if ($data['id']) { diff --git a/app/model/Content.php b/app/model/Content.php index eb29c3fb..356ed4dd 100644 --- a/app/model/Content.php +++ b/app/model/Content.php @@ -12,10 +12,9 @@ namespace app\model; /** * 设置模型 */ -class ContentModel { +class Content extends Model { + + protected $auto = ['update_time']; + protected $insert = ['create_time', 'status' => 1]; - protected $type = array( - 'create_time' => 'integer', - 'update_time' => 'integer', - ); } \ No newline at end of file diff --git a/app/model/Model.php b/app/model/Model.php index 8ef58d78..9f9c24f2 100644 --- a/app/model/Model.php +++ b/app/model/Model.php @@ -15,64 +15,60 @@ namespace app\model; class Model extends \think\Model{ protected $auto = ['update_time']; - protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间|time_format\r\nupdate_time:更新时间|time_format"]; + protected $insert = ['name', 'create_time', 'status' => 1, 'list_grid' => "id:ID\r\ntitle:标题\r\ncreate_time:添加时间\r\nupdate_time:更新时间"]; protected $type = array( - 'id' => 'integer', - 'create_time' => 'integer', - 'update_time' => 'integer', + 'id' => 'integer' ); - // protected static function init() { - // self::beforeInsert(function ($event) { - // $data = $event->toArray(); - // $tablename = strtolower($data['name']); - // //实例化一个数据库操作类 - // $db = new \com\Datatable(); - // //检查表是否存在并创建 - // if (!$db->CheckTable($tablename)) { - // //创建新表 - // return $db->initTable($tablename, $data['title'], 'id')->query(); - // } else { - // return false; - // } - // }); - // self::afterInsert(function ($event) { - // $data = $event->toArray(); + protected static function onAfterInsert($model){ + $data = $model->getDate(); - // $fields = include (APP_PATH . 'admin/fields.php'); - // if (!empty($fields)) { - // foreach ($fields as $key => $value) { - // if ($data['is_doc']) { - // $fields[$key]['model_id'] = $data['id']; - // } else { - // if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) { - // $fields[$key]['model_id'] = $data['id']; - // } else { - // unset($fields[$key]); - // } - // } - // } - // model('Attribute')->saveAll($fields); - // } - // return true; - // }); - // self::beforeUpdate(function ($event) { - // $data = $event->toArray(); - // if (isset($data['attribute_sort']) && $data['attribute_sort']) { - // $attribute_sort = json_decode($data['attribute_sort'], true); + $fields = [ + 'title'=> ['name' => 'title', 'title' => '标题', 'type' => 'text', 'length' => 200, 'extra' => '', 'remark' => '标题', 'is_show' => 1, 'is_must' => 1, 'value'=>''], + 'category_id' => ['name' => 'category_id', 'title' => '栏目', 'type' => 'bind', 'length' => 10, 'extra' => 'category', 'remark' => '栏目', 'is_show' => 1, 'is_must' => 1, 'value'=>'0'], + 'uid' => ['name' => 'uid', 'title' => '用户UID', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '用户UID', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'], + 'cover_id' => ['name' => 'cover_id', 'title' => '内容封面', 'type' => 'image', 'length' => 10, 'extra' => '', 'remark' => '内容封面', 'is_show' => 1, 'is_must' => 0, 'value'=>''], + 'description' => ['name' => 'description', 'title' => '内容描述', 'type' => 'textarea', 'length' => '', 'extra' => '', 'remark' => '内容描述', 'is_show' => 1, 'is_must' => 0, 'value'=>''], + 'content' => ['name' => 'content', 'title' => '内容', 'type' => 'editor', 'length' => '', 'extra' => '', 'remark' => '内容', 'is_show' => 1, 'is_must' => 0, 'value'=>''], + 'status' => ['name' => 'status', 'title' => '数据状态', 'type' => 'select', 'length' => 2, 'extra' => "-1:删除\r\n0:禁用\r\n1:正常\r\n2:待审核\r\n3:草稿", 'remark' => '数据状态', 'is_show' => 1, 'is_must' => 1, 'value'=>'1'], + 'is_top' => ['name' => 'is_top', 'title' => '是否置顶', 'type' => 'bool', 'length' => 2, 'extra' => '', 'remark' => '是否置顶', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'], + 'view' => ['name' => 'view', 'title' => '浏览数量', 'type' => 'num', 'length' => 11, 'extra' => '', 'remark' => '浏览数量', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'], + 'update_time' => ['name' => 'update_time', 'title' => '更新时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '更新时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'], + 'create_time' => ['name' => 'create_time', 'title' => '添加时间', 'type' => 'datetime', 'length' => 11, 'extra' => '', 'remark' => '添加时间', 'is_show' => 0, 'is_must' => 1, 'value'=>'0'], + ]; + if (!empty($fields)) { + foreach ($fields as $key => $value) { + if ($data['is_doc']) { + $fields[$key]['model_id'] = $data['id']; + } else { + if (in_array($key, array('uid', 'status', 'view', 'create_time', 'update_time'))) { + $fields[$key]['model_id'] = $data['id']; + } else { + unset($fields[$key]); + } + } + } + (new Attribute())->saveAll($fields); + } + return true; + } - // if (!empty($attribute_sort)) { - // foreach ($attribute_sort as $key => $value) { - // db('Attribute')->where('id', 'IN', $value)->setField('group_id', $key); - // foreach ($value as $k => $v) { - // db('Attribute')->where('id', $v)->setField('sort', $k); - // } - // } - // } - // } - // return true; - // }); - // } + protected static function onAfterUpdate($model){ + $data = $model->getDate(); + if (isset($data['attribute_sort']) && $data['attribute_sort']) { + $attribute_sort = json_decode($data['attribute_sort'], true); + + if (!empty($attribute_sort)) { + foreach ($attribute_sort as $key => $value) { + db('Attribute')->where('id', 'IN', $value)->setField('group_id', $key); + foreach ($value as $k => $v) { + db('Attribute')->where('id', $v)->setField('sort', $k); + } + } + } + } + return true; + } protected function setAttributeSortAttr($value) { return $value ? json_encode($value) : ''; diff --git a/composer.json b/composer.json index f1f42ff3..7bba00d7 100755 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "xin/helper": "^1.0.0", "sent/tree": "^1.0.0", "lotofbadcode/phpspirit_dbskeleton": "^1.0", - "sent/think-addons": "^1.0.0" + "sent/think-addons": "^1.0.0", + "overtrue/wechat": "^4.2" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/config/session.php b/config/session.php index c1ef6e16..fd57f792 100755 --- a/config/session.php +++ b/config/session.php @@ -13,7 +13,7 @@ return [ // 存储连接标识 当type使用cache的时候有效 'store' => null, // 过期时间 - 'expire' => 1440, + 'expire' => 1000 * 60 * 60, // 前缀 'prefix' => '', ]; diff --git a/view/admin/category/edit.html b/view/admin/category/edit.html index 25a59443..2061c7cd 100644 --- a/view/admin/category/edit.html +++ b/view/admin/category/edit.html @@ -7,7 +7,7 @@
-
+