feat(system): 新增方舟账号管理与任务接口
- 新增 MarkController 控制器,提供方舟任务列表获取和新建任务接口 - 实现 IMarkService 接口及 MarkServiceImpl 实现类,支持注册、登录和MD5加密功能 - 在 CacheConstants 中添加方舟账号 Redis key 常量 - 集成 RestTemplate用于调用方舟API,支持自动注册与登录机制- 添加文件上传支持,用于新建任务时提交文件 - 实现 token 失效自动重新登录逻辑,确保接口调用稳定性
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.ruoyi.web.controller.tool;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.annotation.Anonymous;
|
||||
import com.ruoyi.common.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.system.service.IMarkService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Handler;
|
||||
|
||||
@RequestMapping("/tool/mark")
|
||||
@RestController
|
||||
@Anonymous
|
||||
public class MarkController {
|
||||
private static final String API_SECRET = "e10adc3949ba59abbe56e057f20f883e";
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Autowired
|
||||
private IMarkService markService;
|
||||
|
||||
/**
|
||||
* 获取任务列表
|
||||
*/
|
||||
@GetMapping("/task")
|
||||
public AjaxResult Task() {
|
||||
try {
|
||||
String token = redisCache.getCacheMapValue(CacheConstants.MARK_ACCOUNT_KEY, "token");
|
||||
String d = "{\"name\":\"\",\"page_size\":20,\"current_page\":1}";
|
||||
long ts = System.currentTimeMillis();
|
||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("c", "TaskPageList");
|
||||
formData.add("d", d);
|
||||
formData.add("t", token);
|
||||
formData.add("s", markService.md5(ts + d + API_SECRET));
|
||||
formData.add("ts", String.valueOf(ts));
|
||||
formData.add("website", "1");
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
|
||||
String result = restTemplate.postForObject("https://api.fangzhoujingxuan.com/Task", requestEntity, String.class);
|
||||
JsonNode json = objectMapper.readTree(result);
|
||||
return AjaxResult.success(json.get("D").asText());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 新建任务
|
||||
@PostMapping("newTask")
|
||||
public AjaxResult newTask(@RequestParam("file") MultipartFile file) {
|
||||
try {
|
||||
String token = redisCache.getCacheMapValue(CacheConstants.MARK_ACCOUNT_KEY, "token");
|
||||
if (token == null) token = markService.reg();
|
||||
String data =String.format("{\"name\":\"%s\",\"type\":1}", file.getOriginalFilename()) ;
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("c", "Create");
|
||||
formData.add("t", token);
|
||||
formData.add("ts",System.currentTimeMillis());
|
||||
formData.add("d", data);
|
||||
formData.add("s", markService.md5(System.currentTimeMillis() + data + API_SECRET));
|
||||
formData.add("website", "1");
|
||||
formData.add("files", file.getResource());
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(formData, headers);
|
||||
String result = restTemplate.postForObject("https://api.fangzhoujingxuan.com/Task", requestEntity, String.class);
|
||||
JsonNode jsonNode = objectMapper.readTree(result);
|
||||
if(jsonNode.get("S").asInt()==-1006){
|
||||
token= markService.login();
|
||||
formData.add("t", token);
|
||||
requestEntity = new HttpEntity<>(formData, headers);
|
||||
result = restTemplate.postForObject("https://api.fangzhoujingxuan.com/Task", requestEntity, String.class);
|
||||
jsonNode= objectMapper.readTree(result);
|
||||
|
||||
}
|
||||
return jsonNode.get("S").asInt()==1?AjaxResult.success(result): AjaxResult.error( jsonNode.get("S").asText());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user