|
...
|
...
|
@@ -5,10 +5,48 @@ namespace App\Http\Controllers\Aside; |
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Utils\EncryptUtils;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
class BaseController extends Controller
|
|
|
|
{
|
|
|
|
protected $param = [];//所有请求参数
|
|
|
|
protected $token = ''; //token
|
|
|
|
protected $request = [];//助手函数
|
|
|
|
protected $allCount = 0;//总条数
|
|
|
|
protected $p = 1;//当前页
|
|
|
|
protected $row = 20;//每页条数
|
|
|
|
protected $header = [];//设置请求头参数
|
|
|
|
protected $order = 'id';
|
|
|
|
protected $map = [];//处理后的参数
|
|
|
|
protected $uid = 0;
|
|
|
|
/**
|
|
|
|
* 获取所有参数
|
|
|
|
*/
|
|
|
|
public function __construct(Request $request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
$this->param = $this->request->all();
|
|
|
|
$this->token = $this->request->header('token');
|
|
|
|
$this->get_param();
|
|
|
|
$this->auth_token();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function auth_token(){
|
|
|
|
$info = Cache::get($this->token);
|
|
|
|
if(isset($info) && !empty($info)){
|
|
|
|
$this->uid = $info['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 成功返回
|
|
|
|
* @param array $data
|
|
...
|
...
|
@@ -36,6 +74,88 @@ class BaseController extends Controller |
|
|
|
$response = [
|
|
|
|
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
|
|
|
|
}
|
|
|
|
return response()->json($response);
|
|
|
|
return response()->json($response)->header($this->header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name 参数过滤
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function get_param(){
|
|
|
|
$param = $this->param;
|
|
|
|
foreach ($param as $k => $v){
|
|
|
|
if(is_array($v)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch ($k){
|
|
|
|
case "order":
|
|
|
|
$this->order = $v;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
$this->p = $v;
|
|
|
|
break;
|
|
|
|
case 'row':
|
|
|
|
$this->row = $v;
|
|
|
|
break;
|
|
|
|
case "create_at":
|
|
|
|
$this->_btw[0] = $v;
|
|
|
|
$this->_btw[1] = date('Y-m-d H:i:s',time());
|
|
|
|
$this->map['create_at'] = ['between', $this->_btw];
|
|
|
|
break;
|
|
|
|
case "update_at":
|
|
|
|
$this->_btw[1] = $v;
|
|
|
|
$this->map['update_at'] = ['between', $this->_btw];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!empty($v)) {
|
|
|
|
$this->map[$k] = $v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @name 统一返回参数
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function response($msg,$code = 200,$data = [],$result_code = null,$type = 'application/json'){
|
|
|
|
$result_code === null && $result_code = $code;
|
|
|
|
$result = [
|
|
|
|
'msg' =>$msg,
|
|
|
|
'code'=>$result_code,
|
|
|
|
'data'=>$data
|
|
|
|
];
|
|
|
|
$this->header['Content-Type'] = $type;
|
|
|
|
$this->header['token'] = $this->token;
|
|
|
|
$response = Response::create(json_encode($result),$code,$this->header);
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 方法请求输出数据(带分页参数)
|
|
|
|
* @param $data
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
protected function result($lists) {
|
|
|
|
$data['data'] = $lists;
|
|
|
|
$data['page'] = $this->setPages();
|
|
|
|
$this->response('success', 200, $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置分页返回参数()
|
|
|
|
*/
|
|
|
|
protected function setPages() {
|
|
|
|
$page_count = $this->allCount > $this->row ? ceil($this->allCount / $this->row) : 1;
|
|
|
|
$this->header['Total-Count'] = $this->allCount; //总条数
|
|
|
|
$this->header['Page-Count'] = $page_count; //总页数
|
|
|
|
$this->header['Current-Page'] = $this->p; //当前页数
|
|
|
|
$this->header['Per-Page'] = $this->row; //每页条数
|
|
|
|
return $this->header;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|