From 00c10321628bd27a2a546323651792cba3f8d354 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 May 2024 20:26:52 +0800 Subject: [PATCH] 1 --- addons/alioss/controller/Index.php | 40 +++++++++++++++++++++++++ application/admin/command/UserLevel.php | 36 +++++++++++++++++++++- application/api/controller/Article.php | 4 +-- application/api/controller/Company.php | 2 +- application/api/controller/Notice.php | 4 +-- application/command.php | 1 + application/common.php | 8 ++++- 7 files changed, 88 insertions(+), 7 deletions(-) diff --git a/addons/alioss/controller/Index.php b/addons/alioss/controller/Index.php index 9f77dfa..6e5e697 100755 --- a/addons/alioss/controller/Index.php +++ b/addons/alioss/controller/Index.php @@ -279,4 +279,44 @@ class Index extends Controller } } + + /** + * 附件库图片同步阿里云OSS + */ + public function uploadLocalToOss() + { + $config = get_addon_config('alioss'); + if ($config['uploadmode'] != 'server') { + $this->error("无需执行该操作"); + } + if (!$config['accessKeyId'] || !$config['accessKeySecret']) { + $this->error("请先配置好阿里云OSS"); + } + if (!class_exists('\OSS\OssClient')) { + $this->error("请安装composer require symfony/polyfill-php72"); + } + + $oss = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']); + + $attachments = Attachment::where('storage', 'local') + ->where('createtime', '>', '1711610369')->select(); + $i = 1; + foreach ($attachments as $attachment) { + $filePath = ROOT_PATH . 'public' . $attachment->url; +// var_dump($filePath); + + try { +// var_dump($config); + $ret = $oss->uploadFile($config['bucket'], ltrim($attachment->url, "/"), $filePath); + sleep(1); + $i++; + //成功不做任何操作 + } catch (\Exception $e) { + \think\Log::write($e->getMessage()); + $this->error($e->getMessage()); + } + } + $this->success("同步成功 {$i}条数据"); + } + } diff --git a/application/admin/command/UserLevel.php b/application/admin/command/UserLevel.php index b2a4f0e..dc7d3a7 100755 --- a/application/admin/command/UserLevel.php +++ b/application/admin/command/UserLevel.php @@ -4,6 +4,8 @@ namespace app\admin\command; use app\admin\model\AuthRule; use app\admin\model\User; +use app\common\model\Attachment; +use OSS\OssClient; use ReflectionClass; use ReflectionMethod; use think\Cache; @@ -32,8 +34,40 @@ class UserLevel extends Command protected function execute(Input $input, Output $output) { + $config = get_addon_config('alioss'); + if ($config['uploadmode'] != 'server') { + throw new Exception("无需执行该操作"); + } + if (!$config['accessKeyId'] || !$config['accessKeySecret']) { + throw new Exception("请先配置好阿里云OSS"); + } + if (!class_exists('\OSS\OssClient')) { + throw new Exception("请安装composer require symfony/polyfill-php72"); + } + + $oss = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']); + + $attachments = Attachment::where('storage', 'local') + ->where('createtime', '>', '1711610369')->select(); + $i = 1; + foreach ($attachments as $attachment) { + $filePath = ROOT_PATH . 'public' . $attachment->url; +// var_dump($filePath); + + try { + echo $filePath.PHP_EOL; + $ret = $oss->uploadFile($config['bucket'], ltrim($attachment->url, "/"), $filePath); + var_dump($ret); + sleep(1); + $i++; + //成功不做任何操作 + } catch (\Exception $e) { + \think\Log::write($e->getMessage()); + throw new Exception($e->getMessage()); + } + } + $output->info("同步成功 {$i}条数据"); - $output->info("Build Successed!"); } /** diff --git a/application/api/controller/Article.php b/application/api/controller/Article.php index af9b197..c8cccfd 100755 --- a/application/api/controller/Article.php +++ b/application/api/controller/Article.php @@ -27,7 +27,7 @@ class Article extends Api ->paginate(); foreach ($list as &$row) { - $row['image'] = $this->request->domain().$row['image']; + $row['image'] = formatImage($row['image']); } $this->success('营销配置', $list); } @@ -41,7 +41,7 @@ class Article extends Api ->where('status', 1) ->where('id', $id) ->find(); - $detail['image'] = $this->request->domain().$detail['image']; + $detail['image'] = formatImage($detail['image']); $this->success('success', $detail); } } \ No newline at end of file diff --git a/application/api/controller/Company.php b/application/api/controller/Company.php index 64a55b4..ff1ac4f 100755 --- a/application/api/controller/Company.php +++ b/application/api/controller/Company.php @@ -20,7 +20,7 @@ class Company extends Api public function list() { $list = $this->model->where('status', 1)->paginate(10); foreach ($list as &$row) { - $row['image'] = $this->request->domain().$row['image']; + $row['image'] = formatImage($row['image']); } $this->success('资质列表', $list); } diff --git a/application/api/controller/Notice.php b/application/api/controller/Notice.php index 05ac644..e8bf892 100755 --- a/application/api/controller/Notice.php +++ b/application/api/controller/Notice.php @@ -18,7 +18,7 @@ class Notice extends Api { public function list() { $list = $this->model->where('status', 1)->paginate(10); foreach ($list as &$row) { - $row['image'] = $this->request->domain().$row['image']; + $row['image'] = formatImage($row['image']); } $this->success('公告列表', $list); } @@ -32,7 +32,7 @@ class Notice extends Api { ->where('status', 1) ->where('id', $id) ->find(); - $detail['image'] = $this->request->domain().$detail['image']; + $detail['image'] = formatImage($detail['image']); $this->success('success', $detail); } } \ No newline at end of file diff --git a/application/command.php b/application/command.php index ab4178a..93bbb5f 100755 --- a/application/command.php +++ b/application/command.php @@ -17,4 +17,5 @@ return [ 'app\admin\command\Min', 'app\admin\command\Addon', 'app\admin\command\Api', + 'app\admin\command\UserLevel', ]; diff --git a/application/common.php b/application/common.php index 2788fd4..4640bf8 100755 --- a/application/common.php +++ b/application/common.php @@ -562,7 +562,13 @@ EOT; } function formatImage($url) { - return request()->domain().$url; + $config = get_addon_config('alioss'); + if ($config['apiupload'] == 1) { + $url = $config['cdnurl'] . $url; + } else { + $url = request()->domain().$url; + } + return $url; } function dd($data) {