Merge branch 'main' of https://dd3skj.picp.vip/zhaoxinyu/v4
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package com.budwk.app.base.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.budwk.app.base.constant.RedisConstant;
|
||||
import com.budwk.app.base.exception.BaseException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nutz.integration.jedis.RedisService;
|
||||
import org.nutz.ioc.impl.PropertiesProxy;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author zzr
|
||||
* @name:DataCenterProperties
|
||||
* @Date 2025/9/15 15:34
|
||||
* @注释 数据中心配置文件,存放接口地址和参数配置
|
||||
*/
|
||||
@Slf4j
|
||||
@Data
|
||||
@IocBean(create = "init")
|
||||
public class DataCenterProperties {
|
||||
|
||||
@Inject
|
||||
private PropertiesProxy conf;
|
||||
@Inject
|
||||
private RedisService redisService;
|
||||
|
||||
private String appId;
|
||||
private String tokenUrl;
|
||||
private Map<String, String> codes = new HashMap<>();
|
||||
private Map<String, String> urls = new HashMap<>();
|
||||
|
||||
public void init() {
|
||||
Map<String, String> all = conf.toMap();
|
||||
appId = all.get("data-center.app-id");
|
||||
tokenUrl = all.get("data-center.token-url");
|
||||
extract(all, "data-center.codes.", codes);
|
||||
extract(all, "data-center.urls.", urls);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描配置 把 prefix 开头的 key 变成 Map
|
||||
* @param prefix 配置前缀
|
||||
* @param target 目标值
|
||||
*/
|
||||
private void extract(Map<String, String> all, String prefix, Map<String, String> target) {
|
||||
all.forEach((k, v) -> {
|
||||
if (k.startsWith(prefix)) {
|
||||
target.put(k.substring(prefix.length()), v);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取配置
|
||||
* @param key 接口类型枚举
|
||||
* @return 相关配置
|
||||
*/
|
||||
public Credential credential(String key) {
|
||||
String code = codes.get(key);
|
||||
String url = urls.get(key);
|
||||
if (code == null || url == null) {
|
||||
throw new BaseException("未知业务: " + key);
|
||||
}
|
||||
String token = getToken(Integer.parseInt(code), key);
|
||||
return new Credential(url, token);
|
||||
}
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public static class Credential {
|
||||
private String url;
|
||||
private String token;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* @param code
|
||||
* @param keyPrefix
|
||||
* @return token
|
||||
*/
|
||||
private String getToken(Integer code, String keyPrefix) {
|
||||
String dateCenterPrefix = RedisConstant.DATE_CENTER_PREFIX + keyPrefix;
|
||||
String token = redisService.get(dateCenterPrefix);
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
return token;
|
||||
} else {
|
||||
HttpRequest httpRequest = HttpUtil.createPost(tokenUrl);
|
||||
Map<String, Object> reqBody = Map.of(
|
||||
"appId", appId,
|
||||
"code", code
|
||||
);
|
||||
httpRequest.body(JSONUtil.toJsonStr(reqBody));
|
||||
JSONObject resp = JSONUtil.parseObj(httpRequest.execute().body());
|
||||
|
||||
if (resp.getInt("code") != 200) {
|
||||
throw new BaseException("获取token失败: " + resp.getStr("msg"));
|
||||
} else {
|
||||
JSONObject data = resp.getJSONObject("data");
|
||||
String tokenStr = data.getStr("token");
|
||||
Integer expire = data.getInt("expire");
|
||||
redisService.setex(dateCenterPrefix, expire, tokenStr);
|
||||
return tokenStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,4 +62,9 @@ public class RedisConstant {
|
||||
* 用户登录锁前缀
|
||||
*/
|
||||
public static final String USER_LOGIN_LOCK_PREFIX = PLATFORM_REDIS_PREFIX + "user:login:lock:";
|
||||
|
||||
/**
|
||||
* 数据接口锁前缀
|
||||
*/
|
||||
public static final String DATE_CENTER_PREFIX = PLATFORM_REDIS_PREFIX + "datacenter:token:";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user