CP_Utils.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using H3;
  5. public class CP_Utils
  6. {
  7. /********** 莱蒂 **********/
  8. /// 附件同步到钉盘
  9. public static string syncDingDriveForProject(H3.IEngine engine, string projectName, string attachments) {
  10. Dictionary < string, object > bodys = new Dictionary<string, object>();
  11. bodys.Add("projectName", projectName); // 项目名称
  12. bodys.Add("attachments", attachments); // 同步附件
  13. List < H3.BizBus.ItemSchema > structures = new List<H3.BizBus.ItemSchema>(); // 响应类型
  14. structures.Add(new H3.BizBus.ItemSchema("status", "同步状态", H3.Data.BizDataType.String, null));
  15. H3.BizBus.BizStructure data = CP_Utils.invokeVendorService(engine, "sync", bodys, structures); // 请求处理
  16. // 传递同步状态
  17. return data["status"] + string.Empty;
  18. }
  19. /********** 工具 **********/
  20. /**
  21. * 氚云HTTP
  22. * 1. code 公用请求,code区分业务逻辑
  23. * 2. bodys 请求参数,默认POST,application/json
  24. * 3. structures 返回数据格式定义, 统一使用对象响应
  25. */
  26. public static H3.BizBus.BizStructure invokeVendorService(H3.IEngine engine, string code, Dictionary < string, object > bodys, List < H3.BizBus.ItemSchema > structures) {
  27. // 请求信息
  28. Dictionary < string, string > headers = new Dictionary<string, string>();
  29. Dictionary < string, string > querys = new Dictionary<string, string>();
  30. querys.Add("code", code);
  31. // 定义响应数据整体结构体
  32. H3.BizBus.BizStructureSchema structureSchema = new H3.BizBus.BizStructureSchema();
  33. structureSchema.Add(new H3.BizBus.ItemSchema("code", "结果状态码", H3.Data.BizDataType.Int, null));
  34. structureSchema.Add(new H3.BizBus.ItemSchema("success", "请求状态位", H3.Data.BizDataType.Bool, null));
  35. structureSchema.Add(new H3.BizBus.ItemSchema("message", "描述信息", H3.Data.BizDataType.String, null));
  36. // 定义响应数据的 data 属性 的结构体
  37. H3.BizBus.BizStructureSchema dataSchema = new H3.BizBus.BizStructureSchema();
  38. foreach(H3.BizBus.ItemSchema itemSchame in structures)
  39. {
  40. dataSchema.Add(itemSchame);
  41. }
  42. //将 data 属性的结构体添加进整体的响应数据结构体 [H3.Data.BizDataType.BizStructureArray 集合,H3.Data.BizDataType.BizStructure 对象]
  43. structureSchema.Add(new H3.BizBus.ItemSchema("data", "响应数据", H3.Data.BizDataType.BizStructure, dataSchema));
  44. //调用Invoke接口,系统底层访问第三方接口的Invoke方法
  45. H3.BizBus.InvokeResult response = engine.BizBus.InvokeApi(
  46. H3.Organization.User.SystemUserId, //固定值,无需改变
  47. H3.BizBus.AccessPointType.ThirdConnection, //固定值,无需改变
  48. "h3yun-http", //连接编码,对应 插件中心 中配置的连接的编码
  49. "POST", //请求方式,取值:GET / POST
  50. "application/json", //请求数据类型: json - "application/json";xml - "text/html;charset=utf-8"
  51. headers, querys, bodys, structureSchema);
  52. if(response != null)
  53. {
  54. // 氚云调用是否成功
  55. if(response.Code == 0)
  56. {
  57. // 获取返回数据,此实例对应完整的响应JSON
  58. H3.BizBus.BizStructure rspData = response.Data;
  59. if(((bool) rspData["success"]) == true)
  60. {
  61. // 对象响应【氚云自定义字段,需要通过[]直接取值才有效,序列化后返回前端,仅会保留结构】
  62. // H3.BizBus.BizStructure[] datas = rspData["data"] as H3.BizBus.BizStructure[]; // 集合解析
  63. // H3.BizBus.BizStructure data = rspData["data"] as H3.BizBus.BizStructure; // 对象解析
  64. return rspData["data"] as H3.BizBus.BizStructure;
  65. } else
  66. {
  67. throw new Exception("响应异常," + rspData["message"]);
  68. }
  69. } else
  70. {
  71. throw new Exception("氚云异常," + response.Message);
  72. }
  73. } else
  74. {
  75. throw new Exception("系统异常,接口无响应");
  76. }
  77. }
  78. }