feat: Auth/Cache/Util 增强

- SimApiAuth/Cache:Redis 连接串支持密码/DB 索引(host:port,password=xxx,db=2)
- InMemory 模式加过期机制(TokenEntry/CacheEntry 带 expireAt,过期自动移除)
- Redis 客户端 autoHello=false 兼容 Redis 8.x(HELLO 3 RESP3 响应解析失败)
- SimApiAuth.generateToken 用 UUID v4
- SimApiUtil 补齐:newGuid / fromJson<T> / base64Encode(Any) / base64DecodeTo<T>
This commit is contained in:
2026-08-16 22:46:57 +08:00
parent a73a986f8b
commit 8a6c783459
3 changed files with 196 additions and 26 deletions
+36
View File
@@ -11,7 +11,9 @@ import std.random.*
import stdx.crypto.digest.*
import stdx.encoding.hex.*
import stdx.encoding.base64.*
import stdx.encoding.json.*
import std.regex.*
import soulsoft_serialization.*
import simapi.communications.*
import simapi.macros.*
@@ -153,4 +155,38 @@ public class SimApiUtil {
public static func json(obj: ?Any): String {
SimApiJson.json(obj)
}
/**
* 从 JSON 字符串反序列化为 T(对齐 C# SimApiUtil.FromJson<T>)。
* @param T 目标类型(需实现 ISerialization<T>,如 @Serialization DTO、基础类型等)。
* @param jsonString JSON 字符串。
* @return 反序列化结果。
*/
public static func fromJson<T>(jsonString: String): T where T <: ISerialization<T> {
JsonSerializer.deserializeObject<T>(jsonString)
}
/**
* 对象 Base64 编码(对象 → JSON → Base64,对齐 C# Base64Encode(object))。
* @param obj 任意对象(DTO/基础类型/HashMap 等)。
* @return Base64 字符串。
*/
public static func base64Encode(obj: Any): String {
let json = if (let ser: ISerializable <- obj) {
ser.serializeObject().toJson().toString()
} else {
SimApiJson.json(Some(obj))
}
base64Encode(json)
}
/**
* Base64 → JSON → T 反序列化(对齐 C# Base64Decode<T>)。
* @param T 目标类型(需实现 ISerialization<T>)。
* @param base64Str Base64 字符串。
* @return 反序列化结果。
*/
public static func base64DecodeTo<T>(base64Str: String): T where T <: ISerialization<T> {
fromJson<T>(base64Decode(base64Str))
}
}