作者 刘锟

update

@@ -5,6 +5,7 @@ namespace App\Console\Commands\Test; @@ -5,6 +5,7 @@ namespace App\Console\Commands\Test;
5 use App\Helper\Arr; 5 use App\Helper\Arr;
6 use App\Http\Logic\Bside\Product\CategoryLogic; 6 use App\Http\Logic\Bside\Product\CategoryLogic;
7 use App\Models\Com\Notify; 7 use App\Models\Com\Notify;
  8 +use App\Models\CustomModule\CustomModuleContent;
8 use App\Models\Devops\ServerConfig; 9 use App\Models\Devops\ServerConfig;
9 use App\Models\Devops\ServersIp; 10 use App\Models\Devops\ServersIp;
10 use App\Models\Domain\DomainCreateTask; 11 use App\Models\Domain\DomainCreateTask;
@@ -16,6 +17,7 @@ use App\Models\Product\Product; @@ -16,6 +17,7 @@ use App\Models\Product\Product;
16 use App\Models\Project\DeployBuild; 17 use App\Models\Project\DeployBuild;
17 use App\Models\Project\DeployOptimize; 18 use App\Models\Project\DeployOptimize;
18 use App\Models\Project\Project; 19 use App\Models\Project\Project;
  20 +use App\Models\RouteMap\RouteMap;
