221 lines
8.2 KiB
C#
221 lines
8.2 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
[Route("api/[controller]")]
|
||
[ApiController]
|
||
public class AccountController : ControllerBase
|
||
{
|
||
private readonly IAccountService _accountService;
|
||
private readonly List<K3CloudOption> _kingdeeOptions;
|
||
private readonly K3CloudApiUtils _utils;
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化控制器,加载ICO对象
|
||
/// </summary>
|
||
/// <param name="utils"></param>
|
||
/// <param name="accountService"></param>
|
||
/// <param name="logger"></param>
|
||
public AccountController(K3CloudApiUtils utils, IAccountService accountService)
|
||
{
|
||
_utils = utils;
|
||
_accountService = accountService;
|
||
_kingdeeOptions = _utils.GetKingdeeOptions();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 登录账号
|
||
/// </summary>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
[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, "登录失败");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取登录描述信息
|
||
/// </summary>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
[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<object>(loginValidate);
|
||
return AjaxResult.Success(reuslt);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取账套数据中心信息
|
||
/// </summary>
|
||
/// <param name="jModel"></param>
|
||
/// <returns></returns>
|
||
[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<List<Dictionary<string, dynamic>>>(result));
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 根据用户名返回组织范围
|
||
/// </summary>
|
||
/// <param name="dataCenter"></param>
|
||
/// <returns></returns>
|
||
[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
|
||
);
|
||
client.LoginByAppSecret(kingdeeOption.AcctID, kingdeeOption.UserName, kingdeeOption.AppID, kingdeeOption.AppSec, kingdeeOption.LCID);
|
||
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();
|
||
|
||
//queryParam.FilterString += @$"FUserAccount = '{dataCenter.UserName}'";
|
||
var requestInfo = queryParam.ToString();
|
||
var resultList = client.ExecuteBillQuery(requestInfo);
|
||
//var resultString = client.BillQuery(queryParam.ToString());
|
||
//// 包含ErrorCode认定为失败
|
||
//if (resultString.Contains("ErrorCode"))
|
||
//{
|
||
// var errorResult = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
// var responseStatus = errorResult?.Result?.ResponseStatus;
|
||
// Exception error = new K3CloudException("查看单据列表出错", responseStatus);
|
||
// throw error;
|
||
//}
|
||
|
||
//List<dynamic>? result = JsonSerializer.Deserialize<List<dynamic>>(resultString);
|
||
//if (result?.Count == 0)
|
||
//{
|
||
// return AjaxResult.Error(500, "用户名没有组织权限,或用户名不存在!");
|
||
//}
|
||
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
|
||
foreach (var item in resultList)
|
||
{
|
||
Dictionary<string, string> ResultItem = new Dictionary<string, string>();
|
||
ResultItem.Add("FUserID", item[0].ToString());
|
||
ResultItem.Add("FName", item[1].ToString());
|
||
ResultItem.Add("FUserAccount", item[2].ToString());
|
||
ResultItem.Add("FOrgOrgId", item[3].ToString());
|
||
ResultItem.Add("FOrgOrgNumber", item[4].ToString());
|
||
ResultItem.Add("FOrgOrgName", item[5].ToString());
|
||
result.Add(ResultItem);
|
||
}
|
||
|
||
|
||
return AjaxResult.Success(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 退出账号
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[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("未知错误");
|
||
}
|
||
}
|
||
} |