BaseController.php 1.2 KB
<?php

namespace App\Http\Controllers\Cside;

use App\Http\Controllers\Controller;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;

class BaseController extends Controller
{
    const SUCCESS = 200;
    const ERROR = 400;
    protected $param = [];//所有请求参数
    protected $token = ''; //token
    protected $request = [];//助手函数
    protected $page = 1;//当前页
    protected $row = 20;//每页条数
    protected $header = [];//设置请求头参数
    protected $order = 'id';
    protected $map = [];//处理后的参数
    protected $uid = 0;
    protected $user = [];//当前登录用户详情
    public function __construct(){
    }

    /**
     * 响应
     * @throws HttpResponseException
     */
    public function response($msg = null,string $code = self::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
    {
        $result = [
            'msg' => $msg,
            'code' => $code,
            'data' => $data,
        ];
        $this->header['Content-Type'] = $type;
        $this->header['token'] = $this->token;
        $response =  response($result,$result_code,$this->header);
        throw new HttpResponseException($response);
    }
}