using Gatedge.K3Cloud.Utils.Exceptions; using Gatedge.K3Cloud.Utils.Model.K3Request; using Gatedge.K3Cloud.Utils.Model.K3Result; using Gatedge.K3Cloud.Utils.Model.K3Result.Model; using Gatedge.K3Cloud.Utils.Option; using Kingdee.CDP.WebApi.SDK; using Kingdee.CDP.WebApi.SDK.DataEntity; using System.Text.Json; namespace Gatedge.K3Cloud.Utils { /// /// K3CloudApi帮助类 /// public class K3CloudApiUtils { private List _options; private K3CloudApi _cloudApi; private LoginInfo _userInfo; /// /// 构造K3CloudApiUtils,option、env是从ICO容器中拿到的对象 /// /// 金蝶配置 public K3CloudApiUtils(List option) { _options = option; } /// /// 获取Kingdee配置信息 /// /// public List GetKingdeeOptions() { return _options; } /// /// 根据Token中的信息创建K3CloudApi对象 /// /// /// public void InitCloudApi(LoginInfo User) { var userName = User.UserName; var lcId = User.LCId; var orgNum = User.OrgNum; var DBID = User.DBID; var serverUrl = User.ServerUrl; K3CloudOption? kingdeeOption = _options .Where(n => n.AcctID == DBID && n.ServerUrl.ToUpperInvariant() == serverUrl.ToUpperInvariant()) .FirstOrDefault(); if (kingdeeOption == null) { //throw new Exception("配置文件没有对应的第三方授权登录信息!"); throw new Exception(@$"配置文件没有对应的第三方授权登录信息! 已配置信息:{JsonSerializer.Serialize(_options)} 请求信息:{JsonSerializer.Serialize(User)} "); } _cloudApi = new K3CloudApi(serverUrl); _cloudApi.InitClient( acctID: DBID, appID: kingdeeOption?.AppID, appSec: kingdeeOption?.AppSec, serverUrl: serverUrl, userName: userName, lcid: lcId, orgNum: orgNum ); this._userInfo = User; var loginInfo = _cloudApi.LoginByAppSecret( DBID, userName, kingdeeOption?.AppID, kingdeeOption?.AppSec, lcId ); } /// /// 获取默认用户的api /// /// /// public K3CloudApi CreateDefaultK3CloudApi(LoginInfo loginInfo) { K3CloudOption? kingdeeOption = _options .Where(n => n.AcctID == loginInfo.DBID && n.ServerUrl.ToUpperInvariant() == loginInfo.ServerUrl.ToUpperInvariant()) .FirstOrDefault(); if (kingdeeOption == null) { throw new Exception("配置文件没有对应的第三方授权登录信息!"); } // 默认登录信息从配置文件appsetting.json获取AppID、AppSec,其余从前端传参 ThirdPassPortInfo thirdPassPortInfo = new ThirdPassPortInfo(); thirdPassPortInfo.CloudDbId = loginInfo.DBID; thirdPassPortInfo.ApiAppId = kingdeeOption?.AppID; thirdPassPortInfo.AppSec = kingdeeOption?.AppSec; thirdPassPortInfo.CloudUrl = loginInfo.ServerUrl; thirdPassPortInfo.Language = loginInfo.LCId.ToString(); thirdPassPortInfo.CloudUser = loginInfo.UserName; _cloudApi = new K3CloudApi(thirdPassPortInfo, 30); return _cloudApi; } /// /// 创建金蝶云API SDK /// /// /// public K3CloudApiUtils GetDefaultK3CloudApiUtil(LoginInfo loginInfo) { K3CloudOption? kingdeeOption = _options .Where(n => n.AcctID == loginInfo.DBID && n.ServerUrl.ToUpperInvariant() == loginInfo.ServerUrl.ToUpperInvariant()) .FirstOrDefault(); if (kingdeeOption == null) { throw new Exception("配置文件没有对应的第三方授权登录信息!"); } // 默认登录信息从配置文件appsetting.json获取AppID、AppSec,其余从前端传参 var logInfo = new LoginInfo() { UserName = kingdeeOption.UserName, LCId = kingdeeOption.LCID, OrgNum = kingdeeOption.OrgNumber, DBID = kingdeeOption.AcctID, ServerUrl = kingdeeOption.ServerUrl, }; var k3CloudApiUtil = new K3CloudApiUtils(_options); k3CloudApiUtil.InitCloudApi(logInfo); return k3CloudApiUtil; } /// /// 创建金蝶云API SDK /// /// /// public K3CloudApiUtils GetDefaultK3CloudApiUtil() { if (this._userInfo == null) { throw new Exception("当前工具类没有,初始化用户信息"); } return GetDefaultK3CloudApiUtil(_userInfo); } /// /// 获取金蝶云星空SDK实例 /// /// public K3CloudApi GetApiClient() { return _cloudApi; } /// /// 查询列表 /// /// /// public ListResult QueryList(Query queryParam) { var datastr = queryParam.ToString(); var ApiResult = _cloudApi.ExecuteBillQuery(datastr); var resultString = Newtonsoft.Json.JsonConvert.SerializeObject(ApiResult); // 包含ErrorCode认定为失败 if (resultString.Contains("ErrorCode")) { var errResultString = Newtonsoft.Json.JsonConvert.SerializeObject(ApiResult.First().First()); var errorResult = JsonSerializer.Deserialize(errResultString); var responseStatus = errorResult?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据列表出错", responseStatus); throw error; } var fieldKeys = queryParam?.FieldKeys?.Split(',').ToList(); if (fieldKeys == null || fieldKeys.Count == 0) { return new ListResult(new List>()); } List> result = new List>(); foreach (var item in ApiResult) { var line = new Dictionary(); foreach (var fieldkey in fieldKeys) { var index = fieldKeys.IndexOf(fieldkey); line.Add(fieldkey, item[index]); } result.Add(line); } return new ListResult(result); } /// /// 查询单据 /// /// 表单ID /// 参数 /// /// public object Query(string formId, View viewBill) { var jsonData = viewBill.ToString(); var resultString = _cloudApi.View(formId, jsonData); var result = JsonSerializer.Deserialize(resultString); // SB云星空 v7和v8的响应信息格式不同,导致无法使用统一的处理方式 if (viewBill.Version >= 8) { if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据出错", responseStatus); throw error; } } else { if (result?.Result?.ResponseStatus != null) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据出错", responseStatus); throw error; } } var data = result?.Result?.Result; return data; } /// /// 查询单据 /// /// 表单ID /// 参数 /// /// public T Query(string formId, View viewBill) { var jsonData = viewBill.ToString(); var resultString = _cloudApi.View(formId, jsonData); var result = JsonSerializer.Deserialize(resultString); if (viewBill.Version >= 8) { if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据出错", responseStatus); throw error; } } else { if (result?.Result?.ResponseStatus != null) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("查看单据出错", responseStatus); throw error; } } var data = result?.Result?.Result; var dataString = JsonSerializer.Serialize(data); return JsonSerializer.Deserialize(dataString); } /// /// 下推接口 /// /// 表单ID /// 参数 /// public K3CloudResponseStatus Push(string formId, Push billPush) { var pushString = billPush.ToString(); var resultString = _cloudApi.Push(formId, pushString); var result = JsonSerializer.Deserialize(resultString); var data = result?.Result?.ResponseStatus; return data; } /// /// 保存接口 /// /// /// /// public K3CloudResponseStatus Save(string formId, Save billSave) { var requestString = billSave.ToString(); var resultString = _cloudApi.Save(formId, requestString); var result = JsonSerializer.Deserialize(resultString); if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("单据保存出错", responseStatus); throw error; } var data = result?.Result?.ResponseStatus; return data; } /// /// 批量保存接口 /// /// /// /// public K3CloudResponseStatus BatchSave(string formId, PatchSave patchSave) { var requestString = patchSave.ToString(); var resultString = _cloudApi.BatchSave(formId, requestString); var result = JsonSerializer.Deserialize(resultString); if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("单据批量保存出错", responseStatus); throw error; } var data = result?.Result?.ResponseStatus; return data; } /// /// 删除接口 /// /// /// /// public K3CloudResponseStatus Delete(string formId, Delete billdelete) { var paramStr = billdelete.ToString(); var resultString = _cloudApi.Delete(formId, paramStr); var result = JsonSerializer.Deserialize(resultString); if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("单据删除出错", responseStatus); throw error; } var data = result?.Result?.ResponseStatus; return data; } /// /// 提交接口 /// /// /// /// public K3CloudResponseStatus Submit(string formId, Submit billSubmit) { var resultString = _cloudApi.Submit(formId, billSubmit.ToString()); var result = JsonSerializer.Deserialize(resultString); var data = result?.Result?.ResponseStatus; return data; } /// /// 提交接口 /// /// /// /// public K3CloudResponseStatus CancelAssign(string formId, CancelAssign billSubmit) { var resultString = _cloudApi.CancelAssign(formId, billSubmit.ToString()); var result = JsonSerializer.Deserialize(resultString); var data = result?.Result?.ResponseStatus; return data; } /// /// 审核接口 /// /// /// /// public K3CloudResponseStatus Audit(string formId, Audit billAudit) { var resultString = _cloudApi.Audit(formId, billAudit.ToString()); var result = JsonSerializer.Deserialize(resultString); var data = result?.Result?.ResponseStatus; return data; } /// /// 文件系统 /// /// /// public Model.K3Result.Model.K3CloudResult FileDonwload(FileParam fileParam) { var resultString = _cloudApi.AttachmentDownLoad(fileParam.ToString()); var result = JsonSerializer.Deserialize(resultString); if (result?.Result?.ResponseStatus?.IsSuccess != true) { var responseStatus = result?.Result?.ResponseStatus; Exception error = new K3CloudException("附件下载出错", responseStatus); throw error; } return result?.Result; } } }