开始实现使用内部的上传逻辑;

This commit is contained in:
2022-07-11 17:41:20 +08:00
parent 5081cd8790
commit 3078dfad39
12 changed files with 396 additions and 777 deletions

View File

@@ -2,17 +2,6 @@
新版基于`EasyAdmin`打造,`EasyAdmin`是一款优秀的开源后台框架,致谢.
[![Php Version](https://img.shields.io/badge/php-%3E=7.1.0-brightgreen.svg?maxAge=2592000&color=yellow)](https://github.com/php/php-src)
[![Mysql Version](https://img.shields.io/badge/mysql-%3E=5.7-brightgreen.svg?maxAge=2592000&color=orange)](https://www.mysql.com/)
[![Thinkphp Version](https://img.shields.io/badge/thinkphp-%3E=6.0.2-brightgreen.svg?maxAge=2592000)](https://github.com/top-think/framework)
[![Layui Version](https://img.shields.io/badge/layui-=2.5.5-brightgreen.svg?maxAge=2592000&color=critical)](https://github.com/sentsin/layui)
[![Layuimini Version](https://img.shields.io/badge/layuimini-%3E=2.0.4.2-brightgreen.svg?maxAge=2592000&color=ff69b4)](https://github.com/ulthon/layuimini)
[![ulthon_admin Doc](https://img.shields.io/badge/docs-passing-green.svg?maxAge=2592000)](http://doc.ulthon.com/home/read/ulthon_admin/home.html)
[![ulthon_admin License](https://img.shields.io/badge/license-mulan-green?maxAge=2592000&color=blue)](https://gitee.com/ulthon/ulthon_admin/blob/master/LICENSE)
## 项目介绍
只为`开发人员`服务,只为`需求定制`服务.
@@ -73,7 +62,7 @@ php think run
* 文档地址:[http://doc.ulthon.com/home/read/ulthon_admin/home.html](http://doc.ulthon.com/home/read/ulthon_admin/home.html)
* 演示地址:[http://admin.demo.ulthon.com](http://admin.demo.ulthon.com)账号admin密码123456。备注只有查看信息的权限
* 演示地址:[http://admin.demo.ulthon.com/admin](http://admin.demo.ulthon.com/admin)账号admin密码123456。备注只有查看信息的权限
## 代码仓库
@@ -106,9 +95,7 @@ php think run
* 对table表格再次封装在使用上更加舒服
* 根据table的`cols`参数再次进行封装,提供接口实现`image``switch``list`等功能,再次基础上可以自己再次扩展
* 根据table参数一键生成`搜索表单`,无需自己编写
* 完善的后台操作日志
* 记录用户的详细操作信息
* 按月份进行`分表记录`
* 默认使用数据库记录日志
* 一键部署静态资源到OSS上
* 所有在`public\static`目录下的文件都可以一键部署
* 一个配置项切换静态资源oss/本地)

View File

@@ -6,9 +6,11 @@ namespace app\admin\controller;
use app\admin\model\SystemUploadfile;
use app\common\controller\AdminController;
use app\common\service\MenuService;
use app\common\service\UploadService;
use EasyAdmin\upload\Uploadfile;
use think\db\Query;
use think\facade\Cache;
use think\facade\Filesystem;
class Ajax extends AdminController
{
@@ -59,27 +61,20 @@ class Ajax extends AdminController
'upload_type' => $this->request->post('upload_type'),
'file' => $this->request->file('file'),
];
$uploadConfig = sysconfig('upload');
empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
$rule = [
'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
];
$this->validate($data, $rule);
try {
$upload = Uploadfile::instance()
->setUploadType($data['upload_type'])
->setUploadConfig($uploadConfig)
->setFile($data['file'])
->save();
$upload_service = new UploadService($data['upload_type']);
$upload_service->validateException($data['file']);
$result = $upload_service->save($data['file']);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
if ($upload['save'] == true) {
$this->success($upload['msg'], ['url' => $upload['url']]);
} else {
$this->error($upload['msg']);
}
$this->success('上传成功', $result);
}
/**
@@ -158,5 +153,4 @@ class Ajax extends AdminController
];
return json($data);
}
}
}

View File

@@ -17,8 +17,8 @@ $ul_system_config = array(
array(
"name" => "upload_type",
"group" => "upload",
"value" => "local",
"remark" => "当前上传方式 local,alioss,qnoss,txoss",
"value" => "local_public",
"remark" => "当前上传方式 local_public,alioss,qnoss,txoss",
"sort" => 0,
),
array(
@@ -45,7 +45,7 @@ $ul_system_config = array(
array(
"name" => "upload_allow_type",
"group" => "upload",
"value" => "local,alioss,qnoss,txcos",
"value" => "local_public,alioss,qnoss,txcos",
"remark" => "可用的上传文件方式",
"sort" => 0,
),

View File

@@ -3,7 +3,7 @@
<div class="layui-form-item">
<label class="layui-form-label required">存储方式</label>
<div class="layui-input-block">
{foreach ['local'=>'本地存储','alioss'=>'阿里云oss','qnoss'=>'七牛云oss','txcos'=>'腾讯云cos'] as $key=>$val}
{foreach ['local_public'=>'本地存储','alioss'=>'阿里云oss','qnoss'=>'七牛云oss','txcos'=>'腾讯云cos'] as $key=>$val}
<input type="radio" v-model="upload_type" name="upload_type" lay-filter="upload_type" value="{$key}" title="{$val}" {if $key==sysconfig('upload','upload_type')}checked=""{/if}>
{/foreach}
</div>
@@ -53,7 +53,7 @@
<label class="layui-form-label required">空间名称</label>
<div class="layui-input-block">
<input type="text" name="alioss_bucket" class="layui-input" lay-verify="required" lay-reqtext="请输入空间名称" placeholder="请输入空间名称" value="{:sysconfig('upload','alioss_bucket')}">
<tip>例子:easy-admin</tip>
<tip>例子:ulthon-admin</tip>
</div>
</div>
@@ -61,7 +61,7 @@
<label class="layui-form-label required">访问域名</label>
<div class="layui-input-block">
<input type="text" name="alioss_domain" class="layui-input" lay-verify="required" lay-reqtext="请输入访问域名" placeholder="请输入访问域名" value="{:sysconfig('upload','alioss_domain')}">
<tip>例子:easy-admin.oss-cn-shenzhen.aliyuncs.com</tip>
<tip>例子:ulthon-admin.oss-cn-shenzhen.aliyuncs.com</tip>
</div>
</div>
@@ -93,7 +93,7 @@
<label class="layui-form-label required">存储桶名称</label>
<div class="layui-input-block">
<input type="text" name="tecos_bucket" class="layui-input" lay-verify="required" lay-reqtext="请输入存储桶名称" placeholder="请输入存储桶名称" value="{:sysconfig('upload','tecos_bucket')}">
<tip>例子:easyadmin-1251997243</tip>
<tip>例子:ulthon-admin-1251997243</tip>
</div>
</div>
@@ -117,7 +117,7 @@
<label class="layui-form-label required">存储空间</label>
<div class="layui-input-block">
<input type="text" name="qnoss_bucket" class="layui-input" lay-verify="required" lay-reqtext="请输入存储桶地域" placeholder="请输入存储桶地域" value="{:sysconfig('upload','qnoss_bucket')}">
<tip>例子:easyadmin</tip>
<tip>例子:ulthon-admin</tip>
</div>
</div>

View File

@@ -7,7 +7,6 @@
<input name="head_img" class="layui-input layui-col-xs6" lay-verify="required" lay-reqtext="请上传文件" placeholder="请上传文件" value="">
<div class="layuimini-upload-btn">
<span><a class="layui-btn" data-upload="head_img" data-upload-number="*" data-upload-exts="*"><i class="fa fa-upload"></i> 上传文件</a></span>
<span><a class="layui-btn layui-btn-normal"><i class="fa fa-list"></i> 选择文件</a></span>
</div>
</div>
</div>

View File

@@ -3,6 +3,7 @@
use app\common\service\AuthService;
use think\facade\Cache;
use think\facade\Request;
use think\route\Url;
if (!function_exists('__url')) {
@@ -219,3 +220,28 @@ if (!function_exists('unparse_url')) {
return "$scheme$user$pass$host$port$path$query$fragment";
}
}
function build_upload_url($url)
{
$config = sysconfig('upload');
$upload_type = $config['upload_type'] ?? 'local_public';
$prefix_url = '';
switch ($upload_type) {
case 'local_public':
$prefix_url = Request::domain();
break;
case 'qnoss':
$prefix_url = $config['qnoss_domain'];
break;
default:
# code...
break;
}
return trim($prefix_url, '/') . '/' . trim($url, '/');
}

View File

@@ -0,0 +1,66 @@
<?php
namespace app\common\service;
use think\facade\Filesystem;
use think\facade\Validate;
use think\File;
class UploadService
{
protected $uploadType = 'local_public';
public function __construct($upload_type = 'local_public')
{
$uploadConfig = sysconfig('upload');
empty($upload_type) && $upload_type = $uploadConfig['upload_type'];
$this->uploadType = $upload_type;
}
public function validate($file, $allow_ext = null, $allow_size = null, $fail_exception = false)
{
$uploadConfig = sysconfig('upload');
if (!is_null($allow_ext)) {
$uploadConfig['upload_allow_ext'] = $allow_ext;
}
if (!is_null($allow_size)) {
$uploadConfig['upload_allow_size'] = $allow_size;
}
$rule = [
'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
];
return Validate::failException($fail_exception)->check([
'upload_type' => $this->uploadType,
'file' => $file
], $rule);
}
public function validateException($file, $allow_ext = null, $allow_size = null)
{
return $this->validate($file, $allow_ext, $allow_size, true);
}
public function save(File $file)
{
$save_name = Filesystem::disk($this->uploadType)->putFile('upload', $file, function () {
return date('Ymd') . DIRECTORY_SEPARATOR . uniqid();
});
$url = build_upload_url($save_name);
return [
'url' => $url,
'save_name' => $save_name
];
}
}

View File

@@ -25,13 +25,13 @@
"topthink/think-captcha": "^3.0",
"aliyuncs/oss-sdk-php": "^2.3",
"qcloud/cos-sdk-v5": "^2.0",
"qiniu/php-sdk": "^7.2",
"alibabacloud/client": "^1.5",
"zhongshaofa/easy-admin": "^1.0.2",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.4",
"phpoffice/phpspreadsheet": "^1.22",
"topthink/think-migration": "^3.0"
"topthink/think-migration": "^3.0",
"overtrue/flysystem-qiniu": "*"
},
"require-dev": {
"symfony/var-dumper": "^4.2",

982
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,7 +11,7 @@ return [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
'public' => [
'local_public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
@@ -21,6 +21,9 @@ return [
// 可见性
'visibility' => 'public',
],
'qnoss' => [
'type' => 'Qiniu'
]
// 更多的磁盘配置信息
],
];

View File

@@ -0,0 +1,20 @@
<?php
namespace think\filesystem\driver;
use League\Flysystem\AdapterInterface;
use think\filesystem\Driver;
use Overtrue\Flysystem\Qiniu\QiniuAdapter;
class Qiniu extends Driver
{
protected function createAdapter(): AdapterInterface
{
return new QiniuAdapter(
sysconfig('upload','qnoss_access_key'),
sysconfig('upload','qnoss_secret_key'),
sysconfig('upload','qnoss_bucket'),
sysconfig('upload','qnoss_domain')
);
}
}

View File

@@ -41,8 +41,8 @@
<p class="desc">{$data.description|default=''}</p>
<p class="operation">
<a class="start" target="_blank" href="http://doc.ulthon.com/home/read/ulthon_admin/home.html"> 起步 <i class="icon-play"></i></a>
<a class="github button-grya" target="_blank" href="https://github.com/zhongshaofa/ulthon_admin"><i class="icon-github-big"></i>GitHub</a>
<a class="gitee button-grya" target="_blank" href="https://gitee.com/zhongshaofa/ulthon_admin"><i class="icon-gitee"></i>Gitee</a>
<a class="gitee button-grya" target="_blank" href="https://gitee.com/ulthon/ulthon_admin"><i class="icon-gitee"></i>Gitee</a>
</p>
</div>
<!-- 框架特性-->