BaseLogic.php 4.8 KB
<?php

namespace App\Http\Logic\Bside;


use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\File\ImageController;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Logic\Logic;
use App\Models\File\Image as ImageModel;
use App\Models\UpdateNotify;
use Illuminate\Support\Facades\Cache;

/**
 * @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 = request()->all();
        $this->user = Cache::get(request()->header('token'));
        $this->project = (new ProjectLogic())->getInfo($this->user['project_id']);
    }


    /**
     * 列表
     * @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 :上传图片返回hash
     * @return void
     * @author :liyuhang
     * @method
     */
    public function upload(){
        $files = $this->request->file('image');
        $hash = hash_file('md5', $files->getPathname());
        //查看文件是否存在
        $imageModel = new ImageModel();
        $image_hash = $imageModel->read(['hash'=>$hash]);
        if($image_hash !== false){
            return $hash;
        }
        $this->config = config('filesystems.disks.upload');
        $this->uploads = config('upload.default_image');
        $this->path = $this->config['root'].$this->uploads['path'].'/';
        $url = $this->path;
        $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
        $res =  $files->move($url,$fileName);
        if ($res === false) {
            return false;
        }
        $data = [
            'path' => $url.$fileName,
            'created_at' => date('Y-m-d H:i:s',time()),
            'size' => $res->getSize(),
            'hash' => $hash,
            'type'=>$files->getClientOriginalExtension(),
        ];
        $rs = $imageModel->add($data);
        if ($rs === false) {
            return false;
        }
        return $hash;
    }

    /**
     * @name :自增或自减
     * @return bool
     * @author :liyuhang
     * @method
     */
    public function set_num($model,$data,$type = 'add',$num = 1){
        if(is_array($data)){
            foreach ($data as $v){
                $this->set_num($model,$v,$type,$num);
            }
        }else{
            if($type == 'del'){
                $rs = $model::where('id',$data)->decrement('num',$num);
            }else{
                $rs = $model::where('id',$data)->increment('num',$num);
            }
        }
        return $rs;
    }

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

    /**
     * @name   :(通知更新)projectUrl
     * @author :lyh
     * @method :post
     * @time   :2023/6/6 14:09
     */
    function updateNotify($data)
    {
        $param = [
            'project_id'=>$data['project_id'],
            'type'=>$data['type'],
            'route'=>$data['route'],
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s')
        ];
        return (new UpdateNotify())->insertGetId($param);
    }
}