|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Front; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repository\Admin\ContentRepository; |
|
6
|
|
|
use App\Repository\Admin\EntityRepository; |
|
7
|
|
|
use App\Repository\Front\CommentRepository; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Facades\Auth; |
|
10
|
|
|
use Illuminate\Support\Facades\Cache; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Illuminate\Support\Facades\Log; |
|
13
|
|
|
|
|
14
|
|
|
class CommentController extends BaseController |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* 发布一条评论 |
|
18
|
|
|
* |
|
19
|
|
|
* @param Request $request |
|
20
|
|
|
* @param int $entityId 模型ID |
|
21
|
|
|
* @param int $contentId 内容ID |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
public function save(Request $request, $entityId, $contentId) |
|
25
|
|
|
{ |
|
26
|
|
|
$result = $this->checkParam($entityId, $contentId); |
|
27
|
|
|
if ($result !== true) { |
|
28
|
|
|
return $result; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$content = (string) $request->post('content', ''); |
|
32
|
|
|
// 暂不支持html |
|
33
|
|
|
$content = strip_tags($content); |
|
34
|
|
|
if ($content === '') { |
|
35
|
|
|
return [ |
|
36
|
|
|
'code' => 5, |
|
37
|
|
|
'msg' => '评论内容不能为空', |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
if (mb_strlen($content) > 1024) { |
|
41
|
|
|
return [ |
|
42
|
|
|
'code' => 6, |
|
43
|
|
|
'msg' => '评论内容过长', |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
$pid = (int) $request->post('pid', 0); |
|
47
|
|
|
if ($pid < 0) { |
|
48
|
|
|
return [ |
|
49
|
|
|
'code' => 7, |
|
50
|
|
|
'msg' => 'invalid pid', |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
if ($pid > 0 && !($parentComment = \App\Repository\Admin\CommentRepository::find($pid))) { |
|
54
|
|
|
return [ |
|
55
|
|
|
'code' => 8, |
|
56
|
|
|
'msg' => '引用评论不存在', |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$rid = $pid === 0 ? $pid : ($parentComment->rid === 0 ? $parentComment->id : $parentComment->rid); |
|
|
|
|
|
|
62
|
|
|
\App\Repository\Admin\CommentRepository::add([ |
|
63
|
|
|
'entity_id' => $entityId, |
|
64
|
|
|
'content_id' => $contentId, |
|
65
|
|
|
'pid' => $pid, |
|
66
|
|
|
'rid' => $rid, |
|
67
|
|
|
'content' => $content, |
|
68
|
|
|
'user_id' => Auth::guard('member')->id(), |
|
69
|
|
|
]); |
|
70
|
|
|
if ($rid > 0) { |
|
71
|
|
|
// 清除缓存 |
|
72
|
|
|
Cache::forget('comment_replay:' . $rid); |
|
73
|
|
|
|
|
74
|
|
|
// 回复数+1 |
|
75
|
|
|
CommentRepository::addReplyCount($rid); |
|
76
|
|
|
} |
|
77
|
|
|
return [ |
|
78
|
|
|
'code' => 0, |
|
79
|
|
|
'msg' => '', |
|
80
|
|
|
'reload' => true, |
|
81
|
|
|
]; |
|
82
|
|
|
} catch (Throwable $e) { |
|
83
|
|
|
Log::error($e); |
|
84
|
|
|
return [ |
|
85
|
|
|
'code' => 500, |
|
86
|
|
|
'msg' => '评论失败:内部错误', |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 获取评论 |
|
93
|
|
|
* |
|
94
|
|
|
* @param Request $request |
|
95
|
|
|
* @param int $entityId 模型ID |
|
96
|
|
|
* @param int $contentId 内容ID |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function list(Request $request, $entityId, $contentId) |
|
100
|
|
|
{ |
|
101
|
|
|
$result = $this->checkParam($entityId, $contentId); |
|
102
|
|
|
if ($result !== true) { |
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$limit = (int) $request->get('limit', 10); |
|
107
|
|
|
$limit = ($limit > 0 && $limit <= 20) ? $limit : 10; |
|
108
|
|
|
$rid = (int) $request->get('rid', 0); |
|
109
|
|
|
|
|
110
|
|
|
$condition = [ |
|
111
|
|
|
'content_id' => ['=', $contentId], |
|
112
|
|
|
'entity_id' => ['=', $entityId], |
|
113
|
|
|
'rid' => ['=', $rid], |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
$data = CommentRepository::list($limit, $condition); |
|
117
|
|
|
|
|
118
|
|
|
return [ |
|
119
|
|
|
'code' => 0, |
|
120
|
|
|
'msg' => '', |
|
121
|
|
|
'data' => $data |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* 对评论进行操作。喜欢、不喜欢、中性(取消喜欢、取消不喜欢) |
|
127
|
|
|
* |
|
128
|
|
|
* @param int $id |
|
129
|
|
|
* @param string $action |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function operate($id, $action) |
|
133
|
|
|
{ |
|
134
|
|
|
$result = CommentRepository::$action($id, Auth::guard('member')->id()); |
|
135
|
|
|
|
|
136
|
|
|
return [ |
|
137
|
|
|
'code' => 0, |
|
138
|
|
|
'msg' => '', |
|
139
|
|
|
'data' => $result |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function checkParam($entityId, $contentId) |
|
144
|
|
|
{ |
|
145
|
|
|
$entity = EntityRepository::find($entityId); |
|
146
|
|
|
if (!$entity) { |
|
147
|
|
|
return [ |
|
148
|
|
|
'code' => 1, |
|
149
|
|
|
'msg' => '模型不存在', |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
ContentRepository::setTable($entity->table_name); |
|
154
|
|
|
if (!ContentRepository::find($contentId)) { |
|
155
|
|
|
return [ |
|
156
|
|
|
'code' => 2, |
|
157
|
|
|
'msg' => '内容不存在', |
|
158
|
|
|
]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|