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\TagRequest; |
10
|
|
|
use App\Repository\Admin\TagRepository; |
11
|
|
|
use Illuminate\Database\QueryException; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Illuminate\View\View; |
15
|
|
|
use App\Model\Admin\ContentTag; |
16
|
|
|
use Illuminate\Support\Facades\DB; |
17
|
|
|
|
18
|
|
|
class TagController extends Controller |
19
|
|
|
{ |
20
|
|
|
protected $formNames = ['name', 'created_at']; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
|
26
|
|
|
$this->breadcrumb[] = ['title' => '标签列表', 'url' => route('admin::tag.index')]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 标签管理-标签列表 |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function index() |
34
|
|
|
{ |
35
|
|
|
$this->breadcrumb[] = ['title' => '标签列表', 'url' => '']; |
36
|
|
|
return view('admin.tag.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 = TagRepository::list($perPage, $condition); |
51
|
|
|
|
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 标签管理-新增标签 |
57
|
|
|
* |
58
|
|
|
*/ |
59
|
|
|
public function create() |
60
|
|
|
{ |
61
|
|
|
$this->breadcrumb[] = ['title' => '新增标签', 'url' => '']; |
62
|
|
|
return view('admin.tag.add', ['breadcrumb' => $this->breadcrumb]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 标签管理-保存标签 |
67
|
|
|
* |
68
|
|
|
* @param TagRequest $request |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function save(TagRequest $request) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
TagRepository::add($request->only($this->formNames)); |
75
|
|
|
return [ |
76
|
|
|
'code' => 0, |
77
|
|
|
'msg' => '新增成功', |
78
|
|
|
'redirect' => true |
79
|
|
|
]; |
80
|
|
|
} catch (QueryException $e) { |
81
|
|
|
return [ |
82
|
|
|
'code' => 1, |
83
|
|
|
'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前标签已存在' : '其它错误'), |
84
|
|
|
'redirect' => false |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 标签管理-编辑标签 |
91
|
|
|
* |
92
|
|
|
* @param int $id |
93
|
|
|
* @return View |
94
|
|
|
*/ |
95
|
|
|
public function edit($id) |
96
|
|
|
{ |
97
|
|
|
$this->breadcrumb[] = ['title' => '编辑标签', 'url' => '']; |
98
|
|
|
|
99
|
|
|
$model = TagRepository::find($id); |
100
|
|
|
return view('admin.tag.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* 标签管理-更新标签 |
105
|
|
|
* |
106
|
|
|
* @param TagRequest $request |
107
|
|
|
* @param int $id |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function update(TagRequest $request, $id) |
111
|
|
|
{ |
112
|
|
|
$data = $request->only($this->formNames); |
113
|
|
|
try { |
114
|
|
|
TagRepository::update($id, $data); |
115
|
|
|
return [ |
116
|
|
|
'code' => 0, |
117
|
|
|
'msg' => '编辑成功', |
118
|
|
|
'redirect' => true |
119
|
|
|
]; |
120
|
|
|
} catch (QueryException $e) { |
121
|
|
|
return [ |
122
|
|
|
'code' => 1, |
123
|
|
|
'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前标签已存在' : '其它错误'), |
124
|
|
|
'redirect' => false |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 标签管理-删除标签 |
131
|
|
|
* |
132
|
|
|
* @param int $id |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function delete($id) |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
DB::beginTransaction(); |
139
|
|
|
|
140
|
|
|
TagRepository::delete($id); |
141
|
|
|
ContentTag::where('tag_id', $id)->delete(); |
142
|
|
|
|
143
|
|
|
DB::commit(); |
144
|
|
|
return [ |
145
|
|
|
'code' => 0, |
146
|
|
|
'msg' => '删除成功', |
147
|
|
|
'redirect' => route('admin::tag.index') |
148
|
|
|
]; |
149
|
|
|
} catch (\RuntimeException $e) { |
150
|
|
|
DB::rollBack(); |
151
|
|
|
return [ |
152
|
|
|
'code' => 1, |
153
|
|
|
'msg' => '删除失败:' . $e->getMessage(), |
154
|
|
|
'redirect' => false |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|