From 6e299a6e699a4d716e280547894f4ea02af08de4 Mon Sep 17 00:00:00 2001 From: liuxuewen <947752894@qq.com> Date: Mon, 29 Apr 2024 11:18:58 +0800 Subject: [PATCH] =?UTF-8?q?change:=20=E4=BC=98=E5=8C=96=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=20delete:=20=E5=88=A0=E9=99=A4hutool=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +- .../halo/afdian/service/SponsorService.java | 53 +++++++++---------- src/main/resources/plugin.yaml | 2 +- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 83e2523..377fcbc 100644 --- a/build.gradle +++ b/build.gradle @@ -16,8 +16,7 @@ repositories { } dependencies { - implementation platform('run.halo.tools.platform:plugin:2.11.0-SNAPSHOT') - implementation 'cn.hutool:hutool-all:5.8.20' + implementation platform('run.halo.tools.platform:plugin:2.14.0-SNAPSHOT') compileOnly 'run.halo.app:api' testImplementation 'run.halo.app:api' diff --git a/src/main/java/run/halo/afdian/service/SponsorService.java b/src/main/java/run/halo/afdian/service/SponsorService.java index adb4bdc..67b41bc 100644 --- a/src/main/java/run/halo/afdian/service/SponsorService.java +++ b/src/main/java/run/halo/afdian/service/SponsorService.java @@ -1,16 +1,17 @@ package run.halo.afdian.service; -import cn.hutool.http.HttpRequest; -import cn.hutool.json.JSONObject; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import run.halo.afdian.utils.EncryptUtils; import run.halo.app.plugin.ReactiveSettingFetcher; -import java.util.Calendar; @Service @Slf4j @@ -22,39 +23,37 @@ public SponsorService(ReactiveSettingFetcher settingFetcher) { this.settingFetcher = settingFetcher; } + // 创建webClient + private WebClient webClient = WebClient.builder().baseUrl("https://afdian.net").build(); + /** * 获取第一页的爱发电赞助用户 - * */ public Mono getSponsorList() { - return this.settingFetcher.get("basic").flatMap(base ->{ + return this.settingFetcher.get("basic").flatMap(base -> { String token = base.get("token").asText(); String userId = base.get("userId").asText(); - String url = "https://afdian.net/api/open/query-sponsor"; - JSONObject params = new JSONObject(); + String url = "/api/open/query-sponsor"; + Map params = new HashMap<>(); var timeInMillis = Calendar.getInstance().getTimeInMillis() / 1000; - var sign = token.concat("params{\"page\":1}ts") - .concat(String.valueOf(timeInMillis)).concat("user_id").concat(userId); + var sign = token.concat("params{\"page\":1}ts").concat(String.valueOf(timeInMillis)) + .concat("user_id").concat(userId); String signMd5 = EncryptUtils.encrypt32(sign); - params.set("user_id", userId); - params.set("params", "{\"page\":1}"); - params.set("ts", timeInMillis); - params.set("sign", signMd5); - - var execute = HttpRequest.post(url).body(params.toString()).execute(); - var body = execute.body(); - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode jsonNode = null; - try { - jsonNode = objectMapper.readTree(body); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - log.info(jsonNode.toString()); - return Mono.just(jsonNode); + params.put("user_id", userId); + params.put("params", "{\"page\":1}"); + params.put("ts", timeInMillis); + params.put("sign", signMd5); + + var body = + webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON) // JSON数据类型 + .body(BodyInserters.fromValue(params)) // JSON字符串数据 + .retrieve() // 获取响应体 + .bodyToMono(JsonNode.class);// 响应数据类型转换 + log.info(body.toString()); + return body; }); } diff --git a/src/main/resources/plugin.yaml b/src/main/resources/plugin.yaml index c1c63c3..51d4b63 100644 --- a/src/main/resources/plugin.yaml +++ b/src/main/resources/plugin.yaml @@ -7,7 +7,7 @@ metadata: name: plugin-afdian spec: enabled: true - requires: ">=2.11.0" + requires: ">=2.14.0" author: name: Carol website: https://xindu.site