Skip to content

Commit

Permalink
add: 新增分页查询接口
Browse files Browse the repository at this point in the history
add: 新增默认赞赏展示页面
  • Loading branch information
carolcoral committed May 27, 2024
1 parent 1239b06 commit 71bef11
Show file tree
Hide file tree
Showing 16 changed files with 892 additions and 88 deletions.
9 changes: 2 additions & 7 deletions OWNERS
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
reviewers:
- ruibaby
- guqing
- JohnNiang
- wangzhen-fit2cloud
- Carol

approvers:
- ruibaby
- guqing
- JohnNiang
- Carol
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
> 提供API接口信息返回爱发电赞助信息
> 预览效果: [新 · 都在](https://blog.xindu.site/)
> 预览效果: [新 · 都在](https://blog.xindu.site/afdian)
## TODO
- [ ] 获取用户全部赞助方案并展示
- [ ] 获取用户全部商品内容并展示
- [ ] 提供独立的爱发电赞助展示页面
- [x] 提供独立的爱发电赞助展示页面

## 使用
1. 启动插件
2. 在插件配置中填写自己的userId和token
* user_id: 登录爱发电网站后访问[开发者页面](https://afdian.net/dashboard/dev),复制`开发者`下方的`user_id`
* token: 登录爱发电网站后访问[开发者页面](https://afdian.net/dashboard/dev),在页面最下方`API Token(用来主动请求API用)`点击`生成`,然后复制生成的token即可
3. 主题开发者可以使用下面的接口完成定制化开发

* 主题开发者可以使用下面的接口完成定制化开发
* 使用地址 `/afdian` 可访问插件默认展示页面

## 开发环境

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/site/xindu/afdian/config/BaseSettingConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ public class BaseSettingConfig {
public static final String CONFIG_MAP_NAME = "plugin-afdian-config";
public static final String GROUP = "basic";

/**
* 用户token
*/
private String token = "";

/**
* 用户ID
*/
private String userId = "";

/**
* 赞助地址
*/
private String sponsorUrl = "";

}
7 changes: 7 additions & 0 deletions src/main/java/site/xindu/afdian/entity/SponsorEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package site.xindu.afdian.entity;

import lombok.Data;
import java.util.Date;
import java.util.List;

@Data
Expand All @@ -22,7 +23,13 @@ public static class SponsorData{
@Data
public static class SponsorJsonData{
private List<Object> sponsor_plans;
private Double all_sum_amount;
private Long first_pay_time;
private Long last_pay_time;
private String firstPayTime;
private String lastPayTime;
private User user;
private Long user_private_id;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.xindu.afdian.service;
package site.xindu.afdian.finder;

import com.fasterxml.jackson.databind.JsonNode;
import reactor.core.publisher.Mono;
Expand All @@ -7,7 +7,7 @@
/**
* 为主题提供爱发电Finder接口
*/
public interface AfdianFinderService {
public interface AfdianFinder {

/**
* 分页获取赞助者信息
Expand All @@ -21,4 +21,11 @@ public interface AfdianFinderService {
*/
Mono<JsonNode> listAllSponsor();

/**
* 格式化时间 yyyy-MM-dd
* @param timestamp 时间戳
* @return time String
*/
String parseTime(Long timestamp);

}
61 changes: 61 additions & 0 deletions src/main/java/site/xindu/afdian/finder/impl/AfdianFinderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package site.xindu.afdian.finder.impl;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Mono;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.theme.finders.Finder;
import site.xindu.afdian.entity.SponsorEntity;
import site.xindu.afdian.finder.AfdianFinder;
import site.xindu.afdian.service.SponsorService;
import site.xindu.afdian.utils.DataUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Finder("afdianFinder")
public class AfdianFinderImpl implements AfdianFinder {

@Autowired
private SponsorService sponsorService;

/**
* 获取全部赞助者信息
*
* @return {@link SponsorEntity}
*/
@Override
public Mono<JsonNode> listSponsor(int pageNumber) {
var mono = sponsorService.getSponsorList(pageNumber);
return DataUtils.changePayTime(mono);
}

/**
* 获取全部赞助者信息
*
* @return {@link SponsorEntity}
*/
@Override
public Mono<JsonNode> listAllSponsor() {
var mono = sponsorService.listAllSponsor();
return DataUtils.changePayTime(mono);
}

/**
* 格式化时间 yyyy-MM-dd
*
* @param timestamp 时间戳
* @return time String
*/
@Override
public String parseTime(Long timestamp) {
var length = timestamp.toString().length();
if (length == 10){
timestamp = timestamp * 1000;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(new Date(timestamp));
}
}
48 changes: 48 additions & 0 deletions src/main/java/site/xindu/afdian/service/AfdianRouter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package site.xindu.afdian.service;

import java.util.HashMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.app.theme.TemplateNameResolver;
import site.xindu.afdian.entity.SponsorEntity;
import site.xindu.afdian.finder.AfdianFinder;

@Slf4j
@RequiredArgsConstructor
@Configuration(proxyBeanMethods = false)
public class AfdianRouter {

private final TemplateNameResolver templateNameResolver;

private final AfdianFinder afdianFinder;

private final ReactiveSettingFetcher settingFetcher;

@Bean
RouterFunction<ServerResponse> momentRouterFunction() {
return RouterFunctions.route().GET("/afdian", this::renderPage).build();
}

Mono<ServerResponse> renderPage(ServerRequest request) {
// 准备需要提供给模板的默认数据
var model = new HashMap<String, Object>();
Mono<String> sponsorUrl = this.settingFetcher.get("basic").map(setting ->
setting.get("sponsorUrl").asText("https://afdian.net/a/carolcoral")
)
.defaultIfEmpty("https://afdian.net/a/carolcoral");
model.put("sponsorUrl", sponsorUrl);
log.info(model.toString());
return templateNameResolver.resolveTemplateNameOrDefault(request.exchange(), "afdian")
.flatMap(templateName -> ServerResponse.ok().render(templateName, model));
}

}
40 changes: 40 additions & 0 deletions src/main/java/site/xindu/afdian/service/CustomHeadProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package site.xindu.afdian.service;

import org.pf4j.PluginWrapper;
import org.springframework.stereotype.Component;
import org.springframework.util.PropertyPlaceholderHelper;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.processor.element.IElementModelStructureHandler;
import reactor.core.publisher.Mono;
import run.halo.app.theme.dialect.TemplateHeadProcessor;
import java.util.Properties;

@Component
public class CustomHeadProcessor implements TemplateHeadProcessor {

private final PropertyPlaceholderHelper
PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}");
private final PluginWrapper pluginWrapper;

@Override
public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
return Mono.just(this.contactFormHtml()).doOnNext((html) -> {
IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(html));
}).then();
}

private String contactFormHtml() {
Properties properties = new Properties();
properties.setProperty("version", this.pluginWrapper.getDescriptor().getVersion());
properties.setProperty("pluginStaticPath", "/plugins/plugin-afdian/assets/static");
return this.PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders("", properties);
}

public CustomHeadProcessor(PluginWrapper pluginWrapper) {
this.pluginWrapper = pluginWrapper;
}
}
34 changes: 0 additions & 34 deletions src/main/java/site/xindu/afdian/service/DefaultSettingFetcher.java

This file was deleted.

12 changes: 7 additions & 5 deletions src/main/java/site/xindu/afdian/service/SponsorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.plugin.ReactiveSettingFetcher;
import site.xindu.afdian.entity.SponsorEntity;
import site.xindu.afdian.utils.DataUtils;
import site.xindu.afdian.utils.EncryptUtils;

@Service
Expand All @@ -34,7 +35,7 @@ public SponsorService(ReactiveSettingFetcher settingFetcher) {
* 获取第一页的爱发电赞助用户
*/
public Mono<JsonNode> getSponsorList(int pageNumber) {
return 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 @@ -52,10 +53,11 @@ public Mono<JsonNode> getSponsorList(int pageNumber) {
params.put("sign", signMd5);

return webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON) // JSON数据类型
.body(BodyInserters.fromValue(params)) // JSON字符串数据
.retrieve() // 获取响应体
.bodyToMono(JsonNode.class);
.body(BodyInserters.fromValue(params)) // JSON字符串数据
.retrieve() // 获取响应体
.bodyToMono(JsonNode.class);
});
return DataUtils.changePayTime(mono);
}

/**
Expand Down Expand Up @@ -83,6 +85,6 @@ public Mono<JsonNode> listAllSponsor() {
}
data.setList(sources);
});
return firstSponsorList;
return DataUtils.changePayTime(firstSponsorList);
}
}

This file was deleted.

Loading

0 comments on commit 71bef11

Please sign in to comment.