You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
user_center/app/Http/Controllers/Api/BaseController.php

51 lines
1.4 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class BaseController extends Controller
{
//
protected $rules;
protected $params;
protected $request;
protected $model;
public function __construct(Request $request)
{
$this->request = $request;
$this->params = $request->input();
$this->initialize();
}
protected function initialize() {
$this->request->attributes->add([
'rules' => $this->rules[explode('@', $this->request->route()->getActionName())[1]] ?? [],
'attributes' => $this->model->attrs,
]);
$this->middleware('param_verify');
}
protected function success($data = [], $msg = '请求成功', $code = 200)
{
if($data instanceof ResourceCollection){
$response = ['code' => $code, 'msg' => $msg] + $data->toArray($this->request);
} else {
$response = ['code' => $code, 'msg' => $msg, 'data' => $data,];
}
return response()->json($response, $code);
}
protected function error($code = 400, $msg = '请求失败', $data = [])
{
return response()->json([
'code' => $code,
'msg' => $msg,
'data' => $data,
], $code);
}
}