BaseLogic.php 5.8 KB
<?php

namespace App\Http\Logic\Bside;


use App\Enums\Common\Common;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Logic;
use App\Models\Com\UpdateNotify;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteDelete;
use App\Models\Service\Service;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

/**
 * @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常
 */
class BaseLogic extends Logic
{

    protected $requestAll;

    protected  $param;

    protected  $request;

    protected $user;

    protected $project;

    protected $side = Common::B;

    public function __construct()
    {
        $this->request = request();
        $this->requestAll = $this->getParam();
        $this->user = Cache::get(request()->header('token'));
        if(!empty($this->user)){
            $this->project = $this->getProjectInfo();
        }
    }

    /**
     * @remark :获取项目详情
     * @name   :getProjectInfo
     * @author :lyh
     * @method :post
     * @time   :2023/8/31 15:11
     */
    public function getProjectInfo(){
        $info = Cache::get('user-'.$this->user['project_id']);
        if($info === false){
            $projectModel = new Project();
            $info = $projectModel->with('payment')->with('deploy_build')
                ->with('deploy_optimize')->with('online_check')->where(['id'=>$this->user['project_id']])->first();
            if($info['extend_type'] != 0){
                $info['type'] = $info['extend_type'];
            }
            Cache::add('user-'.$this->user['project_id'],$info);
        }
        return $this->success($info);
    }

    /**
     * @remark :请求参数处理
     * @name   :getParam
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:34
     */
    public function getParam(){
        $requestAll = $this->request->all();
        foreach ($requestAll as $k => $v){
            if(is_array($v)){
                continue;
            }else{
                if(Str::endsWith($k, '_link')){
                    unset($requestAll[$k]);
                }
            }
        }
        return $this->success($requestAll);
    }

    /**
     * 列表
     * @param array $map
     * @param array $sort
     * @param array $columns
     * @param int $limit
     * @return array
     * @author zbj
     * @date 2023/4/13
     */
    public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
    {
        $map[] = ['project_id' => $this->user['project_id']];
        return parent::getList($map, $sort, $columns, $limit);
    }

    /**
     * @param $id
     * @return mixed
     * @author zbj
     * @date 2023/4/15
     */
    public function getCacheInfo($id)
    {
        $info = parent::getCacheInfo($id);
        if ($info && $info['project_id'] != $this->user['project_id']) {
            $info = null;
        }
        return $info;
    }

    /**
     * 保存
     * @param $param
     * @return array
     * @throws BsideGlobalException
     * @author zbj
     * @date 2023/4/13
     */
    public function save($param)
    {
        $param['project_id'] = $this->user['project_id'];
        return parent::save($param);
    }

    /**
     * 批量删除
     * @param $ids
     * @param array $map
     * @return array
     * @author zbj
     * @date 2023/4/13
     */
    public function delete($ids, $map = [])
    {
        $map[] = ['project_id' => $this->user['project_id']];
        return parent::delete($ids, $map);
    }

    /**
     * @name   :生成一条新路由记录
     * @author :lyh
     * @method :post
     * @time   :2023/6/6 14:09
     */
    public function addUpdateNotify($type,$route)
    {
        $updateNotifyModel = new UpdateNotify();
        $info = $updateNotifyModel->read(['project_id'=>$this->user['project_id'],'route'=>$route,'status'=>1]);
        if($info === false){
            $param = [
                'project_id'=>$this->user['project_id'], 'type'=>$type, 'route'=>$route,'status'=>1
            ];
            $updateNotifyModel->add($param);
        }else{
            $updateNotifyModel->edit(['route'=>$route],['project_id'=>$this->user['project_id'],'type'=>$type,'status'=>1]);
        }
        return $this->success();
    }

    /**
     * @remark :删除路由通知C端
     * @name   :curlDelRoute
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 14:43
     */
    public function curlDelRoute($data){
        $data['project_id'] = $this->user['project_id'];
        $str = http_build_query($data);
        $url = $this->user['domain'].'api/delHtml/?'.$str;
        $serverIpModel = new ServersIp();
        $serversIpInfo = $serverIpModel->read(['id'=>$this->project['serve_id']],['servers_id']);
        if($serversIpInfo && ($serversIpInfo['servers_id'] == ServerConfig::SELF_SITE_ID)){
            //自建站服务器直接返回
            return $this->success();
        }
        if($serversIpInfo && ($serversIpInfo['servers_id'] != 1)){//TODO::当前项目通知不过 ,跳过自动更新
            exec('curl -k "'.$url.'" > /dev/null 2>&1 &');
        }else{
            shell_exec('curl -k "'.$url.'"');
        }
        return $this->success();
    }

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

}