Skip to content

Commit

Permalink
change: 优化依赖
Browse files Browse the repository at this point in the history
delete: 删除hutool依赖
  • Loading branch information
carolcoral committed Apr 29, 2024
1 parent 6093823 commit 6e299a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 26 additions & 27 deletions src/main/java/run/halo/afdian/service/SponsorService.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,39 +23,37 @@ public SponsorService(ReactiveSettingFetcher settingFetcher) {
this.settingFetcher = settingFetcher;
}

// 创建webClient
private WebClient webClient = WebClient.builder().baseUrl("https://afdian.net").build();

/**
* 获取第一页的爱发电赞助用户
*
*/
public Mono<JsonNode> 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<String, Object> 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;
});

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e299a6

Please sign in to comment.