feat(一周安排小组件): 外跳入口配置(link_app_code/link_path) + 地址按应用注册表解析
build / build-push (push) Successful in 2m24s
build / build-push (push) Successful in 2m24s
- 新增 WidgetLinkResolver:走 SDK OpenApiAppClient.getAppByCode 取 pc_url,归一化(丢 fragment/去尾斜杠)+#+路由;60s TTL+30s 短路;查无/不可达/路由写绝对 URL 一律 null(不猜、不渲染) - widget_catalog 与 widget_school_setting 各加 link_app_code/link_path(校管可覆盖本校,NULL=沿用全局) - 生效值合成走 COALESCE;新增「两列必须成对」校验;「全空=清除覆盖」判定由 4 字段扩到 6 字段 - getUserSettings 按需下发 overviewUrl(解析不出则不 put) - sql/widget_catalog_v3.sql(生产 proj11 已执行两条 ALTER + weekplan 配置) - seeder 空库给 weekplan 默认 work-week-plan + /weekplan
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package com.yunhai.userhome.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.yunhai.platform.openapi.base.OpenApiAppClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 小组件外跳地址解析:{@code app_code} → 应用注册表 {@code pc_url} → 绝对跳转地址。
|
||||
*
|
||||
* <p>「地址不写死」落点:库里只存**子应用 code + 路由**,域名一律来自应用注册表
|
||||
* (UserBase {@code /oapi/app/by-code/{appCode}},经 SDK {@link OpenApiAppClient#getAppByCode} 调用,
|
||||
* 签名自动)。拼接口径与消息中心 {@code jumpUrl} 一致:{@code pc_url} 归一化
|
||||
* (丢 fragment、去尾斜杠)后拼 {@code "#" + path}。</p>
|
||||
*
|
||||
* <p>降级纪律:未配置 / 查无 pc_url / 注册表不可达 / 路由写成绝对 URL —— 一律返回 {@code null},
|
||||
* 调用方不渲染跳转入口(pc_url 空就是空,不猜测、不回落)。</p>
|
||||
*
|
||||
* <p>缓存与短路:注册表地址 60s TTL(低频变更,避免每次进桌面都打一次 UserBase);查询失败进 30s
|
||||
* 短路窗口,窗口内沿用旧缓存,避免下游停摆时每次渲染都吃一次连接超时。</p>
|
||||
*
|
||||
* <p>TODO 跨服务公共抽取:本实现只解决「小组件这一侧」;消息中心 {@code MsgCenterProxyService}
|
||||
* 仍保留自己的 by-code 缓存与短路窗口。待平台层({{@code yh-spring-boot-starter}})提供带缓存的
|
||||
* 地址解析,或 UserBase oapi 直接下发归一化跳转基址后,本类应退化为薄壳。</p>
|
||||
*/
|
||||
@Service
|
||||
public class WidgetLinkResolver {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WidgetLinkResolver.class);
|
||||
|
||||
/** 注册表地址缓存时长 */
|
||||
private static final long PC_URL_TTL_MS = 60_000L;
|
||||
|
||||
/** 注册表不可达之后的静默期 */
|
||||
private static final long PC_URL_DOWN_MS = 30_000L;
|
||||
|
||||
@Autowired
|
||||
private OpenApiAppClient appClient;
|
||||
|
||||
/** app_code → pc_url(空串 = 查无,避免脏 code 反复出站) */
|
||||
private final ConcurrentHashMap<String, String> pcUrlCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final ConcurrentHashMap<String, Long> pcUrlAt = new ConcurrentHashMap<>();
|
||||
|
||||
/** 注册表不可达短路截止时间戳 */
|
||||
private volatile long downUntil;
|
||||
|
||||
/**
|
||||
* 解析外跳地址;无法解析返回 null(调用方不渲染入口)。
|
||||
*
|
||||
* @param appCode 应用注册表 app_code,如 {@code work-week-plan}
|
||||
* @param linkPath 子应用内路由(hash 路由),如 {@code /weekplan};允许写成 {@code #/weekplan} 或 {@code weekplan}
|
||||
*/
|
||||
public String resolve(String appCode, String linkPath) {
|
||||
if (!StringUtils.hasText(appCode) || !StringUtils.hasText(linkPath)) {
|
||||
return null;
|
||||
}
|
||||
String path = normalizePath(linkPath);
|
||||
if (path == null) {
|
||||
log.warn("[widget] 外跳路由不合法(应为子应用内相对路由,禁止写绝对 URL):app={} path={}", appCode, linkPath);
|
||||
return null;
|
||||
}
|
||||
String pcUrl = pcUrlOf(appCode.trim());
|
||||
if (!StringUtils.hasText(pcUrl)) {
|
||||
return null;
|
||||
}
|
||||
String base = pcUrl.trim();
|
||||
int hash = base.indexOf('#');
|
||||
if (hash >= 0) {
|
||||
base = base.substring(0, hash);
|
||||
}
|
||||
while (base.endsWith("/")) {
|
||||
base = base.substring(0, base.length() - 1);
|
||||
}
|
||||
if (!StringUtils.hasText(base)) {
|
||||
return null;
|
||||
}
|
||||
return base + "#" + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由归一化:去首部 {@code #}、去空白,保证以 {@code /} 开头;
|
||||
* 绝对 URL(http/https)视为配置错误返回 null(域名只能来自注册表)。
|
||||
*/
|
||||
private String normalizePath(String linkPath) {
|
||||
String path = linkPath.trim();
|
||||
if (path.startsWith("http://") || path.startsWith("https://")) {
|
||||
return null;
|
||||
}
|
||||
if (path.startsWith("#")) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
if (path.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return path.startsWith("/") ? path : "/" + path;
|
||||
}
|
||||
|
||||
/** 注册表 pc_url 查询:命中缓存则返回;查无记空串;不可达走短路窗口(窗口内沿用旧值) */
|
||||
private String pcUrlOf(String appCode) {
|
||||
Long at = pcUrlAt.get(appCode);
|
||||
if (at != null && System.currentTimeMillis() - at < PC_URL_TTL_MS) {
|
||||
return emptyToNull(pcUrlCache.get(appCode));
|
||||
}
|
||||
if (System.currentTimeMillis() < downUntil) {
|
||||
return emptyToNull(pcUrlCache.get(appCode));
|
||||
}
|
||||
try {
|
||||
JsonNode app = appClient.getAppByCode(appCode);
|
||||
downUntil = 0L;
|
||||
String pcUrl = app == null || app.isNull() ? null : text(app, "pcUrl");
|
||||
pcUrlCache.put(appCode, pcUrl == null ? "" : pcUrl);
|
||||
pcUrlAt.put(appCode, System.currentTimeMillis());
|
||||
if (!StringUtils.hasText(pcUrl)) {
|
||||
log.warn("[widget] 注册表无 pc_url,外跳入口不渲染:app={}", appCode);
|
||||
}
|
||||
return pcUrl;
|
||||
} catch (Exception e) {
|
||||
downUntil = System.currentTimeMillis() + PC_URL_DOWN_MS;
|
||||
log.warn("[widget] 应用注册表不可达({}ms 内外跳入口沿用旧值/不渲染):app={} {}",
|
||||
PC_URL_DOWN_MS, appCode, e.toString());
|
||||
return emptyToNull(pcUrlCache.get(appCode));
|
||||
}
|
||||
}
|
||||
|
||||
private String emptyToNull(String value) {
|
||||
return StringUtils.hasText(value) ? value : null;
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String field) {
|
||||
JsonNode value = node.get(field);
|
||||
return value == null || value.isNull() ? null : value.asText();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user