BaseLogic.php 3.1 KB
<?php

namespace App\Http\Logic\Bside;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Logic;
use App\Models\Image as ImageModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

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

    protected $requestAll;

    protected  $param;

    protected  $request;

    protected $user;

    public function __construct()
    {
        $this->request = request();
        $this->requestAll = request()->all();
        $this->user = Cache::get(request()->header('token'));
    }


    /**
     * 列表
     * @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 :上传图片
     * @return void
     * @author :liyuhang
     * @method
     */
    public function upload(){
        $image = $this->request->file('image');
        if(empty($image)){
            return $this->fail('没有上传图片',Code::USER_ERROR);
        }
        $url = './../uploads/images/';
        $filename = date('ymdHis').rand(10000,99999);
        $res =  $this->request->file('image')->move($url,$filename);
        if ($res === false) {
            return $this->fail($image->getError(), Code::USER_ERROR);
        }
        $data = [
            'path' => $url.$filename,
            'created_at' => date('Y-m-d H:i:s',time()),
            'size' => $res->getSize(),
            'hash' => hash_file('md5', $res->getPathname()),
            'type'=>$image->getClientOriginalExtension(),
//            'mime'=>$image->getMimeType()
        ];
        $imageModel = new ImageModel();
        $rs = $imageModel->add($data);
        if ($rs === false) {
            return $this->fail('添加失败', Code::USER_ERROR);
        }
        return $data['hash'];
    }
}