BlogCategoryController.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace App\Http\Controllers\Bside\Blog;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Requests\Bside\Blog\BlogCategoryRequest;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use Illuminate\Http\Request;
class BlogCategoryController extends BaseController
{
/**
* @name :博客分类列表
* @return json
* @author :liyuhang
* @method
*/
public function lists(BlogCategoryModel $blogCategoryModel){
//搜索条件
$lists = $blogCategoryModel->lists($this->map,$this->page,$this->row);
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @name :获取当前分类详情
* @return void
* @author :liyuhang
* @method
*/
public function info(Request $request,BlogCategoryModel $blogCategoryModel){
$request->validate([
'id'=>['required']
],[
'id.required' => 'ID不能为空'
]);
$info = $blogCategoryModel->read($this->param);
if($info === false){
$this->response('error',Code::USER_ERROR);
}
$this->response('success',Code::SUCCESS,$info);
}
/**
* @name :添加分类
* @return json
* @author :liyuhang
* @method
*/
public function add(BlogCategoryRequest $request,BlogCategoryModel $blogCategoryModel,BlogModel $blogModel){
$request->validated();
$this->param['project_id'] = $this->user['project_id'];
$this->param['operator_id'] = $this->uid;
$this->param['create_id'] = $this->uid;
DB::beginTransaction();
$rs = $blogCategoryModel->add($this->param);
if($rs === false){
DB::rollBack();
$this->response('error',Code::USER_ERROR);
}
//TODO::判断当前分内是否为一级分类
if(isset($this->param['pid']) && !empty($this->param['pid'])){
//查看当前上级分类下是否有其他分类
$cate_info = $blogCategoryModel->read(['pid'=>$this->param['pid'],'id'=>['!=',$blogCategoryModel->id]]);
if($cate_info === false){
//查看当前上一级分类下是否有商品
$news_info = $blogModel->read(['category_id'=>$this->param['pid'],'pid'=>0]);
if($news_info !== false){
//更新所有商品到当前分类
$rs = $blogModel->edit(['category_id'=>$blogCategoryModel->id],['category_id'=>$this->param['pid']]);
if($rs === false){
DB::rollBack();
$this->response('error',Code::USER_ERROR);
}
}
}
}
//TODO::写入日志
DB::commit();
$this->response('success');
}
/**
* @name :编辑分类
* @return void
* @author :liyuhang
* @method
*/
public function edit(BlogCategoryRequest $request,BlogCategoryModel $blogCategoryModel){
$request->validate([
'id'=>['required']
],[
'id.required' => 'ID不能为空'
]);
$this->param['operator_id'] = $this->uid;
$rs = $blogCategoryModel->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
//TODO::写入日志
$this->response('success',Code::SUCCESS);
}
/**
* @name :编辑状态/与排序
* @return void
* @author :liyuhang
* @method
*/
public function status(Request $request,BlogCategoryModel $blogCategoryModel){
$request->validate([
'id'=>['required'],
],[
'id.required' => 'ID不能为空',
]);
$this->param['operator_id'] = $this->uid;
$rs = $blogCategoryModel->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
$this->response('success');
}
/**
* @name :删除分类
* @return void
* @author :liyuhang
* @method
*/
public function del(Request $request,BlogCategoryModel $blogCategoryModel,BlogModel $blogModel){
$request->validate([
'id'=>['required'],
],[
'id.required' => 'ID不能为空',
]);
foreach ($this->param['id'] as $v){
//查询是否有子分类
$rs = $blogCategoryModel->read(['pid'=>$v],['id']);
if($rs !== false){
$this->response('当前分类拥有子分类不允许删除',Code::USER_ERROR);
}
//查看当前分内下是否有博客
$rs = $blogModel->read(['category_id'=>$v],['id']);
if($rs !== false){
$this->response('当前分类拥有博客',Code::USER_ERROR);
}
}
$this->param['id'] = ['in',$this->param['id']];
$rs = BlogCategoryModel->del($this->param);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
//TODO::写入操作日志
$this->response('success');
}
}