diff --git a/VOL.WebApi/Startup.cs b/VOL.WebApi/Startup.cs index 8abaa49..21da34f 100644 --- a/VOL.WebApi/Startup.cs +++ b/VOL.WebApi/Startup.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Autofac; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -24,6 +18,13 @@ using Newtonsoft.Json; using Quartz; using Quartz.Impl; using Swashbuckle.AspNetCore.SwaggerGen; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; using VOL.Core.Configuration; using VOL.Core.DBManager; using VOL.Core.Extensions; @@ -227,6 +228,20 @@ namespace VOL.WebApi ServeUnknownFileTypes = true }); app.Use(HttpRequestMiddleware.Context); + app.Use(async (context, next) => + { + // 将请求路径中连续的 "//" 替换为单个 "/" + var pathValue = context.Request.Path.Value ?? string.Empty; + if (pathValue.Contains("//")) + { + var normalized = Regex.Replace(pathValue, "/{2,}", "/"); + context.Request.Path = new PathString(normalized); + } + + // Do work that can write to the Response. + await next.Invoke(); + // Do logging or other work that doesn't write to the Response. + }); //2021.06.27增加创建默认upload文件夹 string _uploadPath = (env.ContentRootPath + "/Upload").ReplacePath(); diff --git a/VOL.WebApi/appsettings.json b/VOL.WebApi/appsettings.json index ff98603..b729ad1 100644 --- a/VOL.WebApi/appsettings.json +++ b/VOL.WebApi/appsettings.json @@ -21,6 +21,7 @@ //mysql连接字符串 "DbConnectionString": " Data Source=127.0.0.1;Database=laservall_data;AllowLoadLocalInfile=true;User ID=root;Password=1a2b3c4d@-=-=-=;allowPublicKeyRetrieval=true;pooling=true;CharSet=utf8;port=43306;sslmode=none;", + //"DbConnectionString": " Data Source=192.168.88.129;Database=laservall_data;AllowLoadLocalInfile=true;User ID=root;Password=rd5ZytWRknEtmjwW;allowPublicKeyRetrieval=true;pooling=true;CharSet=utf8;port=43306;sslmode=none;", //PgSql连接字符串 // "DbConnectionString": "Host=127.0.0.1;Port=5432;User id=postgres;password=jxx_abcd;Database=vol_v3;", @@ -32,7 +33,7 @@ "RedisConnectionString": "127.0.0.1,SyncTimeout=15000", //redis连接字符串(最好加密) "UseRedis": "true", //是否使用redis,如果不使用,默认使用Memory内置缓存 - "UseSignalR": "true" //是否使用SignalR,注意需要将端的地址配置到下面的CorsUrls属性中 + "UseSignalR": "false" //是否使用SignalR,注意需要将端的地址配置到下面的CorsUrls属性中 }, "Secret": { //秘钥配置 "JWT": "AA3925441FFA4B5DB4E64A29B53CE413", //jwt这里一定修改下,任意修改几个字符的值 diff --git a/VOL.YSErp/Models/YSConfig.cs b/VOL.YSErp/Models/YSConfig.cs index 69424a8..d5340e7 100644 --- a/VOL.YSErp/Models/YSConfig.cs +++ b/VOL.YSErp/Models/YSConfig.cs @@ -16,7 +16,7 @@ namespace VOL.YSErp.Models public class YSConfig { public string ApiUrl = "https://c4.yonyoucloud.com"; // 正式环境 -> c4, 测试环境 -> c1 - public string Appkey = "6fe5b0b6163e45c081cc9a40848d5840";//"08f9377fd9ae45219e7dd3fbebb9858a";//"99355863c0ed4363b09f8511d48f67e4"; - public string AppSecret = "ceeca798e84268a2d59f97c860e4c1223bf786df";//"8f4b0bf535fcdb6fa5b8ce5c630b740ddaa3b3be";//"3ca452060b684ccc982836cd5f0b6116811cb8ce"; + public string Appkey = "6fe5b0b6163e45c081cc9a40848d5840"; + public string AppSecret = "ceeca798e84268a2d59f97c860e4c1223bf786df"; } } diff --git a/VOL.YSErp/Services/Biz/YSERPService.cs b/VOL.YSErp/Services/Biz/YSERPService.cs index 00a6c3f..1fc1b19 100644 --- a/VOL.YSErp/Services/Biz/YSERPService.cs +++ b/VOL.YSErp/Services/Biz/YSERPService.cs @@ -24,10 +24,11 @@ namespace VOL.YSErp.Services.Biz // YS API URLs private readonly string _tokenUrl; - private readonly string _employeeListUrl; - private readonly string _updateEmployeeUrl; + private readonly string _employeeListUrl; private readonly string _deptListUrl; private readonly string _empInfoUrl; + private static readonly string[] resposeSuccessCodes = ["200", "00000"]; + public YSERPService(SystemToken token, YSConfig config) { _token = token; @@ -38,7 +39,7 @@ namespace VOL.YSErp.Services.Biz _tokenUrl = $"{_apiUrl}/iuap-api-auth/open-auth/selfAppAuth/getAccessToken"; _employeeListUrl = $"{_apiUrl}/iuap-api-gateway/yonbip/hrcloud/staff/listdetailmdd"; - _updateEmployeeUrl = $"{_apiUrl}/iuap-api-gateway/yonbip/hrcloud/batchInsertOrUpdateDetailNew"; + //_updateEmployeeUrl = $"{_apiUrl}/iuap-api-gateway/yonbip/hrcloud/batchInsertOrUpdateDetailNew"; _deptListUrl = $"{_apiUrl}/iuap-api-gateway/yonbip/digitalModel/basedoc/dept/list"; _empInfoUrl = $"{_apiUrl}/iuap-api-gateway/yonbip/hrcloud/HRCloud/getStaffDetail"; } @@ -160,20 +161,20 @@ namespace VOL.YSErp.Services.Biz var token = await GetValidTokenAsync(); if (string.IsNullOrEmpty(token)) { - return new List(); + return []; } var allEmployees = new List(); var pageIndex = 1; const int pageSize = 50; - Logger.Info($"员工信息API: {_employeeListUrl}"); + //Logger.Info($"员工信息API: {_employeeListUrl}"); while (true) { try { - Logger.Info($"获取YS员工数据,第 {pageIndex} 页..."); + //Logger.Info($"获取YS员工数据,第 {pageIndex} 页..."); var payload = new { @@ -195,24 +196,25 @@ namespace VOL.YSErp.Services.Biz { Logger.Error("认证失败: 无效的API令牌或权限不足"); _accessToken = null; - return []; + //return []; + throw new Exception("认证失败: 无效的YS ERP API令牌或权限不足"); } response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var data = JsonSerializer.Deserialize>(content); - if (!new[] { "200", "00000" }.Contains(data.code)) + if (data == null || !resposeSuccessCodes.Contains(data.code)) { - var errorMsg = data.message; + var errorMsg = data?.message; Logger.Error($"API业务错误: {errorMsg}"); - throw new Exception(errorMsg); + throw new Exception($"API业务错误: 【{errorMsg}】"); } var recordList = data.data.recordList; if (recordList?.Count == 0) { - Logger.Info("没有更多员工记录"); + //Logger.Info("没有更多员工记录"); break; } @@ -240,68 +242,7 @@ namespace VOL.YSErp.Services.Biz //_logger.LogInformation($"共获取 {allEmployees.Count} 条YS员工记录"); return allEmployees; } - - public async Task UpdateEmployeesAsync(List> employeeUpdates) - { - var token = await GetValidTokenAsync(); - if (string.IsNullOrEmpty(token)) - { - return false; - } - - //_logger.LogInformation($"YS更新内容: {JsonSerializer.Serialize(employeeUpdates)}"); - - try - { - var request = new HttpRequestMessage(HttpMethod.Post, $"{_updateEmployeeUrl}?access_token={token}") - { - Content = new StringContent( - JsonSerializer.Serialize(new { data = employeeUpdates }), - Encoding.UTF8, - "application/json") - }; - - var response = await _httpClient.SendAsync(request); - //_logger.LogInformation($"YS更新响应状态: {response.StatusCode}"); - - var content = await response.Content.ReadAsStringAsync(); - //_logger.LogInformation($"YS更新响应内容: {content}"); - - response.EnsureSuccessStatusCode(); - var data = JsonSerializer.Deserialize(content); - - if (data.GetProperty("code").GetString() == "00000") - { - //_logger.LogInformation($"成功更新 {employeeUpdates.Count} 名员工信息"); - - if (data.TryGetProperty("data", out var resultData)) - { - - if (resultData.TryGetProperty("messages", out var messages)) - { - foreach (var msg in messages.EnumerateArray()) - { - //_logger.LogWarning($"失败信息: {msg}"); - } - } - } - - return true; - } - else - { - var errorMsg = data.GetProperty("message").GetString(); - Logger.Error($"更新失败: {errorMsg}"); - return false; - } - } - catch (Exception ex) - { - Logger.Error( $"更新员工信息失败,{ex}"); - return false; - } - } - + public async Task> GetAllDepartmentsAsync() { var token = await GetValidTokenAsync(); @@ -310,8 +251,7 @@ namespace VOL.YSErp.Services.Biz return []; } var allDepts = new List(); - - //Logger.Info($"员工信息API: {_employeeListUrl}"); + try { @@ -344,25 +284,24 @@ namespace VOL.YSErp.Services.Biz response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var data = JsonSerializer.Deserialize>(content); - if (!new[] { "200", "00000" }.Contains(data.code)) + if (data == null || !resposeSuccessCodes.Contains(data.code)) { - var errorMsg = data.message; + var errorMsg = data?.message; Logger.Error($"API业务错误: {errorMsg}"); - throw new Exception(errorMsg); + throw new Exception($"API业务错误:【{errorMsg}】"); } - var recordList = data.data; if(recordList?.Count == 0) { - Logger.Info("没有更多部门记录"); + //Logger.Info("没有更多部门记录"); return allDepts; } else { allDepts.AddRange(recordList ?? []); - } + } } catch (Exception ex) @@ -403,17 +342,18 @@ namespace VOL.YSErp.Services.Biz { Logger.Error("认证失败: 无效的API令牌或权限不足"); _accessToken = null; - return new YSERPEmployeeInfo(); + //return new YSERPEmployeeInfo(); + throw new Exception("认证失败: 无效的YS ERP API令牌或权限不足"); } response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var data = JsonSerializer.Deserialize>(content); - if (!new[] { "200", "00000" }.Contains(data.code)) + if (data==null || !resposeSuccessCodes.Contains(data.code)) { - var errorMsg = data.message; + var errorMsg = data?.message; Logger.Error($"API业务错误: {errorMsg}"); - throw new Exception(errorMsg); + throw new Exception($"API业务错误: 【{errorMsg}】"); } return data.data;