导入/导出功能初步实现

This commit is contained in:
yuxin-pc 2025-06-07 18:38:22 +08:00
parent 7404ea6191
commit 3a4430dcdc
5 changed files with 81 additions and 11 deletions

View File

@ -6,8 +6,11 @@
<el-button @click="addCrawlRule()" style="margin-left:20px;" type="success" size="mini"><i
class="el-icon-plus"></i> 新增
</el-button>
<el-button @click="addCrawlRule()" style="margin-left:20px;" type="info" size="mini"><i
class="el-icon-top-right"></i> 导出
<el-button type="primary" style="margin-left:20px;" size="mini" @click="handleExport"><i
class="el-icon-download"></i> 导出
</el-button>
<el-button type="warning" style="margin-left:20px;" size="mini" @click="handleImport"><i
class="el-icon-upload2"></i> 导入
</el-button>
</el-col>
<el-col :span="20">
@ -68,11 +71,11 @@
<!-- 内容展示部分 -->
<targetTable :metaData="metadata" :tableData="tableData" :isLoading="isLoading" labelName="状态"
@selection-change="handleSelectionChange">
<!-- <template #status="{index,tagStu}">-->
<!-- <el-tag type="danger" v-if="tagStu.status===2">已停止</el-tag>-->
<!-- <el-tag type="warning" v-if="tagStu.status===1">已暂停</el-tag>-->
<!-- <el-tag type="success" v-if="tagStu.status===0">已启用</el-tag>-->
<!-- </template>-->
<!-- <template #status="{index,tagStu}">-->
<!-- <el-tag type="danger" v-if="tagStu.status===2">已停止</el-tag>-->
<!-- <el-tag type="warning" v-if="tagStu.status===1">已暂停</el-tag>-->
<!-- <el-tag type="success" v-if="tagStu.status===0">已启用</el-tag>-->
<!-- </template>-->
<template #default="{ index,handscope }">
<el-tooltip v-if="handscope.row.status === 0" class="item" effect="dark"
content="取消采集"

View File

@ -3,8 +3,8 @@ module.exports = {
devServer: {
host: '0.0.0.0',
port: 8080,
// proxy: 'http://127.0.0.1:8081'
proxy: 'http://38.54.94.107:28081'
proxy: 'http://127.0.0.1:8081'
// proxy: 'http://38.54.94.107:28081'
},
chainWebpack: config => {
config.plugin('html')

View File

@ -8,10 +8,15 @@ import com.jsc.oscm.entity.TargetWebsite;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.TargetWebsiteService;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -120,4 +125,34 @@ public class TargetWebsiteController {
return JSON.toJSONString(returnT);
}
@GetMapping("/target/website/export")
public ResponseEntity<byte[]> exportWebsites() throws IOException {
String jsonData = targetWebsiteService.exportAllWebsites();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setContentDispositionFormData("attachment", "target_websites.json");
return ResponseEntity.ok()
.headers(headers)
.body(jsonData.getBytes());
}
/**
* 从JSON文件导入目标网站数据
*
* @param file 上传的JSON文件
* @return 导入结果消息
*/
@PostMapping("/target/website/import")
public ResponseEntity<String> importWebsites(@RequestParam("file") MultipartFile file) {
try {
int count = targetWebsiteService.importWebsites(file);
return ResponseEntity.ok("成功导入 " + count + " 条记录");
} catch (IOException e) {
return ResponseEntity.badRequest().body("导入失败: " + e.getMessage());
}
}
}

View File

@ -2,6 +2,8 @@ package com.jsc.oscm.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsc.oscm.dao.TargetWebsiteRepository;
import com.jsc.oscm.entity.RulesFilter;
import com.jsc.oscm.entity.RulesParser;
@ -10,8 +12,10 @@ import com.jsc.oscm.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -151,4 +155,32 @@ public class TargetWebsiteService {
return resObj;
}
private final ObjectMapper objectMapper = new ObjectMapper();
public String exportAllWebsites() throws IOException {
List<TargetWebsite> websites = targetWebsiteRepository.findAll();
return objectMapper.writeValueAsString(websites);
}
/**
* 从JSON文件导入目标网站数据
* @param file 包含网站数据的JSON文件
* @return 导入的记录数量
*/
public int importWebsites(MultipartFile file) throws IOException {
// 读取JSON文件内容
String content = new String(file.getBytes());
// 解析JSON为对象列表
List<TargetWebsite> websites = objectMapper.readValue(
content,
new TypeReference<List<TargetWebsite>>() {}
);
// 保存到数据库
targetWebsiteRepository.saveAll(websites);
return websites.size();
}
}

View File

@ -1,6 +1,6 @@
server.port=8081
server.servlet.context-path=/api
spring.datasource.url=jdbc:mysql://47.113.231.200:28089/oscm?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.url=jdbc:mysql://38.54.94.107:28089/oscm?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=passok123A
@ -16,7 +16,7 @@ crawl.media.project=MediaSpiders
crawl.media.spider.user=WeiboUserSpider
crawl.media.spider.search=WeiboSearchSpider
crawl.project.file.path=/data/oscm/
xxl.job.admin.server=http://47.115.228.133:28082/
xxl.job.admin.server=http://38.54.94.107:28082/
xxl.job.executor.groupid=1
xxl.job.executor.handlerMulti=demoJobHandler
xxl.job.executor.handlerSingle=scheduleHandler