完成内容管理

This commit is contained in:
augushong
2020-04-18 16:01:27 +08:00
parent dc15576de8
commit 84c1a51b86
18 changed files with 1548 additions and 253 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\admin\controller;
use app\model\Category as ModelCategory;
use think\facade\View;
use think\Request;
@@ -18,6 +19,14 @@ class Category extends Common
{
//
$list = ModelCategory::getListLevel();
if($this->request->isAjax()){
return json_message($list);
}
View::assign('list',$list);
return View::fetch();
}
@@ -30,6 +39,10 @@ class Category extends Common
{
//
$list = ModelCategory::getListLevel();
View::assign('list_category',$list);
return View::fetch();
}
@@ -42,6 +55,33 @@ class Category extends Common
public function save(Request $request)
{
//
$post_data = $request->post();
if(empty($post_data['title'])){
return $this->error('标题不能为空',null,500);
}
$model_category = ModelCategory::where('title',$post_data['title'])
->where('pid',$post_data['pid'])
->find();
if(!empty($model_category)){
$this->error('相同名称相同级别不能出现两次',null,500);
}
if($post_data['pid'] != 0){
$model_parent_category = ModelCategory::where('id',$post_data['pid'])->find();
$post_data['level'] = $model_parent_category->level + 1;
}
ModelCategory::create($post_data);
return $this->success('添加成功','index');
}
/**
@@ -64,6 +104,15 @@ class Category extends Common
public function edit($id)
{
//
$model_category = ModelCategory::find($id);
$list = ModelCategory::getListLevel();
View::assign('list_category',$list);
View::assign('category',$model_category);
return View::fetch();
}
/**
@@ -76,6 +125,36 @@ class Category extends Common
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_category = ModelCategory::where('title',$post_data['title'])
->where('pid',$post_data['pid'])
->where('id','<>',$id)
->find();
if(!empty($model_category)){
$this->error('相同名称相同级别不能出现两次');
}
if($post_data['pid'] != 0){
$model_parent_category = ModelCategory::where('id',$post_data['pid'])->find();
$post_data['level'] = $model_parent_category->level + 1;
}else{
$post_data['level'] = 1;
}
$model_category = ModelCategory::find($id);
$model_category->save($post_data);
return $this->success('保存成功','index');
}
/**
@@ -87,5 +166,25 @@ class Category extends Common
public function delete($id)
{
//
if($id == 0){
return json_message('错误');
}
$model_category = ModelCategory::find($id);
$pid = 0;
if($model_category->pid != 0){
$pid = $model_category->pid;
}
ModelCategory::where('pid',$id)->update(['pid'=>$pid]);
$model_category->delete();
return json_message();
}
}

View File

@@ -4,7 +4,11 @@ declare(strict_types=1);
namespace app\admin\controller;
use app\model\Category;
use app\model\Post as ModelPost;
use app\model\PostCategory;
use app\model\PostTag;
use app\model\Tag;
use think\facade\View;
use think\Request;
@@ -21,7 +25,7 @@ class Post extends Common
$list = ModelPost::paginate();
View::assign('list',$list);
View::assign('list', $list);
return View::fetch();
}
@@ -47,6 +51,35 @@ class Post extends Common
public function save(Request $request)
{
//
$post_data = $request->post();
$categorys = [];
$tags = [];
if (isset($post_data['categorys'])) {
$categorys = $post_data['categorys'];
unset($post_data['categorys']);
}
if (isset($post_data['tags'])) {
$tags = $post_data['tags'];
unset($post_data['tags']);
}
$model_post = ModelPost::create($post_data);
foreach ($categorys as $category) {
PostCategory::create([
'post_id' => $model_post->id,
'category_id' => $category
]);
}
foreach ($tags as $tag) {
PostTag::create([
'post_id' => $model_post->id,
'tag_id' => $tag
]);
}
return $this->success('添加成功', 'index');
}
/**
@@ -69,6 +102,13 @@ class Post extends Common
public function edit($id)
{
//
$model_post = ModelPost::find($id);
View::assign('post', $model_post);
return View::fetch();
}
/**
@@ -81,6 +121,63 @@ class Post extends Common
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_post = ModelPost::find($id);
$categorys = [];
$tags = [];
if (isset($post_data['categorys'])) {
$categorys = $post_data['categorys'];
unset($post_data['categorys']);
}
if (isset($post_data['tags'])) {
$tags = $post_data['tags'];
unset($post_data['tags']);
}
$model_post->save($post_data);
$old_category_list = PostCategory::where('post_id', $id)->select();
$old_category_id_list = array_column((array)$old_category_list, 'id');
$old_tag_list = PostTag::where('post_id', $id)->select();
$old_tag_id_list = array_column((array)$old_tag_list, 'id');
// 旧的有新的没有
foreach ($old_category_list as $model_category) {
if (!in_array($model_category->id, $categorys)) {
$model_category->delete();
}
}
foreach ($old_tag_list as $model_tag) {
if (!in_array($model_tag->id, $tags)) {
$model_tag->delete();
}
}
// 旧的没有新的有
foreach ($categorys as $category) {
if (!in_array($category, $old_category_id_list)) {
PostCategory::create([
'post_id' => $model_post->id,
'category_id' => $category
]);
}
}
foreach ($tags as $tag) {
if (!in_array($tag, $old_tag_id_list)) {
PostTag::create([
'post_id' => $model_post->id,
'tag_id' => $tag
]);
}
}
return $this->success('保存成功', 'index');
}
/**

View File

@@ -1,85 +1,125 @@
<?php
declare (strict_types = 1);
declare(strict_types=1);
namespace app\admin\controller;
use app\model\Tag as ModelTag;
use think\facade\View;
use think\Request;
class Tag
class Tag extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list_tag = ModelTag::order('id desc')->paginate();
if($this->request->isAjax()){
return json_message($list_tag);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
View::assign('list',$list_tag);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
$arr = explode(' ',$post_data['tags']);
$arr = array_unique(array_filter($arr));
foreach ($arr as $tag) {
$model_tag = ModelTag::where('title',$tag)->find();
if(empty($model_tag)){
ModelTag::create(['title'=>$tag]);
}
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
return json_message();
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$post_data['title'] = str_replace(' ','',$post_data['title']);
$model_tag = ModelTag::find($id);
$model_tag->save($post_data);
return json_message();
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}