From 7acca79fbba6ffc18c155e0968828c1d8809c21a Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Tue, 15 Apr 2025 16:28:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=95=B0=E6=8D=AE=E8=BF=81?= =?UTF-8?q?=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/DataMigration.php | 165 ++++++++++++++++++++++++++++------ 1 file changed, 140 insertions(+), 25 deletions(-) diff --git a/app/command/DataMigration.php b/app/command/DataMigration.php index 44cd2c7d..ece50487 100644 --- a/app/command/DataMigration.php +++ b/app/command/DataMigration.php @@ -31,27 +31,46 @@ class DataMigration extends Command $output->writeln($msg); }; - // 迁移tock产品分类 - // $this->productTcoCategory(); + try { + // 迁移tock产品分类 + // $this->productTcoCategory(); - // 迁移产品分类 - // $this->productCategory(); + // 迁移产品分类 + // $this->productCategory(); - // 迁移文章 - // $this->migrateArticle([ - // 1 => 2, - // 2 => 3, - // 33 => 4, - // 36 => 5, - // 16 => 7, - // 31 => 8, - // 32 => 9 - // ]); + // 迁移文章 + // $this->migrateArticle([ + // 1 => 2, + // 2 => 3, + // 33 => 4, + // 36 => 5, + // 16 => 7, + // 31 => 8, + // 32 => 9 + // ]); - // 迁移faq - // $this->migrateFaq(); + // 迁移faq + // $this->migrateFaq(); - $output->writeln('success'); + // 迁移附件下载 + $this->migrateAttachment([ + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 43 => 6, + 22 => 8, + 24 => 11, + 25 => 12, + 26 => 13, + 44 => 9, + 45 => 10 + ]); + $output->writeln('success'); + } catch(\Throwable $th) { + $output->writeln($th->getMessage() .':' . $th->getLine()); + } } // 迁移tco产品分类 @@ -145,7 +164,7 @@ class DataMigration extends Command foreach ($article as $v) { // 处理封面图片 $image = ''; - $ret = $uploadMgr->upload($uploadMgr->download($v['picture']), 'image'); + $ret = $uploadMgr->upload($uploadMgr->download($v['picture']), 'image', 'article'); if ($ret['code'] == 0) { $image = $ret['data']['path']; } else { @@ -200,14 +219,14 @@ class DataMigration extends Command { $faq = Db::connect('old') ->name('fq') - ->where('id', '>=', 10) + ->where('id', '>', 0) ->where('stat', '>=', 0) ->select(); $uploadMgr = new UploadMannager(); foreach ($faq as $key => $val) { $image = ''; - $ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image'); + $ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image', 'faq'); if ($ret['code'] == 0) { $image = $ret['data']['path']; } else { @@ -226,6 +245,77 @@ class DataMigration extends Command Db::name('faq')->insert($item); } } + + // 迁移附件 + private function migrateAttachment($category_map = []) + { + $attachment = Db::connect('old') + ->name('download') + ->where('id', '>', 0) + ->where('country_code', 'in', ['ZH', 'US']) + ->order(['id' => 'asc']) + ->cursor(); + + $uploadMgr = new UploadMannager(); + foreach ($attachment as $key => $val) { + $image = ''; + $image_ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image'); + if ($image_ret['code'] == 0) { + $image = $image_ret['data']['path']; + } else { + $image = $image_ret['msg']; + } + $paths = explode(',', $val['downloadpath']); + $btns = explode(',', $val['downloadpath64']); + $attach = []; + foreach ($paths as $k => $path) { + $file_path = ''; + if (\think\helper\Str::contains($path, "oos.oricogroup.com")) { + $file_path = $path; + } else { + $attach_ret = $uploadMgr->upload($uploadMgr->download($path), 'attachment'); + if ($attach_ret['code'] == 0) { + $file_path = $attach_ret['data']['path']; + } else { + $file_path = $attach_ret['msg']; + } + } + + $file_ext = ''; + $last_sper_idx = strrpos($path, '.'); + if ($last_sper_idx !== false) { + $file_ext = substr($path, $last_sper_idx + 1); + if (substr($path, $last_sper_idx - 3, $last_sper_idx) == 'tar') { + $file_ext = 'tar.' . $file_ext; + } + } + + $attach[] = [ + 'file_path' => $file_path, + 'file_ext' => $file_ext, + 'btn_name' => $btns[$k], + ]; + } + $item = [ + 'language_id' => $val['country_code'] == 'ZH' ? 1 : 2, + 'category_id' => $category_map[$val['cid']], + 'name' => $val['name'], + 'desc' => $val['description'], + 'image' => $image, + 'applicable_to' => $val['app_model'], + 'support_platform' => $val['support_os'], + 'attach' => json_encode($attach), + 'sort' => $val['sort'] == 9999 ? 0 : $val['sort'], + 'recommend' => $val['recommend'], + 'seo_title' => $val['seo_title'], + 'seo_keywords' => $val['seo_keyword'], + 'seo_desc' => $val['seo_description'], + 'deleted_at' => $val['stat'] == -1 ? date('Y-m-d H:i:s') : null + ]; + Db::name('attachment')->insert($item); + $this->println('迁移附件ID:' . $val['id']); + } + } } class UploadMannager @@ -254,9 +344,14 @@ class UploadMannager if (\think\helper\Str::startsWith($file_name, 'http')) { $need = 'orico.com.cn'; if (!\think\helper\Str::contains($file_name, $need)) { - return $url; + if (\think\helper\Str::contains($file_name, 'https')) { + // 避免https过期情况 + $url = 'http://' . \think\helper\Str::substr($file_name, mb_strlen('https://')); + } + } else { + $url = self::DOWNLOAD_BASE_API . \think\helper\Str::substr($file_name, mb_strpos($file_name, $need) + mb_strlen($need)); } - $url = self::DOWNLOAD_BASE_API . \think\helper\Str::substr($file_name, mb_strpos($file_name, $need) + mb_strlen($need)); + $file_name = '/' . \think\helper\Str::substr($url, mb_strpos($url, '://') + 3); } else { $url = self::DOWNLOAD_BASE_API . str_replace(" ", "%20", $file_name); } @@ -279,11 +374,27 @@ class UploadMannager } // 上传图片 - public function upload($file_path, $field_name) { + public function upload($file_path, $field_name, $module = 'unknown') { if (empty($file_path)) { return ['code' => 0, 'msg' => 'file_path为空', 'data' => ['path' => '']]; } - $ch = curl_init(self::UPLOAD_BASE_API . '/admapi/v1/images/faq/upload'); + if (\think\helper\Str::startsWith($file_path, 'http')) { + return ['code' => 0, 'msg' => 'file_path为http', 'data' => ['path' => $file_path]]; + } + + switch($field_name){ + case 'image': + $url_path = "/admapi/v1/images/$module/upload"; + break; + case 'attachment': + $url_path = '/admapi/v1/attachment/upload'; + break; + case 'video': + $url_path = "v1/video/$module/upload"; + default: + $url_path = 'image'; + } + $ch = curl_init(self::UPLOAD_BASE_API . $url_path); $post_data = [ $field_name => new \CURLFile($file_path) ]; @@ -310,7 +421,11 @@ class UploadMannager return $this->upload($file_path, $field_name); } - return json_decode($response, true); + $ret = json_decode($response, true); + if (empty($ret)) { + throw new \Exception($response); + } + return $ret; } // 登录获取token