BaseController.php 5.2 KB
<?php

namespace App\Http\Controllers\Bside;

use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Models\ProjectMenu;
use App\Models\ProjectRole as ProjectRoleModel;
use \Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\Exceptions\HttpResponseException;
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;
    protected $user = [];//当前登录用户详情
    /**
     * 获取所有参数
     */
    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();
        $this->auth_role();
    }

    /**
     * @name
     * @return void
     * @author :liyuhang
     * @method
     */
    public function auth_token(){
        $info = Cache::get($this->token);
        $this->user = $info;
        $this->uid = $info['id'];
        //操作权限设置
        $projectRoleModel = new ProjectRoleModel();
        $role_info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
        //获取当前操作的控制器与方法
        $action = $this->request->route()->getAction();
        //查询当前用户是否拥有权限操作
        $projectMenuModel = new ProjectMenu();
        $menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
        if($menu_id !== false && strpos($role_info['role_menu'], $menu_id['id']) === false){
            $this->response('拦截',Code::USER_PERMISSION_ERROE);
        }
    }

    /**
     * @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 "created_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 "updated_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;
    }

    /**
     * @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;
    }
}