|
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 Illuminate\Database\QueryException; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
use Illuminate\Support\Str; |
|
15
|
|
|
use Illuminate\View\View; |
|
16
|
|
|
use Illuminate\Support\Facades\Log; |
|
17
|
|
|
use App\Exceptions\CreateTableException; |
|
18
|
|
|
|
|
19
|
|
|
class EntityController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
protected $formNames = [ |
|
22
|
|
|
'name', 'table_name', 'description', 'is_internal', 'enable_comment', 'is_show_content_manage' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct(); |
|
28
|
|
|
|
|
29
|
|
|
$this->breadcrumb[] = ['title' => '模型列表', 'url' => route('admin::entity.index')]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* 模型管理-模型列表 |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
|
|
public function index() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->breadcrumb[] = ['title' => '模型列表', 'url' => '']; |
|
39
|
|
|
return view('admin.entity.index', ['breadcrumb' => $this->breadcrumb]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 模型列表数据接口 |
|
44
|
|
|
* |
|
45
|
|
|
* @param Request $request |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function list(Request $request) |
|
49
|
|
|
{ |
|
50
|
|
|
$perPage = (int) $request->get('limit', 50); |
|
51
|
|
|
$condition = $request->only($this->formNames); |
|
52
|
|
|
|
|
53
|
|
|
$data = EntityRepository::list($perPage, $condition); |
|
54
|
|
|
|
|
55
|
|
|
return $data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* 模型管理-新增模型 |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
|
|
public function create() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->breadcrumb[] = ['title' => '新增模型', 'url' => '']; |
|
65
|
|
|
return view('admin.entity.add', ['breadcrumb' => $this->breadcrumb]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* 模型管理-保存模型 |
|
70
|
|
|
* |
|
71
|
|
|
* @param EntityRequest $request |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function save(EntityRequest $request) |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
$createDB = $request->post('is_modify_db'); |
|
78
|
|
|
EntityRepository::add($request->only($this->formNames), $createDB); |
|
79
|
|
|
return [ |
|
80
|
|
|
'code' => 0, |
|
81
|
|
|
'msg' => '新增成功', |
|
82
|
|
|
'redirect' => true |
|
83
|
|
|
]; |
|
84
|
|
|
} catch (CreateTableException $e) { |
|
85
|
|
|
return [ |
|
86
|
|
|
'code' => 2, |
|
87
|
|
|
'msg' => '新增失败:创建数据库表失败,数据表已存在或其它原因', |
|
88
|
|
|
'redirect' => false |
|
89
|
|
|
]; |
|
90
|
|
|
} catch (QueryException $e) { |
|
91
|
|
|
return [ |
|
92
|
|
|
'code' => 1, |
|
93
|
|
|
'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'), |
|
94
|
|
|
'redirect' => false |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* 模型管理-编辑模型 |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $id |
|
103
|
|
|
* @return View |
|
104
|
|
|
*/ |
|
105
|
|
|
public function edit($id) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->breadcrumb[] = ['title' => '编辑模型', 'url' => '']; |
|
108
|
|
|
|
|
109
|
|
|
$model = EntityRepository::find($id); |
|
110
|
|
|
return view('admin.entity.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* 模型管理-更新模型 |
|
115
|
|
|
* |
|
116
|
|
|
* @param EntityRequest $request |
|
117
|
|
|
* @param int $id |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function update(EntityRequest $request, $id) |
|
121
|
|
|
{ |
|
122
|
|
|
$data = $request->only($this->formNames); |
|
123
|
|
|
unset($data['table_name']); |
|
124
|
|
|
try { |
|
125
|
|
|
EntityRepository::update($id, $data); |
|
126
|
|
|
return [ |
|
127
|
|
|
'code' => 0, |
|
128
|
|
|
'msg' => '编辑成功', |
|
129
|
|
|
'redirect' => true |
|
130
|
|
|
]; |
|
131
|
|
|
} catch (QueryException $e) { |
|
132
|
|
|
Log::error($e); |
|
133
|
|
|
return [ |
|
134
|
|
|
'code' => 1, |
|
135
|
|
|
'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'), |
|
136
|
|
|
'redirect' => false |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* 模型管理-删除模型 |
|
143
|
|
|
* |
|
144
|
|
|
* @param Request $request |
|
145
|
|
|
* @param integer $id |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
public function delete(Request $request, $id) |
|
149
|
|
|
{ |
|
150
|
|
|
$password = $request->post('password'); |
|
151
|
|
|
if (!$password) { |
|
152
|
|
|
return [ |
|
153
|
|
|
'code' => 1, |
|
154
|
|
|
'msg' => '密码不能为空', |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
if (!Auth::guard('admin')->attempt(['id' => $request->user()->id, 'password' => $password])) { |
|
158
|
|
|
return [ |
|
159
|
|
|
'code' => 2, |
|
160
|
|
|
'msg' => '密码错误', |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
EntityRepository::delete($id); |
|
164
|
|
|
return [ |
|
165
|
|
|
'code' => 0, |
|
166
|
|
|
'msg' => '删除成功', |
|
167
|
|
|
'reload' => true |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|