Skip to content

Commit 206af1a

Browse files
committed
update: config 操作
1 parent 748613a commit 206af1a

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Models\Config;
6+
use App\Http\Controllers\Controller;
7+
use Illuminate\Http\Request;
8+
9+
class ConfigController extends Controller
10+
{
11+
/**
12+
* @var int 默认分页条数
13+
*/
14+
public $perPage = 10;
15+
16+
/**
17+
* Create a new AuthController instance.
18+
* 要求附带email和password(数据来源users表)
19+
*
20+
* @return void
21+
*/
22+
public function __construct(Request $request)
23+
{
24+
// 这里额外注意了:官方文档样例中只除外了『login』
25+
// 这样的结果是,token 只能在有效期以内进行刷新,过期无法刷新
26+
// 如果把 refresh 也放进去,token 即使过期但仍在刷新期以内也可刷新
27+
// 不过刷新一次作废
28+
$this->middleware(['auth:api', 'role']);
29+
// 另外关于上面的中间件,官方文档写的是『auth:api』
30+
// 但是我推荐用 『jwt.auth』,效果是一样的,但是有更加丰富的报错信息返回
31+
32+
$perPage = intval($request->input('perPage'));
33+
$this->perPage = $perPage ?? 11;
34+
}
35+
36+
/**
37+
* Display a listing of the resource.
38+
*
39+
* @return \Illuminate\Http\Response
40+
*/
41+
public function index()
42+
{
43+
$list = Config::orderBy('sort')->paginate($this->perPage);
44+
return $this->out(200, $list);
45+
}
46+
47+
/**
48+
* Show the form for creating a new resource.
49+
*
50+
* @return \Illuminate\Http\Response
51+
*/
52+
public function create()
53+
{
54+
return $this->out(200, ['method' => 'create']);
55+
}
56+
57+
/**
58+
* Store a newly created resource in storage.
59+
* 新增入库操作
60+
*
61+
* @param Request $request
62+
* @return \Illuminate\Http\Response
63+
*/
64+
public function store(Request $request)
65+
{
66+
// 会出现 Unknown column 'guid' in 'field list' 不存在的字段入库报错问题
67+
// $rs = Config::insert($request->all());
68+
$input = $request->all();
69+
is_null($input['sort']) && $input['sort'] = 100;
70+
$model = new Config($input);
71+
if ($model->save()) {
72+
return $this->out(200, ['data' => ['id' => $model->id]]);
73+
} else {
74+
return $this->out(4000);
75+
}
76+
77+
}
78+
79+
/**
80+
* Display the specified resource.
81+
* 展示某个详情数据
82+
*
83+
* @param Config $Config
84+
*
85+
* @return \Illuminate\Http\Response
86+
*/
87+
public function show(Config $Config)
88+
{
89+
return $this->out(200, $Config);
90+
}
91+
92+
/**
93+
* Show the form for editing the specified resource.
94+
* 编辑展示数据
95+
*
96+
* @param int $id
97+
*
98+
* @return \Illuminate\Http\Response
99+
*/
100+
public function edit($id)
101+
{
102+
$data = Config::findOrFail($id);
103+
return $this->out(200, $data);
104+
}
105+
106+
/**
107+
* Update the specified resource in storage.
108+
* 更新数据
109+
*
110+
* @param Request $request
111+
* @param int $id
112+
* @return \Illuminate\Http\Response
113+
*/
114+
public function update(Request $request, $id)
115+
{
116+
$input = $request->all();
117+
// $model = new Config();$model->save($input, ['id' => $id]);
118+
// 老版本更新操作如下,新版本先查询再更新
119+
// Config::where('id', $id)->update($input)
120+
$Config = Config::findOrFail($id);
121+
if ($Config->update($input)) {
122+
return $this->out(200, ['data' => ['id' => $id]]);
123+
} else {
124+
return $this->out(4000);
125+
}
126+
}
127+
128+
/**
129+
* Remove the specified resource from storage.
130+
*
131+
* @param int $id
132+
* @return \Illuminate\Http\Response
133+
* @throws \Exception
134+
*/
135+
public function destroy($id)
136+
{
137+
if (Config::findOrFail($id)->delete()) {
138+
$data = ['msg' => '删除成功', 'errno' => 0];
139+
} else {
140+
$data = ['msg' => '删除失败', 'errno' => 2];
141+
}
142+
return $this->out(200, $data);
143+
}
144+
}

laravel/routes/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
Route::get('roles/store', 'RoleController@store');
5252
Route::resource('permissions', 'PermissionController');
5353
Route::resource('roles', 'RoleController');
54+
55+
// 配置相关
56+
Route::resource('config', 'Api\ConfigController');
5457
});
5558

5659

0 commit comments

Comments
 (0)