BaseController.php 7.5 KB
<?php

namespace App\Http\Controllers\Bside;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Controllers\Controller;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Requests\Scene;
use App\Models\User\User as UserModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;

class BaseController extends Controller
{
    protected $param = [];//所有请求参数
    protected $token = ''; //token
    protected $request = [];//助手函数
    protected $page = 1;//当前页
    protected $row = 20;//每页条数
    protected $header = [];//设置请求头参数
    protected $order = 'created_at';
    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'];
            //参数处理
            $this->get_param();
            //日志记录
            $this->set_user_log();
        }
    }
    /**
     * @name :参数过滤
     * @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 "status":
                    $this->map['status'] = $v;
                    break;
                case "category_id":
                    $this->map['category_id'] = ['like','%,'.$v.',%'];;
                    break;
                case "name":
                    $this->map['name'] = ['like','%'.$v.'%'];
                    break;
                case "start_at":
                    if(!empty($v)){
                        $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":
                    if(!empty($v)){
                        $this->_btw[1]                             = $v;
                        $this->map['created_at'] = ['between', $this->_btw];
                    }
                    break;
                default:
                    $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' => $this->_extents($data),
        ];
        $this->header['Content-Type'] = $type;
        $this->header['token'] = $this->token;
        $response =  response($result,$result_code,$this->header);;
        throw new HttpResponseException($response);
    }

    /**
     * 成功返回
     * @param array $data
     * @param string $code
     * @param bool $objectData
     * @return JsonResponse
     */
    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);
    }
    /**
     * @param $data
     * @name :返回参数处理
     * @return array|string
     * @author :liyuhang
     * @method
     */
    protected function _extents($data) {
        if (empty($data) || !is_array($data)) {
            return empty($data) ? is_array($data) ? [] : '' : $data;
        }
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $data[$k] = $this->_extents($v);
            } else {
                if (is_null($v)) {
                    $data[$k] = '';
                    continue;
                }
                //获取操作人
                switch ((string) $k) {
                    case 'image':
                        $data['image_link'] = url('/b/image/' . $v);
                        break;
                    case 'images':
                        $v = explode(',',$v);
                        foreach ($v as $k1=>$v1){
                            $data['images_link'][$k1] = url('/b/image/' . $v1);
                        }
                        break;
                    case 'file':
                        $data['file_link'] = url('/b/file_hash/' . $v);
                        break;
                    case 'operator_id':
                        if(!empty($v)){
                            $name = (new UserModel())->read(['id'=>$v],['id','name']);
                            $data['operator_name'] = (isset($name['name']) && !empty($name['name'])) ? $name['name'] : '无名称';
                        }
                        break;
                }
            }
        }
        return $data;
    }

    /**
     * @name :写入操作日志
     * @return void
     * @author :liyuhang
     * @method
     */
    public function set_user_log(){
        //生成日志
        $log = config('logging.operator_log');
        if(isset($log) && $log['log'] == true){
            if(empty($log['action']) || (strpos($log['action'],$this->request->route()->getName()) < 0)){
                Common::set_user_log(['model'=>$this->request->route()->getName(),'remark'=>'请求的参数:param = '.json_encode($this->param),'operator_id'=>$this->uid]);
            }
        }
        return true;
    }


    /**
     * 是否post请求
     * @return bool
     */
    protected final function isPost()
    {
        return \Illuminate\Support\Facades\Request::isMethod('post');
    }

    /**
     * @name   :(获取当前登录用户域名并通知更新)projectUrl
     * @author :lyh
     * @method :post
     * @time   :2023/6/6 14:09
     */
    public function projectUrlNotify($str = ''){
        $urlStr = 'api/updateHtmlNotify?model='.$str;
        $domain = $this->getProjectDomain();
        if(!empty($domain)){
            $url = $domain.$urlStr;
            return http_get($url);
        }
        return false;
    }

    public function getProjectDomain(){
        $project = (new ProjectLogic())->getInfo($this->user['project_id']);
        if(!empty($project['deploy_optimize']['domain'])){
            return $project['deploy_optimize']['domain'];
        }
        if(!empty($project['deploy_build']['test_domain'])){
            return $project['deploy_build']['test_domain'];
        }
        return '';
    }
}