-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
User.php
265 lines (249 loc) · 8.78 KB
/
User.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
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
<?php
/**
* User: Wisp X
* Date: 2018/9/26
* Time: 19:37
* Link: https://github.com/wisp-x
*/
namespace app\index\controller;
use app\common\model\Folders;
use app\common\model\Images;
use think\Db;
use think\facade\Config;
use think\facade\Session;
use think\Exception;
use think\Validate;
class User extends Base
{
public function images($keyword = '', $folderId = 0, $limit = 60)
{
$images = $folders = [];
if ($this->request->isPost()) {
try {
$model = $this->user->images()->order('create_time', 'desc');
$folders = $this->user->folders()->where('parent_id', $folderId)->select();
if (!empty($keyword)) {
$model = $model->where('pathname|alias_name|sha1|md5|ip', 'like', "%{$keyword}%");
}
if (is_numeric($folderId)) {
$model = $model->where('folder_id', $folderId);
}
$images = $model->paginate($limit);
} catch (Exception $e) {
$this->error($e->getMessage());
}
$this->success('success', null, [
'images' => $images,
'folders'=> $folders
]);
}
return $this->fetch();
}
public function deleteImages($deleteId = null)
{
Db::startTrans();
try {
$id = $deleteId ? $deleteId : $this->request->post('id');
$deletes = []; // 需要删除的文件
$images = $this->user->images()->where('id', 'in', $id)->select();
foreach ($images as &$value) {
// 查找是否有相同 md5 的文件记录,有的话则只删除记录不删除文件
if (!$this->exists($value)) {
$deletes[$value->strategy][] = $value->pathname;
}
$value->delete();
unset($value);
}
// 是否开启软删除(开启了只删除记录,不删除文件)
if (!$this->getConfig('soft_delete')) {
$strategy = [];
// 实例化所有储存策略驱动
$strategyAll = array_keys(Config::pull('strategy'));
foreach ($strategyAll as $value) {
// 获取储存策略驱动
$strategy[$value] = $this->getStrategyInstance($value);
}
foreach ($deletes as $key => $val) {
if (1 === count($val)) {
if (!$strategy[$key]->delete(isset($val[0]) ? $val[0] : null)) {
// throw new Exception('删除失败');
}
} else {
if (!$strategy[$key]->deletes($val)) {
// throw new Exception('批量删除失败');
}
}
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
if ($deleteId) {
return false;
}
$this->error($e->getMessage());
}
if ($deleteId) {
return true;
}
$this->success('删除成功');
}
public function createFolder()
{
if ($this->request->isPost()) {
try {
$parentId = $this->request->post('parent_id');
$name = $this->request->post('name');
$data = [
'user_id' => $this->user->id,
'parent_id' => $parentId,
'name' => $name
];
$validate = $this->validate($data, 'Folders');
if (true !== $validate) {
throw new Exception($validate);
}
Folders::create($data);
} catch (Exception $e) {
$this->error($e->getMessage());
}
$this->success('创建成功');
}
}
public function deleteFolder()
{
if ($this->request->isPost()) {
Db::startTrans();
try {
$id = $this->request->post('id');
$folders = $images = [];
$this->getDeleteFoldersAndImages($id, $folders, $images);
$folders[] = (int) $id;
$folders && Folders::destroy($folders, true);
$images && $this->deleteImages($images);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('删除成功');
}
}
public function getFolders($parentId = 0)
{
if ($this->request->isPost()) {
$folders = $this->user->folders()->where('parent_id', $parentId)->select();
$this->success('success', null, $folders);
}
}
public function moveImages($ids, $folderId)
{
if ($this->request->isPost()) {
$count = $this->user->folders()->where('id', $folderId)->count();
if ($count || $folderId == 0) {
if (Images::where('id', 'in', $ids)->setField('folder_id', $folderId)) {
$this->success('移动成功');
}
$this->error('移动失败');
} else {
$this->error('该文件夹不存在!');
}
}
}
public function renameFolder()
{
if ($this->request->isPost()) {
Db::startTrans();
try {
$id = $this->request->post('id');
$parentId = $this->request->post('parent_id');
$name = $this->request->post('name');
$data = [
'id' => $id,
'parent_id' => $parentId,
'user_id' => $this->user->id,
'name' => $name
];
$validate = $this->validate($data, 'Folders');
if (true !== $validate) {
throw new Exception($validate);
}
Folders::update($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('重命名成功');
}
}
public function renameImage()
{
if ($this->request->isPost()) {
try {
$id = $this->request->post('id');
$name = $this->request->post('name');
$validate = Validate::make(['name|别名' => 'require|max:60|chsDash']);
if (!$validate->check(['name' => $name])) {
throw new \Exception($validate->getError());
}
if (!$this->user->images()->where('id', $id)->update(['alias_name' => $name])) {
throw new \Exception('重命名失败');
}
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('重命名成功');
}
}
/**
* 检测除本身图片以外的记录是否存在
*
* @param Images $image
* @return float|string
*/
private function exists(Images $image)
{
return Images::where('id', 'neq', $image->id)->where('md5', $image->md5)->count();
}
private function getDeleteFoldersAndImages($folderId, &$folders, &$images)
{
$folderList = $this->user->folders()->where('parent_id', $folderId)->column('id');
$imagesList = $this->user->images()->where('folder_id', $folderId)->column('id');
if ($imagesList) {
$images = array_merge($images, $imagesList);
}
foreach ($folderList as &$value) {
$folders[] = $value;
$this->getDeleteFoldersAndImages($value, $folders, $images);
}
}
public function settings()
{
if ($this->request->isPost()) {
try {
$data = $this->request->post();
$validate = $this->validate($data, 'Users.edit');
if (true !== $validate) {
throw new Exception($validate);
}
if ($data['password_old']) {
if (md5($data['password_old']) != $this->user->password) {
throw new Exception('原密码不正确');
}
}
if (!$data['password']) unset($data['password']);
$this->user->save($data);
} catch (Exception $e) {
$this->error($e->getMessage());
}
$this->success('保存成功');
}
return $this->fetch();
}
public function logout()
{
Session::delete('uid');
$this->redirect(url('/'));
}
}