|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Libs\MyLib;
|
|
|
|
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();
|
|
|
|
env('DB_DATABASE', 'forge');
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function calc($operator, $value1, $value2)
|
|
|
|
{
|
|
|
|
switch ($operator){
|
|
|
|
case 'eq':
|
|
|
|
return $value1 == $value2;
|
|
|
|
case 'neq':
|
|
|
|
return $value1 != $value2;
|
|
|
|
case 'gt':
|
|
|
|
return $value1 > $value2;
|
|
|
|
case 'egt':
|
|
|
|
return $value1 >= $value2;
|
|
|
|
case 'lt':
|
|
|
|
return $value1 < $value2;
|
|
|
|
case 'elt':
|
|
|
|
return $value1 <= $value2;
|
|
|
|
case 'heq':
|
|
|
|
return $value1 === $value2;
|
|
|
|
case 'nheq':
|
|
|
|
return $value1 !== $value2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|