NewsLogic.php
7.0 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
<?php
namespace App\Http\Logic\Bside\News;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;
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(){
//拼接参数
DB::beginTransaction();
try {
$this->param = $this->paramProcessing($this->param);
if(isset($this->param['id']) && !empty($this->param['id'])){
//是否更新路由
$id = $this->editNewsRoute($this->param['id'],$this->param['url']);
$this->edit($this->param,['id'=>$this->param['id']]);
}else{
$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]);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('添加失败');
}
//通知更新
$this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_NEWS, 'route'=>$route]);
return $this->success();
}
/**
* @remark :查看是否编辑路由
* @name :editCategoryRoute
* @author :lyh
* @method :post
* @time :2023/9/7 11:05
*/
public function editNewsRoute($id,$route){
//生成一条删除路由记录
$info = $this->model->read(['id'=>$id],['id','url']);
if($info['url'] != $route){
$data = [
'source'=>RouteMap::SOURCE_NEWS,
'route'=>$info['url'],
];
$this->setRouteDeleteSave($data);
}
return $id;
}
/**
* @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);
$newsCategoryLogic = new NewsCategoryLogic();
$info = $newsCategoryLogic->get_category_name($info);
return $this->success($info);
}
/**
* @name :逻辑删除
* @return void
* @author :liyuhang
* @method
*/
public function newsDel(){
$ids = $this->param['id'];
DB::beginTransaction();
try {
$this->param['id'] = ['in',$this->param['id']];
$this->model->del($this->param);
foreach ($ids as $id){
$this->delRoute($id);
}
DB::commit();
}catch (Exception $e){
DB::rollBack();
$this->fail('当前数据不存在');
}
return $this->success();
}
/**
* @name :(参数处理)paramProcessing
* @author :lyh
* @method :post
* @time :2023/6/13 11:30
*/
public function paramProcessing($param){
if(isset($this->param['id'])){
$param['operator_id'] = $this->user['id'];
$param['category_id'] = ','.trim($param['category_id'],',').',';
}else{
$param['create_id'] = $this->user['id'];
$param['operator_id'] = $this->user['id'];
$param['project_id'] = $this->user['project_id'];
$param['category_id'] = ','.$param['category_id'].',';
}
return $this->success($param);
}
/**
* @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{
$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 :delRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:50
*/
public function delRoute($id){
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
//生成一条删除路由记录
$info = $this->model->read(['id'=>$id],['id','route']);
$data = [
'source'=>RouteMap::SOURCE_NEWS,
'route'=>$info['route'],
];
$this->setRouteDeleteSave($data);
return $this->success();
}
}