mirror of
https://github.com/cjango/dcat-vue.git
synced 2025-12-06 14:20:03 +08:00
一些优化
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
[demo: http://dcat.weiwait.cn (admin:admin)](http://dcat.weiwait.cn/admin/demo-settings 'user: admin psw: admin')
|
||||
|
||||
### 依赖扩展
|
||||
[freyo/flysystem-qcloud-cos-v5](https://github.com/freyo/flysystem-qcloud-cos-v5)
|
||||
[overtrue/laravel-filesystem-cos](https://github.com/overtrue/laravel-filesystem-cos)
|
||||
|
||||
[overtrue/laravel-filesystem-qiniu](https://github.com/overtrue/laravel-filesystem-qiniu)
|
||||
|
||||
@@ -29,9 +29,10 @@
|
||||
}
|
||||
```
|
||||
|
||||
### 文件系统-通过一级菜单使用
|
||||
[//]: # (### 文件系统-通过一级菜单使用)
|
||||
|
||||

|
||||
[//]: # ()
|
||||
[//]: # ()
|
||||
|
||||
### 示例图片
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
"dcat/laravel-admin": "~2.0",
|
||||
"iidestiny/laravel-filesystem-oss": "^3.1",
|
||||
"overtrue/laravel-filesystem-qiniu": "^2.1",
|
||||
"freyo/flysystem-qcloud-cos-v5": "^2.0"
|
||||
"overtrue/laravel-filesystem-cos": "^3.1",
|
||||
"qcloud_sts/qcloud-sts-sdk": "^3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -5,7 +5,6 @@ namespace Weiwait\DcatVue;
|
||||
use Dcat\Admin\Extend\ServiceProvider;
|
||||
use Dcat\Admin\Admin;
|
||||
use Dcat\Admin\Form;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Weiwait\DcatVue\Field\DateRange;
|
||||
use Weiwait\DcatVue\Field\Distpicker;
|
||||
@@ -18,8 +17,6 @@ use Weiwait\DcatVue\Field\MultipleImage;
|
||||
use Weiwait\DcatVue\Field\MultipleSelect;
|
||||
use Weiwait\DcatVue\Field\Select;
|
||||
use Weiwait\DcatVue\Field\Tag;
|
||||
use Weiwait\DcatVue\Field\Vue;
|
||||
use Weiwait\DcatVue\Models\FilesystemConfig;
|
||||
|
||||
class DcatVueServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -75,51 +72,16 @@ class DcatVueServiceProvider extends ServiceProvider
|
||||
return;
|
||||
}
|
||||
|
||||
app()->booted(function () {
|
||||
config()->set('filesystems.disks.oss', [
|
||||
'access_key' => FilesystemConfig::get('oss_access_key'),
|
||||
'secret_key' => FilesystemConfig::get('oss_secret_key'),
|
||||
'endpoint' => FilesystemConfig::get('oss_endpoint'),
|
||||
'bucket' => FilesystemConfig::get('oss_bucket'),
|
||||
'driver' => 'oss',
|
||||
'root' => '',
|
||||
'isCName' => false,
|
||||
]);
|
||||
$configs = admin_setting_array('weiwait_filesystem');
|
||||
|
||||
config()->set('filesystems.disks.qiniu', [
|
||||
'access_key' => FilesystemConfig::get('qiniu_access_key'),
|
||||
'secret_key' => FilesystemConfig::get('qiniu_secret_key'),
|
||||
'domain' => FilesystemConfig::get('qiniu_domain'),
|
||||
'bucket' => FilesystemConfig::get('qiniu_bucket'),
|
||||
'driver' => 'qiniu',
|
||||
]);
|
||||
// dd($configs);
|
||||
|
||||
config()->set('filesystems.disks.cosv5', [
|
||||
'driver' => 'cosv5',
|
||||
'region' => FilesystemConfig::get('cos_region'),
|
||||
'credentials' => [
|
||||
'appId' => FilesystemConfig::get('cos_app_id'),
|
||||
'secretId' => FilesystemConfig::get('cos_secret_id'),
|
||||
'secretKey' => FilesystemConfig::get('cos_secret_key'),
|
||||
'token' => null,
|
||||
],
|
||||
'timeout' => 60,
|
||||
'connect_timeout' => 60,
|
||||
'bucket' => FilesystemConfig::get('cos_bucket'),
|
||||
'cdn' => FilesystemConfig::get('cos_cdn'),
|
||||
'scheme' => config('admin.https') ? 'https' : 'http',
|
||||
'read_from_cdn' => false,
|
||||
'cdn_key' => null,
|
||||
'encrypt' => false,
|
||||
]);
|
||||
|
||||
config()->set('filesystems.default', FilesystemConfig::get('disk', config('filesystems.default')));
|
||||
|
||||
config()->set('admin.auth.except', array_merge((array)config('admin.auth.except'), ['weiwait*']));
|
||||
app()->booted(function () use ($configs) {
|
||||
Helper::injectFilesystemConfig($configs);
|
||||
});
|
||||
|
||||
Event::listen('admin:booted', function () {
|
||||
config()->set('admin.upload.disk', FilesystemConfig::get('disk', config('admin.upload.disk')));
|
||||
Event::listen('admin:booted', function () use ($configs) {
|
||||
config()->set('admin.upload.disk', $configs['disk']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,21 +2,59 @@
|
||||
|
||||
namespace Weiwait\DcatVue\Forms;
|
||||
|
||||
use Dcat\Admin\Http\JsonResponse;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Weiwait\DcatVue\Models\FilesystemConfig as Model;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use OSS\Core\OssException;
|
||||
use OSS\Model\CorsConfig;
|
||||
use OSS\Model\CorsRule;
|
||||
|
||||
class FilesystemConfig extends Form
|
||||
{
|
||||
/**
|
||||
* Handle the form request.
|
||||
*
|
||||
* @param array $input
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(array $input)
|
||||
public function handle(array $input): JsonResponse
|
||||
{
|
||||
return Model::adminFormHandle($this, $input);
|
||||
try {
|
||||
$this->ossHandler($input);
|
||||
|
||||
admin_setting(['weiwait_filesystem' => $input]);
|
||||
|
||||
is_file(app()->getCachedConfigPath()) && Artisan::call('config:cache');
|
||||
} catch (\Throwable $exception) {
|
||||
return $this->response()->error($exception->getMessage());
|
||||
}
|
||||
|
||||
return $this->response()->success('修改成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws OssException
|
||||
*/
|
||||
protected function ossHandler(array $data)
|
||||
{
|
||||
$config = collect(admin_setting_array('weiwait_filesystem'));
|
||||
|
||||
$originChanged = collect($config->get('oss_allowed_origins', []))->diff($data['oss_allowed_origins']);
|
||||
$methodChanged = collect($config->get('oss_allowed_methods', []))->diff($data['oss_allowed_methods']);
|
||||
$masChanged = collect($config->get('oss_mas', 0))->diff($data['oss_mas']);
|
||||
|
||||
if ($originChanged || $methodChanged || $masChanged) {
|
||||
$rule = new CorsRule();
|
||||
$rule->setMaxAgeSeconds($data['oss_mas']);
|
||||
|
||||
foreach ($data['oss_allowed_methods'] as $method) {
|
||||
$rule->addAllowedMethod($method);
|
||||
}
|
||||
|
||||
foreach ($data['oss_allowed_origins'] as $origin) {
|
||||
$rule->addAllowedOrigin($origin);
|
||||
}
|
||||
|
||||
$config = new CorsConfig();
|
||||
$config->addRule($rule);
|
||||
$kernel = Storage::disk('oss')->getAdapter()->ossKernel();
|
||||
$kernel->putBucketCors($data['oss_bucket'], $config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,15 +63,20 @@ class FilesystemConfig extends Form
|
||||
public function form()
|
||||
{
|
||||
$this->radio('disk', '存储位置')
|
||||
->options(['public' => '本地', 'oss' => '阿里云', 'cosv5' => '腾讯云', 'qiniu' => '七牛云'])
|
||||
->options(['public' => '本地', 'oss' => '阿里云', 'cos' => '腾讯云', 'qiniu' => '七牛云'])
|
||||
->default('public')
|
||||
->when('oss', function (Form $form) {
|
||||
$form->text('oss_access_key', 'AccessKey');
|
||||
$form->text('oss_secret_key', 'SecretKey');
|
||||
$form->text('oss_bucket', 'Bucket');
|
||||
$form->text('oss_endpoint', 'Endpoint');
|
||||
|
||||
$form->vList('oss_allowed_origins', 'origins');
|
||||
$form->checkbox('oss_allowed_methods', 'methods')
|
||||
->options(array_combine(['GET', 'POST', 'PUT', 'DELETE', 'HEAD'], ['GET', 'POST', 'PUT', 'DELETE', 'HEAD']));
|
||||
$form->number('oss_mas', '缓存时间')->default(0);
|
||||
})
|
||||
->when('cosv5', function (Form $form) {
|
||||
->when('cos', function (Form $form) {
|
||||
$form->text('cos_app_id', 'Appid');
|
||||
$form->text('cos_secret_id', 'SecretId');
|
||||
$form->text('cos_secret_key', 'SecretKey');
|
||||
@@ -56,25 +99,6 @@ class FilesystemConfig extends Form
|
||||
*/
|
||||
public function default(): array
|
||||
{
|
||||
return Model::get([
|
||||
'disk',
|
||||
'oss_access_key',
|
||||
'oss_secret_key',
|
||||
'oss_endpoint',
|
||||
'oss_bucket',
|
||||
|
||||
'qiniu_access_key',
|
||||
'qiniu_secret_key',
|
||||
'qiniu_bucket',
|
||||
'qiniu_domain',
|
||||
|
||||
|
||||
'cos_app_id',
|
||||
'cos_secret_id',
|
||||
'cos_secret_key',
|
||||
'cos_region',
|
||||
'cos_bucket',
|
||||
'cos_cdn',
|
||||
]);
|
||||
return admin_setting_array('weiwait_filesystem');
|
||||
}
|
||||
}
|
||||
|
||||
107
src/Helper.php
Normal file
107
src/Helper.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue;
|
||||
|
||||
use QCloud\COSSTS\Sts;
|
||||
|
||||
class Helper
|
||||
{
|
||||
public static function injectFilesystemConfig(array $configs): void
|
||||
{
|
||||
$configs = collect($configs);
|
||||
|
||||
config()->set('filesystems.disks.oss', [
|
||||
'access_key' => $configs->get('oss_access_key'),
|
||||
'secret_key' => $configs->get('oss_secret_key'),
|
||||
'endpoint' => $configs->get('oss_endpoint'),
|
||||
'bucket' => $configs->get('oss_bucket'),
|
||||
'driver' => 'oss',
|
||||
'root' => '',
|
||||
'isCName' => false,
|
||||
]);
|
||||
|
||||
config()->set('filesystems.disks.qiniu', [
|
||||
'access_key' => $configs->get('qiniu_access_key'),
|
||||
'secret_key' => $configs->get('qiniu_secret_key'),
|
||||
'domain' => $configs->get('qiniu_domain'),
|
||||
'bucket' => $configs->get('qiniu_bucket'),
|
||||
'driver' => 'qiniu',
|
||||
]);
|
||||
|
||||
config()->set('filesystems.disks.cosv5', [
|
||||
'driver' => 'cosv5',
|
||||
'region' => $configs->get('cos_region'),
|
||||
'credentials' => [
|
||||
'appId' => $configs->get('cos_app_id'),
|
||||
'secretId' => $configs->get('cos_secret_id'),
|
||||
'secretKey' => $configs->get('cos_secret_key'),
|
||||
'token' => null,
|
||||
],
|
||||
'timeout' => 60,
|
||||
'connect_timeout' => 60,
|
||||
'bucket' => $configs->get('cos_bucket'),
|
||||
'cdn' => $configs->get('cos_cdn'),
|
||||
'scheme' => config('admin.https') ? 'https' : 'http',
|
||||
'read_from_cdn' => false,
|
||||
'cdn_key' => null,
|
||||
'encrypt' => false,
|
||||
]);
|
||||
|
||||
config()->set('filesystems.disks.cos', [
|
||||
'driver' => 'cos',
|
||||
'region' => $configs->get('cos_region'),
|
||||
'app_id' => $configs->get('cos_app_id'),
|
||||
'secret_id' => $configs->get('cos_secret_id'),
|
||||
'secret_key' => $configs->get('cos_secret_key'),
|
||||
'token' => null,
|
||||
'bucket' => $configs->get('cos_bucket'),
|
||||
'cdn' => $configs->get('cos_cdn'),
|
||||
'use_https' => config('admin.https'),
|
||||
'read_from_cdn' => false,
|
||||
'cdn_key' => null,
|
||||
'encrypt' => false,
|
||||
'guzzle' => [
|
||||
'timeout' => 60,
|
||||
'connect_timeout' => 60,
|
||||
],
|
||||
]);
|
||||
|
||||
config()->set('filesystems.default', $configs->get('disk'));
|
||||
|
||||
config()->set('admin.upload.disk', $configs->get('disk'));
|
||||
}
|
||||
|
||||
public static function getCosAuth(): array
|
||||
{
|
||||
// 配置参数
|
||||
$config = array(
|
||||
'url' => 'https://sts.tencentcloudapi.com/',
|
||||
'domain' => 'sts.tencentcloudapi.com',
|
||||
'proxy' => '',
|
||||
'secretId' => config('filesystems.disks.cos.secret_id'), // 固定密钥
|
||||
'secretKey' => config('filesystems.disks.cos.secret_key'), // 固定密钥
|
||||
'bucket' => config('filesystems.disks.cos.bucket') . '-' . config('filesystems.disks.cos.app_id'), // 换成你的 bucket
|
||||
'region' => config('filesystems.disks.cos.region'), // 换成 bucket 所在园区
|
||||
'durationSeconds' => 1800, // 密钥有效期
|
||||
// 允许操作(上传)的对象前缀,可以根据自己网站的用户登录态判断允许上传的目录,例子: user1/* 或者 * 或者a.jpg
|
||||
// 请注意当使用 * 时,可能存在安全风险,详情请参阅:https://cloud.tencent.com/document/product/436/40265
|
||||
'allowPrefix' => '*',
|
||||
// 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
|
||||
'allowActions' => array(
|
||||
// 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
|
||||
// 简单上传
|
||||
'name/cos:PutObject',
|
||||
'name/cos:PostObject',
|
||||
// 分片上传
|
||||
'name/cos:InitiateMultipartUpload',
|
||||
'name/cos:ListMultipartUploads',
|
||||
'name/cos:ListParts',
|
||||
'name/cos:UploadPart',
|
||||
'name/cos:CompleteMultipartUpload'
|
||||
)
|
||||
);
|
||||
|
||||
// 获取临时密钥,计算签名
|
||||
return (new Sts())->getTempKeys($config);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ use Dcat\Admin\Layout\Content;
|
||||
use Dcat\Admin\Admin;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Qiniu\Config;
|
||||
use Weiwait\DcatVue\Forms\FilesystemConfig;
|
||||
use Weiwait\DcatVue\Helper;
|
||||
use Weiwait\DcatVue\Models\ChinaArea;
|
||||
use Weiwait\DcatVue\Models\WeiwaitUpload;
|
||||
|
||||
@@ -59,9 +61,12 @@ class DcatVueController extends Controller
|
||||
];
|
||||
case 'cos':
|
||||
case 'cosv5':
|
||||
$url = parse_url(Storage::disk('cosv5')->url(''));
|
||||
$url = parse_url(Storage::disk('cos')->url(''));
|
||||
$auth = Helper::getCosAuth();
|
||||
return [
|
||||
'auth' => Storage::disk('cosv5')->getFederationTokenV3($request['filename']),
|
||||
'auth' => [
|
||||
'token' => Arr::get($auth, 'credentials.sessionToken'),
|
||||
],
|
||||
'host' => $url['scheme'] . '://' . $url['host'] . '/' . $request['filename'],
|
||||
];
|
||||
default:
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Models;
|
||||
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FilesystemConfig extends Model
|
||||
{
|
||||
const INDEPENDENT = "independent";
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* key 或者数组 keys 获取配置
|
||||
*/
|
||||
public static function get($keys, $default = '')
|
||||
{
|
||||
if (is_array($keys)) {
|
||||
$data = [];
|
||||
foreach ($keys as $key) {
|
||||
$data[$key] = self::get($key);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (null == $value = Cache::get('setting.wechat.config.' . $keys)) {
|
||||
$value = self::query()->where('key', $keys)->value('value');
|
||||
|
||||
Cache::put('setting.wechat.config.' . $keys, $value, now()->addDay());
|
||||
}
|
||||
|
||||
return $value ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置项
|
||||
*/
|
||||
public static function set(array $settings)
|
||||
{
|
||||
foreach ($settings as $key => $value) {
|
||||
self::query()->updateOrCreate(['key' => $key], ['value' => $value ?? '']);
|
||||
|
||||
Cache::put('setting.wechat.config.' . $key, $value, now()->addDay());
|
||||
}
|
||||
}
|
||||
|
||||
public static function adminFormHandle(Form $form, array $input): \Dcat\Admin\Http\JsonResponse
|
||||
{
|
||||
try {
|
||||
self::set($input);
|
||||
} catch (\Exception $exception) {
|
||||
return $form
|
||||
->response()
|
||||
->error($exception->getMessage());
|
||||
}
|
||||
|
||||
return $form
|
||||
->response()
|
||||
->success('修改成功');
|
||||
}
|
||||
}
|
||||
@@ -3,44 +3,16 @@
|
||||
namespace Weiwait\DcatVue;
|
||||
|
||||
use Dcat\Admin\Extend\Setting as Form;
|
||||
use Dcat\Admin\Models\Menu;
|
||||
use Weiwait\DcatVue\Models\FilesystemConfig;
|
||||
|
||||
class Setting extends Form
|
||||
{
|
||||
public function form()
|
||||
{
|
||||
$this->switch('independent', '独立菜单')
|
||||
->default(FilesystemConfig::get(FilesystemConfig::INDEPENDENT, false));
|
||||
$this->text('menu_name', '菜单名称')->default(__('文件存储'));
|
||||
// Todo
|
||||
}
|
||||
|
||||
public function handle(array $input): \Dcat\Admin\Http\JsonResponse
|
||||
public function handle(array $input)
|
||||
{
|
||||
FilesystemConfig::set(['independent' => (boolean)$input['independent']]);
|
||||
|
||||
/** @var Menu $menu */
|
||||
$menu = config('admin.database.menu_model');
|
||||
|
||||
if ($input['independent']) {
|
||||
$res = $menu::query()->updateOrCreate(
|
||||
['uri' => 'dcat-filesystem-config'],
|
||||
[
|
||||
'title' => $input['menu_name'],
|
||||
'show' => 1,
|
||||
'icon' => 'fa-envelope',
|
||||
]
|
||||
);
|
||||
|
||||
FilesystemConfig::set(['independent_menu_id' => $res->getKey()]);
|
||||
|
||||
return $this->response()->success('菜单已生成')->refresh();
|
||||
} else {
|
||||
if ($menu = $menu::query()->find(FilesystemConfig::get('independent_menu_id'))) {
|
||||
$menu->delete();
|
||||
}
|
||||
|
||||
return $this->response()->success('菜单已删除')->refresh();
|
||||
}
|
||||
// Todo
|
||||
}
|
||||
}
|
||||
|
||||
34
updates/drop_filesystem_configs_table.php
Normal file
34
updates/drop_filesystem_configs_table.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateFielsystemConfig extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::dropIfExists('filesystem_configs');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::create('filesystem_configs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('key', 50)->unique()->comment('配置key');
|
||||
$table->string('name')->default('')->comment('配置名称');
|
||||
$table->text('value')->comment('配置值');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -63,4 +63,8 @@ return [
|
||||
'一些优化',
|
||||
'新增多选组件',
|
||||
],
|
||||
'2.5.0' => [
|
||||
'一些优化',
|
||||
'drop_filesystem_configs_table.php',
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user