364 lines
13 KiB
C#
364 lines
13 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// K3CloudApi帮助类
|
||
/// </summary>
|
||
public class K3CloudApiUtils
|
||
{
|
||
private List<K3CloudOption> _options;
|
||
private K3CloudApi _cloudApi;
|
||
private LoginInfo _userInfo;
|
||
|
||
/// <summary>
|
||
/// 构造K3CloudApiUtils,option、env是从ICO容器中拿到的对象
|
||
/// </summary>
|
||
/// <param name="option">金蝶配置</param>
|
||
public K3CloudApiUtils(List<K3CloudOption> option)
|
||
{
|
||
_options = option;
|
||
}
|
||
/// <summary>
|
||
/// 获取Kingdee配置信息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public List<K3CloudOption> GetKingdeeOptions()
|
||
{
|
||
return _options;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据Token中的信息创建K3CloudApi对象
|
||
/// </summary>
|
||
/// <param name="User"></param>
|
||
/// <returns></returns>
|
||
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("配置文件没有对应的第三方授权登录信息!");
|
||
}
|
||
_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;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 获取默认用户的api
|
||
/// </summary>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建金蝶云API SDK
|
||
/// </summary>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建金蝶云API SDK
|
||
/// </summary>
|
||
/// <param name="loginInfo"></param>
|
||
/// <returns></returns>
|
||
public K3CloudApiUtils GetDefaultK3CloudApiUtil()
|
||
{
|
||
|
||
if (this._userInfo == null)
|
||
{
|
||
throw new Exception("当前工具类没有,初始化用户信息");
|
||
}
|
||
return GetDefaultK3CloudApiUtil(_userInfo);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取金蝶云星空SDK实例
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public K3CloudApi GetApiClient()
|
||
{
|
||
return _cloudApi;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询列表
|
||
/// </summary>
|
||
/// <param name="queryParam"></param>
|
||
/// <returns></returns>
|
||
public ListResult QueryList(Query queryParam)
|
||
{
|
||
var datastr = queryParam.ToString();
|
||
var resultString = _cloudApi.BillQuery(datastr);
|
||
// 包含ErrorCode认定为失败
|
||
if (resultString.Contains("ErrorCode"))
|
||
{
|
||
var errorResult = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
var responseStatus = errorResult?.Result?.ResponseStatus;
|
||
Exception error = new K3CloudException("查看单据列表出错", responseStatus);
|
||
throw error;
|
||
}
|
||
List<Dictionary<string, object>>? result = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(resultString);
|
||
return new ListResult(result);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询单据
|
||
/// </summary>
|
||
/// <param name="formId">表单ID</param>
|
||
/// <param name="viewBill">参数</param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
public object Query(string formId, View viewBill)
|
||
{
|
||
var jsonData = viewBill.ToString();
|
||
var resultString = _cloudApi.View(formId, jsonData);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
if (result?.Result?.ResponseStatus?.IsSuccess != true)
|
||
{
|
||
var responseStatus = result?.Result?.ResponseStatus;
|
||
Exception error = new K3CloudException("查看单据出错", responseStatus);
|
||
throw error;
|
||
}
|
||
var data = result?.Result?.Result;
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询单据
|
||
/// </summary>
|
||
/// <param name="formId">表单ID</param>
|
||
/// <param name="viewBill">参数</param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
public T Query<T>(string formId, View viewBill)
|
||
{
|
||
var jsonData = viewBill.ToString();
|
||
var resultString = _cloudApi.View(formId, jsonData);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
if (result?.Result?.ResponseStatus?.IsSuccess != true)
|
||
{
|
||
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<T>(dataString);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下推接口
|
||
/// </summary>
|
||
/// <param name="formId">表单ID</param>
|
||
/// <param name="billPush">参数</param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus Push(string formId, Push billPush)
|
||
{
|
||
var pushString = billPush.ToString();
|
||
var resultString = _cloudApi.Push(formId, pushString);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
var data = result?.Result?.ResponseStatus;
|
||
return data;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 保存接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="billSave"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus Save<T>(string formId, Save<T> billSave)
|
||
{
|
||
var requestString = billSave.ToString();
|
||
var resultString = _cloudApi.Save(formId, requestString);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量保存接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="patchSave"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus BatchSave<T>(string formId, PatchSave<T> patchSave)
|
||
{
|
||
var requestString = patchSave.ToString();
|
||
var resultString = _cloudApi.BatchSave(formId, requestString);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(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;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 删除接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="billdelete"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus Delete(string formId, Delete billdelete)
|
||
{
|
||
var paramStr = billdelete.ToString();
|
||
var resultString = _cloudApi.Delete(formId, paramStr);
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(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;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 提交接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="billSubmit"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus Submit(string formId, Submit billSubmit)
|
||
{
|
||
var resultString = _cloudApi.Submit(formId, billSubmit.ToString());
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
var data = result?.Result?.ResponseStatus;
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 提交接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="billSubmit"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus CancelAssign(string formId, CancelAssign billSubmit)
|
||
{
|
||
var resultString = _cloudApi.CancelAssign(formId, billSubmit.ToString());
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
var data = result?.Result?.ResponseStatus;
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 审核接口
|
||
/// </summary>
|
||
/// <param name="formId"></param>
|
||
/// <param name="billAudit"></param>
|
||
/// <returns></returns>
|
||
public K3CloudResponseStatus Audit(string formId, Audit billAudit)
|
||
{
|
||
var resultString = _cloudApi.Audit(formId, billAudit.ToString());
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
var data = result?.Result?.ResponseStatus;
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 文件系统
|
||
/// </summary>
|
||
/// <param name="fileParam"></param>
|
||
/// <returns></returns>
|
||
public Model.K3Result.Model.K3CloudResult FileDonwload(FileParam fileParam)
|
||
{
|
||
var resultString = _cloudApi.AttachmentDownLoad(fileParam.ToString());
|
||
|
||
var result = JsonSerializer.Deserialize<KingdeeResult>(resultString);
|
||
if (result?.Result?.ResponseStatus?.IsSuccess != true)
|
||
{
|
||
var responseStatus = result?.Result?.ResponseStatus;
|
||
Exception error = new K3CloudException("附件下载出错", responseStatus);
|
||
throw error;
|
||
}
|
||
return result?.Result;
|
||
}
|
||
}
|
||
}
|