Commit 0343a4e1 authored by 王源's avatar 王源
Browse files

分割rpc文件

parent bb7cfa5a
Loading
Loading
Loading
Loading

Exporter.MD

deleted100644 → 0
+0 −40
Original line number Diff line number Diff line
# 导出Excel csv工具使用说明
1、导出Excel,不使用模板的情况
```$xslt
    $exporter = new Exporter(Exporter::EXPORTER_TYPE_XLSX,'导出产品信息');#不用模板的情况
    
    //填充表头
    $title = ['产品','颜色','尺码','sku']
    $list = Product::with(["color","size"])->get()->toArray();
    $exporter->append($title)
    ->append($list,['name','color.name','size.name|未设定尺码','sku'])//填充数据
    ->setUnprotectRange("A1:B".$exporter->getCurrentRowIndex())//设置保护
    ->download(Exporter::DOWNLOAD_TYPE_STREAM,"产品数据");//下载数据
```

2、导出Excel,使用模板的情况
```$xslt
    //根据模板导出器
    $exporter = new Exporter(Exporter::EXPORTER_TYPE_XLSX, 'template/xxx.xlsx', '导出产品信息');#使用模板来生成
    
    
    
    //新增sheet并使用它
    $exporter->addSheet('颜色参考表', true);
    //填充表头
    $title = ['颜色id','中文颜色名','英文颜色名']
    $exporter->append($title)
    ->append(Color::get(["id", "cn_name", "en_name"]))
    ->setUnprotectRange();//全表保护
    //设置保护
    //使用别的sheet表
    $exporter->setSheetByIndex(0)->setBeginRowIndex(2)->setBeginColumnChar("C");//从第三列开始填充数据
    //填充数据
    $list = Product::with(["color","size"])->get()->toArray();
    $exporter->append($list,['name','color.name','size.name|未设定尺码','sku']);
    
    $file= $exporter->download(Exporter::DOWNLOAD_TYPE_RETURN_FILE_PATH,"产品数据");
    
    echo $file;
        
```
+0 −60
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Meibuyu\Micro\Amqp\Producer;

use Exception;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;

/**
 * @Producer(exchange="micro", routingKey="exception-log")
 */
class ExceptionLogProducer extends ProducerMessage
{

