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

77 lines
2.0 KiB

4 years ago
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
4 years ago
use App\Libs\MyLib;
4 years ago
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();
4 years ago
env('DB_DATABASE', 'forge');
4 years ago
$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);
}
4 years ago
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;
}
}
4 years ago
}