|
|
@@ -11,7 +11,7 @@ using Newtonsoft.Json.Linq;
|
|
|
namespace UAS_LabelMachine.Kingdee
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// 金蝶AI苍穹(金蝶AI苍穹) OpenAPI 客户端 —— 基于原生 HttpWebRequest 直调 REST,无 SDK 依赖。
|
|
|
+ /// 金蝶AI(金蝶AI) OpenAPI 客户端 —— 基于原生 HttpWebRequest 直调 REST,无 SDK 依赖。
|
|
|
/// 参考:
|
|
|
/// OpenAPI调用流程 https://vip.kingdee.com/knowledge/543448746756000256?productLineId=40
|
|
|
/// 增强型Token认证 https://vip.kingdee.com/article/537672007622826496
|
|
|
@@ -22,20 +22,20 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
/// 调 /kapi/oauth2/getToken 换 access_token(OAuth2.0, 默认2小时有效, 放请求头);
|
|
|
/// 2. 每个业务请求头必须带: Content-Type=application/json、charset=utf-8、
|
|
|
/// x-acgw-identity(网关身份标识, 强制)、access_token;
|
|
|
- /// 3. 业务接口是"操作API"零代码发布模式: 查询/保存/删除/审核等API需在苍穹端
|
|
|
+ /// 3. 业务接口是"操作API"零代码发布模式: 查询/保存/删除/审核等API需在端
|
|
|
/// 【开放服务云→OpenAPI→操作API】发布后才有路径(如 kapi/xxx/query), 无星空那种
|
|
|
/// "传FormId即可查任意表"的统一端点;
|
|
|
/// 4. 统一响应契约: { data, errorCode, message, status }, status=true 成功;
|
|
|
/// 5. 基础资料引用默认用 number 而非内码。
|
|
|
///
|
|
|
/// 兼容说明: 为减少主工程改动, 本类保留 K3CloudApiClient 类名与 Login/EnsureLogin/
|
|
|
- /// ExecuteBillQueryToTable 等方法签名, 内部已全部改为苍穹 OpenAPI 实现。
|
|
|
- /// ExecuteBillQueryToTable 在苍穹下需 QueryParam.ApiPath 指向已发布的查询操作API。
|
|
|
+ /// ExecuteBillQueryToTable 等方法签名, 内部已全部改为 OpenAPI 实现。
|
|
|
+ /// ExecuteBillQueryToTable 在下需 QueryParam.ApiPath 指向已发布的查询操作API。
|
|
|
/// </summary>
|
|
|
public class K3CloudApiClient : IDisposable
|
|
|
{
|
|
|
// ===== 连接参数(从 App.config kingdee:xxx 读取) =====
|
|
|
- public string ServerUrl { get; private set; } // 苍穹域名, 末尾带 /, 如 https://xxx.kingdee.com/
|
|
|
+ public string ServerUrl { get; private set; } // 域名, 末尾带 /, 如 https://xxx.kingdee.com/
|
|
|
public string AccountId { get; private set; } // 数据中心id
|
|
|
public string ClientId { get; private set; } // 第三方应用系统编码(appId)
|
|
|
public string ClientSecret { get; private set; } // 第三方应用密钥(appSecret)
|
|
|
@@ -129,7 +129,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
// getToken 接口本身不需要 access_token 头, 但需要 x-acgw-identity
|
|
|
string ret = PostRaw("kapi/oauth2/getToken", bodyJson, withTokenHeader: false);
|
|
|
|
|
|
- // 灵活解析: 苍穹 getToken 响应可能是 {data,errorCode,message,status} 统一结构,
|
|
|
+ // 灵活解析: getToken 响应可能是 {data,errorCode,message,status} 统一结构,
|
|
|
// 也可能直接平铺 {access_token,expires_in,...}; 只要能拿到 access_token 就算成功
|
|
|
JObject jo = null;
|
|
|
try { jo = JObject.Parse(ret); } catch { }
|
|
|
@@ -159,7 +159,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
{
|
|
|
// 拿不到 token, 把原始响应抛出来方便排查
|
|
|
- throw new Exception("苍穹getToken失败: 未能从响应中获取access_token。" +
|
|
|
+ throw new Exception("getToken失败: 未能从响应中获取access_token。" +
|
|
|
"原始响应=" + (ret != null && ret.Length > 2000 ? ret.Substring(0, 2000) + "..." : ret));
|
|
|
}
|
|
|
|
|
|
@@ -196,7 +196,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
#region 通用调用
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 通用调用苍穹 OpenAPI。apiPath 形如 "kapi/xxx/query"(不带前导斜杠) 或 "/kapi/xxx/query"。
|
|
|
+ /// 通用调用 OpenAPI。apiPath 形如 "kapi/xxx/query"(不带前导斜杠) 或 "/kapi/xxx/query"。
|
|
|
/// 自动带 access_token + x-acgw-identity + Content-Type + charset 头。返回原始响应字符串。
|
|
|
/// </summary>
|
|
|
public string Invoke(string apiPath, string bodyJson)
|
|
|
@@ -204,10 +204,10 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
return PostRaw(apiPath, bodyJson, withTokenHeader: true);
|
|
|
}
|
|
|
|
|
|
- /// <summary>POST 苍穹接口(内部统一实现)</summary>
|
|
|
+ /// <summary>POST 接口(内部统一实现)</summary>
|
|
|
private string PostRaw(string apiPath, string bodyJson, bool withTokenHeader)
|
|
|
{
|
|
|
- if (string.IsNullOrEmpty(apiPath)) throw new Exception("苍穹 apiPath 为空");
|
|
|
+ if (string.IsNullOrEmpty(apiPath)) throw new Exception(" apiPath 为空");
|
|
|
string path = apiPath.StartsWith("/") ? apiPath.Substring(1) : apiPath;
|
|
|
string url = ServerUrl + path;
|
|
|
|
|
|
@@ -223,7 +223,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
req.ContentType = "application/json";
|
|
|
req.Timeout = TimeoutMs;
|
|
|
req.ReadWriteTimeout = TimeoutMs;
|
|
|
- // 苍穹强制/必要请求头
|
|
|
+ // 强制/必要请求头
|
|
|
req.Headers["charset"] = "utf-8";
|
|
|
if (!string.IsNullOrEmpty(AcgwIdentity))
|
|
|
req.Headers["x-acgw-identity"] = AcgwIdentity;
|
|
|
@@ -231,7 +231,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
req.Headers["x-acgw-appid"] = ClientId; // 网关颁发的应用id(部分接口要求)
|
|
|
if (withTokenHeader && !string.IsNullOrEmpty(_accessToken))
|
|
|
{
|
|
|
- req.Headers["accesstoken"] = _accessToken; // 苍穹API文档要求: accesstoken(无下划线)
|
|
|
+ req.Headers["accesstoken"] = _accessToken; // API文档要求: accesstoken(无下划线)
|
|
|
req.Headers["accessToken"] = _accessToken; // 客户接口要求: accessToken(大写T)
|
|
|
req.Headers["access_token"] = _accessToken; // 兼容旧版/部分接口仍用 access_token
|
|
|
}
|
|
|
@@ -266,11 +266,11 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
}
|
|
|
catch { }
|
|
|
}
|
|
|
- throw new Exception("苍穹请求失败(" + statusCode + "): " + errBody + " | URL=" + url, ex);
|
|
|
+ throw new Exception("请求失败(" + statusCode + "): " + errBody + " | URL=" + url, ex);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>解析苍穹统一响应 {data, errorCode, message, status}</summary>
|
|
|
+ /// <summary>解析统一响应 {data, errorCode, message, status}</summary>
|
|
|
public static UnifiedResponse ParseUnifiedResponse(string retJson)
|
|
|
{
|
|
|
var r = new UnifiedResponse();
|
|
|
@@ -302,17 +302,17 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
- #region 业务接口(苍穹 batchQuery)
|
|
|
+ #region 业务接口( batchQuery)
|
|
|
|
|
|
/// <summary>
|
|
|
- /// FormId → 苍穹 batchQuery 接口路径映射。
|
|
|
- /// 苍穹没有星空的统一 ExecuteBillQuery 端点,每个业务对象的查询API路径不同。
|
|
|
+ /// FormId → batchQuery 接口路径映射。
|
|
|
+ /// 没有星空的统一 ExecuteBillQuery 端点,每个业务对象的查询API路径不同。
|
|
|
/// 新接口路径找到后在此追加。
|
|
|
/// </summary>
|
|
|
private static readonly Dictionary<string, string> FormIdToApiPath = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
|
|
{
|
|
|
{ "BD_MATERIAL", "/kapi/v2/basedata/bd_material/batchQuery" },
|
|
|
- // 以下待补: 找到苍穹端接口路径后加入
|
|
|
+ // 以下待补: 找到端接口路径后加入
|
|
|
// { "BOS_ASSISTANTDATA_DETAIL", "/kapi/v2/xxx/xxx/batchQuery" },
|
|
|
// { "BD_CUSTOMER", "/kapi/v2/xxx/xxx/batchQuery" },
|
|
|
// { "SAL_DELIVERYNOTICE", "/kapi/v2/xxx/xxx/batchQuery" },
|
|
|
@@ -321,8 +321,8 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
|
|
|
/// <summary>
|
|
|
/// 兼容原 SDK 的 ExecuteBillQueryToTable 用法。
|
|
|
- /// 苍穹下: 根据 FormId 查映射表找到对应 batchQuery 接口路径,
|
|
|
- /// 把星空 FilterString 转为苍穹批量查询参数,调用后返回 DataTable。
|
|
|
+ /// 下: 根据 FormId 查映射表找到对应 batchQuery 接口路径,
|
|
|
+ /// 把星空 FilterString 转为批量查询参数,调用后返回 DataTable。
|
|
|
/// </summary>
|
|
|
public DataTable ExecuteBillQueryToTable(QueryParam param)
|
|
|
{
|
|
|
@@ -334,23 +334,23 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
apiPath = FormIdToApiPath[param.FormId];
|
|
|
else
|
|
|
throw new NotImplementedException(
|
|
|
- "苍穹未配置 FormId=" + param.FormId + " 的 batchQuery 路径。" +
|
|
|
+ "未配置 FormId=" + param.FormId + " 的 batchQuery 路径。" +
|
|
|
"请在金蝶后台【API文档】页面找到对应业务对象的批量查询接口," +
|
|
|
"将路径加入 K3CloudApiClient.FormIdToApiPath 映射表,或在 QueryParam.ApiPath 中指定。");
|
|
|
}
|
|
|
|
|
|
- // 2. 组装苍穹 batchQuery 请求体
|
|
|
+ // 2. 组装 batchQuery 请求体
|
|
|
string bodyJson = BuildBatchQueryBody(param);
|
|
|
string ret = Invoke(apiPath, bodyJson);
|
|
|
|
|
|
// 3. 解析响应
|
|
|
JToken queryData = ParseBatchQueryResponse(ret);
|
|
|
|
|
|
- // 4. 转 DataTable(苍穹返回对象数组,列名用属性名)
|
|
|
+ // 4. 转 DataTable(返回对象数组,列名用属性名)
|
|
|
return ToDataTable(queryData, param.FieldKeys);
|
|
|
}
|
|
|
|
|
|
- /// <summary>组装苍穹 batchQuery 请求体</summary>
|
|
|
+ /// <summary>组装 batchQuery 请求体</summary>
|
|
|
private string BuildBatchQueryBody(QueryParam param)
|
|
|
{
|
|
|
var body = new Dictionary<string, object>
|
|
|
@@ -359,7 +359,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
{ "pageNo", (param.StartRow / (param.Limit > 0 ? param.Limit : 2000)) + 1 }
|
|
|
};
|
|
|
|
|
|
- // 从星空 FilterString 提取查询条件,转为苍穹参数
|
|
|
+ // 从星空 FilterString 提取查询条件,转为参数
|
|
|
ParseFilterToBatchParams(param.FilterString, body);
|
|
|
|
|
|
// 如果有额外参数(通过 BatchQueryParams 传入),合并
|
|
|
@@ -373,7 +373,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 把星空 FilterString 解析为苍穹 batchQuery 参数。
|
|
|
+ /// 把星空 FilterString 解析为 batchQuery 参数。
|
|
|
/// 支持的模式:
|
|
|
/// FId='xxx' → id: ["xxx"]
|
|
|
/// FMATERIALID in (a,b)→ id: [a, b]
|
|
|
@@ -418,7 +418,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
// 其他不支持的过滤条件 → 忽略
|
|
|
}
|
|
|
|
|
|
- /// <summary>星空字段名 → 苍穹 batchQuery 参数名映射</summary>
|
|
|
+ /// <summary>星空字段名 → batchQuery 参数名映射</summary>
|
|
|
private string MapFieldKeyToBatchParam(string fieldKey)
|
|
|
{
|
|
|
string fk = fieldKey.ToUpperInvariant();
|
|
|
@@ -429,17 +429,17 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
return "id"; // 默认当 id
|
|
|
}
|
|
|
|
|
|
- /// <summary>解析苍穹 batchQuery 响应,返回 data 部分</summary>
|
|
|
+ /// <summary>解析 batchQuery 响应,返回 data 部分</summary>
|
|
|
private JToken ParseBatchQueryResponse(string ret)
|
|
|
{
|
|
|
- if (string.IsNullOrEmpty(ret)) throw new Exception("苍穹查询返回空响应");
|
|
|
+ if (string.IsNullOrEmpty(ret)) throw new Exception("查询返回空响应");
|
|
|
|
|
|
JObject jo = null;
|
|
|
try { jo = JObject.Parse(ret); } catch
|
|
|
{
|
|
|
// 可能直接是数组
|
|
|
try { return JArray.Parse(ret); }
|
|
|
- catch { throw new Exception("苍穹查询响应解析失败: raw=" + ret); }
|
|
|
+ catch { throw new Exception("查询响应解析失败: raw=" + ret); }
|
|
|
}
|
|
|
|
|
|
// 统一结构 {data, errorCode, message, status}
|
|
|
@@ -449,7 +449,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
bool ok = (st.Type == JTokenType.Boolean) ? st.Value<bool>()
|
|
|
: string.Equals(st.ToString(), "true", StringComparison.OrdinalIgnoreCase);
|
|
|
if (!ok)
|
|
|
- throw new Exception("苍穹查询失败: " + (string)jo["message"] + "(errorCode=" + (string)jo["errorCode"] + ") | raw=" + ret);
|
|
|
+ throw new Exception("查询失败: " + (string)jo["message"] + "(errorCode=" + (string)jo["errorCode"] + ") | raw=" + ret);
|
|
|
|
|
|
var data = jo["data"];
|
|
|
// data 可能直接是数组,也可能包含 rows 字段
|
|
|
@@ -466,7 +466,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
return jo;
|
|
|
}
|
|
|
|
|
|
- /// <summary>把苍穹查询返回 data 灵活转成 DataTable</summary>
|
|
|
+ /// <summary>把查询返回 data 灵活转成 DataTable</summary>
|
|
|
private static DataTable ToDataTable(JToken data, string fieldKeys)
|
|
|
{
|
|
|
var dt = new DataTable();
|
|
|
@@ -522,7 +522,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 苍穹批量查询。传入 batchQuery 路径和查询参数字典, 返回 DataTable(列名=苍穹返回字段名)。
|
|
|
+ /// 批量查询。传入 batchQuery 路径和查询参数字典, 返回 DataTable(列名=返回字段名)。
|
|
|
/// 自动分页: 每页查 pageSize 条, 翻页直到取完全部数据。
|
|
|
/// 请求格式: pageSize/pageNo 在根级(层级0), 查询条件在 data 对象里(层级1)。
|
|
|
/// 例如: client.BatchQuery("/kapi/v2/basedata/bd_material/batchQuery",
|
|
|
@@ -567,7 +567,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
bool ok = (st != null && st.Type == JTokenType.Boolean) ? st.Value<bool>()
|
|
|
: string.Equals((string)st, "true", StringComparison.OrdinalIgnoreCase);
|
|
|
if (!ok)
|
|
|
- throw new Exception("苍穹查询失败: " + (string)jo["message"] + "(errorCode=" + (string)jo["errorCode"] + ") | raw=" + ret);
|
|
|
+ throw new Exception("查询失败: " + (string)jo["message"] + "(errorCode=" + (string)jo["errorCode"] + ") | raw=" + ret);
|
|
|
|
|
|
JToken respData = jo["data"];
|
|
|
|
|
|
@@ -656,12 +656,12 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 查询参数。苍穹下根据 FormId 自动匹配 batchQuery 路径; 也可通过 ApiPath 手动指定。
|
|
|
- /// FilterString 会被自动解析为苍穹批量查询参数; 也可通过 BatchQueryParams 手动传苍穹参数。
|
|
|
+ /// 查询参数。下根据 FormId 自动匹配 batchQuery 路径; 也可通过 ApiPath 手动指定。
|
|
|
+ /// FilterString 会被自动解析为批量查询参数; 也可通过 BatchQueryParams 手动传参数。
|
|
|
/// </summary>
|
|
|
public class QueryParam
|
|
|
{
|
|
|
- /// <summary>苍穹 batchQuery 接口路径(可选, 不填则按 FormId 查映射表)</summary>
|
|
|
+ /// <summary> batchQuery 接口路径(可选, 不填则按 FormId 查映射表)</summary>
|
|
|
public string ApiPath { get; set; }
|
|
|
public string FormId { get; set; }
|
|
|
public string FieldKeys { get; set; }
|
|
|
@@ -671,7 +671,7 @@ namespace UAS_LabelMachine.Kingdee
|
|
|
public int Limit { get; set; }
|
|
|
public int StartRow { get; set; }
|
|
|
|
|
|
- /// <summary>苍穹 batchQuery 额外参数(可选, 如 start_modifytime/status 等)。会与 FilterString 解析结果合并。</summary>
|
|
|
+ /// <summary> batchQuery 额外参数(可选, 如 start_modifytime/status 等)。会与 FilterString 解析结果合并。</summary>
|
|
|
public Dictionary<string, object> BatchQueryParams { get; set; }
|
|
|
}
|
|
|
}
|