    public function __construct($data)
    {
        try {
            $data['operator'] = Auth::user()['name'];
        } catch (HttpResponseException $e) {
            put_log('获取操作人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        // 获取trace中真实文件和行数
        if (!empty($data['trace'])) {
            preg_match('/(\/var\/www\/app\/.+?\.php)(?=\()/', $data['trace'], $matchFiles);
            if (!empty($matchFiles[0])) {
                $data['file'] = $matchFiles[0];
                $file = str_replace('/', '\/', $matchFiles[0]);
                $file = str_replace('.', '\.', $file);
                $pattern = '/(?<=' . $file . '\()[0-9]+(?=\))/';
                preg_match($pattern, $data['trace'], $matchLines);
                if (!empty($matchLines[0])) {
                    $data['line'] = $matchLines[0];
                }
            }
        }
        if (!empty($data['file'])) {
            // 只对项目app文件夹下的错误获取编码人
            preg_match('/(\/var\/www\/app\/)/', $data['file'], $matchPaths);
            if (!empty($matchPaths[0])) {
                try {
                    exec("cd {$matchPaths[0]} && git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
                    if (!empty($output[0]) && is_string($output[0])) {
                        preg_match('/(?<=\()[^ ]+/', $output[0], $matchCoders);
                        if (!empty($matchCoders[0])) {
                            $data['coder'] = $matchCoders[0];
                        }
                    }
                } catch (Exception $e) {
                    put_log('获取编码人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
                }
            }
        }
        $this->payload = $data;
    }

}

src/Annotation/AutoPerm.php

deleted100644 → 0
+0 −30
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/4/9
 * Time: 14:59
 */

namespace Meibuyu\Micro\Annotation;

use Hyperf\Di\Annotation\AbstractAnnotation;

/**
 * @Annotation
 * @Target({"CLASS"})
 */
class AutoPerm extends AbstractAnnotation
{

    /**
     * @var string
     */
    public $prefix = '';

    /**
     * @var array
     */
    public $exclude = [];

}
 No newline at end of file

src/Annotation/Perm.php

deleted100644 → 0
+0 −30
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/4/8
 * Time: 13:59
 */

namespace Meibuyu\Micro\Annotation;

use Hyperf\Di\Annotation\AbstractAnnotation;

/**
 * @Annotation
 * @Target({"METHOD"})
 */
class Perm extends AbstractAnnotation
{

    /**
     * @var string
     */
    public $name = '';

    public function __construct($value = null)
    {
        $this->bindMainProperty('name', $value);
    }

}
 No newline at end of file
+0 −119
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/4/8
 * Time: 14:48
 */

namespace Meibuyu\Micro\Aspect;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Utils\Str;
use Meibuyu\Micro\Annotation\AutoPerm;
use Meibuyu\Micro\Annotation\Perm;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Handler\PermHandler;

/**
 * @Aspect()
 */
class PermAnnotationAspect extends AbstractAspect
{

    /**
     * @Inject()
     * @var PermHandler
     */
    private $permHandler;

    /**
     * @Inject
     * @var ConfigInterface
     */
    protected $config;

    public $annotations = [
        Perm::class,
        AutoPerm::class,
    ];

    /**
     * @param ProceedingJoinPoint $proceedingJoinPoint
     * @return mixed
     * @throws HttpResponseException
     * @throws \Hyperf\Di\Exception\Exception
     */
    public function process(ProceedingJoinPoint $proceedingJoinPoint)
    {
        $perm = $this->genPermName($proceedingJoinPoint);
        if ($perm) {
            if ($this->permHandler->check($perm)) {
                return $proceedingJoinPoint->process();
            } else {
                throw new HttpResponseException('当前用户没有此操作权限');
            }
        }
        return $proceedingJoinPoint->process();
    }

    // 生成权限名
    public function genPermName(ProceedingJoinPoint $proceedingJoinPoint)
    {
        /** @var AutoPerm $autoPerm */
        /** @var Perm $perm */
        [$autoPerm, $perm] = $this->getAnnotations($proceedingJoinPoint);
        $className = $proceedingJoinPoint->className;
        $methodName = $proceedingJoinPoint->methodName;
        if ($autoPerm && in_array($methodName, $autoPerm->exclude)) {
            return false; // 跳过不需要鉴权的方法
        }
        $prefix = $autoPerm && $autoPerm->prefix ? $autoPerm->prefix : $this->genPrefix($className);
        $name = $perm && $perm->name ? $perm->name : $this->genName($methodName);
        return $this->parsePermName($prefix, $name);
    }

    // 拼接权限名
    protected function parsePermName($prefix, $name)
    {
        // 注意每个应用的app_name的唯一性
        $appName = trim($this->config->get('app_name'), '_');
        $prefix = trim($prefix, '_');
        $name = trim($name, '_');
        return $appName . '_' . $prefix . '_' . $name;
    }

    // 生成前缀
    protected function genPrefix(string $className): string
    {
        $handledNamespace = Str::replaceFirst('Controller', '', Str::after($className, '\\Controller\\'));
        $namespaceLength = strrpos($handledNamespace, '\\');
        $prefix = $namespaceLength ? substr($handledNamespace, $namespaceLength + 1) : $handledNamespace;
        $prefix = Str::snake($prefix);
        $prefix = str_replace('__', '_', $prefix);
        return $prefix;
    }

    // 生成名称
    protected function genName(string $methodName): string
    {
        $methodName = Str::snake($methodName);
        $methodName = str_replace('__', '_', $methodName);
        return $methodName;
    }

    // 获取注解
    public function getAnnotations(ProceedingJoinPoint $proceedingJoinPoint)
    {
        $metadata = $proceedingJoinPoint->getAnnotationMetadata();
        return [
            $metadata->class[AutoPerm::class] ?? null,
            $metadata->method[Perm::class] ?? null
        ];
    }

}
 No newline at end of file
Loading