新增附件事件

This commit is contained in:
JaguarJack 2020-06-01 21:25:46 +08:00
parent 39c0ec4c48
commit fe6ea82014
3 changed files with 67 additions and 36 deletions

View File

@ -0,0 +1,17 @@
<?php
namespace catchAdmin\system\events;
use catchAdmin\system\model\Attachments;
class AttachmentEvent
{
public function handle($params)
{
$file = $params['file'];
unset($params['file']);
Attachments::store($params, $file);
}
}

View File

@ -89,6 +89,10 @@ return [
'operateLog' => [ 'operateLog' => [
OperateLogEvent::class, OperateLogEvent::class,
], ],
// 附件操作
'attachment' => [
\catchAdmin\system\events\AttachmentEvent::class,
],
// 路由加载 // 路由加载
'RouteLoaded' => [ 'RouteLoaded' => [
LoadModuleRoutes::class LoadModuleRoutes::class

View File

@ -63,10 +63,12 @@ class CatchUpload
if ($path) { if ($path) {
$url = self::getCloudDomain($this->getDriver()) . $path; $url = self::getCloudDomain($this->getDriver()) . $path;
Attachments::create(array_merge([ event('attachment', [
'path' => $path, 'path' => $path,
'url' => $url, 'url' => $url,
], $this->data($file))); 'driver' => $this->getDriver(),
'file' => $file,
]);
return $url; return $url;
} }
@ -215,7 +217,12 @@ class CatchUpload
return $this; return $this;
} }
/**
* 初始化配置
*
* @time 2020年06月01日
* @return void
*/
protected function initUploadConfig() protected function initUploadConfig()
{ {
$configModel = app(Config::class); $configModel = app(Config::class);
@ -224,41 +231,44 @@ class CatchUpload
if ($upload) { if ($upload) {
$disk = app()->config->get('filesystem.disks'); $disk = app()->config->get('filesystem.disks');
$uploadConfigs = $configModel->getConfig($upload->id);
// 读取上传可配置数据
foreach ($uploadConfigs as $key => &$config) {
// $disk[$key]['type'] = $key;
// 腾讯云配置处理
if (strtolower($key) == 'qcloud') {
$config['credentials'] = [
'appId' => $config['app_id'] ?? '',
'secretKey' => $config['secret_key'] ?? '',
'secretId' => $config['secret_id'] ?? '',
];
$readFromCdn = $config['read_from_cdn'] ?? false;
$config['read_from_cdn'] = $readFromCdn ? true : false;
}
// OSS 配置
if (strtolower($key) == 'oss') {
$isCname = $config['is_cname'] ?? false;
$config['is_cname'] = $isCname ? true : false;
}
}
// 合并数组 $uploadConfigs = $configModel->getConfig($upload->id);
array_walk($disk, function (&$item, $key) use ($uploadConfigs){ if (!empty($uploadConfigs)) {
if (!in_array($key, ['public', 'local'])) { // 读取上传可配置数据
if ($uploadConfigs[$key] ?? false) { foreach ($uploadConfigs as $key => &$config) {
foreach ($uploadConfigs[$key] as $k => $value) { // $disk[$key]['type'] = $key;
$item[$k] = $value; // 腾讯云配置处理
} if (strtolower($key) == 'qcloud') {
$config['credentials'] = [
'appId' => $config['app_id'] ?? '',
'secretKey' => $config['secret_key'] ?? '',
'secretId' => $config['secret_id'] ?? '',
];
$readFromCdn = $config['read_from_cdn'] ?? false;
$config['read_from_cdn'] = $readFromCdn ? true : false;
}
// OSS 配置
if (strtolower($key) == 'oss') {
$isCname = $config['is_cname'] ?? false;
$config['is_cname'] = $isCname ? true : false;
} }
} }
});
// 重新分配配置 // 合并数组
app()->config->set([ array_walk($disk, function (&$item, $key) use ($uploadConfigs) {
'disk' => $disk, if (!in_array($key, ['public', 'local'])) {
], 'filesystem'); if ($uploadConfigs[$key] ?? false) {
foreach ($uploadConfigs[$key] as $k => $value) {
$item[$k] = $value;
}
}
}
});
// 重新分配配置
app()->config->set([
'disk' => $disk,
], 'filesystem');
}
} }
} }