19 use App\Models\Template\BCustomTemplate; 21 use App\Models\Template\BCustomTemplate;
20 use App\Models\WebSetting\WebLanguage; 22 use App\Models\WebSetting\WebLanguage;
21 use App\Services\BatchExportService; 23 use App\Services\BatchExportService;
@@ -41,7 +43,197 @@ class Temp extends Command @@ -41,7 +43,197 @@ class Temp extends Command
41 43
42 public function handle() 44 public function handle()
43 { 45 {
  46 + $this->specialImport();
  47 + }
  48 +
  49 + /**
  50 + * 3531项目导入扩展模块内容
  51 + * @return bool
  52 + * @author Akun
  53 + * @date 2025/06/05 10:47
  54 + */
  55 + public function specialImport()
  56 + {
  57 + $file_url = 'https://ecdn6.globalso.com/upload/p/3531/file/2025-06/news.csv';
  58 + $domain = 'www.hybio.com.cn';
  59 + $project_id = 3531;
  60 + $is_gbk = 0;
  61 + $file_code_type = $this->get_code_type($file_url);
  62 + if ($file_code_type === false) {
  63 + echo 'date:' . date('Y-m-d H:i:s') . ', import fail, error: 文件编码格式错误' . PHP_EOL;
  64 + return true;
  65 + } elseif ($file_code_type === 'GBK') {
  66 + $is_gbk = 1;
  67 + setlocale(LC_ALL, 'zh_CN');
  68 + }
  69 +
  70 + //读取csv文件
  71 + $line_of_text = [];
  72 + try {
  73 + $opts = [
  74 + 'http' => [
  75 + 'method' => 'GET',
  76 + 'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
  77 + ],
  78 + 'ssl' => [
  79 + 'verify_peer' => false,
  80 + 'verify_peer_name' => false
  81 + ]
  82 + ];
  83 + $file_handle = fopen($file_url, 'r', null, stream_context_create($opts));
  84 + while (!feof($file_handle)) {
  85 + $line_of_text[] = fgetcsv($file_handle, 0, ',');
  86 + }
  87 + fclose($file_handle);
  88 + } catch (\Exception $e) {
  89 + $this->output('import fail, error: ' . $e->getMessage());
  90 + return true;
  91 + }
  92 +
  93 + $total_count = 0; //总条数
  94 + $success_count = 0; //成功导入条数
  95 + $repeat_count = 0; //过滤已存在条数
  96 + $fail_count = 0;//导入失败条数
  97 + $fail_line = [];//失败行数
  98 + if (count($line_of_text) > 1) {
  99 +
  100 + //设置数据库
  101 + $project = ProjectServer::useProject($project_id);
  102 + if ($project) {
  103 + foreach ($line_of_text as $k => $v) {
  104 + if ($k > 0 && isset($v[0]) && $v[0]) {
  105 +
  106 + if ($is_gbk) {
  107 + foreach ($v as $kk => $vv) {
  108 + $v[$kk] = mb_convert_encoding($vv, 'utf-8', 'gbk');
  109 + }
  110 + }
  111 +
  112 + $v[0] = $this->special2str($v[0]);
  113 +
  114 + $total_count += 1;
  115 + try {
  116 + if ($this->importModule($project_id, $domain, $v)) {
  117 + $success_count += 1;
  118 + } else {
  119 + $repeat_count += 1;
  120 + }
  121 + } catch (\Exception $e) {
  122 + $fail_count += 1;
  123 + $fail_line[] = $k + 1;
  124 + $this->output('title: ' . $v[0] . ', import fail, error: ' . $e->getMessage());
  125 + }
  126 + }
  127 + }
  128 + }
  129 + //关闭数据库
  130 + DB::disconnect('custom_mysql');
  131 + }
  132 +
  133 + $this->output('import end, total count: ' . $total_count . ', success count: ' . $success_count . ', repeat_count count: ' . $repeat_count . ', fail_count count: ' . $fail_count);
  134 +
  135 + return true;
  136 + }
  137 +
  138 + protected function importModule($project_id, $domain, $data)
  139 + {
  140 + $model = new CustomModuleContent();
  141 + $module = $model->read(['name' => $data[0]]);
  142 + if (!$module) {
  143 +
  144 + $content = '';
  145 + if ($data[4] ?? '') {
  146 + //处理内容中的图片
  147 + preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result);
  148 + if ($result[2] ?? []) {
  149 + foreach ($result[2] as $img) {
  150 + $new_img = check_remote_url_down($img, $project_id, $domain, 1);
  151 + $new_img && $data[4] = str_replace($img, $new_img, $data[4]);
  152 + }
  153 + }
  154 +
  155 + //处理内容中的视频
  156 + preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video);
  157 + if ($result_video[2] ?? []) {
  158 + foreach ($result_video[2] as $video) {
  159 + $new_video = check_remote_url_down($video, $project_id, $domain, 1);
  160 + $new_video && $data[4] = str_replace($video, $new_video, $data[4]);
  161 + }
  162 + }
  163 +
  164 + $content = $data[4];
  165 + }
  166 +
  167 + $seo_title = '';
  168 + if ($data[6] ?? '') {
  169 + $seo_title = substr(strip_tags($data[6]), 0, 70);
  170 + }
  171 +
  172 + $seo_keywords = '';
  173 + if ($data[7] ?? '') {
  174 + $seo_keywords = substr(strip_tags(str_replace('^v6sp$', ',', $data[7])), 0, 255);
  175 + }
  176 +
  177 + $seo_description = '';
  178 + if ($data[8] ?? '') {
  179 + $seo_description = substr(strip_tags($data[8]), 0, 200);
  180 + }
  181 +
  182 + $release_at = date('Y-m-d H:i:s');
  183 + if ($data[9] ?? '') {
  184 + $release_at = date('Y-m-d H:i:s', strtotime($data[9]));
  185 + }
  186 +
  187 +
  188 + $id = $model->addReturnId(
  189 + [
  190 + 'name' => $data[0],
  191 + 'category_id' => ',1,',
  192 + 'module_id' => 2,
  193 + 'content' => $content,
  194 + 'seo_title' => $seo_title,
  195 + 'seo_keywords' => $seo_keywords,
  196 + 'seo_description' => $seo_description,
  197 + 'project_id' => $project_id,
  198 + 'operator_id' => 8143,
  199 + 'status' => 0,
  200 + 'route' => '',
  201 + 'release_at' => $release_at
  202 + ]
  203 + );
  204 +
  205 + $route = RouteMap::setRoute($data[0], RouteMap::SOURCE_MODULE, $id, $project_id);
  206 +
  207 + $model->edit(['route' => $route], ['id' => $id]);
  208 +
  209 + return true;
  210 + }
  211 +
  212 + return false;
  213 + }
  214 +
  215 + //特殊字符转换
  216 + protected function special2str($str)
  217 + {
  218 + if (strpos($str, ';') === false) {
  219 + return $str;
  220 + }
  221 +
  222 + $list = [
  223 + '&lt;' => '<',
  224 + '&gt;' => '>',
  225 + '&amp;' => '&',
  226 + '&acute;' => "'",
  227 + '&quot;' => '"',
  228 + '&nbsp;' => ' ',
  229 + '&#x27;' => "'"
  230 + ];
  231 +
  232 + foreach ($list as $k => $v) {
  233 + $str = str_replace($k, $v, $str);
  234 + }
44 235
  236 + return $str;
45 } 237 }
46 238
47 /** 239 /**