using Gatedge.K3Cloud.Utils; using Gatedge.K3Cloud.Utils.Common; using Gatedge.K3Cloud.Utils.Exceptions; using Gatedge.K3Cloud.Utils.Model.K3Request; using Gatedge.K3Cloud.Utils.Model.K3Result; using Gatedge.K3Cloud.Utils.Option; using Gatedge.ScanCode.Common; using Gatedge.ScanCode.Extension; using Gatedge.ScanCode.Models.Dto; using Gatedge.ScanCode.Services.IServices; using Kingdee.CDP.WebApi.SDK; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Data; using System.Text.Json; namespace Gatedge.ScanCode.Controllers { /// /// /// [Route("api/[controller]")] [ApiController] public class AccountController : ControllerBase { private readonly IAccountService _accountService; private readonly List _kingdeeOptions; private readonly K3CloudApiUtils _utils; /// /// 初始化控制器,加载ICO对象 /// /// /// /// public AccountController(K3CloudApiUtils utils, IAccountService accountService) { _utils = utils; _accountService = accountService; _kingdeeOptions = _utils.GetKingdeeOptions(); } /// /// 登录账号 /// /// /// [HttpPost("login")] public AjaxResult Login(LoginInfo loginInfo) { var clienter = _utils.CreateDefaultK3CloudApi(loginInfo); //数据中心DBID修改为由前端传参 var loginValidate = clienter.Login(loginInfo.DBID, loginInfo.UserName, loginInfo.Password, loginInfo.LCId); if (loginValidate) { var token = _accountService.Login(loginInfo); var ajaxResult = AjaxResult.Success(); ajaxResult.Add("token", token); return ajaxResult; } return AjaxResult.Error(500, "登录失败"); } /// /// 获取登录描述信息 /// /// /// [HttpPost("getDetails")] [Authorize] public AjaxResult GetDetails(LoginInfo loginInfo) { // 初始化连接对象 _utils.InitCloudApi(loginInfo); K3CloudApi clienter = _utils.CreateDefaultK3CloudApi(loginInfo); var loginValidate = clienter.ValidateLogin( loginInfo.DBID, loginInfo.UserName, loginInfo.Password, loginInfo.LCId ); var reuslt = JsonSerializer.Deserialize(loginValidate); return AjaxResult.Success(reuslt); } /// /// 获取账套数据中心信息 /// /// /// [HttpPost("GetDataCenterList")] public AjaxResult GetDataCenterList(DataCenter jModel) { if (string.IsNullOrWhiteSpace(jModel.ServerUrl)) { return AjaxResult.Error(500, "ServerUrl为空"); } //访问金蝶官方接口获取数据中心 HttpClients httpClient = new HttpClients(); httpClient.Url = jModel.ServerUrl + "/Kingdee.BOS.ServiceFacade.ServicesStub.Account.AccountService.GetDataCenterList.common.kdsvc"; string result = httpClient.Post(); return AjaxResult.Success(JsonSerializer.Deserialize>>(result)); } /// /// 根据用户名返回组织范围 /// /// /// [HttpPost("SearchOrg")] public AjaxResult SearchOrg([FromBody] DataCenter dataCenter) { if (string.IsNullOrWhiteSpace(dataCenter.UserName)) { return AjaxResult.Error(500, "用户名为空"); } if (string.IsNullOrWhiteSpace(dataCenter.DBID)) { return AjaxResult.Error(500, "DBID为空"); } if (string.IsNullOrWhiteSpace(dataCenter.ServerUrl)) { return AjaxResult.Error(500, "ServerUrl为空"); } K3CloudOption? kingdeeOption = _kingdeeOptions .Where(n => n.AcctID == dataCenter.DBID && n.ServerUrl.ToUpperInvariant() == dataCenter.ServerUrl.ToUpperInvariant()) .FirstOrDefault(); if (kingdeeOption == null) { return AjaxResult.Error(500, "配置文件没有对应的第三方授权登录信息!"); } //组装登录信息 K3CloudApi client = new K3CloudApi(); client.InitClient( acctID: kingdeeOption.AcctID, appID: kingdeeOption.AppID, appSec: kingdeeOption.AppSec, serverUrl: kingdeeOption.ServerUrl, userName: kingdeeOption.UserName, lcid: kingdeeOption.LCID, orgNum: kingdeeOption.OrgNumber ); Query queryParam = new Query() { FormId = "SEC_User", FieldKeys = "FUserID,FName,FUserAccount,FOrgOrgId,FOrgOrgId.FNumber,FOrgOrgName", }; FilterList filterString = new FilterList(); FilterItem FNameItem = new FilterItem("FUserAccount", "67", dataCenter.UserName, "0"); filterString.AddFilterItem(FNameItem); queryParam.FilterString = filterString.GetFilterString(); var resultString = client.BillQuery(queryParam.ToString()); // 包含ErrorCode认定为失败 if (resultString.Contains("ErrorCode")) { var errorResult = JsonSerializer.Deserialize(resultString); var responseStatus = errorResult?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据列表出错", responseStatus); throw error; } List? result = JsonSerializer.Deserialize>(resultString); if (result?.Count == 0) { return AjaxResult.Error(500, "用户名没有组织权限,或用户名不存在!"); } return AjaxResult.Success(result); } /// /// 退出账号 /// /// [HttpPost("logout")] [Authorize] public AjaxResult Logout() { // 初始化连接对象 var loginInfo = User.GetLoginInfoByClaimsPrincipal(); _utils.InitCloudApi(loginInfo); K3CloudApi clienter = _utils.GetApiClient(); var isLogout = clienter.Logout(); if (isLogout) { return AjaxResult.Success(); } return AjaxResult.Error("未知错误"); } } }