using System; using System.Collections.Generic; using System.Text; using H3; public class CP_Utils { /********** 莱蒂 **********/ /// 附件同步到钉盘 public static string syncDingDriveForProject(H3.IEngine engine, string projectName, string attachments) { Dictionary < string, object > bodys = new Dictionary(); bodys.Add("projectName", projectName); // 项目名称 bodys.Add("attachments", attachments); // 同步附件 List < H3.BizBus.ItemSchema > structures = new List(); // 响应类型 structures.Add(new H3.BizBus.ItemSchema("status", "同步状态", H3.Data.BizDataType.String, null)); H3.BizBus.BizStructure data = CP_Utils.invokeVendorService(engine, "sync", bodys, structures); // 请求处理 // 传递同步状态 return data["status"] + string.Empty; } /********** 工具 **********/ /** * 氚云HTTP * 1. code 公用请求,code区分业务逻辑 * 2. bodys 请求参数,默认POST,application/json * 3. structures 返回数据格式定义, 统一使用对象响应 */ public static H3.BizBus.BizStructure invokeVendorService(H3.IEngine engine, string code, Dictionary < string, object > bodys, List < H3.BizBus.ItemSchema > structures) { // 请求信息 Dictionary < string, string > headers = new Dictionary(); Dictionary < string, string > querys = new Dictionary(); querys.Add("code", code); // 定义响应数据整体结构体 H3.BizBus.BizStructureSchema structureSchema = new H3.BizBus.BizStructureSchema(); structureSchema.Add(new H3.BizBus.ItemSchema("code", "结果状态码", H3.Data.BizDataType.Int, null)); structureSchema.Add(new H3.BizBus.ItemSchema("success", "请求状态位", H3.Data.BizDataType.Bool, null)); structureSchema.Add(new H3.BizBus.ItemSchema("message", "描述信息", H3.Data.BizDataType.String, null)); // 定义响应数据的 data 属性 的结构体 H3.BizBus.BizStructureSchema dataSchema = new H3.BizBus.BizStructureSchema(); foreach(H3.BizBus.ItemSchema itemSchame in structures) { dataSchema.Add(itemSchame); } //将 data 属性的结构体添加进整体的响应数据结构体 [H3.Data.BizDataType.BizStructureArray 集合,H3.Data.BizDataType.BizStructure 对象] structureSchema.Add(new H3.BizBus.ItemSchema("data", "响应数据", H3.Data.BizDataType.BizStructure, dataSchema)); //调用Invoke接口,系统底层访问第三方接口的Invoke方法 H3.BizBus.InvokeResult response = engine.BizBus.InvokeApi( H3.Organization.User.SystemUserId, //固定值,无需改变 H3.BizBus.AccessPointType.ThirdConnection, //固定值,无需改变 "h3yun-http", //连接编码,对应 插件中心 中配置的连接的编码 "POST", //请求方式,取值:GET / POST "application/json", //请求数据类型: json - "application/json";xml - "text/html;charset=utf-8" headers, querys, bodys, structureSchema); if(response != null) { // 氚云调用是否成功 if(response.Code == 0) { // 获取返回数据,此实例对应完整的响应JSON H3.BizBus.BizStructure rspData = response.Data; if(((bool) rspData["success"]) == true) { // 对象响应【氚云自定义字段,需要通过[]直接取值才有效,序列化后返回前端,仅会保留结构】 // H3.BizBus.BizStructure[] datas = rspData["data"] as H3.BizBus.BizStructure[]; // 集合解析 // H3.BizBus.BizStructure data = rspData["data"] as H3.BizBus.BizStructure; // 对象解析 return rspData["data"] as H3.BizBus.BizStructure; } else { throw new Exception("响应异常," + rspData["message"]); } } else { throw new Exception("氚云异常," + response.Message); } } else { throw new Exception("系统异常,接口无响应"); } } }