UploadExample.php 3.24 KB
Newer Older
chentianyu's avatar
chentianyu committed
1 2
<?php

chentianyu's avatar
chentianyu committed
3
namespace Meibuyu\Common\UploadOss\Example;
chentianyu's avatar
chentianyu committed
4 5 6 7 8

use App\Controller\AbstractController;
use Hyperf\HttpMessage\Upload\UploadedFile;
use Meibuyu\Common\UploadOss\UploadOssService;
use Meibuyu\Micro\Exceptions\HttpResponseException;
chentianyu's avatar
chentianyu committed
9
use Meibuyu\Common\UploadOss\Example\Common\Enum\OssEnum;
chentianyu's avatar
chentianyu committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
use Meibuyu\Micro\Model\Auth;

/**
 * @author chentianyu
 */
class UploadExample extends AbstractController
{
    /**
     * @Inject
     * @var UploadOssService
     */
    private $service;

    /**
     * 先创建记录上传信息表 oss_files;
     */
//CREATE TABLE `oss_files` (
//`id` int(11) NOT NULL AUTO_INCREMENT,
//`type` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件类型',
//`module` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '模块名',
//`source_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '源文件名',
//`name` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件名',
//`path` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件路径',
//`user_id` varchar(16) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户id',
//`ext` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件后缀',
//`size` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '文件大小',
//`created_at` timestamp NULL DEFAULT NULL,
//PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='oss文件表'

    /**
     * 上传图片
     * @return mixed
     * @throws HttpResponseException
     * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
     * @author chentianyu
     */
    public function image()
    {
        $image = $this->request->file('image');
        $userId = Auth::id()??0;
        $module = OssEnum::PRODUCT;
        if (!$image) {
            throw new HttpResponseException('请上传图片');
        }
        /**
         * UploadedFile $image
         * string $module 模块名称,常量定义在 namespace App\Common\Enum\OssEnum
         * int $userId
chentianyu's avatar
chentianyu committed
59
         * bool $uniqueFileName 是否重新生成唯一文件名(默认否,沿用源文件名,注意文件重复上传会报oss错误。)
chentianyu's avatar
chentianyu committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
         */
        $res = $this->service->uploadImage($image, $module, $userId, $uniqueFileName = false);
        return success('上传成功', $res);
    }

    /**
     * 上传文件
     * @return mixed
     * @throws HttpResponseException
     * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
     * @author chentianyu
     */
    public function file()
    {
chentianyu's avatar
chentianyu committed
74
        $file = $this->request->file('file');
chentianyu's avatar
chentianyu committed
75 76 77
        $userId = Auth::id()??0;
        $module = OssEnum::PRODUCT_CHILD;
        if (!$file) {
chentianyu's avatar
chentianyu committed
78
            throw new HttpResponseException('请上传文件');
chentianyu's avatar
chentianyu committed
79 80 81 82 83
        }
        /**
         * UploadedFile $file
         * string $module 模块名称,常量定义在 namespace App\Common\Enum\OssEnum
         * int $userId
chentianyu's avatar
chentianyu committed
84
         * bool $uniqueFileName 是否重新生成唯一文件名(默认否,沿用源文件名,注意文件重复上传会报oss错误。)
chentianyu's avatar
chentianyu committed
85 86 87 88 89 90 91 92
         * array $options 其他选项
         */
        $res = $this->service->uploadFile($file, $module, $userId, $uniqueFileName = false, $options = ['maxSize' => 10 * 1024 * 1024]);
        return success('上传成功', $res);
    }


}