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\EntityRequest; |
10
|
|
|
use App\Repository\Admin\EntityRepository; |
11
|
|
|
use App\Model\Admin\Entity; |
12
|
|
|
use Illuminate\Database\QueryException; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Illuminate\Support\Facades\Auth; |
15
|
|
|
use Illuminate\Support\Str; |
16
|
|
|
use Illuminate\View\View; |
17
|
|
|
use Illuminate\Support\Facades\Log; |
18
|
|
|
use App\Exceptions\CreateTableException; |
19
|
|
|
|
20
|
|
|
class EntityController extends Controller |
21
|
|
|
{ |
22
|
|
|
protected $formNames = [ |
23
|
|
|
'name', 'table_name', 'description', 'is_internal', 'enable_comment', 'is_show_content_manage' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
|
30
|
|
|
$this->breadcrumb[] = ['title' => '模型列表', 'url' => route('admin::entity.index')]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 模型管理-模型列表 |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
public function index() |
38
|
|
|
{ |
39
|
|
|
$this->breadcrumb[] = ['title' => '模型列表', 'url' => '']; |
40
|
|
|
return view('admin.entity.index', ['breadcrumb' => $this->breadcrumb]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 模型管理-模型列表数据接口 |
45
|
|
|
* |
46
|
|
|
* @param Request $request |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function list(Request $request) |
50
|
|
|
{ |
51
|
|
|
$perPage = (int) $request->get('limit', 50); |
52
|
|
|
$condition = $request->only($this->formNames); |
53
|
|
|
|
54
|
|
|
$data = EntityRepository::list($perPage, $condition); |
55
|
|
|
|
56
|
|
|
return $data; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 模型管理-新增模型 |
61
|
|
|
* |
62
|
|
|
*/ |
63
|
|
|
public function create() |
64
|
|
|
{ |
65
|
|
|
$this->breadcrumb[] = ['title' => '新增模型', 'url' => '']; |
66
|
|
|
return view('admin.entity.add', ['breadcrumb' => $this->breadcrumb]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 模型管理-保存模型 |
71
|
|
|
* |
72
|
|
|
* @param EntityRequest $request |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function save(EntityRequest $request) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$createDB = $request->post('is_modify_db'); |
79
|
|
|
EntityRepository::add($request->only($this->formNames), $createDB); |
80
|
|
|
return [ |
81
|
|
|
'code' => 0, |
82
|
|
|
'msg' => '新增成功', |
83
|
|
|
'redirect' => true |
84
|
|
|
]; |
85
|
|
|
} catch (CreateTableException $e) { |
86
|
|
|
return [ |
87
|
|
|
'code' => 2, |
88
|
|
|
'msg' => '新增失败:创建数据库表失败,数据表已存在或其它原因', |
89
|
|
|
'redirect' => false |
90
|
|
|
]; |
91
|
|
|
} catch (QueryException $e) { |
92
|
|
|
return [ |
93
|
|
|
'code' => 1, |
94
|
|
|
'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'), |
95
|
|
|
'redirect' => false |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* 模型管理-编辑模型 |
102
|
|
|
* |
103
|
|
|
* @param int $id |
104
|
|
|
* @return View |
105
|
|
|
*/ |
106
|
|
|
public function edit($id) |
107
|
|
|
{ |
108
|
|
|
$this->breadcrumb[] = ['title' => '编辑模型', 'url' => '']; |
109
|
|
|
|
110
|
|
|
$model = EntityRepository::find($id); |
111
|
|
|
return view('admin.entity.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 模型管理-更新模型 |
116
|
|
|
* |
117
|
|
|
* @param EntityRequest $request |
118
|
|
|
* @param int $id |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function update(EntityRequest $request, $id) |
122
|
|
|
{ |
123
|
|
|
$data = $request->only($this->formNames); |
124
|
|
|
unset($data['table_name']); |
125
|
|
|
try { |
126
|
|
|
EntityRepository::update($id, $data); |
127
|
|
|
return [ |
128
|
|
|
'code' => 0, |
129
|
|
|
'msg' => '编辑成功', |
130
|
|
|
'redirect' => true |
131
|
|
|
]; |
132
|
|
|
} catch (QueryException $e) { |
133
|
|
|
Log::error($e); |
134
|
|
|
return [ |
135
|
|
|
'code' => 1, |
136
|
|
|
'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'), |
137
|
|
|
'redirect' => false |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 模型管理-删除模型 |
144
|
|
|
* |
145
|
|
|
* @param Request $request |
146
|
|
|
* @param integer $id |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function delete(Request $request, $id) |
150
|
|
|
{ |
151
|
|
|
$password = $request->post('password'); |
152
|
|
|
if (!$password) { |
153
|
|
|
return [ |
154
|
|
|
'code' => 1, |
155
|
|
|
'msg' => '密码不能为空', |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
if (!Auth::guard('admin')->attempt(['id' => $request->user()->id, 'password' => $password])) { |
159
|
|
|
return [ |
160
|
|
|
'code' => 2, |
161
|
|
|
'msg' => '密码错误', |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
EntityRepository::delete($id); |
165
|
|
|
return [ |
166
|
|
|
'code' => 0, |
167
|
|
|
'msg' => '删除成功', |
168
|
|
|
'reload' => true |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* 模型管理-复制模型 |
174
|
|
|
* |
175
|
|
|
* @param Request $request |
176
|
|
|
* @param integer $id |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function copy(Request $request, $id) |
180
|
|
|
{ |
181
|
|
|
$this->validate($request, [ |
182
|
|
|
'table_name' => ['required', 'max:64', 'regex:/^[0-9a-zA-Z$_]+$/'], |
183
|
|
|
], [ |
184
|
|
|
'table_name.required' => '表名称不能为空', |
185
|
|
|
'table_name.max' => '表名称长度不能超过64', |
186
|
|
|
'table_name.regex' => '表名称格式有误', |
187
|
|
|
]); |
188
|
|
|
|
189
|
|
|
try { |
190
|
|
|
$tableName = $request->post('table_name'); |
191
|
|
|
EntityRepository::copy($tableName, $id); |
192
|
|
|
return [ |
193
|
|
|
'code' => 0, |
194
|
|
|
'msg' => '复制成功', |
195
|
|
|
'reload' => true |
196
|
|
|
]; |
197
|
|
|
} catch (\RuntimeException $e) { |
198
|
|
|
return [ |
199
|
|
|
'code' => 5, |
200
|
|
|
'msg' => $e->getMessage(), |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* 模型管理-添加默认菜单 |
207
|
|
|
* |
208
|
|
|
* @param integer $id |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function menu($id) |
212
|
|
|
{ |
213
|
|
|
try { |
214
|
|
|
$entity = Entity::findOrFail($id); |
215
|
|
|
EntityRepository::addDefaultMenus($entity); |
216
|
|
|
return [ |
217
|
|
|
'code' => 0, |
218
|
|
|
'msg' => '添加成功', |
219
|
|
|
]; |
220
|
|
|
} catch (\RuntimeException $e) { |
221
|
|
|
return [ |
222
|
|
|
'code' => 5, |
223
|
|
|
'msg' => $e->getMessage(), |
224
|
|
|
]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|