大幅优化性能;优化显示逻辑、效果
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
> 基于原作者v1.4.3代码修改
|
||||
|
||||
#### v1.4.3-6
|
||||
1. 大幅优化分类查询性能
|
||||
2. URL作者参数改为tag
|
||||
3. 作者分类依据改为“链接”插件ID(新旧作者姓名将归为一类)
|
||||
|
||||
#### v1.4.3-5
|
||||
1. 补充ui/package.json内"@rsbuild/core": "^1.4.3"依赖
|
||||
2. 新增单条朋友圈页面(前端模板friend-post.html;访问地址/friends/friend-post-xxxxx)
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
version=1.4.3-5
|
||||
version=1.4.3-6
|
||||
|
||||
@@ -3,6 +3,7 @@ package la.moony.friends.finders;
|
||||
import la.moony.friends.finders.impl.FriendFinderImpl;
|
||||
import la.moony.friends.vo.FriendPostVo;
|
||||
import la.moony.friends.vo.LinkGroupVo;
|
||||
import la.moony.friends.vo.LinkVo;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.extension.ListResult;
|
||||
@@ -27,4 +28,6 @@ public interface FriendFinder {
|
||||
Flux<LinkGroupVo> linkGroupBy();
|
||||
|
||||
Flux<String> listAllAuthors();
|
||||
|
||||
Flux<LinkVo> listAuthorLinks();
|
||||
}
|
||||
|
||||
@@ -112,6 +112,16 @@ public class FriendFinderImpl implements FriendFinder {
|
||||
.distinct();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<LinkVo> listAuthorLinks() {
|
||||
var listOptions = new ListOptions();
|
||||
listOptions.setFieldSelector(FieldSelector.of(isNull("metadata.deletionTimestamp")));
|
||||
return client.listAll(Link.class, listOptions, defaultLinkSort())
|
||||
.filter(link -> link.getSpec() != null
|
||||
&& StringUtils.isNotBlank(link.getSpec().getDisplayName()))
|
||||
.map(LinkVo::from);
|
||||
}
|
||||
|
||||
private Mono<ListResult<FriendPostVo>> pageFriendPost(ListOptions queryOptions, PageRequest page){
|
||||
var listOptions = new ListOptions();
|
||||
var query = all();
|
||||
|
||||
@@ -2,6 +2,7 @@ package la.moony.friends.rest;
|
||||
|
||||
import la.moony.friends.finders.FriendFinder;
|
||||
import la.moony.friends.vo.FriendPostVo;
|
||||
import la.moony.friends.vo.LinkVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -13,17 +14,18 @@ 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.extension.ListResult;
|
||||
import run.halo.app.plugin.ReactiveSettingFetcher;
|
||||
import run.halo.app.theme.TemplateNameResolver;
|
||||
import run.halo.app.theme.router.ModelConst;
|
||||
import run.halo.app.theme.router.PageUrlUtils;
|
||||
import run.halo.app.theme.router.UrlContextListResult;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static run.halo.app.theme.router.PageUrlUtils.totalPage;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@@ -47,12 +49,15 @@ public class FriendRouter {
|
||||
|
||||
private Mono<ServerResponse> handlerFunction(ServerRequest request) {
|
||||
return templateNameResolver.resolveTemplateNameOrDefault(request.exchange(), "friends")
|
||||
.flatMap( templateName -> ServerResponse.ok().render(templateName,
|
||||
java.util.Map.of(ModelConst.TEMPLATE_ID, templateName,
|
||||
"friends", friendPostList(request),
|
||||
"authors", friendFinder.listAllAuthors().collectList(),
|
||||
"title",getFriendTitle())
|
||||
));
|
||||
.flatMap(templateName -> {
|
||||
Mono<List<LinkVo>> authors = friendFinder.listAuthorLinks().collectList().cache();
|
||||
return ServerResponse.ok().render(templateName,
|
||||
Map.of(ModelConst.TEMPLATE_ID, templateName,
|
||||
"friends", friendPostList(request, authors),
|
||||
"authors", authors,
|
||||
"title", getFriendTitle())
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> friendPostDetail(ServerRequest request) {
|
||||
@@ -71,50 +76,38 @@ public class FriendRouter {
|
||||
"友链朋友圈");
|
||||
}
|
||||
|
||||
private Mono<UrlContextListResult<FriendPostVo>> friendPostList(ServerRequest request) {
|
||||
String path = request.path();
|
||||
private Mono<UrlContextListResult<FriendPostVo>> friendPostList(ServerRequest request,
|
||||
Mono<List<LinkVo>> authorLinks) {
|
||||
int pageNum = pageNumInPathVariable(request);
|
||||
String linkName = request.queryParam("linkName")
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.orElse(null);
|
||||
// --- 新增:获取 author 参数 ---
|
||||
String author = request.queryParam("author")
|
||||
String tag = request.queryParam("tag")
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.orElse(null);
|
||||
|
||||
// --- 新增:构建查询参数字符串 ---
|
||||
StringBuilder queryString = new StringBuilder();
|
||||
if (StringUtils.isNotBlank(linkName)) {
|
||||
queryString.append("linkName=").append(linkName);
|
||||
}
|
||||
if (StringUtils.isNotBlank(author)) {
|
||||
if (queryString.length() > 0) {
|
||||
queryString.append("&");
|
||||
}
|
||||
queryString.append("author=").append(author);
|
||||
}
|
||||
final String finalQueryString = queryString.toString();
|
||||
return authorLinks.flatMap(links -> {
|
||||
String linkName = linkNameByDisplayName(links, tag);
|
||||
String finalQueryString = buildQueryString(tag);
|
||||
|
||||
return this.settingFetcher.get("base")
|
||||
.map(item -> item.get("pageSize").asInt(10))
|
||||
.defaultIfEmpty(10)
|
||||
.flatMap(pageSize -> {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("page",pageNum);
|
||||
params.put("size",pageSize);
|
||||
if(StringUtils.isNotBlank(linkName)){
|
||||
params.put("linkName",linkName);
|
||||
if (StringUtils.isNotBlank(tag) && StringUtils.isBlank(linkName)) {
|
||||
return Mono.just(emptyFriendPostList(pageNum, pageSize, finalQueryString));
|
||||
}
|
||||
// --- 新增:把 author 也放进 params ---
|
||||
if(StringUtils.isNotBlank(author)){
|
||||
params.put("author",author);
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("page", pageNum);
|
||||
params.put("size", pageSize);
|
||||
if (StringUtils.isNotBlank(linkName)) {
|
||||
params.put("linkName", linkName);
|
||||
}
|
||||
|
||||
return friendFinder.list(params)
|
||||
.map(list -> {
|
||||
// --- 修改:生成链接时带上查询参数 ---
|
||||
String baseNextUrl = PageUrlUtils.nextPageUrl(path, totalPage(list));
|
||||
String basePrevUrl = PageUrlUtils.prevPageUrl(path);
|
||||
String baseNextUrl = "/friends?page=" + (list.getPage() + 1);
|
||||
String basePrevUrl = list.getPage() > 2
|
||||
? "/friends?page=" + (list.getPage() - 1)
|
||||
: "/friends";
|
||||
|
||||
String nextUrl = appendQueryString(baseNextUrl, finalQueryString);
|
||||
String prevUrl = appendQueryString(basePrevUrl, finalQueryString);
|
||||
@@ -126,6 +119,43 @@ public class FriendRouter {
|
||||
.build();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private UrlContextListResult<FriendPostVo> emptyFriendPostList(int page, int size,
|
||||
String queryString) {
|
||||
String nextUrl = appendQueryString("/friends?page=" + (page + 1), queryString);
|
||||
String prevUrl = appendQueryString(page > 2 ? "/friends?page=" + (page - 1) : "/friends",
|
||||
queryString);
|
||||
return new UrlContextListResult.Builder<FriendPostVo>()
|
||||
.listResult(new ListResult<>(page, size, 0, List.of()))
|
||||
.nextUrl(nextUrl)
|
||||
.prevUrl(prevUrl)
|
||||
.build();
|
||||
}
|
||||
|
||||
private String buildQueryString(String tag) {
|
||||
if (StringUtils.isNotBlank(tag)) {
|
||||
return "tag=" + encodeQueryValue(tag);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String linkNameByDisplayName(List<LinkVo> authorLinks, String displayName) {
|
||||
if (StringUtils.isBlank(displayName)) {
|
||||
return null;
|
||||
}
|
||||
return authorLinks.stream()
|
||||
.filter(link -> link.getMetadata() != null && link.getSpec() != null)
|
||||
.filter(link -> StringUtils.equals(displayName, link.getSpec().getDisplayName()))
|
||||
.map(link -> link.getMetadata().getName())
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private String encodeQueryValue(String value) {
|
||||
return URLEncoder.encode(value, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
// --- 新增:辅助方法,用于拼接查询参数 ---
|
||||
@@ -142,6 +172,9 @@ public class FriendRouter {
|
||||
|
||||
private int pageNumInPathVariable(ServerRequest request) {
|
||||
String page = request.pathVariables().get("page");
|
||||
if (StringUtils.isBlank(page)) {
|
||||
page = request.queryParam("page").orElse("1");
|
||||
}
|
||||
return NumberUtils.toInt(page, 1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user