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