refactor: DTO 字段去下划线 + SimApiBaseResponse 不再可继承 + 删除 SimApiJson/toJsonString
- 纯 POCO DTO 字段去下划线:AuthDto(id/name/applicationId/profileId/scene 等)、 SimApiLoginItem(id/types/meta/extra)、AesBodyRequest(data)、 SimApiBaseResponse 系列(code/message/data/list/page/count/total) - SimApiBaseResponse 去 open(不再允许继承)+ 删 @SerializerParent(无子类继承) - SimApiResponse<T> / SimApiDataResponse 改为组合(自带 code/message/data) - 删除 toJsonString(无使用点,统一走 simapi_serialization) - 删除 SimApiJson.cj:json() → SimApiUtil.json() 直接序列化; escapeJson() 迁入 SimApiUtil - 同步更新所有字段引用(AuthCenter/AuthIam/HttpClient/ExceptionMiddleware 等) - 保留 sqlsharp 实体 User/Bot 的 _ 前缀(soulsoft 宏依赖)
This commit is contained in:
@@ -17,7 +17,7 @@ import simapi.interfaces.*
|
||||
* AES body 请求({"data": "密文"},对齐 C# SimApiOneFieldRequest<string>)。
|
||||
*/
|
||||
public class AesBodyRequest {
|
||||
public var _data: String = ""
|
||||
public var data: String = ""
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +54,7 @@ public class SimApiAesBodyChecker {
|
||||
|
||||
// 2. 反序列化 {"data": "密文"}
|
||||
let req = JsonSerializer.Deserialize<AesBodyRequest>(body)
|
||||
if (req._data.isEmpty()) {
|
||||
if (req.data.isEmpty()) {
|
||||
SimApiError.error(code: 400, message: "请求体缺少密文Data字段")
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public class SimApiAesBodyChecker {
|
||||
}
|
||||
|
||||
// 5. 解密
|
||||
let jsonStr = SimApiAesUtil.decrypt(req._data, key.getOrThrow())
|
||||
let jsonStr = SimApiAesUtil.decrypt(req.data, key.getOrThrow())
|
||||
if (jsonStr.isEmpty()) {
|
||||
SimApiError.error(code: 400, message: "解密失败")
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ public class SimApiAuth {
|
||||
public func login(loginItem: SimApiLoginItem, expireSeconds!: Int64 = 604800, token!: String = ""): String {
|
||||
let newToken = if (token.isEmpty()) { generateToken() } else { token }
|
||||
let tokenKey = "${tokenCachePrefix}${newToken}"
|
||||
let setKey = "${tokenSetCachePrefix}${loginItem._id}"
|
||||
let setKey = "${tokenSetCachePrefix}${loginItem.id}"
|
||||
let json = loginItemJson(loginItem)
|
||||
|
||||
if (let Some(redis) <- _redis) {
|
||||
@@ -93,10 +93,10 @@ public class SimApiAuth {
|
||||
redis.expire(setKey, expireSeconds)
|
||||
} else {
|
||||
_tokenStore[newToken] = TokenEntry(json, nowMillis() + expireSeconds * 1000)
|
||||
var tokens = _userTokens.get(loginItem._id)
|
||||
var tokens = _userTokens.get(loginItem.id)
|
||||
if (tokens == None) {
|
||||
tokens = HashSet<String>()
|
||||
_userTokens[loginItem._id] = tokens.getOrThrow()
|
||||
_userTokens[loginItem.id] = tokens.getOrThrow()
|
||||
}
|
||||
tokens.getOrThrow().add(newToken)
|
||||
}
|
||||
@@ -187,7 +187,7 @@ public class SimApiAuth {
|
||||
public func logout(token: String): Unit {
|
||||
let item = getLogin(token)
|
||||
if (let Some(item) <- item) {
|
||||
removeTokenOfUser(item._id, token)
|
||||
removeTokenOfUser(item.id, token)
|
||||
}
|
||||
let tokenKey = "${tokenCachePrefix}${token}"
|
||||
if (let Some(redis) <- _redis) {
|
||||
|
||||
@@ -131,8 +131,8 @@ public open class SimApiHttpClient {
|
||||
SimApiError.errorWhenFalse(response.isSuccessStatusCode, code: response.statusCode, message: "HTTP ERROR: ${response.statusCode}")
|
||||
let json = response.content.readAsString()
|
||||
let result = JsonSerializer.Deserialize<SimApiResponse<T>>(json)
|
||||
SimApiError.errorWhen(result._code != 200, code: result._code, message: result._message)
|
||||
return result._data.getOrThrow()
|
||||
SimApiError.errorWhen(result.code != 200, code: result.code, message: result.message)
|
||||
return result.data.getOrThrow()
|
||||
} finally {
|
||||
response.close()
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ struct SimApiActionInvoker {
|
||||
let requiredTypes = auth.`type`.split(",")
|
||||
var matched = false
|
||||
for (t in requiredTypes) {
|
||||
if (loginItem._types.contains(t)) {
|
||||
if (loginItem.types.contains(t)) {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
|
||||
@@ -172,10 +172,34 @@ public class SimApiUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象序列化为 JSON 字符串(委托给 SimApiJson.json 统一实现,对齐 C# SimApiUtil.Json)。
|
||||
* 将对象序列化为 JSON 字符串(对齐 C# SimApiUtil.Json)。
|
||||
* @param obj 任意对象(None 输出 null)。
|
||||
*/
|
||||
public static func json(obj: ?Any): String {
|
||||
SimApiJson.json(obj)
|
||||
if (let Some(obj) <- obj) {
|
||||
return JsonSerializer.Serialize(obj)
|
||||
}
|
||||
"null"
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON 字符串转义(对齐 C# 内部转义逻辑)。
|
||||
* @param s 原始字符串。
|
||||
* @return 转义后可直接放入 JSON 字符串字面量的内容。
|
||||
*/
|
||||
public static func escapeJson(s: String): String {
|
||||
var sb = StringBuilder()
|
||||
for (c in s.runes()) {
|
||||
match (c) {
|
||||
case '"' => sb.append("\\\"")
|
||||
case '\\' => sb.append("\\\\")
|
||||
case '\n' => sb.append("\\n")
|
||||
case '\r' => sb.append("\\r")
|
||||
case '\t' => sb.append("\\t")
|
||||
case _ => sb.append(c)
|
||||
}
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,7 +218,7 @@ public class SimApiUtil {
|
||||
* @return Base64 字符串。
|
||||
*/
|
||||
public static func base64Encode(obj: Any): String {
|
||||
let json = SimApiJson.json(Some(obj))
|
||||
let json = json(Some(obj))
|
||||
base64Encode(json)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user