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