mirror of
https://github.com/cjango/dcat-vue.git
synced 2025-12-06 22:40:03 +08:00
first commit
This commit is contained in:
95
src/DcatVueServiceProvider.php
Normal file
95
src/DcatVueServiceProvider.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue;
|
||||
|
||||
use Dcat\Admin\Extend\ServiceProvider;
|
||||
use Dcat\Admin\Admin;
|
||||
use Dcat\Admin\Form;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Weiwait\DcatVue\Field\File;
|
||||
use Weiwait\DcatVue\Field\MultipleFile;
|
||||
use Weiwait\DcatVue\Field\Vue;
|
||||
use Weiwait\DcatVue\Models\FilesystemConfig;
|
||||
|
||||
class DcatVueServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected $js = [
|
||||
'js/index.js',
|
||||
];
|
||||
protected $css = [
|
||||
'css/index.css',
|
||||
];
|
||||
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$this->hackConfigs();
|
||||
|
||||
// Form::extend('vue', Vue::class);
|
||||
Form::extend('file', File::class);
|
||||
Form::extend('multipleFile', MultipleFile::class);
|
||||
|
||||
Admin::asset()->css(Admin::asset()->getAlias('@weiwait.dcat-vue')['css']);
|
||||
|
||||
$this->publishable();
|
||||
}
|
||||
|
||||
public function settingForm()
|
||||
{
|
||||
return new Setting($this);
|
||||
}
|
||||
|
||||
protected function hackConfigs()
|
||||
{
|
||||
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,
|
||||
]);
|
||||
|
||||
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',
|
||||
]);
|
||||
|
||||
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')));
|
||||
});
|
||||
|
||||
Event::listen('admin:booted', function () {
|
||||
config()->set('admin.upload.disk', FilesystemConfig::get('disk', config('admin.upload.disk')));
|
||||
});
|
||||
}
|
||||
}
|
||||
66
src/Field/File.php
Normal file
66
src/Field/File.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Field;
|
||||
|
||||
use Dcat\Admin\Form\Field;
|
||||
use Weiwait\DcatVue\Models\WeiwaitUpload;
|
||||
|
||||
class File extends Field\File
|
||||
{
|
||||
protected $view = 'weiwait.dcat-vue::file';
|
||||
protected string $disk;
|
||||
|
||||
protected function prepareInputValue($file)
|
||||
{
|
||||
WeiwaitUpload::query()->whereIn('name', (array) $file)->delete();
|
||||
|
||||
return parent::prepareInputValue($file);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
if (!$this->shouldRender()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->setDefaultClass();
|
||||
|
||||
$this->callComposing();
|
||||
|
||||
$this->withScript();
|
||||
|
||||
$this->withProvides();
|
||||
|
||||
$this->addVariables([
|
||||
'provides' => $this->variables(),
|
||||
]);
|
||||
|
||||
return view($this->view(), $this->variables());
|
||||
}
|
||||
|
||||
protected function formatAttributes()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
protected function withProvides()
|
||||
{
|
||||
$disk = config('admin.upload.disk', config('filesystems.default'));
|
||||
|
||||
$this->addVariables([
|
||||
'component' => 'File',
|
||||
'multiple' => false,
|
||||
'disk' => $this->disk ?? $disk,
|
||||
'dir' => $this->getDirectory(),
|
||||
'uploaded_url' => route('dcat.admin.weiwait.file.uploaded'),
|
||||
'obs_config_url' => route('dcat.admin.weiwait.file.obs-config'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function disk($disk): self
|
||||
{
|
||||
$this->disk = $disk;
|
||||
|
||||
return parent::disk($disk);
|
||||
}
|
||||
}
|
||||
66
src/Field/MultipleFile.php
Normal file
66
src/Field/MultipleFile.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Field;
|
||||
|
||||
use Dcat\Admin\Form\Field;
|
||||
use Weiwait\DcatVue\Models\WeiwaitUpload;
|
||||
|
||||
class MultipleFile extends Field\MultipleFile
|
||||
{
|
||||
protected $view = 'weiwait.dcat-vue::file';
|
||||
protected string $disk;
|
||||
|
||||
protected function prepareInputValue($file): array
|
||||
{
|
||||
WeiwaitUpload::query()->whereIn('name', (array) $file)->delete();
|
||||
|
||||
return parent::prepareInputValue($file);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
if (!$this->shouldRender()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->setDefaultClass();
|
||||
|
||||
$this->callComposing();
|
||||
|
||||
$this->withScript();
|
||||
|
||||
$this->withProvides();
|
||||
|
||||
$this->addVariables([
|
||||
'provides' => $this->variables(),
|
||||
]);
|
||||
|
||||
return view($this->view(), $this->variables());
|
||||
}
|
||||
|
||||
protected function formatAttributes()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
protected function withProvides()
|
||||
{
|
||||
$disk = config('admin.upload.disk', config('filesystems.default'));
|
||||
|
||||
$this->addVariables([
|
||||
'component' => 'File',
|
||||
'multiple' => true,
|
||||
'disk' => $this->disk ?? $disk,
|
||||
'dir' => $this->getDirectory(),
|
||||
'uploaded_url' => route('dcat.admin.weiwait.file.uploaded'),
|
||||
'obs_config_url' => route('dcat.admin.weiwait.file.obs-config'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function disk($disk): self
|
||||
{
|
||||
$this->disk = $disk;
|
||||
|
||||
return parent::disk($disk);
|
||||
}
|
||||
}
|
||||
100
src/Field/Vue.php
Normal file
100
src/Field/Vue.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Field;
|
||||
|
||||
use Dcat\Admin\Form\Field;
|
||||
use Dcat\Admin\Support\Helper;
|
||||
use Dcat\Admin\Widgets\Checkbox as WidgetCheckbox;
|
||||
|
||||
class Vue extends Field\Checkbox
|
||||
{
|
||||
protected $view = 'weiwait.dcat-vue::index';
|
||||
|
||||
protected array $watch = [];
|
||||
|
||||
public function render()
|
||||
{
|
||||
$this->checkboxRender();
|
||||
$this->selectRender();
|
||||
|
||||
if (! $this->shouldRender()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->setDefaultClass();
|
||||
|
||||
$this->callComposing();
|
||||
|
||||
$this->withScript();
|
||||
|
||||
$this->addVariables([
|
||||
'checked' => $this->checked,
|
||||
'watch' => $this->watch,
|
||||
]);
|
||||
|
||||
$this->addVariables([
|
||||
'provides' => $this->variables(),
|
||||
]);
|
||||
|
||||
return view($this->view(), $this->variables());
|
||||
}
|
||||
|
||||
protected function formatAttributes()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
public function disabled($disabled): Vue
|
||||
{
|
||||
$this->addVariables([
|
||||
'disabled' => (array) $disabled
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function watch($column, $handler): Vue
|
||||
{
|
||||
$this->watch[] = [
|
||||
'type' => "$column:change",
|
||||
'handler' => $handler,
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function checkboxRender()
|
||||
{
|
||||
if ($this->options instanceof \Closure) {
|
||||
$this->options(
|
||||
$this->options->call($this->values(), $this->value(), $this)
|
||||
);
|
||||
}
|
||||
|
||||
$this->addCascadeScript();
|
||||
}
|
||||
|
||||
protected function selectRender()
|
||||
{
|
||||
$this->addDefaultConfig([
|
||||
'allowClear' => true,
|
||||
'placeholder' => [
|
||||
'id' => '',
|
||||
'text' => $this->placeholder(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->formatOptions();
|
||||
|
||||
$this->addVariables([
|
||||
'options' => $this->options,
|
||||
'groups' => $this->groups,
|
||||
'configs' => $this->config,
|
||||
'cascadeScript' => $this->getCascadeScript(),
|
||||
]);
|
||||
|
||||
$this->initSize();
|
||||
|
||||
$this->attribute('data-value', implode(',', Helper::array($this->value())));
|
||||
}
|
||||
}
|
||||
80
src/Forms/FilesystemConfig.php
Normal file
80
src/Forms/FilesystemConfig.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Forms;
|
||||
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Weiwait\DcatVue\Models\FilesystemConfig as Model;
|
||||
|
||||
class FilesystemConfig extends Form
|
||||
{
|
||||
/**
|
||||
* Handle the form request.
|
||||
*
|
||||
* @param array $input
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(array $input)
|
||||
{
|
||||
return Model::adminFormHandle($this, $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a form here.
|
||||
*/
|
||||
public function form()
|
||||
{
|
||||
$this->radio('disk', '存储位置')
|
||||
->options(['public' => '本地', 'oss' => '阿里云', 'cosv5' => '腾讯云', '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');
|
||||
})
|
||||
->when('cosv5', function (Form $form) {
|
||||
$form->text('cos_app_id', 'Appid');
|
||||
$form->text('cos_secret_id', 'SecretId');
|
||||
$form->text('cos_secret_key', 'SecretKey');
|
||||
$form->text('cos_region', 'Region');
|
||||
$form->text('cos_bucket', 'Bucket');
|
||||
$form->text('cos_cdn', 'CDN');
|
||||
})
|
||||
->when('qiniu', function (Form $form) {
|
||||
$form->text('qiniu_access_key', 'AccessKey');
|
||||
$form->text('qiniu_secret_key', 'SecretKey');
|
||||
$form->text('qiniu_bucket', 'Bucket');
|
||||
$form->text('qiniu_domain', '域名');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The data of the form.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
87
src/Http/Controllers/DcatVueController.php
Normal file
87
src/Http/Controllers/DcatVueController.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Http\Controllers;
|
||||
|
||||
use Dcat\Admin\Layout\Content;
|
||||
use Dcat\Admin\Admin;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Qiniu\Config;
|
||||
use Weiwait\DcatVue\Forms\FilesystemConfig;
|
||||
use Weiwait\DcatVue\Models\WeiwaitUpload;
|
||||
|
||||
class DcatVueController extends Controller
|
||||
{
|
||||
public function index(Content $content)
|
||||
{
|
||||
return $content
|
||||
->title('Title')
|
||||
->description('Description')
|
||||
->body(Admin::view('weiwait.dcat-vue::index'));
|
||||
}
|
||||
|
||||
public function uploaded(Request $request)
|
||||
{
|
||||
WeiwaitUpload::clearUnusedFiles();
|
||||
|
||||
WeiwaitUpload::query()
|
||||
->insert(array_map(fn($item) => ['name' => $item, 'created_at' => now(), 'disk' => $request['disk']],
|
||||
(array)$request['files']
|
||||
));
|
||||
}
|
||||
|
||||
public function obsConfig(Request $request)
|
||||
{
|
||||
switch ($request['disk']) {
|
||||
case 'oss':
|
||||
return json_decode(
|
||||
Storage::disk('oss')->getAdapter()->signatureConfig(prefix: config('admin.upload.directory.file'), expire: 3600),
|
||||
true
|
||||
);
|
||||
|
||||
case 'qiniu':
|
||||
$url = (new Config())->getUpHost(
|
||||
config('filesystems.disks.qiniu.access_key'),
|
||||
config('filesystems.disks.qiniu.bucket')
|
||||
);
|
||||
return [
|
||||
'token' => Storage::disk('qiniu')->getAdapter()->getUploadToken($request['filename'], 3600),
|
||||
'host' => $url,
|
||||
];
|
||||
case 'cos':
|
||||
case 'cosv5':
|
||||
$url = parse_url(Storage::disk('cosv5')->url(''));
|
||||
return [
|
||||
'auth' => Storage::disk('cosv5')->getFederationTokenV3($request['filename']),
|
||||
'host' => $url['scheme'] . '://' . $url['host'] . '/' . $request['filename'],
|
||||
];
|
||||
default:
|
||||
return [
|
||||
'host' => route('dcat.admin.weiwait.file.upload'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function upload(Request $request)
|
||||
{
|
||||
if ($file = $request->file('file')) {
|
||||
$name = basename($request['key']);
|
||||
$path = dirname($request['key']);
|
||||
|
||||
Storage::disk()->putFileAs($path, $file, $name);
|
||||
|
||||
return [
|
||||
'key' => $request['key']
|
||||
];
|
||||
}
|
||||
|
||||
return response($request->all(), 400);
|
||||
}
|
||||
|
||||
public function filesystem(Content $content): Content
|
||||
{
|
||||
return $content
|
||||
->body(new FilesystemConfig());
|
||||
}
|
||||
}
|
||||
16
src/Http/routes.php
Normal file
16
src/Http/routes.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
use Weiwait\DcatVue\Http\Controllers;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
// 文件存储配置表单
|
||||
Route::get('dcat-filesystem-config', [Controllers\DcatVueController::class, 'filesystem']);
|
||||
// 各个驱动直传配置
|
||||
Route::get('weiwait/file/obs-config', [Controllers\DcatVueController::class, 'obsConfig'])
|
||||
->name('weiwait.file.obs-config');
|
||||
// 文件上传后回调
|
||||
Route::post('weiwait/file/uploaded', [Controllers\DcatVueController::class, 'uploaded'])
|
||||
->name('weiwait.file.uploaded');
|
||||
// 本地上传
|
||||
Route::post('weiwait/file/upload', [Controllers\DcatVueController::class, 'upload'])
|
||||
->name('weiwait.file.upload');
|
||||
64
src/Models/FilesystemConfig.php
Normal file
64
src/Models/FilesystemConfig.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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('修改成功');
|
||||
}
|
||||
}
|
||||
23
src/Models/WeiwaitUpload.php
Normal file
23
src/Models/WeiwaitUpload.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class WeiwaitUpload extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name'
|
||||
];
|
||||
|
||||
public static function clearUnusedFiles()
|
||||
{
|
||||
self::query()->where('created_at', '<', now()->subDay())
|
||||
->get()
|
||||
->map(function (self $file) {
|
||||
$file->delete();
|
||||
Storage::disk($file->disk)->delete($file->name);
|
||||
});
|
||||
}
|
||||
}
|
||||
45
src/Setting.php
Normal file
45
src/Setting.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Weiwait\DcatVue;
|
||||
|
||||
use Dcat\Admin\Extend\Setting as Form;
|
||||
use Dcat\Admin\Models\Menu;
|
||||
|
||||
class Setting extends Form
|
||||
{
|
||||
public function form()
|
||||
{
|
||||
$this->switch('independent', '独立菜单')
|
||||
->default(FilesystemConfig::get(FilesystemConfig::INDEPENDENT, false));
|
||||
$this->text('menu_name', '菜单名称')->default(__('文件存储'));
|
||||
}
|
||||
|
||||
public function handle(array $input): \Dcat\Admin\Http\JsonResponse
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user