refactor: 数据迁移

This commit is contained in:
2025-04-15 16:28:31 +08:00
parent 75d97bb81f
commit a6e3f92bee

View File

@@ -31,27 +31,46 @@ class DataMigration extends Command
$output->writeln($msg); $output->writeln($msg);
}; };
// 迁移tock产品分类 try {
// $this->productTcoCategory(); // 迁移tock产品分类
// $this->productTcoCategory();
// 迁移产品分类 // 迁移产品分类
// $this->productCategory(); // $this->productCategory();
// 迁移文章 // 迁移文章
// $this->migrateArticle([ // $this->migrateArticle([
// 1 => 2, // 1 => 2,
// 2 => 3, // 2 => 3,
// 33 => 4, // 33 => 4,
// 36 => 5, // 36 => 5,
// 16 => 7, // 16 => 7,
// 31 => 8, // 31 => 8,
// 32 => 9 // 32 => 9
// ]); // ]);
// 迁移faq // 迁移faq
// $this->migrateFaq(); // $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产品分类 // 迁移tco产品分类
@@ -145,7 +164,7 @@ class DataMigration extends Command
foreach ($article as $v) { foreach ($article as $v) {
// 处理封面图片 // 处理封面图片
$image = ''; $image = '';
$ret = $uploadMgr->upload($uploadMgr->download($v['picture']), 'image'); $ret = $uploadMgr->upload($uploadMgr->download($v['picture']), 'image', 'article');
if ($ret['code'] == 0) { if ($ret['code'] == 0) {
$image = $ret['data']['path']; $image = $ret['data']['path'];
} else { } else {
@@ -200,14 +219,14 @@ class DataMigration extends Command
{ {
$faq = Db::connect('old') $faq = Db::connect('old')
->name('fq') ->name('fq')
->where('id', '>=', 10) ->where('id', '>', 0)
->where('stat', '>=', 0) ->where('stat', '>=', 0)
->select(); ->select();
$uploadMgr = new UploadMannager(); $uploadMgr = new UploadMannager();
foreach ($faq as $key => $val) { foreach ($faq as $key => $val) {
$image = ''; $image = '';
$ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image'); $ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image', 'faq');
if ($ret['code'] == 0) { if ($ret['code'] == 0) {
$image = $ret['data']['path']; $image = $ret['data']['path'];
} else { } else {
@@ -226,6 +245,77 @@ class DataMigration extends Command
Db::name('faq')->insert($item); 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 class UploadMannager
@@ -254,9 +344,14 @@ class UploadMannager
if (\think\helper\Str::startsWith($file_name, 'http')) { if (\think\helper\Str::startsWith($file_name, 'http')) {
$need = 'orico.com.cn'; $need = 'orico.com.cn';
if (!\think\helper\Str::contains($file_name, $need)) { 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 { } else {
$url = self::DOWNLOAD_BASE_API . str_replace(" ", "%20", $file_name); $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)) { if (empty($file_path)) {
return ['code' => 0, 'msg' => 'file_path为空', 'data' => ['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 = [ $post_data = [
$field_name => new \CURLFile($file_path) $field_name => new \CURLFile($file_path)
]; ];
@@ -310,7 +421,11 @@ class UploadMannager
return $this->upload($file_path, $field_name); 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 // 登录获取token