BlogController.php
8.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
namespace App\Http\Controllers\Bside\Blog;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Blog\BlogLogic;
use App\Http\Requests\Bside\Blog\BlogRequest;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogCategory;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\Product\Category;
use App\Models\User\User;
class BlogController extends BaseController
{
/**
* @remark :博客列表
* @name :lists
* @author :lyh
* @method :post
* @time :2023/9/14 10:45
*/
public function lists(BlogModel $blogModel){
$filed = ['id','category_id','operator_id','status','created_at','label_id','image','updated_at','name','sort','url'];
$this->order = 'sort';
$query = $blogModel->orderBy($this->order ,'desc')->orderBy('id','desc');
$query = $this->searchParam($query);
$lists = $query->select($filed)->paginate($this->row, ['*'], 'page', $this->page);
if(!empty($lists)){
$lists = $lists->toArray();
// //获取当前项目的所有分类
$data = $this->getCategoryList();
$user = new User();
foreach ($lists['list'] as $k => $v){
$v['category_name'] = $this->categoryName($v['category_id'],$data);
$v['url'] = $this->user['domain'] .$v['url'];
$v['image_link'] = getImageUrl($v['image']);
$v['operator_name'] = $user->getName($v['operator_id']);
$lists['list'][$k] = $v;
}
}
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :处理列表返回参数
* @name :handleReturnParam
* @author :lyh
* @method :post
* @time :2023/9/14 10:01
*/
public function searchParam(&$query){
$query = $query->where('project_id',$this->user['project_id']);
if (isset($this->map['category_id']) && !empty($this->map['category_id'])) {
$str = [];
$this->getLastLevelIds($this->map['category_id'],$str);
$query->where(function ($subQuery) use ($str) {
foreach ($str as $v) {
$subQuery->orWhereRaw("FIND_IN_SET(?, category_id) > 0", [$v]);
}
});
}
if(isset($this->map['status'])){
$query = $query->where('status',$this->map['status']);
}
if(!empty($this->map['start_at']) && !empty($this->map['end_at'])){
$query->whereBetween('created_at', [$this->map['start_at'],$this->map['end_at']]);
}
return $query;
}
/**
* @remark :获取当前分类的最后一级id
* @name :getLastLevelIds
* @author :lyh
* @method :post
* @time :2023/10/20 15:02
*/
public function getLastLevelIds($id, &$str = []) {
$cateModel = new BlogCategoryModel();
$subList = $cateModel->where('pid', $id)->get();
if ($subList->isEmpty()) {
// 如果没有子集,将当前 ID 添加到最后一级 ID 数组
$str[] = $id;
} else {
// 如果有子集,继续向下遍历
foreach ($subList as $v) {
$this->getLastLevelIds($v->id, $str);
}
}
}
/**
* @remark :获取所有分类名称
* @name :getCategoryList
* @author :lyh
* @method :post
* @time :2023/9/14 13:56
*/
public function getCategoryList(){
$categoryModel = new BlogCategory();
$data = [];
$cateList = $categoryModel->list(['project_id'=>$this->user['project_id']],['id','name']);
if(!empty($cateList)){
foreach ($cateList as $value){
$data[$value['id']] = $value['name'];
}
}
return $data;
}
/**
* @remark :获取分类名称
* @name :categoryName
* @author :lyh
* @method :post
* @time :2023/9/14 13:58
*/
public function categoryName($category_id,$data){
$category_name = '';
if(!empty($category_id) && !empty($data)){
$arr = explode(',',trim($category_id,','));
foreach ($arr as $v){
if(isset($data[$v])){
$category_name .= $data[$v].',';
}
}
$category_name = trim($category_name,',');
}
return $category_name;
}
/**
* @remark :根据状态数量
* @name :getStatusNumber
* @author :lyh
* @method :post
* @time :2023/6/19 9:39
*/
public function getStatusNumber(BlogLogic $blogLogic){
$data = $blogLogic->getStatusNumber();
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :(搜索)获取分类
* @name :get_category_list
* @author :lyh
* @method :post
* @time :2023/10/19 15:08
*/
public function get_category_list(){
$this->map['status'] = 0;
$this->map['project_id'] = $this->user['project_id'];
$blogCategoryModel = new BlogCategoryModel();
$cate_list = $blogCategoryModel->list($this->map,'sort');
if($cate_list === false){
$this->fail('error',Code::USER_ERROR);
}
$list = [];
foreach ($cate_list as $v){
$v = (array)$v;
if ($v['pid'] == 0) {
$v['sub'] = _get_child($v['id'], $cate_list);
$list[] = $v;
}
}
$this->response('success',Code::SUCCESS,$list);
}
/**
* @name :获取当前博客详情
* @author :liyuhang
* @method
*/
public function info(BlogLogic $blogLogic){
$this->request->validate([
'id'=>['required']
],[
'id.required' => 'ID不能为空'
]);
$info = $blogLogic->blogInfo();
$this->response('success',Code::SUCCESS,$info);
}
/**
* @remark :保存数据
* @name :save
* @author :lyh
* @method :post
* @time :2023/9/7 13:40
*/
public function save(BlogRequest $request,BlogLogic $blogLogic){
$request->validated();
$blogLogic->blogSave();
$this->response('success');
}
/**
* @name :编辑新闻seo
* @author :liyuhang
* @method
*/
public function edit_seo(BlogLogic $blogLogic){
$this->request->validate([
'id'=>['required'],
'seo_title'=>['required'],
'seo_description'=>['required'],
'seo_keywords'=>['required'],
],[
'id.required' => 'ID不能为空',
'seo_title.required' => 'seo_title不能为空',
'seo_description.required' => 'seo_description不能为空',
'seo_keywords.required' => 'seo_description不能为空',
]);
$blogLogic->edit_seo();
$this->response('success');
}
/**
* @name :编辑博客状态/排序
* @author :liyuhang
* @method
*/
public function status(BlogLogic $blogLogic){
$this->request->validate([
'id'=>['required','array'],
],[
'id.required' => 'ID不能为空',
'id.array' => 'ID为数组',
]);
$blogLogic->blogStatus();
//TODO::写入日志
$this->response('success');
}
/**
* @name :删除博客(批量逻辑删除)
* @author :liyuhang
* @method
*/
public function del(BlogLogic $blogLogic){
$this->request->validate([
'id'=>['required','array'],
],[
'id.required' => 'ID不能为空',
'id.array' => 'ID为数组',
]);
$blogLogic->blogDel();
$this->response('success');
}
/**
* @remark :排序
* @name :sort
* @author :lyh
* @method :post
* @time :2023/8/22 10:24
*/
public function sort(BlogLogic $logic){
$this->request->validate([
'id'=>'required',
'sort'=>'required'
],[
'id.required' => '产品ID不能为空',
'sort.required'=>'排序字段不能为空'
]);
$logic->setSort();
$this->response('success');
}
}