BaseController.php 8.3 KB
<?php

namespace App\Http\Controllers\Bside;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Common;
use App\Http\Controllers\Controller;
use App\Http\Requests\Scene;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
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 = [];//当前登录用户详情
    /**
     * ceshi获取所有参数
     */
    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);
            Cache::put($this->token, $info, 3600 * 12);//更新缓存时间
            $this->user = $info;
            $this->uid = $info['id'];
            //参数处理
            $this->getParam();
            if(!empty($this->user)){
                $this->project = Cache::get('user-'.$this->user['project_id']);
            }
            //日志记录
            $this->set_user_log();
        }
    }

    /**
     * @remark :请求参数处理
     * @name   :getParam
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:34
     */
    public function getParam(){
        foreach ($this->param as $k => $v){
            if(is_array($v)){
                $this->map[$k] = $v;
            }else{
                $this->getMap($k,$v);
            }
        }
    }

    /**
     * @remark :搜索参数处理
     * @name   :getMap
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 10:22
     */
    public function getMap($k,$v){
        switch ($k){
            case "order":
                $this->order = $v;
                break;
            case 'page':
                $this->page = $v;
                break;
            case 'row':
            case 'size':
                $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['created_at'] = ['between', $this->_btw];
                break;
            default:
                if (!empty($v) || $v == 0) {
                    $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 = [
            'code' => $code->value,
            'data' => $data,
            'message' => $msg == ' ' ? $code->description : $msg,
        ];
        $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($data)
    {
        return $data;
    }

    /**
     * @notes: 错误抛出
     * @param string $code
     * @param string $message
     * @throws BsideGlobalException
     */
    public function fail(string $message = "", string $code = Code::SYSTEM_ERROR)
    {
        throw new BsideGlobalException($code, $message);
    }

    /**
     * @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()) === false)){
                Common::set_user_log(['model'=>$this->request->route()->getName(),'remark'=>'请求的参数:param = '.json_encode($this->param),'operator_id'=>$this->uid,'type'=>isset($this->user['manager_id']) ? 1 : 0]);
            }
        }
        return true;
    }


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


    /**
     * @remark :获取当前用户选择的模版
     * @name   :getTemplateId
     * @author :lyh
     * @method :post
     * @time   :2024/1/3 10:52
     */
    public function getTemplateId($source,$is_list){
        $template_id = 0;
        if($this->user['is_customized'] == BTemplate::IS_VISUALIZATION) {//定制项目
            $type = $this->getCustomizedType($source, $is_list);//获取定制界面类型
            //查看当前页面是否定制,是否开启可视化
            $page_array = (array)$this->user['is_visualization']->page_array;//获取所有定制界面
            if (in_array($type, $page_array)) {//是定制界面
                return $this->success($template_id);
            }
        }
        $bSettingModel = new Setting();
        $info = $bSettingModel->read(['project_id'=>$this->user['project_id']]);
        if($info === false){
            return $this->success($template_id);
        }
        $template_id = $info['template_id'];
        return $this->success($template_id);
    }

    /**
     * @remark :定制页面头部类型---根据source获取type类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 11:20
     */
    public function getCustomizedType($source,$is_list){
        $type = BTemplate::TYPE_HOME;
        if($source == BTemplate::SOURCE_PRODUCT){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_PRODUCT_LIST;
            }else{
                $type = BTemplate::TYPE_PRODUCT_DETAIL;
            }
        }
        if($source == BTemplate::SOURCE_BLOG){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_BLOG_LIST;
            }else{
                $type = BTemplate::TYPE_BLOG_DETAIL;
            }
        }
        if($source == BTemplate::SOURCE_NEWS){
            if($is_list == BTemplate::IS_LIST){
                $type = BTemplate::TYPE_NEWS_LIST;
            }else{
                $type = BTemplate::TYPE_NEWS_DETAIL;
            }
        }
        return $type;
    }
    /**
     * @remark :查看是否已装修
     * @name   :getIsRenovation
     * @author :lyh
     * @method :post
     * @time   :2023/9/13 14:02
     */
    public function getIsRenovation($source,$is_list,$template_id,$id,$is_custom = 0){
        $webTemplateModel = new BTemplate();
        $param = [
            'source'=>$source,
            'project_id'=>$this->user['project_id'],
            'source_id'=>$id,
            'template_id'=>$template_id,
            'is_list'=>$is_list,
            'is_custom'=>$is_custom
        ];
        $templateInfo = $webTemplateModel->read($param);
        if($templateInfo !== false){
            return 1;
        }
        return 0;
    }

    /**
     * @remark :批量通知C端
     * @name   :sendHttpC
     * @author :lyh
     * @method :post
     * @time   :2024/6/6 10:26
     */
    public function sendHttpC($url = []){
        //其他服务器:请求对应C端接口
        $c_url = $this->user['domain'].'api/update_page/';
        $param = [
            'project_id' => $this->user['project_id'],
            'type' => 1,
            'route' => 3,
            'url' => $url,
            'language'=> [],
            'is_sitemap' => 0
        ];
        return http_post($c_url, json_encode($param));
    }
}