diff --git a/ReZero/SuperAPI/ApiProvider/ParameterProvider/BindHttpParameters.cs b/ReZero/SuperAPI/ApiProvider/ParameterProvider/BindHttpParameters.cs index 418b8f9..bfa53a6 100644 --- a/ReZero/SuperAPI/ApiProvider/ParameterProvider/BindHttpParameters.cs +++ b/ReZero/SuperAPI/ApiProvider/ParameterProvider/BindHttpParameters.cs @@ -97,6 +97,47 @@ namespace ReZero.SuperAPI string rawJson = formDatas["_rawJson"].ToString(); System.Diagnostics.Debug.WriteLine($"使用_rawJson作为JObject参数: {rawJson}"); data.Value = rawJson; + + // 特殊处理登录接口:如果是token接口,则需要额外处理参数 + if (interInfo.Url == "/api/rezero/token" && dataModel.DefaultParameters.Count == 2) + { + try + { + // 解析原始JSON以获取登录凭证 + var loginParams = Newtonsoft.Json.JsonConvert.DeserializeObject>(rawJson); + if (loginParams != null) + { + // 将用户名和密码分别赋值给对应参数 + var userNameParam = dataModel.DefaultParameters.FirstOrDefault(p => p.Name?.EqualsCase("UserName") == true); + var passwordParam = dataModel.DefaultParameters.FirstOrDefault(p => p.Name?.EqualsCase("Password") == true); + + if (userNameParam != null && loginParams.ContainsKey("UserName")) + { + userNameParam.Value = loginParams["UserName"]; + } + else if (userNameParam != null && loginParams.ContainsKey("userName")) + { + userNameParam.Value = loginParams["userName"]; + } + + if (passwordParam != null && loginParams.ContainsKey("Password")) + { + passwordParam.Value = loginParams["Password"]; + } + else if (passwordParam != null && loginParams.ContainsKey("password")) + { + passwordParam.Value = loginParams["password"]; + } + + System.Diagnostics.Debug.WriteLine($"登录参数已解析: UserName={userNameParam?.Value}, Password={(passwordParam?.Value != null ? "********" : "null")}"); + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"解析登录参数异常: {ex.Message}"); + } + } + return; // 已找到JSON数据,提前返回 } @@ -241,6 +282,11 @@ namespace ReZero.SuperAPI } string parameterValue = context.Request.Query[parameter.Name]; var formData = formDatas.FirstOrDefault(it => it.Key.EqualsCase(parameter.Name ?? "")); + if (formData.Key == null) + { + // 尝试使用不区分大小写的方式查找 + formData = formDatas.FirstOrDefault(it => string.Equals(it.Key, parameter.Name, StringComparison.OrdinalIgnoreCase)); + } if (formData.Key != null) { parameterValue = formData.Value + ""; @@ -293,32 +339,44 @@ namespace ReZero.SuperAPI { System.Diagnostics.Debug.WriteLine($"原始请求体: {body}"); - // 先保存原始JSON,无论是否能解析都确保有原始数据 + // 检查是否是有效的JSON格式 if ((body.StartsWith("{") && body.EndsWith("}")) || (body.StartsWith("[") && body.EndsWith("]"))) { + // 保存原始JSON,仅当不需要将其解析为字典时使用 formDatas["_rawJson"] = body; - } - - try - { - var bodyParams = Newtonsoft.Json.JsonConvert.DeserializeObject>(body); - if (bodyParams != null) + // 检查请求头,如果是 application/json,并且第一个参数是预期为JObject类型, + // 则不要解析为字典,避免重复处理,以防止在BindDefaultParameters中的JObject处理逻辑冲突 + var contentType = context.Request.ContentType?.ToLower() ?? ""; + if (contentType.Contains("application/json")) + { + // 在JObject处理中,我们会直接使用_rawJson,所以这里不需要解析为字典 + // 仅保存原始JSON足够了 + System.Diagnostics.Debug.WriteLine("检测到JSON内容类型,保留原始JSON供JObject处理"); + return; + } + + try { - foreach (var item in bodyParams) + var bodyParams = Newtonsoft.Json.JsonConvert.DeserializeObject>(body); + + if (bodyParams != null) { - if (!formDatas.ContainsKey(item.Key)) + foreach (var item in bodyParams) { - formDatas[item.Key] = item.Value; + if (!formDatas.ContainsKey(item.Key)) + { + formDatas[item.Key] = item.Value; + } } } } - } - catch (Exception ex) - { - System.Diagnostics.Debug.WriteLine($"解析请求体异常: {ex.Message}"); - // 如果解析为字典失败,保持原始JSON已在上面保存 + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"解析请求体异常: {ex.Message}"); + // 如果解析为字典失败,原始JSON已保存 + } } } } diff --git a/SuperAPI/wwwroot/rezero/default_ui/authorization.html b/SuperAPI/wwwroot/rezero/default_ui/authorization.html index e99f831..15bd051 100644 --- a/SuperAPI/wwwroot/rezero/default_ui/authorization.html +++ b/SuperAPI/wwwroot/rezero/default_ui/authorization.html @@ -109,6 +109,7 @@ axios.post("/api/rezero/token?supportCustomDto=true", { userName: this.userName, password: this.password+"" }, jwHeader) .then(response => { this.error = null; + debugger this.result = response.data; }) .catch(error => { diff --git a/SuperAPI/wwwroot/rezero/default_ui/login.html b/SuperAPI/wwwroot/rezero/default_ui/login.html index 8704da3..b3bce1c 100644 --- a/SuperAPI/wwwroot/rezero/default_ui/login.html +++ b/SuperAPI/wwwroot/rezero/default_ui/login.html @@ -170,6 +170,7 @@ .then(response => { this.error = null; this.result = response.data; + debugger if (response.data.message) { th.message = response.data.message; } else {