Skip to content

Commit

Permalink
change: 变更爱发电API地址为配置填写
Browse files Browse the repository at this point in the history
add: 新增获取全部赞助方案、商品、VIP服务Finder
add: 新增获取全部赞助方案、商品列表Finder
add: 新增获取全部作品集Finder
  • Loading branch information
carolcoral committed Jul 16, 2024
1 parent 3e166da commit e2faab2
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.6.2
version=1.6.3
8 changes: 7 additions & 1 deletion src/main/java/site/xindu/afdian/config/AfdianRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class AfdianRouter {
private final ReactiveSettingFetcher settingFetcher;

private static final String THEME_SETTING = "themeSetting";
private static final String BASIC = "basic";

@Bean
RouterFunction<ServerResponse> momentRouterFunction() {
Expand All @@ -50,7 +51,7 @@ private void defaultModel(HashMap<String, Object> model) {
Mono<String> sponsorUrl =
this.settingFetcher.get(THEME_SETTING).map(setting ->
setting.get("sponsorUrl").asText()
).defaultIfEmpty("https://afdian.net/a/carolcoral");
).defaultIfEmpty("https://afdian.com/a/carolcoral");
model.put("sponsorUrl", sponsorUrl);
Mono<Double> sponsorNumber =
this.settingFetcher.get(THEME_SETTING).map(setting ->
Expand All @@ -61,6 +62,11 @@ private void defaultModel(HashMap<String, Object> model) {
setting.get("rewardTopTitle").asText()
).defaultIfEmpty("感谢我的赞赏者们");
model.put("rewardTopTitle", rewardTopTitle);
Mono<String> baseUrl =
this.settingFetcher.get(BASIC).map(setting ->
setting.get("baseUrl").asText()
).defaultIfEmpty("https://afdian.com");
model.put("baseUrl", baseUrl);
}

private void extracted(HashMap<String, Object> model, String theme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class BaseSettingConfig {

public static final String CONFIG_MAP_NAME = "plugin-afdian-config";
public static final String GROUP = "basic";
public static final String GROUP = "account";

/**
* 用户token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class ThemeSettingConfig {
public static final String CONFIG_MAP_NAME = "plugin-afdian-config";
public static final String GROUP = "themeSetting";

/**
* 爱发电地址
*/
private String baseUrl = "";

/**
* 赞助地址
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/site/xindu/afdian/service/AfdianFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,23 @@ public interface AfdianFinder {
*/
Mono<Double> getRewardNumber();

/**
* 获取全部赞助方案、商品、VIP服务
* @return 全部赞助方案、商品、VIP服务
*/
Mono<JsonNode> listAllSponsorship();


/**
* 获取全部赞助方案、商品列表
* @return 全部赞助方案、商品列表
*/
Mono<JsonNode> listPlansAndSales();

/**
* 获取全部作品集
* @return 全部作品集
*/
Mono<JsonNode> listAlbum();

}
30 changes: 30 additions & 0 deletions src/main/java/site/xindu/afdian/service/impl/AfdianFinderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,34 @@ public Mono<Double> getRewardNumber() {
)
.defaultIfEmpty(66.0);
}

/**
* 获取全部赞助方案、商品、VIP服务
*
* @return 全部赞助方案、商品、VIP服务
*/
@Override
public Mono<JsonNode> listAllSponsorship() {
return afdianService.listAllSponsorship();
}

/**
* 获取全部赞助方案、商品列表
*
* @return 全部赞助方案、商品列表
*/
@Override
public Mono<JsonNode> listPlansAndSales() {
return afdianService.listPlansAndSales();
}

/**
* 获取全部作品集
*
* @return 全部作品集
*/
@Override
public Mono<JsonNode> listAlbum() {
return afdianService.listAlbum();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package site.xindu.afdian.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
Expand All @@ -27,27 +28,31 @@ public class AfdianServiceImpl implements AfdianService {

private final String BASIC = "basic";

// 创建webClient
private WebClient webClient;

public AfdianServiceImpl(ReactiveSettingFetcher settingFetcher) {
this.settingFetcher = settingFetcher;
}

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

/**
* 获取token
*
* @return token对象
*/
@Override
public Mono<JsonNode> getAuthToken() {
return this.settingFetcher.get("basic").flatMap(base -> {
return this.settingFetcher.get(BASIC).flatMap(base -> {
String username = base.get("username").asText();
String password = base.get("password").asText();
String url = "/api/passport/login";
Map<String, Object> params = new HashMap<>();
params.put("account", username);
params.put("password", password);

String baseUrl = base.get("baseUrl").asText();
this.webClient = WebClient.builder().baseUrl(baseUrl).build();

return webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON) // JSON数据类型
.body(BodyInserters.fromValue(params)) // JSON字符串数据
.retrieve() // 获取响应体
Expand All @@ -63,7 +68,7 @@ public Mono<JsonNode> getAuthToken() {
*/
@Override
public Mono<JsonNode> getSponsorList(int pageNumber) {
var mono = this.settingFetcher.get("basic").flatMap(base -> {
var mono = this.settingFetcher.get(BASIC).flatMap(base -> {
String token = base.get("token").asText();
String userId = base.get("userId").asText();
String url = "/api/open/query-sponsor";
Expand All @@ -80,6 +85,9 @@ public Mono<JsonNode> getSponsorList(int pageNumber) {
params.put("ts", timeInMillis);
params.put("sign", signMd5);

String baseUrl = base.get("baseUrl").asText();
this.webClient = WebClient.builder().baseUrl(baseUrl).build();

return webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON) // JSON数据类型
.body(BodyInserters.fromValue(params)) // JSON字符串数据
.retrieve() // 获取响应体
Expand Down Expand Up @@ -129,6 +137,13 @@ public Mono<JsonNode> listAllSponsorship() {
var token = res.get("data").get("auth_token").textValue();
String url = "/api/creator/all-plans";
String cookie = "auth_token=".concat(token);

this.settingFetcher.get(BASIC).map(base -> {
String baseUrl = base.get("baseUrl").asText();
this.webClient = WebClient.builder().baseUrl(baseUrl).build();
return this.webClient;
});

return webClient.get().uri(url).header("Cookie", cookie)
.retrieve() // 获取响应体
.bodyToMono(JsonNode.class);
Expand All @@ -146,10 +161,17 @@ public Mono<JsonNode> listPlansAndSales() {
return authToken.flatMap(res -> {
var token = res.get("data").get("auth_token").textValue();
String cookie = "auth_token=".concat(token);
return this.settingFetcher.get(BASIC).flatMap(setting->{
return this.settingFetcher.get(BASIC).flatMap(setting -> {
var userId = setting.get("userId").textValue();
String url = "/api/creator/get-plans?user_id=";
url = url.concat(userId);

this.settingFetcher.get(BASIC).map(base -> {
String baseUrl = base.get("baseUrl").asText();
this.webClient = WebClient.builder().baseUrl(baseUrl).build();
return this.webClient;
});

return webClient.get().uri(url).header("Cookie", cookie)
.retrieve() // 获取响应体
.bodyToMono(JsonNode.class);
Expand All @@ -168,10 +190,17 @@ public Mono<JsonNode> listAlbum() {
return authToken.flatMap(res -> {
var token = res.get("data").get("auth_token").textValue();
String cookie = "auth_token=".concat(token);
return this.settingFetcher.get(BASIC).flatMap(setting->{
return this.settingFetcher.get(BASIC).flatMap(setting -> {
var userId = setting.get("userId").textValue();
String url = "/api/user/get-album-list?user_id=";
url = url.concat(userId);

this.settingFetcher.get(BASIC).map(base -> {
String baseUrl = base.get("baseUrl").asText();
this.webClient = WebClient.builder().baseUrl(baseUrl).build();
return this.webClient;
});

return webClient.get().uri(url).header("Cookie", cookie)
.retrieve() // 获取响应体
.bodyToMono(JsonNode.class);
Expand Down
15 changes: 11 additions & 4 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ spec:
- group: basic
label: 基本设置
formSchema:
- $formkit: text
name: baseUrl
label: 爱发电访问地址
placeholder: 请输入爱发电访问地址(末尾不带/)
validation: required
help: 启用后必填
value: https://afdian.com
- $formkit: text
name: token
label: 用户token
placeholder: 请输入用户TOKEN
validation: required
help: 启用后必填,在 https://afdian.net/dashboard/dev 中 API TOKEN 获取
help: 启用后必填,在 https://afdian.com/dashboard/dev 中 API TOKEN 获取
- $formkit: text
name: userId
label: 用户ID
placeholder: 请输入用户ID
validation: required
help: 启用后必填,在 https://afdian.net/dashboard/dev 中 user_id 获取
help: 启用后必填,在 https://afdian.com/dashboard/dev 中 user_id 获取
- $formkit: text
name: username
label: 用户名
value: username
placeholder: 请输入爱发电登录用户名,如果不输入用户名和密码,获取赞助方案等接口会出现异常
help: 如果不输入用户名和密码,获取赞助方案等接口会出现异常
- $formkit: text
- $formkit: password
name: password
label: 密码
value: password
Expand All @@ -42,7 +49,7 @@ spec:
- $formkit: text
name: sponsorUrl
label: 赞助地址
value: https://afdian.net/a/carolcoral
value: https://afdian.com/a/carolcoral
placeholder: 请输入跳转赞助地址
validation: required
help: 启用后必填,否则爱发电页面可能会报错
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/afdian.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
</div>
</div>
<div class="reward-list-all">
<div class="reward-list-item" th:each="sponsor : ${afdianFinder.listAllSponsor().data.list}" th:attr="onclick=|sponsorSelf('https://afdian.net/u/'+'${sponsor.user.user_id}')|">
<div class="reward-list-item"
th:each="sponsor : ${afdianFinder.listAllSponsor().data.list}"
th:attr="onclick=|sponsorSelf('${baseUrl}'+'/u/'+'${sponsor.user.user_id}')|">
<div>
<div>
<div class="reward-list-item-avatar">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/afdian2.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="flink-list-item"
th:each="sponsor : ${afdianFinder.listAllSponsor().data.list}">
<a class="cf-friends-link" rel="external nofollow" target="_blank"
th:attr="onclick=|sponsorSelf('https://afdian.net/u/'+'${sponsor.user.user_id}')|">
th:attr="onclick=|sponsorSelf('${baseUrl}'+'/u/'+'${sponsor.user.user_id}')|">
<img class="flink-avatar cf-friends-avatar entered loaded"
th:alt="${sponsor.user.name}"
data-ll-status="loaded"
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/afdian3.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<div class="tags-group-wrapper" th:each="sponsor : ${afdianFinder.listAllSponsor().data.list}">
<div class="tags-group-icon-pair">
<a class="tags-group-icon" target="_blank"
th:attr="href='https://afdian.net/u/'+'${sponsor.user.user_id}'" title="">
th:attr="href='${baseUrl}'+'/u/'+'${sponsor.user.user_id}'" title="">
<img th:src="${sponsor.user.avatar}"
th:attr="title=${sponsor.user.name}" data-ll-status="loaded"
class="entered loaded"></a>
Expand All @@ -47,7 +47,7 @@
<div class="flink-list">
<div class="flink-list-item" th:each="sponsor : ${afdianFinder.listAllSponsor().data.list}">
<a class="cf-friends-link" rel="external nofollow" target="_blank"
th:attr="onclick=|sponsorSelf('https://afdian.net/u/'+'${sponsor.user.user_id}')|">
th:attr="onclick=|sponsorSelf('${baseUrl}'+'/u/'+'${sponsor.user.user_id}')|">
<img class="flink-avatar cf-friends-avatar entered loaded" alt=""
th:src="${sponsor.user.avatar}"
data-ll-status="loaded">
Expand Down

0 comments on commit e2faab2

Please sign in to comment.