1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Eddy <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace App\Http\Controllers\Admin; |
7
|
|
|
|
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Http\Requests\Admin\CommentRequest; |
10
|
|
|
use App\Repository\Admin\CommentRepository; |
11
|
|
|
use Illuminate\Database\QueryException; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\View\View; |
15
|
|
|
use Illuminate\Support\Facades\DB; |
16
|
|
|
use Illuminate\Support\Facades\Cache; |
17
|
|
|
|
18
|
|
|
class CommentController extends Controller |
19
|
|
|
{ |
20
|
|
|
protected $formNames = ['content', 'content_id', 'entity_id', 'user_id', 'rid']; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
|
26
|
|
|
$this->breadcrumb[] = ['title' => '评论列表', 'url' => route('admin::comment.index')]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 评论管理-评论列表 |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function index() |
34
|
|
|
{ |
35
|
|
|
$this->breadcrumb[] = ['title' => '评论列表', 'url' => '']; |
36
|
|
|
return view('admin.comment.index', ['breadcrumb' => $this->breadcrumb]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 评论管理-评论列表数据接口 |
41
|
|
|
* |
42
|
|
|
* @param Request $request |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function list(Request $request) |
46
|
|
|
{ |
47
|
|
|
$perPage = (int) $request->get('limit', 50); |
48
|
|
|
$condition = $request->only($this->formNames); |
49
|
|
|
|
50
|
|
|
$data = CommentRepository::list($perPage, $condition); |
51
|
|
|
|
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 评论管理-编辑评论 |
57
|
|
|
* |
58
|
|
|
* @param int $id |
59
|
|
|
* @return View |
60
|
|
|
*/ |
61
|
|
|
public function edit($id) |
62
|
|
|
{ |
63
|
|
|
$this->breadcrumb[] = ['title' => '编辑评论', 'url' => '']; |
64
|
|
|
|
65
|
|
|
$model = CommentRepository::find($id); |
66
|
|
|
return view('admin.comment.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 评论管理-更新评论 |
71
|
|
|
* |
72
|
|
|
* @param CommentRequest $request |
73
|
|
|
* @param int $id |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function update(CommentRequest $request, $id) |
77
|
|
|
{ |
78
|
|
|
$data = $request->only($this->formNames); |
79
|
|
|
try { |
80
|
|
|
CommentRepository::update($id, $data); |
81
|
|
|
return [ |
82
|
|
|
'code' => 0, |
83
|
|
|
'msg' => '编辑成功', |
84
|
|
|
'redirect' => true |
85
|
|
|
]; |
86
|
|
|
} catch (QueryException $e) { |
87
|
|
|
return [ |
88
|
|
|
'code' => 1, |
89
|
|
|
'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前评论已存在' : '其它错误'), |
90
|
|
|
'redirect' => false |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* 评论管理-删除评论 |
97
|
|
|
* |
98
|
|
|
* @param int $id |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function delete($id) |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
|
|
$id = intval($id); |
105
|
|
|
|
106
|
|
|
$comment = CommentRepository::find($id); |
107
|
|
|
if (!$comment) { |
108
|
|
|
throw new \RuntimeException("评论不存在"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (CommentRepository::hasChildren($id)) { |
112
|
|
|
return [ |
113
|
|
|
'code' => 2, |
114
|
|
|
'msg' => '删除失败:只允许删除无回复的评论', |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
DB::transaction(function () use ($id, $comment) { |
119
|
|
|
CommentRepository::delete($id); |
120
|
|
|
// 回复数-1 |
121
|
|
|
CommentRepository::decrementReplyCount($comment->rid); |
122
|
|
|
// 清除缓存 |
123
|
|
|
Cache::forget('comment_replay:' . $comment->rid); |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
return [ |
127
|
|
|
'code' => 0, |
128
|
|
|
'msg' => '删除成功', |
129
|
|
|
'redirect' => route('admin::comment.index') |
130
|
|
|
]; |
131
|
|
|
} catch (\RuntimeException $e) { |
132
|
|
|
return [ |
133
|
|
|
'code' => 1, |
134
|
|
|
'msg' => '删除失败:' . $e->getMessage(), |
135
|
|
|
'redirect' => false |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|