BaseController.php 1.6 KB
<?php

namespace App\Http\Controllers\Cside;

use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;

class BaseController extends Controller
{
    protected $param = [];//所有请求参数
    protected $token = ''; //token
    protected $request = [];//助手函数
    protected $project = [];//当前登录用户详情
    /**
     * 获取所有参数
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->param = $this->request->all();
        $this->token = $this->request->header('token');
        if(!empty($this->token) && !empty(Cache::get($this->token))){
            $info = Cache::get($this->token);
            $this->user = $info;
            $this->uid = $info['id'];
        }

    }


    /**
     * 成功返回
     * @param array $data
     * @param string $code
     * @param bool $objectData
     * @return JsonResponse
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
    {
        if ($objectData) {
            $data = (object)$data;
        }
        $code = Code::fromValue($code);
        $response = [
            'code' => $code->value,
            'data' => $data,
            'msg' => $code->description,
        ];
        return response()->json($response,200);
    }
}