From 3f48492089da47d76e2e52a2c35777ea196e14e2 Mon Sep 17 00:00:00 2001 From: augushong Date: Fri, 25 Oct 2024 20:47:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E5=AD=97=E6=AE=B5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/command/admin/MigrateFileData.php | 14 ++ .../base/admin/model/SystemUploadfileBase.php | 1 + .../command/admin/MigrateFileDataBase.php | 136 ++++++++++++++++++ extend/base/common/model/BaseModelBase.php | 5 + extend/think/UlthonAdminService.php | 2 + 5 files changed, 158 insertions(+) create mode 100644 app/common/command/admin/MigrateFileData.php create mode 100644 extend/base/common/command/admin/MigrateFileDataBase.php diff --git a/app/common/command/admin/MigrateFileData.php b/app/common/command/admin/MigrateFileData.php new file mode 100644 index 0000000..c9ad5c3 --- /dev/null +++ b/app/common/command/admin/MigrateFileData.php @@ -0,0 +1,14 @@ +setName('admin:migrate:file:data') + ->setDescription('将数据库中的文件地址从老地址更新到新地址,只处理数据库数据,不会实际迁移文件'); + } + + protected function execute(Input $input, Output $output) + { + if (empty($this->toDomain)) { + $output->error('请指定新的文件域名'); + + return; + } + + $this->fromDomain = array_filter($this->fromDomain, function ($domain) { + return !empty($domain); + }); + + if (empty($this->fromDomain)) { + $output->error('请指定老的域名'); + + return; + } + + // 指令输出 + $model_files = []; + + foreach ($this->modelPath as $model_path) { + $model_files = array_merge($model_files, glob($model_path . '/*.php')); + } + + $model_class = []; + + foreach ($model_files as $file) { + $class_name = str_replace('.php', '', $file); + $class_name = str_replace('/', '\\', $class_name); + $model_class[] = $class_name; + } + + $field_map = []; + + foreach ($model_class as $class_name) { + if (!isset($field_map[$class_name])) { + $field_map[$class_name] = []; + } + $field_list_file = $class_name::FIELD_LIST_FILE; + if (!empty($field_list_file)) { + $field_map[$class_name] = array_merge($field_map[$class_name], $field_list_file); + } + + $field_list_content = $class_name::FIELD_LIST_CONTENT; + if (!empty($field_list_content)) { + $field_map[$class_name] = array_merge($field_map[$class_name], $field_list_content); + } + } + + $field_map = array_filter($field_map); + + foreach ($field_map as $model_class => $field_list) { + $output->writeln("开始处理 {$model_class}"); + $model_cursor = $model_class::cursor(); + foreach ($model_cursor as $modle_item) { + foreach ($field_list as $field_name) { + $field_value = $modle_item->{$field_name}; + $new_field_value = $this->replaceDomain($field_value); + if ($field_value != $new_field_value) { + $modle_item->{$field_name} = $new_field_value; + $modle_item->save(); + $output->writeln("{$model_class} - {$field_name} - {$field_value} => {$new_field_value}"); + } + } + } + } + } + + private function replaceDomain($field_value) + { + if (empty($field_value)) { + return $field_value; + } + + $from_domain_list = []; + foreach ($this->fromDomain as $from_domain) { + if (!str_starts_with($from_domain, 'http://') && !str_starts_with($from_domain, 'https://')) { + $from_domain_list[] = 'http://' . $from_domain; + $from_domain_list[] = 'https://' . $from_domain; + } else { + $from_domain_list[] = $from_domain; + } + } + + $to_domain = $this->toDomain; + $field_value = str_replace($from_domain_list, $to_domain, $field_value); + + return $field_value; + } +} diff --git a/extend/base/common/model/BaseModelBase.php b/extend/base/common/model/BaseModelBase.php index ac6ea08..fabd886 100644 --- a/extend/base/common/model/BaseModelBase.php +++ b/extend/base/common/model/BaseModelBase.php @@ -10,6 +10,11 @@ use think\Model; class BaseModelBase extends Model { + + public const FIELD_LIST_FILE = []; + + public const FIELD_LIST_CONTENT = []; + /** * 自动清除的缓存值 * diff --git a/extend/think/UlthonAdminService.php b/extend/think/UlthonAdminService.php index e44ad96..10f073b 100644 --- a/extend/think/UlthonAdminService.php +++ b/extend/think/UlthonAdminService.php @@ -2,6 +2,7 @@ namespace think; +use app\common\command\admin\MigrateFileData; use app\common\command\Test; use app\common\event\AdminLoginSuccess\LogEvent; use app\common\event\AdminLoginType\DemoEvent; @@ -50,6 +51,7 @@ class UlthonAdminService extends Service // 绑定命令行 $this->commands([ Test::class, + MigrateFileData::class ]); // 绑定标识容器