处理上传内容临时目录
parent
851f453bf8
commit
32a95b96d7
|
|
@ -70,6 +70,7 @@ class Components extends BaseRenderer {
|
||||||
*/
|
*/
|
||||||
public function imageControl($name, $label){
|
public function imageControl($name, $label){
|
||||||
return amis()->ImageControl($name, $label)
|
return amis()->ImageControl($name, $label)
|
||||||
|
->joinValues(false)
|
||||||
->autoUpload(true)->maxSize('5*1024*1024');//不能大于5M
|
->autoUpload(true)->maxSize('5*1024*1024');//不能大于5M
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,12 +78,25 @@ class Components extends BaseRenderer {
|
||||||
* 图片上传,带裁剪
|
* 图片上传,带裁剪
|
||||||
*/
|
*/
|
||||||
public function cropImageControl($name, $label, $aspectRatio = null){
|
public function cropImageControl($name, $label, $aspectRatio = null){
|
||||||
return amis()->ImageControl($name, $label)
|
return amis()->ImageControl($name, $label)->joinValues(false)
|
||||||
->crop([
|
->crop([
|
||||||
'aspectRatio' => $aspectRatio ?? 1
|
'aspectRatio' => $aspectRatio ?? 1
|
||||||
])->autoUpload(true);
|
])->autoUpload(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 普通文件上传
|
||||||
|
*/
|
||||||
|
public function fileControl($name, $label, $accept = '.txt', $multiple = false){
|
||||||
|
return amis()->FileControl($name, $label ?? __('admin.components.files'))
|
||||||
|
->multiple($multiple)
|
||||||
|
// ->receiver()
|
||||||
|
->joinValues(false)
|
||||||
|
->useChunk(false)
|
||||||
|
->maxSize(20*1024*1024)
|
||||||
|
->accept($accept);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标签选择
|
* 标签选择
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,7 @@ class ArticleController extends AdminController
|
||||||
amis()->DateTimeControl('published_at', __('admin.articles.published_at'))->format('YYYY-MM-DD HH:mm:ss')->description(__('admin.articles.published_at_remark')),
|
amis()->DateTimeControl('published_at', __('admin.articles.published_at'))->format('YYYY-MM-DD HH:mm:ss')->description(__('admin.articles.published_at_remark')),
|
||||||
amis()->SwitchControl('is_enable', __('admin.articles.is_enable'))->value(false),
|
amis()->SwitchControl('is_enable', __('admin.articles.is_enable'))->value(false),
|
||||||
amis()->SwitchControl('is_recommend', __('admin.articles.is_recommend'))->value(false),
|
amis()->SwitchControl('is_recommend', __('admin.articles.is_recommend'))->value(false),
|
||||||
|
Components::make()->fileControl('appendixes', __('admin.articles.appendixes'), '.xsl,.xlsx,.txt,.doc,.docx,.pdf,.pptx'),
|
||||||
])->md(4),
|
])->md(4),
|
||||||
amis()->Wrapper()->body([
|
amis()->Wrapper()->body([
|
||||||
Components::make()->fuEditorControl('content', __('admin.articles.content')),
|
Components::make()->fuEditorControl('content', __('admin.articles.content')),
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Admin\Controllers;
|
||||||
|
|
||||||
|
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
||||||
|
use App\Traits\UploadTrait;
|
||||||
|
|
||||||
|
class IndexController extends AdminController
|
||||||
|
{
|
||||||
|
use UploadTrait;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -29,4 +29,9 @@ Route::group([
|
||||||
$router->resource('system/keywords', \App\Admin\Controllers\KeywordController::class);
|
$router->resource('system/keywords', \App\Admin\Controllers\KeywordController::class);
|
||||||
|
|
||||||
$router->resource('articles', \App\Admin\Controllers\ArticleController::class);
|
$router->resource('articles', \App\Admin\Controllers\ArticleController::class);
|
||||||
|
|
||||||
|
//修改上传
|
||||||
|
$router->post('upload_file', [\App\Admin\Controllers\IndexController::class, 'uploadFile']);
|
||||||
|
$router->post('upload_image', [\App\Admin\Controllers\IndexController::class, 'uploadImage']);
|
||||||
|
$router->post('upload_rich', [\App\Admin\Controllers\IndexController::class, 'uploadRich']);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class Storage implements CastsAttributes
|
||||||
*/
|
*/
|
||||||
public function get($model, $key, $value, $attributes)
|
public function get($model, $key, $value, $attributes)
|
||||||
{
|
{
|
||||||
return $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : StorageFacades::disk(Admin::config('admin.upload.disk'))->url($value)) : '';
|
return $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : StorageFacades::disk(Admin::config('admin.upload.disk'))->url($value)).'?time='.time() : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ class Article extends Model
|
||||||
'is_enable' => 'boolean',
|
'is_enable' => 'boolean',
|
||||||
'is_recommend' => 'boolean',
|
'is_recommend' => 'boolean',
|
||||||
'cover' => Storage::class,
|
'cover' => Storage::class,
|
||||||
|
'appendixes' => 'array',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Services\Admin;
|
||||||
|
|
||||||
use App\Models\Article;
|
use App\Models\Article;
|
||||||
use App\Models\Filters\ArticleFilter;
|
use App\Models\Filters\ArticleFilter;
|
||||||
|
use App\Traits\UploadTrait;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -12,6 +13,8 @@ use Illuminate\Support\Arr;
|
||||||
*/
|
*/
|
||||||
class ArticleService extends BaseService
|
class ArticleService extends BaseService
|
||||||
{
|
{
|
||||||
|
use UploadTrait;
|
||||||
|
|
||||||
protected string $modelName = Article::class;
|
protected string $modelName = Article::class;
|
||||||
|
|
||||||
protected string $modelFilterName = ArticleFilter::class;
|
protected string $modelFilterName = ArticleFilter::class;
|
||||||
|
|
@ -27,6 +30,9 @@ class ArticleService extends BaseService
|
||||||
$data['published_at'] = now();
|
$data['published_at'] = now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data['cover'] = $this->saveImage('cover', 'articles/cover')[0];
|
||||||
|
$data['appendixes'] = $this->saveFile('appendixes', 'articles/appendixes');
|
||||||
|
|
||||||
foreach ($data as $k => $v) {
|
foreach ($data as $k => $v) {
|
||||||
if (!in_array($k, $columns)) {
|
if (!in_array($k, $columns)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -50,6 +56,9 @@ class ArticleService extends BaseService
|
||||||
$data['published_at'] = now();
|
$data['published_at'] = now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data['cover'] = $this->saveImage('cover', 'articles/cover')[0];
|
||||||
|
$data['appendixes'] = $this->saveFile('appendixes', 'articles/appendixes');
|
||||||
|
|
||||||
foreach ($data as $k => $v) {
|
foreach ($data as $k => $v) {
|
||||||
if (!in_array($k, $columns)) {
|
if (!in_array($k, $columns)) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits;
|
||||||
|
|
||||||
|
use Slowlyo\OwlAdmin\Admin;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
trait UploadTrait
|
||||||
|
{
|
||||||
|
public function uploadImage()
|
||||||
|
{
|
||||||
|
return $this->upload('image');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function uploadFile()
|
||||||
|
{
|
||||||
|
return $this->upload();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 富文本内文件上传
|
||||||
|
*/
|
||||||
|
public function uploadRich()
|
||||||
|
{
|
||||||
|
$fromWangEditor = false;
|
||||||
|
$file = request()->file('file');
|
||||||
|
|
||||||
|
if (!$file) {
|
||||||
|
$fromWangEditor = true;
|
||||||
|
$file = request()->file('wangeditor-uploaded-image');
|
||||||
|
if (!$file) {
|
||||||
|
$file = request()->file('wangeditor-uploaded-video');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$file) {
|
||||||
|
return $this->response()->additional(['errno' => 1])->fail(__('admin.upload_file_error'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $file->store(Admin::config('admin.upload.directory.rich'), Admin::config('admin.upload.disk'));
|
||||||
|
|
||||||
|
$link = Storage::disk(Admin::config('admin.upload.disk'))->url($path);
|
||||||
|
|
||||||
|
if ($fromWangEditor) {
|
||||||
|
return $this->response()->additional(['errno' => 0])->success(['url' => $link]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->response()->additional(compact('link'))->success(compact('link'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存放临时文件目录;
|
||||||
|
*/
|
||||||
|
protected function upload($type = 'file')
|
||||||
|
{
|
||||||
|
$file = request()->file('file');
|
||||||
|
|
||||||
|
if (!$file) {
|
||||||
|
return $this->response()->fail(__('admin.upload_file_error'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $file->store(Admin::config('admin.upload.tem_directory.' . $type), Admin::config('admin.upload.disk'));
|
||||||
|
|
||||||
|
return $this->response()->success(['value' => $path]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveImage($field = 'image', $path)
|
||||||
|
{
|
||||||
|
return $this->saveFile($field, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表单提交时,转存实际目录,并保留上传时文件名称;
|
||||||
|
*/
|
||||||
|
public function saveFile($field = 'file', $path)
|
||||||
|
{
|
||||||
|
$file = request()->file($field);
|
||||||
|
|
||||||
|
if (!$file) {
|
||||||
|
$file = request()->get($field);
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileArr = [];
|
||||||
|
//判断是否多个文件;
|
||||||
|
if(isset($file['name'])){
|
||||||
|
$files = [$file];
|
||||||
|
}else{
|
||||||
|
$files =$file;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($files as $file){
|
||||||
|
if(is_array($file)){
|
||||||
|
if(isset($file['name'])){
|
||||||
|
$filePath = $path.'/'.$file['name'];
|
||||||
|
Storage::disk(Admin::config('admin.upload.disk'))->move($file['value'], $filePath);
|
||||||
|
$fileArr[] = $filePath;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
$fileArr[] = $file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fileArr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -61,6 +61,12 @@ return [
|
||||||
'file' => 'files',
|
'file' => 'files',
|
||||||
'rich' => 'rich',
|
'rich' => 'rich',
|
||||||
],
|
],
|
||||||
|
// 临时目录
|
||||||
|
'tem_directory' => [
|
||||||
|
'image' => 'temporary/images',
|
||||||
|
'file' => 'temporary/file',
|
||||||
|
'rich' => 'temporary/rich',
|
||||||
|
]
|
||||||
],
|
],
|
||||||
|
|
||||||
'https' => env('ADMIN_HTTPS', false),
|
'https' => env('ADMIN_HTTPS', false),
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,8 @@ return [
|
||||||
'enabled' => '已启用',
|
'enabled' => '已启用',
|
||||||
'disabled' => '已禁用',
|
'disabled' => '已禁用',
|
||||||
],
|
],
|
||||||
'tag' => '标签'
|
'tag' => '标签',
|
||||||
|
'files' => '文件'
|
||||||
],
|
],
|
||||||
|
|
||||||
'code_generators' => [
|
'code_generators' => [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue