BlogLogic.php
7.9 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
<?php
namespace App\Http\Logic\Bside\Blog;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\Image;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
class BlogLogic extends BaseLogic
{
const STATUS_TWO = 2;
public function __construct()
{
parent::__construct();
$this->model = new Blog();
$this->param = $this->requestAll;
}
/**
* @name :获取分类列表
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function blog_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;
}
}
return $this->success($list);
}
/**
* @name :添加博客
* @return void
* @author :liyuhang
* @method
*/
public function blog_add(){
$condition = [
'name'=>$this->param['name']
];
//查看当前名称是否存在
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前名称已存在');
}
$this->param['create_id'] = $this->user['id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['project_id'] = $this->user['project_id'];
$this->param['created_at'] = date('Y-m-d H:i:s',time());
$this->param['updated_at'] = date('Y-m-d H:i:s',time());
DB::beginTransaction();
try {
if(isset($this->param['image'])){
$data = $this->upload();
$this->param['image'] = $data;
}
$rs = $this->model->insertGetId($this->param);
//分类计数
$category_data = explode(',',trim($this->param['category_id'],','));
$this->set_num(BlogCategoryModel::class,$category_data);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error');
}
//TODO::写入日志
return $this->success();
}
/**
* @name : 编辑博客
* @return void
* @author :liyuhang
* @method
*/
public function blog_edit(){
$condition = [
'id'=>['!=',$this->param['id']],
'name'=>$this->param['name']
];
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前名称已存在');
}
$this->param['operator_id'] = $this->user['id'];
DB::beginTransaction();
try {
$info = $this->model->read(['id'=>$this->param['id']],['id','image','category_id']);
//查看分类是否有更新
if($info['category_id'] !== $this->param['category_id']){
$this->update_category_num($info['category_id'],$this->param['category_id']);
}
//是否有图片更新
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
if($info !== false && !empty($info['image'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
shell_exec('sudo rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['image'] . '*');
$imageModel->del(['hash'=>$info['image']]);
}
$this->param['image'] = $this->upload();
}
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
$this->edit($this->param,['id'=>$this->param['id']]);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error');
}
return $this->success();
}
/**
* @name :编辑seo
* @return void
* @author :liyuhang
* @method
*/
public function edit_seo(){
$this->param['operator_id'] = $this->user['id'];
$rs = $this->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @name :获取数据详情
* @return array
* @author :liyuhang
* @method
*/
public function blog_info(){
//读取缓存
$info = Common::get_user_cache($this->model->getTable(),$this->param['id']);
if(empty($info)){
$info = $this->model->read($this->param);
if($info === false){
$this->fail('error');
}
//获取分类名称
$blogCategoryLogic = new BlogCategoryLogic();
$info = $blogCategoryLogic->get_category_name($info);
//获取标签名称
$blogLabelLogic = new BlogLabelLogic();
$info = $blogLabelLogic->get_label_name($info);
//写入缓存
Common::set_user_cache($info,$this->model->getTable(),$this->param['id']);
}
return $this->success($info);
}
/**
* @name :修改状态
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function blog_status(){
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error',Code::USER_ERROR);
}
return $this->success();
}
/**
* @name :删除
* @return void
* @author :liyuhang
* @method
*/
public function blog_del(){
$ids = $this->param['id'];
//获取所有博客的分类id
$str = $this->get_category_id($ids);
DB::beginTransaction();
try {
$category_data = explode(',',$str);
//分类计数减1
$this->set_num(BlogCategoryModel::class,$category_data,'del');
$this->param['id'] = ['in',$this->param['id']];
$this->del($this->param,$ids);
DB::commit();
}catch (Exception $e){
DB::rollBack();
$this->fail('当前数据不存在');
}
return $this->success();
}
/**
* @param $ids
* @name :删除数据时获取所有数据的分类id
* @return void
* @author :liyuhang
* @method
*/
public function get_category_id($ids = []){
$str = '';
$list = $this->model->list(['id'=>['in',$ids]],'id',['id','category_id']);
foreach ($list as $v){
$str .= trim($v['category_id'],',').',';
}
return trim($str,',');
}
/**
* @name :编辑分类时更新分类计数
* @return void
* @author :liyuhang
* @method
*/
public function update_category_num($category_del = '',$category_add = ''){
//分类计数(减)
$category_del_data = explode(',',trim($category_del,','));
$this->set_num(BlogCategoryModel::class,$category_del_data);
//分类计数(加)
$category_add_data = explode(',',trim($category_add,','));
$this->set_num(BlogCategoryModel::class,$category_add_data,'del');
return true;
}
}