NewsCategoryLogic.php
11.8 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace App\Http\Logic\Bside\News;
use App\Helper\Translate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Com\NoticeLog;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
class NewsCategoryLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new NewsCategoryModel();
$this->param = $this->requestAll;
}
/**
* @remark :获取详情
* @name :info_news_category
* @author :lyh
* @method :post
* @time :2023/10/19 16:04
*/
public function info_news_category(){
$info = $this->model->read($this->param);
$info['url'] = $this->user['domain'] . $info['alias'];
if(!empty($info['banner_image'])){
$info['banner_image'] = getImageUrl($info['banner_image'],$this->user['storage_type'] ?? 0,$this->user['project_location']);
}
return $this->success($info);
}
/**
* @remark :保存处理字段
* @name :handleParam
* @author :lyh
* @method :post
* @time :2024/9/13 9:15
*/
public function handleParam($param)
{
if(isset($param['banner_image']) && !empty($param['banner_image'])){
$param['banner_image'] = str_replace_url($param['banner_image']);
}
if(isset($param['image']) && !empty($param['image'])){
$param['image'] = str_replace_url($param['image']);
}
return $this->success($param);
}
/**
* @remark :保存数据
* @name :newsCategorySave
* @author :lyh
* @method :post
* @time :2023/9/7 14:53
*/
public function newsCategorySave(){
//验证名称是否存在
$this->verifyParamName($this->param['name']);
$this->param = $this->handleParam($this->param);
if(isset($this->param['id']) && !empty($this->param['id'])){
$info = $this->model->read(['id'=>$this->param['id']],['id','alias']);
$id = $this->param['id'];
$this->param['alias'] = RouteMap::setRoute($this->param['alias'], RouteMap::SOURCE_NEWS_CATE, $id, $this->user['project_id']);
$route = $this->param['alias'];
if(empty($route)){
$this->fail('alias路由不能为空');
}
$this->param['operator_id'] = $this->user['id'];
$this->edit($this->param,['id'=>$id]);
$this->curlDelRoute(['new_route'=>$route ?? '','old_route'=>$info['alias'] ?? '']);
}else{
if(!isset($this->param['alias']) || empty($this->param['alias'])){
$this->param['alias'] = Translate::tran($this->param['name'], 'en');
if(empty($this->param['alias'])){
$this->fail('路由翻译错误,请手动输入路由');
}
}
$this->param = $this->addParamProcessing($this->param);
$id = $this->model->addReturnId($this->param);
$route = RouteMap::setRoute($this->param['alias'], RouteMap::SOURCE_NEWS_CATE, $id, $this->user['project_id']);
$this->model->edit(['alias'=>$route],['id'=>$id]);
$this->curlDelRoute(['new_route'=>$route]);
}
$this->addUpdateNotify(RouteMap::SOURCE_NEWS_CATE,$route);
return $this->success(['id'=>$id]);
}
/**
* @remark :修改状态
* @name :status_news_category
* @author :lyh
* @method :post
* @time :2023/11/30 15:13
*/
public function status_news_category(){
$this->param['operator_id'] = $this->user['id'];
$this->edit($this->param,['id'=>$this->param['id']]);
return $this->success();
}
/**
* @remark :删除分类
* @name :del_news_category
* @author :lyh
* @method :post
* @time :2024/5/16 14:58
*/
public function del_news_category(){
$ids = $this->param['id'];
foreach ($ids as $id){
$str = [];
$this->getAllSub($id,$str);
$str[] = $id;
foreach ($str as $value){
//删除路由
$this->delRoute($value);
$this->model->del(['id'=>$value]);
}
}
NoticeLog::createLog(NoticeLog::DELETE_NEWS_CATEGORY, ['project_id' => $this->user['project_id']]);
return $this->success();
}
/**
* @name :(添加分类时获取1级分类)categoryTopList
* @author :lyh
* @method :post
* @time :2023/6/13 9:09
*/
public function categoryTopList(){
$this->param['project_id'] = $this->user['project_id'];
$this->param['status'] = 0;
if(isset($this->param['id']) && !empty($this->param['id'])){
$str = [];
//排序掉当前id下所有子集
$str = $this->getAllSub($this->param['id'],$str);
$str[] = (int)$this->param['id'];
$this->param['id'] = ['not in',$str];
}
$menu = array();
$list = $this->model->list($this->param);
if(!empty($list)){
foreach ($list as $k => $v){
if($v['pid'] == 0){
$v['sub'] = _get_child($v['id'],$list);
$menu[] = $v;
}
}
}
return $this->success($menu);
}
/**
* @remark :获取当前id下所有子集
* @name :getAllSub
* @author :lyh
* @method :post
* @time :2023/10/18 15:10
*/
public function getAllSub($id,&$str = []){
$list = $this->model->list(['pid'=>$id,'status'=>0],['id','pid']);
if(!empty($list)){
foreach ($list as $v){
$str[] = $v['id'];
$this->getAllSub($v['id'],$str);
}
}
return $str;
}
/**
* @name :(验证名称是否存在)verifyParamName
* @author :lyh
* @method :post
* @time :2023/6/13 11:41
*/
public function verifyParamName($name){
if(isset($this->param['id']) && !empty($this->param['id'])){
$condition = ['id'=>['!=',$this->param['id']], 'name'=>$name];
}else{
$condition = ['name'=>$name];
}
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前分类名称已存在');
}
return $this->success();
}
/**
* @name :(参数处理)paramProcessing
* @author :lyh
* @method :post
* @time :2023/6/13 11:30
*/
public function addParamProcessing($param){
$param['project_id'] = $this->user['project_id'];
$param['operator_id'] = $this->user['id'];
$param['create_id'] = $this->user['id'];
return $this->success($param);
}
/**
* @remark :删除路由
* @name :delRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:50
*/
public function delRoute($id){
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_NEWS_CATE, $id, $this->user['project_id']);
$info = $this->model->read(['id'=>$id],['id','alias']);
$this->curlDelRoute(['old_route'=>$info['alias'],'source'=>RouteMap::SOURCE_NEWS_CATE]);
return $this->success();
}
/**
* 新闻导入:分类处理
* @param $project_id
* @param $user_id
* @param $category
* @return string
* @throws \Exception
* @author Akun
* @date 2023/09/20 17:35
*/
public function importNewsCategory($project_id,$user_id,$category){
$return = [];
$cate_arr = explode('^v6sp$',$category);
$pid = 0;
foreach ($cate_arr as $v){
if($v){
$category_info = $this->model->read(['name'=>$v,'pid'=>$pid]);
if(!$category_info){
$id = $this->model->addReturnId(['name'=>$v,'pid'=>$pid,'project_id'=>$project_id,'operator_id'=>$user_id,'create_id'=>$user_id]);
$route = RouteMap::setRoute($v, RouteMap::SOURCE_NEWS_CATE, $id, $project_id);
$this->model->edit(['alias'=>$route],['id'=>$id]);
}else{
$id = $category_info['id'];
}
$return[] = $id;
$pid = $id;
}
}
return $this->getCategory($return);
}
/**
* @remark :获取最后一级分类id
* @name :getLastCategory
* @author :lyh
* @method :post
* @time :2023/10/20 9:02
*/
public function getCategory($category){
$str = '';
foreach ($category as $v){
$str .= $v.',';
}
return !empty($str) ? ','.$str : '';
}
/**
* @remark :排序
* @name :categorySort
* @author :lyh
* @method :post
* @time :2023/9/26 17:38
*/
public function categorySort(){
$rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('系统错误,请联系管理员');
}
return $this->success();
}
/**
* @remark :设置批量排序
* @name :setAllSort
* @author :lyh
* @method :post
* @time :2024/1/10 15:40
*/
public function setAllSort(){
foreach ($this->param['data'] as $k => $v){
$this->model->edit(['sort'=>$v['sort']],['id'=>$v['id']]);
}
return $this->success();
}
/**
* @remark :复制新闻分类
* @name :copyCategory
* @author :lyh
* @method :post
* @time :2024/5/9 9:16
*/
public function copyCategory(){
$info = $this->model->read(['id'=>$this->param['id']]);
$param = [
'name'=>$info['name']."-copy",
'status'=>0,
'sort'=>$info['sort'],
'pid'=>0,
'remark'=>$info['remark'],
'alias'=>$info['alias'],
'project_id'=>$info['project_id'],
'operator_id'=>$this->user['id'],
'create_id'=>$this->user['id'],
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
$save_id = $this->model->insertGetId($param);
$route = RouteMap::setRoute($param['alias'], RouteMap::SOURCE_NEWS_CATE, $save_id, $this->user['project_id']);
$this->model->edit(['alias'=>$route],['id'=>$save_id]);
//同步可视化装修数据
$this->copyTemplate($this->param['id'],$info['project_id'],$save_id);
return $this->success(['id'=>$save_id]);
}
/**
* @remark :同步模版数据
* @name :copyTemplate
* @author :lyh
* @method :post
* @time :2023/7/29 15:53
*/
public function copyTemplate($id,$project_id,$save_id){
$BTemplateModel = new BTemplate();
$list = $BTemplateModel->list(['source'=>BTemplate::SOURCE_NEWS,'is_list'=>BTemplate::IS_LIST,'is_custom'=>BTemplate::IS_NO_CUSTOM,'source_id'=>$id,'project_id'=>$project_id]);
if(!empty($list)){
$data = [];
foreach ($list as $v){
$data[] = [
'html'=>$v['html'],
'project_id'=>$v['project_id'],
'source'=>$v['source'],
'source_id'=>$save_id,
'template_id'=>$v['template_id'],
'section_list_id'=>$v['section_list_id'],
'main_html'=>$v['main_html'],
'main_css'=>$v['main_css'],
'is_list'=>$v['is_list'],
'type'=>$v['type'],
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
}
$rs = $BTemplateModel->insert($data);
if($rs === false){
$this->fail('error');
}
}
return $this->success();
}
}