News.php
4.3 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\Models\News;
use App\Helper\Arr;
use App\Models\Base;
class News extends Base
{
const STATUS_DRAFT = 0;
const STATUS_ONE = 1;
protected $table = 'gl_news';
//连接数据库
protected $connection = 'custom_mysql';
public static function getNumByProjectId($project_id){
return self::where('project_id', $project_id)->where('status', 1)->count();
}
public function getReleaseAtAttribute($value){
if(!$value){
return date('Y-m-d H:i:s', strtotime($this->getAttribute('created_at')));
}
return $value;
}
public function getRelatedNewsIdAttribute($value){
return Arr::setToArr($value);
}
public function getRelatedProductIdAttribute($value){
return Arr::setToArr($value);
}
/**
* @remark :扩展字段根据type返回类型
* @name :setTypValues
* @author :lyh
* @method :post
* @time :2023/12/6 14:43
*/
public function getExtendInfo($news_id){
$extendModel = new NewsExtend();
$list = $extendModel->list(['status'=>1],'id',['id','type','key','title']);
if(empty($list)){
return [];
}
$extendInfoModel = new NewsExtendInfo();
$infoList = $extendInfoModel->list(['news_id'=>$news_id],'created_at');
foreach ($list as $k=>$v){
if($v['type'] == 3 || $v['type'] == 4){
$v['values'] = [];
}else{
$v['values'] = '';
}
if(!empty($infoList)){
foreach ($infoList as $values){
if($v['key'] == $values['key']){
$v = $this->setTypValues($v,$values);
break;
}
}
}
$list[$k] = $v;
}
return $list;
}
/**
* @remark :扩展字段根据type返回类型
* @name :setTypValues
* @author :lyh
* @method :post
* @time :2023/12/6 14:43
*/
public function setTypValues($v,$info){
if($v['type'] == 3){
$arr = json_decode($info['values']);
foreach ($arr as $k1=>$v1){
$v1 = (array)$v1;
$v1['url'] = getImageUrl($v1['url']);
$arr[$k1] = $v1;
}
$v['values'] = $arr;
}elseif($v['type'] == 4){
$arr1 = json_decode($info['values']);
foreach ($arr1 as $k1=>$v1){
$v1 = (array)$v1;
if(isset($v1['url'])){
$v1['url'] = getFileUrl($v1['url']);
}else{
$v1 = getFileUrl($v1);
}
$arr1[$k1] = $v1;
}
$v['values'] = $arr1;
}else{
$v['values'] = $info['values'];
}
return $v;
}
/**
* @remark :保存扩展字段
* @name :saveExtend
* @author :lyh
* @method :post
* @time :2023/11/9 15:02
*/
public function saveExtendInfo($news_id,$extend,$project_id){
//先删除以前的数据
$extendInfoModel = new NewsExtendInfo();
$extendInfoModel->del(['news_id'=>$news_id]);
if(empty($extend)) {
return true;
}
foreach ($extend as $k => $v){
if(empty($v['values'])){
continue;
}
$v = $this->saveHandleExtend($v,$news_id,$project_id);
$extendInfoModel->add($v);
}
return true;
}
/**
* @remark :保存扩展字段时处理数据
* @name :saveHandleExtend
* @author :lyh
* @method :post
* @time :2023/12/6 15:11
*/
public function saveHandleExtend(&$v,$news_id,$project_id){
unset($v['title']);
if($v['type'] == 3){
foreach ($v['values'] as $k1=>$v1){
$v1['url'] = str_replace_url($v1['url']);
$v['values'][$k1] = $v1;
}
$v['values'] = json_encode($v['values']);
}elseif ($v['type'] == 4){
foreach ($v['values'] as $k1=>$v1){
$v1['url'] = str_replace_url($v1['url']);
$v['values'][$k1] = $v1;
}
$v['values'] = json_encode($v['values']);
}
$v['project_id'] = $project_id;
$v['news_id'] = $news_id;
return $v;
}
}