QiniuUploadController.php 2.5 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 上传到七牛云
+----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Http\Controllers\Service
 * @package   App\Http\Controllers\Service
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月13日 01:26:09
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Http\Controllers\Service;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Class QiniuUploadController
 *
 * @category  App\Http\Controllers\Service
 * @package   App\Http\Controllers\Service
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月13日 01:26:09
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class QiniuUploadController extends UploadController
{
    /**
     * 图片上传
     *
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    public function store(Request $request)
    {
        // add by Richer 于 2020年9月22日10:21:07 兼容不同域
        $keys   = array_keys($request->all());
        $field  = $request->field ?? 'images';
        foreach ($keys as $key) {
            if ($key !== 'filetype' && $key !== 'field') {
                $field = $key;
            }
        }
        $filetype   = $request->filetype ?? 'image';
        return parent::upload($request, $field, $filetype);
    }

    /**
     * 上传文件到七牛云服务器
     * 1、根据文件的类型,指定不同的目录
     * 2、如果是文件数组,需要递归调用方法进行上传
     * 3、上传成功后,需要换文件信息保存到本地数据库中,方便前端调用
     *
     * @param $file
     * @return array|bool
     * @throws \Exception
     */
    public function destroy($file)
    {
        if (!$response_upload = \Storage::disk('qiniu')->delete($file)) {
            return [
                'code'  => Response::HTTP_UNPROCESSABLE_ENTITY,
                'data'  => null,
                'msg'   => '删除文件失败失败',
            ];
        }
        // 上传成功,返回文件信息和路径
        return ['code' => 0, 'data' => null, 'msg' => '删除文件成功!'];
    }
}