Completed
Push — master ( 0487e7...b43f52 )
by Jianhua
03:47
created

CommentController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 23 4
C save() 0 64 12
A checkParam() 0 19 3
A operate() 0 8 1
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parentComment does not seem to be defined for all execution paths leading up to this point.
Loading history...
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
                'reply_user_id' => $pid > 0 ? $parentComment->user_id : 0,
70
            ]);
71
            if ($rid > 0) {
72
                // 清除缓存
73
                Cache::forget('comment_replay:' . $rid);
74
75
                // 回复数+1
76
                CommentRepository::addReplyCount($rid);
77
            }
78
            return [
79
                'code' => 0,
80
                'msg' => '',
81
                'reload' => true,
82
            ];
83
        } catch (Throwable $e) {
84
            Log::error($e);
85
            return [
86
                'code' => 500,
87
                'msg' => '评论失败:内部错误',
88
            ];
89
        }
90
    }
91
92
    /**
93
     * 获取评论
94
     *
95
     * @param Request $request
96
     * @param int $entityId 模型ID
97
     * @param int $contentId 内容ID
98
     * @return array
99
     */
100
    public function list(Request $request, $entityId, $contentId)
101
    {
102
        $result = $this->checkParam($entityId, $contentId);
103
        if ($result !== true) {
104
            return $result;
105
        }
106
107
        $limit = (int) $request->get('limit', 10);
108
        $limit = ($limit > 0 && $limit <= 20) ? $limit : 10;
109
        $rid = (int) $request->get('rid', 0);
110
111
        $condition = [
112
            'content_id' => ['=', $contentId],
113
            'entity_id' => ['=', $entityId],
114
            'rid' => ['=', $rid],
115
        ];
116
117
        $data = CommentRepository::list($limit, $condition);
118
119
        return [
120
            'code' => 0,
121
            'msg' => '',
122
            'data' => $data
123
        ];
124
    }
125
126
    /**
127
     * 对评论进行操作。喜欢、不喜欢、中性(取消喜欢、取消不喜欢)
128
     *
129
     * @param int $id
130
     * @param string $action
131
     * @return array
132
     */
133
    public function operate($id, $action)
134
    {
135
        $result = CommentRepository::$action($id, Auth::guard('member')->id());
136
137
        return [
138
            'code' => 0,
139
            'msg' => '',
140
            'data' => $result
141
        ];
142
    }
143
144
    protected function checkParam($entityId, $contentId)
145
    {
146
        $entity = EntityRepository::find($entityId);
147
        if (!$entity) {
148
            return [
149
                'code' => 1,
150
                'msg' => '模型不存在',
151
            ];
152
        }
153
154
        ContentRepository::setTable($entity->table_name);
155
        if (!ContentRepository::find($contentId)) {
156
            return [
157
                'code' => 2,
158
                'msg' => '内容不存在',
159
            ];
160
        }
161
162
        return true;
163
    }
164
}
165