修改控制器文件

路径:app/Http/Controllers/Controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Response;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $statusCode=200;

/**
* @return int
*/
public function getStatusCode(){
return $this -> statusCode;
}

/**
* @param $statusCode
* @return mixed
*/
public function setStatusCode($statusCode){
$this -> statusCode = $statusCode;
return $this;
}

/**
* @param array $data
* @return mixed
*/
public function success($data = []){
return $this -> setStatusCode(200) -> responseSuccess($data);
}

/**
* @param string $msg
* @return mixed
*/
public function successMsg($msg = 'success'){
return $this -> setStatusCode(200) -> responseSuccessMsg($msg);
}

/**
* @param string $msg
* @param string $msg_type msg提示||alter警告||notify通知||box弹窗提示
* @param string $level info普通||success成功||warning警告||error错误
* @return mixed
*/
public function warning($msg = 'warning',$msg_type = 'message',$level = 'info'){
return $this -> setStatusCode(205) -> responseWarning($msg,$msg_type,$level);
}

/**
* @param string $msg
* @return mixed
*/
public function failed($msg = 'failed'){
return $this -> setStatusCode(400) -> responseError($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function notFound($msg = 'not found'){
return $this -> setStatusCode(404) -> responseError($msg);
}

/**
* @param $data
* @return mixed
*/
public function created($data){
return $this -> setStatusCode(201) -> responseSuccess($data);
}

public function exists($msg = 'data already exists'){
return $this -> setStatusCode(202) -> responseWarning($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function badRequest($msg = 'Request Parameter Error!'){
return $this -> setStatusCode(422) -> responseError($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function unauthorized($msg = 'Unauthorized requests'){
return $this -> setStatusCode(401) -> responseError($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function forbidden($msg = 'Insufficient authority'){
return $this -> setStatusCode(403) -> responseError($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function methodNotAllowed($msg = 'This request is disabled'){
return $this -> setStatusCode(405) -> responseError($msg);
}

/**
* @param string $msg
* @return mixed
*/
public function serverError($msg = 'server error!'){
return $this -> setStatusCode(500) -> responseError($msg);
}

/**
* @param $msg
* @return JsonResponse
*/
protected function responseError($msg)
{
return $this -> response([
'result' => 'FAILED',
'status_code' => $this -> getStatusCode(),
'msg' => $msg
])->setStatusCode($this -> getStatusCode());
}

/**
* @param array $data
* @return JsonResponse
*/
protected function responseSuccess($data = []){
return $this -> response([
'result' => 'SUCCESS',
'status_code' => $this -> getStatusCode(),
'data' => $data
])->setStatusCode($this -> getStatusCode());
}

/**
* @param $msg
* @param $msg_type
* @param $level
* @return JsonResponse
*/
public function responseWarning($msg,$msg_type,$level){
return $this -> response([
'result' => 'WARNING',
'status_code' => $this -> getStatusCode(),
'msg' => $msg,
'msg_type' => $msg_type,
'level' => $level
])->setStatusCode($this -> getStatusCode());
}

/**
* @param $msg
* @return JsonResponse
*/
public function responseSuccessMsg($msg){
return $this -> response([
'result' => 'SUCCESS',
'status_code' => $this -> getStatusCode(),
'msg' => $msg
])->setStatusCode($this -> getStatusCode());
}



/**
* @param $data
* @return JsonResponse
*/
public function response($data){
return Response::json($data,$this -> getStatusCode());
}

//手动分页
function paginate($data,$perPage,$current_page){
$allItem = $perPage * 100;
global $total;
if($total > $allItem){
$total = $allItem;
}else{
$total = count($data);
}
$item =array_slice($data,($current_page-1)*$perPage,$perPage);
return new LengthAwarePaginator($item,$total,$perPage,$current_page,[
'path'=> \Illuminate\Pagination\Paginator::resolveCurrentPath(),
'pageName'=>'page'
]);
}
}

使用方法示例

此处提供一个用户组的增删改查示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace App\Http\Controllers\Admin\Base;

use App\Http\Controllers\Controller;
use App\Models\Base\Group;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;

class GroupController extends Controller
{
public function index(){
$data = Group::query()->where('pid',null)->with('son')->get(['id','pid','name','admin_name','phone','remark']);
foreach ($data as $key => $value){
$data[$key]['son'] = $value->son;
}
return $this->success($data);
}

public function store(Request $request){
$param = $request->all();
$create = Group::query()->create($param);
return $create ? $this->successMsg() : $this->failed();
}

public function update(Request $request,$id){
$param = $request->all();
$group = Group::query()->find($id);
$group->fill($param);
return $group->save() ? $this->successMsg() : $this->failed();
}

public function show($id){
$group = Group::query()->find($id);
return $group ? $this->success($group) : $this->failed();
}

public function destroy($id){
return Group::destroy($id) ? $this->successMsg() : $this->failed();
}
}