NewsLogic.php
20.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
namespace App\Http\Logic\Bside\News;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\News\NewsExtendInfo;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Services\CosService;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;
class NewsLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new News();
$this->param = $this->requestAll;
}
/**
* @name :获取分类列表
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function news_get_category_list()
{
$this->map['status'] = 0;
$this->map['project_id'] = $this->user['project_id'];
$newsCategoryModel = new NewsCategoryModel();
$cate_list = $newsCategoryModel->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;
}
}
return $this->success($list);
}
/**
* @remark :保存数据
* @name :newsSave
* @author :lyh
* @method :post
* @time :2023/9/7 11:02
*/
public function newsSave()
{
$this->param = $this->paramProcessing($this->param);
if (isset($this->param['id']) && !empty($this->param['id'])) {
$info = $this->model->read(['id'=>$this->param['id']],['id','url']);
$id = $this->param['id'];
$is_upgrade = $this->param['is_upgrade'] ?? 0;//1:5.0数据 0:6.0
$six_read = $this->param['six_read'] ?? 0;//是否按6.0显示
if($is_upgrade == 0 || $six_read == 1) {
$this->param['url'] = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
}
//是否更新路由
$route = $this->param['url'];
$this->edit($this->param, ['id' => $id]);
$this->curlDelRoute(['new_route'=>$route ?? '','old_route'=>$info['url'] ?? '']);
} else {
$this->param['sort'] = $this->setNewsSort();
$id = $this->model->addReturnId($this->param);
$route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
$this->edit(['url' => $route], ['id' => $id]);
$this->curlDelRoute(['new_route'=>$route]);
}
$this->model->saveExtendInfo($id,$this->param['extend'] ?? [],$this->user['project_id']);
$this->addUpdateNotify(RouteMap::SOURCE_NEWS,$route);
return $this->success(['id'=>$id]);
}
/**
* @remark :设置最新产品的sort排序
* @name :setNewsSort
* @author :lyh
* @method :post
* @time :2023/12/25 9:27
*/
public function setNewsSort(){
$info = $this->model->orderBy('sort','desc')->first();
if(empty($info)){
return 1;
}
$sort = $info['sort']+1;
return $sort;
}
/**
* @name :编辑seo
* @return void
* @author :liyuhang
* @method
*/
public function edit_seo()
{
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param, ['id' => $this->param['id']]);
if ($rs === false) {
$this->fail('error');
}
return $this->success();
}
/**
* @name :修改状态
* @return void
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function news_status()
{
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param, ['id' => ['in', $this->param['id']]]);
if ($rs === false) {
$this->fail('error');
}
return $this->success();
}
/**
* @name :获取详情
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function newsInfo()
{
$info = $this->model->read($this->param);
if($info === false){
$this->fail('当前数据不存在');
}
$info['category_id'] = explode(',',trim($info['category_id'],','));
$info['image_link'] = getImageUrl($info['image'],$this->user['storage_type'],$this->user['project_location']);
$info['og_image'] = getImageUrl(empty($info['og_image']) ? $info['image'] : $info['og_image'],$this->user['storage_type'],$this->user['project_location']);
$info['extend'] = $this->model->getExtendInfo($info['id']);
return $this->success($info);
}
/**
* @name :逻辑删除
* @return void
* @author :liyuhang
* @method
*/
public function newsDel()
{
DB::beginTransaction();
try {
foreach ($this->param['id'] as $id) {
$info = $this->model->read(['id'=>$id],['id','status']);
if($info['status'] != 2){
$this->delRoute($id);
$this->model->edit(['status'=>2],['id'=>$id]);
}else{
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
$this->delRoute($id);
$this->model->del(['id' => $id]);
//删除扩展字段
$extendInfoModel = new NewsExtendInfo();
$extendInfoModel->del(['news_id'=>$id]);
}
}
DB::commit();
} catch (Exception $e) {
DB::rollBack();
$this->fail('error');
}
return $this->success();
}
/**
* @name :(参数处理)paramProcessing
* @author :lyh
* @method :post
* @time :2023/6/13 11:30
*/
public function paramProcessing($param)
{
$param['operator_id'] = $this->user['id'];
if(isset($param['image'])){
$param['image'] = str_replace_url($param['image'] ?? '');
}
if(isset($param['og_image'])){
$param['og_image'] = str_replace_url($param['og_image'] ?? '');
}
if(isset($this->param['text'])){
$param['text'] = $this->handleText($this->param['text']);
}
if(isset($this->param['id'])){
$param['operator_id'] = $this->user['id'];
}else{
$param['create_id'] = $this->user['id'];
$param['project_id'] = $this->user['project_id'];
}
if(isset($param['category_id']) && !empty($param['category_id'])){
$param['category_id'] = $this->getCategory($param['category_id']);
}else{
$param['category_id'] = '';
}
if(isset($this->param['release_at']) && !empty(($this->param['release_at'])) && ($this->param['status'] == 3)){
if($this->param['release_at'] < date('Y-m-d 23:59:59')){
$this->fail('发布时间需大于当天');
}
}
if(isset($param['related_news_id'])){
$param['related_news_id'] = implode(',',$param['related_news_id']);
}
if(isset($param['related_product_id'])){
$param['related_product_id'] = implode(',',$param['related_product_id']);
}
$param['seo_title'] = truncate_text($param['seo_title'] ?? $param['name'],70);
return $this->success($param);
}
/**
* @remark :处理text
* @name :handleText
* @author :lyh
* @method :post
* @time :2024/11/7 17:20
*/
public function handleText($text){
$pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
$matches = [];
preg_match_all($pattern, $text, $matches);
$text = $this->saveBase64Images($matches[1],$text);
return $this->success($text);
}
/**
* @remark :解码图片
* @name :saveBase64Images
* @author :lyh
* @method :post
* @time :2024/11/7 16:52
*/
public function saveBase64Images($imageSources,&$text)
{
foreach ($imageSources as $src) {
if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
$imageType = $match[1]; // Image type (png, jpg, etc.)
$imageUrl = $this->manager_uploads($src,$imageType);
$text = str_replace($src, $imageUrl, $text);
}
}
return $this->success($text);
}
/**
* @remark :自调用
* @name :manager_uploads
* @author :lyh
* @method :post
* @time :2024/11/7 11:00
*/
public function manager_uploads($files,$type = 'png'){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.($this->user['project_id']).'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id']).rand(1,10000)) . '.' .$type;
return getImageUrl($cosService->uploadFile($files,$path,$fileName));
}
/**
* @remark :获取分类(字符串)
* @name :getLastCategory
* @author :lyh
* @method :post
* @time :2023/10/20 9:02
*/
public function getCategory($category){
$str = implode(',',$category);
return !empty(trim(trim($str,','),',')) ? ','.trim(trim($str,','),',').',' : '';
}
/**
* @remark :根据状态获取数量
* @name :getStatusNumber
* @author :lyh
* @method :post
* @time :2023/6/19 9:42
*/
public function getStatusNumber()
{
//三种状态 0:草稿 1:发布 2:回收站
$data = ['dra' => 0, 'pub' => 1, 'del' => 2, 'tal' => 3];
foreach ($data as $k => $v) {
if ($v == 3) {
$data[$k] = $this->model->where(['project_id' => $this->user['project_id']])->count();
} else {
if($v == 0){
$data[$k] = $this->model->formatQuery(['status' => ['in',[0,3]], 'project_id' => $this->user['project_id']])->count();
}else{
$data[$k] = $this->model->where(['status' => $v, 'project_id' => $this->user['project_id']])->count();
}
}
}
return $this->success($data);
}
/**
* @remark :排序
* @name :setSort
* @author :lyh
* @method :post
* @time :2023/8/19 11:16
*/
public function setSort()
{
$rs = $this->model->edit(['sort' => $this->param['sort']], ['id' => $this->param['id']]);
if ($rs === false) {
$this->fail('error');
}
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 :delRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:50
*/
public function delRoute($id)
{
//生成一条删除路由记录
$info = $this->model->read(['id' => $id], ['id', 'url']);
$this->curlDelRoute(['old_route'=>$info['url'],'source'=>RouteMap::SOURCE_NEWS]);
return $this->success();
}
/**
* 新闻导入
* @param $project_id
* @param $user_id
* @param $domain
* @param $data
* @return bool
* @throws \Exception
* @author Akun
* @date 2023/09/20 17:51
*/
public function importNews($project_id, $user_id, $domain, $data)
{
$route = $data[1]??'';//自定义路由
if($route){
$news = $this->model->read(['url' => $route]);
}else{
$news = $this->model->read(['name' => $data[0]]);
}
if (!$news) {
$category_id = '';
if ($data[2]??'') {
//处理分类
$newsCategoryLogic = new NewsCategoryLogic();
$category_id = $newsCategoryLogic->importNewsCategory($project_id, $user_id, $data[2]);
}
$text = '';
if($data[4]??''){
//处理内容中的图片
preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result);
if($result[2]??[]){
foreach ($result[2] as $img){
$new_img = check_remote_url_down($img,$project_id,$domain,1);
$new_img && $data[4] = str_replace($img,$new_img,$data[4]);
}
}
//处理内容中的视频
preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video);
if($result_video[2]??[]){
foreach ($result_video[2] as $video){
$new_video = check_remote_url_down($video,$project_id,$domain,1);
$new_video && $data[4] = str_replace($video,$new_video,$data[4]);
}
}
$text = $data[4];
}
$img = '';
if($data[5]??''){
$one_img = check_remote_url_down($data[5],$project_id,$domain);
if($one_img){
$img = $one_img;
}
}
$seo_title = '';
if($data[6]??''){
$seo_title = substr(strip_tags($data[6]),0,70);
}
$seo_keywords = '';
if($data[7]??''){
$seo_keywords = substr(strip_tags(str_replace('^v6sp$',',',$data[7])),0,255);
}
$seo_description = '';
if($data[8]??''){
$seo_description = substr(strip_tags($data[8]),0,200);
}
$release_at = date('Y-m-d H:i:s');
if($data[9]??''){
$release_at = date('Y-m-d H:i:s',strtotime($data[9]));
}
$sort = 10;
if($data[10]??''){
$sort = $data[10];
}
$id = $this->model->addReturnId(
[
'name' => $data[0],
'category_id' => $category_id,
'text' => $text,
'remark' => $data[3] ?? '',
'image' => $img,
'seo_title' => $seo_title,
'seo_keywords' => $seo_keywords,
'seo_description' => $seo_description,
'project_id' => $project_id,
'operator_id' => $user_id,
'create_id' => $user_id,
'status' => News::STATUS_ONE,
'url' => '',
'release_at' => $release_at,
'sort' => $sort
]
);
//更新路由
if($route){
$route_map = RouteMap::where('project_id', $project_id)->where('source', RouteMap::SOURCE_NEWS)->where('source_id', $id)->first();
if (!$route_map) {
$route_map = new RouteMap();
$route_map->project_id = $project_id;
$route_map->path = RouteMap::SOURCE_NEWS;
$route_map->source = RouteMap::SOURCE_NEWS;
$route_map->source_id = $id;
$route_map->route = $route;
$route_map->save();
}
}else{
$route = RouteMap::setRoute($data[0], RouteMap::SOURCE_NEWS, $id, $project_id);
}
$this->edit(['url' => $route], ['id' => $id]);
return true;
}
return false;
}
/**
* @remark :批量设置产品分类及状态
* @name :batchSetCategory
* @author :lyh
* @method :post
* @time :2023/8/15 17:53
*/
public function batchSetCategory(){
if(isset($this->param['category_id']) && !empty($this->param['category_id'])) {
DB::connection('custom_mysql')->beginTransaction();
$category_id_str = ','.implode(',',$this->param['category_id']).',';
try {
if(!isset($this->param['is_cover']) || ($this->param['is_cover'] == 1)){
//批量覆盖
$param = [
'category_id'=>$category_id_str,
'status'=>$this->param['status']
];
$this->model->edit($param,['id'=>['in',$this->param['id']]]);
DB::connection('custom_mysql')->commit();
}else{
foreach ($this->param['id'] as $id){
//获取当前产品的分类
$newsInfo = $this->model->read(['id'=>$id],['id','category_id']);
$category_ids = explode(',',trim($newsInfo['category_id'],','));
$category_ids_arr = array_values(array_unique(array_merge($category_ids,$this->param['category_id'])));
$category_ids_str = ','.implode(',',$category_ids_arr).',';
$this->model->edit(['category_id'=>$category_ids_str],['id'=>$id]);
}
}
DB::connection('custom_mysql')->commit();
}catch (\Exception $e){
DB::connection('custom_mysql')->rollBack();
$this->fail('系统错误,请联系管理员');
}
}
return $this->success();
}
/**
* @remark :复制新闻
* @name :copyNewsInfo
* @author :lyh
* @method :post
* @time :2024/4/28 14:51
*/
public function copyNewsInfo(){
$info = $this->model->read(['id'=>$this->param['id']]);
$param = [
'name'=>$info['name']."-1",
'status'=>0,
'sort'=>$info['sort'],
'category_id'=>$info['category_id'],
'text'=>$info['text'],
'remark'=>$info['remark'],
'url'=>$info['url'],
'image'=>$info['image'],
'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['url'], RouteMap::SOURCE_NEWS, $save_id, $this->user['project_id']);
$this->model->edit(['url'=>$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_DETAIL,'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'],
'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();
}
}