金蝶云星空自定义接口标准格式原创
9人赞赏了该文章
552次浏览
编辑于2023年10月24日 16:18:42
沧海月明珠有泪,蓝田日暖玉生烟
第一步:定义全局返回实体类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HB.Kingdee.Model { public class CommonOutPut { public bool isSuccess { get; set; } public string msg { get; set; } public object data { get; set; } } }
第二步:定义API类和方法,方法入参和出参都采用字符串格式
using HB.Kingdee.Model; using HB.Kingdee.Util; using Kingdee.BOS; using Kingdee.BOS.ServiceFacade.KDServiceFx; using Kingdee.BOS.ServiceHelper; using Kingdee.BOS.ServiceHelper.ManagementCenter; using Kingdee.BOS.WebApi.ServicesStub; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HB.Kingdee.ServicePlugln.WebApi.SAL { public class OutStockWebApi : AbstractWebApiBusinessService { private Context _ctx; public OutStockWebApi(KDServiceContext context) : base(context) { _ctx = this.KDContext.Session.AppContext; } public string UpdateSignDate(string param) { CommonOutPut outPut = new CommonOutPut(); UpdateSignDateInPut rec = JsonHelper.JsonToT<UpdateSignDateInPut>(param); string updateSql = string.Format(@"update T_SAL_OUTSTOCK set F_DA_SignDate = '{0}' where FBillNo = '{1}'",rec.signDate,rec.billNo); int count = DBServiceHelper.Execute(_ctx, updateSql); if(count > 0) { outPut.isSuccess = true; outPut.msg = "更新成功!"; } else { outPut.isSuccess = false; outPut.msg = "更新失败!"; } return JsonHelper.TToJson<CommonOutPut>(outPut); } } public class UpdateSignDateInPut { /// <summary> /// 销售出库单单号 /// </summary> public string billNo; /// <summary> /// 签收日期 /// </summary> public string signDate; } }
第三步:定义方法入参实体类,将Json字符串反序列化成实体类
public class UpdateSignDateInPut { /// <summary> /// 销售出库单单号 /// </summary> public string billNo; /// <summary> /// 签收日期 /// </summary> public string signDate; }
第四步:新增Json帮助类,用于序列化和反序列化Json字符串
using Newtonsoft.Json; namespace HB.Kingdee.Util { /// <summary> /// Json操作工具类 /// </summary> public static class JsonHelper { /// <summary> /// 将string类型的Json串转换为实体类(反序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="param"></param> /// <returns></returns> public static T JsonToT<T>(string param) { return JsonConvert.DeserializeObject<T>(param); } /// <summary> /// 将实体类转换为Json类型的字符串(序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string TToJson<T>(T t) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented); return json; } } }