-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathAlbumController.php
228 lines (208 loc) · 8.14 KB
/
AlbumController.php
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
<?php
declare(strict_types=1);
namespace App\Controller\Blog;
use App\Constants\StatusCode;
use App\Controller\AbstractController;
use App\Foundation\Annotation\Explanation;
use App\Model\Blog\PhotoAlbum;
use App\Model\System\DictData;
use App\Model\System\GlobalConfig;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Middleware\RequestMiddleware;
use App\Middleware\PermissionMiddleware;
/**
* 相册控制器
* Class AlbumController
* @Controller(prefix="blog/picture_module/album")
*/
class AlbumController extends AbstractController
{
/**
* @Inject()
* @var PhotoAlbum
*/
private $photoAlbum;
/**
* 获取相册列表
* @RequestMapping(path="list", methods="get")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* @Middleware(PermissionMiddleware::class)
* })
*/
public function index()
{
$photoAlbumQuery = $this->photoAlbum->newQuery();
$albumName = $this->request->input('album_name') ?? '';
$albumStatus = $this->request->input('album_status') ?? '';
if (!empty($albumName)) $photoAlbumQuery->where('album_name', 'like', '%' . $albumName . '%');
if (strlen($albumStatus) > 0) $photoAlbumQuery->where('album_status', $albumStatus);
$total = $photoAlbumQuery->count();
$photoAlbumQuery = $this->pagingCondition($photoAlbumQuery, $this->request->all());
$data = $photoAlbumQuery->get();
return $this->success([
'list' => $data,
'total' => $total,
]);
}
/**
* 获取相册列表
* @RequestMapping(path="album_option", methods="get")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* })
*/
public function albumOptionList()
{
$photoAlbumQuery = $this->photoAlbum->newQuery();
$photoAlbumQuery = $photoAlbumQuery->select('id', 'album_name');
$data = $photoAlbumQuery->get();
return $this->success([
'list' => $data,
]);
}
/**
* @Explanation(content="添加相册")
* @RequestMapping(path="store", methods="post")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* @Middleware(PermissionMiddleware::class)
* })
*/
public function store()
{
$postData = $this->request->all();
$params = [
'album_name' => $postData['album_name'] ?? '',
'album_desc' => $postData['album_desc'] ?? '',
'album_type' => $postData['album_type'] ?? 1,
'album_status' => $postData['album_status'] ?? 1,
'album_author' => $postData['album_author'] ?? '',
'album_cover' => $postData['album_cover'] ?? '',
'album_question' => $postData['album_question'] ?? '',
'album_answer' => $postData['album_answer'] ?? '',
'album_sort' => $postData['album_sort'] ?? 99
];
//配置验证
$rules = [
'album_name' => 'required',
];
//错误信息
$message = [
'album_name.required' => '[album_name]缺失',
];
if ($params['album_type'] == 2) {
$rules['album_question'] = 'required';
$rules['album_answer'] = 'required';
$message['album_question.required'] = '[album_question]缺失';
$message['album_answer.required'] = '[album_answer]缺失';
}
$this->verifyParams($params, $rules, $message);
$photoAlbumObj = new PhotoAlbum();
$photoAlbumObj->album_name = $params['album_name'];
$photoAlbumObj->album_desc = $params['album_desc'];
$photoAlbumObj->album_type = $params['album_type'];
$photoAlbumObj->album_status = $params['album_status'];
$photoAlbumObj->album_author = $params['album_author'];
$photoAlbumObj->album_cover = $params['album_cover'];
$photoAlbumObj->album_question = $params['album_question'];
$photoAlbumObj->album_answer = $params['album_answer'];
$photoAlbumObj->album_sort = $params['album_sort'];
if (!$photoAlbumObj->save()) $this->throwExp(StatusCode::ERR_EXCEPTION, '添加相册错误');
return $this->successByMessage('添加相册成功');
}
/**
* 获取单个字典数据信息
* @param int $id
* @RequestMapping(path="edit/{id}", methods="get")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* })
* @return \Psr\Http\Message\ResponseInterface
*/
public function edit(int $id)
{
$dictDataInfo = PhotoAlbum::findById($id);
if (empty($dictDataInfo)) $this->throwExp(StatusCode::ERR_USER_ABSENT, '获取相册信息失败');
return $this->success([
'list' => $dictDataInfo
]);
}
/**
* @Explanation(content="修改相册信息")
* @param int $id
* @RequestMapping(path="update/{id}", methods="put")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* @Middleware(PermissionMiddleware::class)
* })
* @return \Psr\Http\Message\ResponseInterface
*/
public function update(int $id)
{
if (empty($id)) $this->throwExp(StatusCode::ERR_VALIDATION, 'ID 不能为空');
$postData = $this->request->all();
$params = [
'id' => $id,
'album_name' => $postData['album_name'] ?? '',
'album_desc' => $postData['album_desc'] ?? '',
'album_type' => $postData['album_type'] ?? 1,
'album_status' => $postData['album_status'] ?? 1,
'album_author' => $postData['album_author'] ?? '',
'album_cover' => $postData['album_cover'] ?? '',
'album_question' => $postData['album_question'] ?? '',
'album_answer' => $postData['album_answer'] ?? '',
'album_sort' => $postData['album_sort'] ?? 99
];
//配置验证
$rules = [
'album_name' => 'required',
'id' => 'required|integer',
];
//错误信息
$message = [
'album_name.required' => '[album_name]缺失',
'id.required' => '[id]缺失',
'id.integer' => '[id] 必须为整型',
];
$this->verifyParams($params, $rules, $message);
$photoAlbumObj = PhotoAlbum::findById($id);
$photoAlbumObj->album_name = $params['album_name'];
$photoAlbumObj->album_desc = $params['album_desc'];
$photoAlbumObj->album_type = $params['album_type'];
$photoAlbumObj->album_status = $params['album_status'];
$photoAlbumObj->album_author = $params['album_author'];
$photoAlbumObj->album_cover = $params['album_cover'];
$photoAlbumObj->album_question = $params['album_question'];
$photoAlbumObj->album_answer = $params['album_answer'];
$photoAlbumObj->album_sort = $params['album_sort'];
if (!$photoAlbumObj->save()) $this->throwExp(StatusCode::ERR_EXCEPTION, '修改相册信息错误');
return $this->successByMessage('修改相册信息成功');
}
/**
* @Explanation(content="删除相册信息")
* @param int $id
* @RequestMapping(path="destroy/{id}", methods="delete")
* @Middlewares({
* @Middleware(RequestMiddleware::class),
* @Middleware(PermissionMiddleware::class)
* })
* @return \Psr\Http\Message\ResponseInterface
*/
public function destroy(int $id)
{
if ($id == 0) {
$idArr = $this->request->input('id') ?? [];
if (empty($idArr) || !is_array($idArr)) $this->throwExp(StatusCode::ERR_VALIDATION, '参数类型不正确');
if (!PhotoAlbum::whereIn('id', $idArr)->delete()) $this->throwExp(StatusCode::ERR_EXCEPTION, '删除失败');
}else {
if (!intval($id)) $this->throwExp(StatusCode::ERR_VALIDATION, '参数错误');
if (!PhotoAlbum::destroy($id)) $this->throwExp(StatusCode::ERR_EXCEPTION, '删除失败');
}
return $this->successByMessage('删除相册成功');
}
}