BaseController.php 5.2 KB
<?php

namespace App\Http\Controllers\Bside;

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

class BaseController extends Controller
{
    protected $param = [];//所有请求参数
    protected $token = ''; //token
    protected $request = [];//助手函数
    protected $allCount = 0;//总条数
    protected $page = 1;//当前页
    protected $row = 20;//每页条数
    protected $header = [];//设置请求头参数
    protected $order = 'id';
    protected $map = [];//处理后的参数
    protected $uid = 0;
    protected $user = [];//当前登录用户详情
    /**
     * 获取所有参数
     */
    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'];
        }else{
            return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
        }
        $this->get_param();
    }
    /**
     * 成功返回
     * @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,
        ];
        $this->header['token'] = $this->token;
        return response()->json($response,200,$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 'page':
                    $this->page = $v;
                    break;
                case 'row':
                    $this->row = $v;
                    break;
                case "name":
                    $this->map['name'] = ['like','%'.$v.'%'];
                    break;
                case "start_at":
                    $this->_btw[0]                             = $v;
                    $this->_btw[1]                             = date('Y-m-d H:i:s',time());
                    $this->map['created_at'] = ['between', $this->_btw];
                    break;
                case "end_at":
                    $this->_btw[1]                             = $v;
                    $this->map['updated_at'] = ['between', $this->_btw];
                    break;
                default:
                    if (!empty($v)) {
                        $this->map[$k] = $v;
                    }
                    break;
            }
        }
    }
    /**
     * @name 统一返回参数
     * @return JsonResponse
     * @author :liyuhang
     * @method
     */
    public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
    {
        $code = Code::fromValue($code);
        $result = [
            'msg' => $msg == ' ' ? $code->description : $msg,
            'code' => $code->value,
            'data' => $data,
        ];
        $this->header['Content-Type'] = $type;
        $this->header['token'] = $this->token;
        $response =  response($result,$result_code,$this->header);;
        throw new HttpResponseException($response);
    }


    /**
     * 菜单权限->得到子级数组
     * @param int
     * @return array
     */
    public function _get_child($my_id, $arr)
    {
        $new_arr = array();
        foreach ($arr as $k => $v) {
            $v = (array)$v;
            if ($v['pid'] == $my_id) {
                $v['sub'] = $this->_get_child($v['id'],$arr);
                $new_arr[] = $v;
            }
        }
        return $new_arr ? $new_arr : false;
    }

    /**
     * @name :上传图片
     * @return void
     * @author :liyuhang
     * @method
     */
    public function uploads(){
        $files = $this->request->file('file');
        if(empty($files)){
            return $this->response('没有上传文件',Code::USER_ERROR);
        }
        $url = './uploads/images/';
        $param = $this->request->post();
        if($this->request->hasFile('image') && $files->isValid()){
            $filename = date('ymdHis').rand(10000,99999).$this->request->file('image');
            $this->request->file('image')->move('./uploads/images/',$filename);
        }else{
            return false;
        }
        return $url.$filename;
    }

}