webapi实现单据保存提交和审核原创
27人赞赏了该文章
8,847次浏览
编辑于2021年08月24日 19:46:23
一、说明:
下面的代码实现需要引用Kingdee.BOS.WebApi.Client.dll组件,以自定义单据A为示例,其formId为:KKK_BillA,运行结果如下图:
二、实现代码如下
1. 接口调用类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using Kingdee.BOS.WebApi.Client; namespace ConsoleTest.WebApiClient { /// <summary> /// 公共接口类 /// </summary> public class KDClient { private K3CloudApiClient _client; //平台封装的客户端接口 private string _serviceUrl = "http://localhost:2800/K3Cloud/"; //服务器地址 public K3CloudApiClient Client { get { return this._client; } } public KDClient(string serviceUrl = "") { var sUrl = serviceUrl == "" ? this._serviceUrl : serviceUrl; this._client = new K3CloudApiClient(sUrl); } /// <summary> /// 参数固定情况下的登陆 /// </summary> /// <returns></returns> public bool Login2() { return this.Login("5f191f0ff4b034","demo","888888"); } // 登陆 public bool Login(string dbId, string userName, string pwd, int lcId = 2052) { var result = this._client.ValidateLogin(dbId, userName, pwd, lcId); JObject joResult = JObject.Parse(result); int resultType = joResult["LoginResultType"].Value<int>(); if (resultType == 1) return true; //表示登陆成功 return false; //其他不成功,可以记录日志把整个结果记录下来 } } }
2.具体单据调用接口类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; namespace ConsoleTest.WebApiClient { /// <summary> /// 具体单据调用接口类 /// </summary> public class BillAClient { private KDClient _kdClient; private string _formId="KKK_BillA"; //表单formId private bool _isLogin = false; //是否登陆过,每个实例登陆一次即可 private bool _isLoginSuccess = false; public bool IsLoginSuccess //是否登陆成功 { get { if (_isLogin == false) { this._isLoginSuccess = this._kdClient.Login2(); this._isLogin = true; } return this._isLoginSuccess; } } /// <summary> /// 构造函数 /// </summary> public BillAClient() { this._kdClient = new KDClient(); } /// <summary> /// 保存提交和审核 /// </summary> public void SaveWithSubmitAndAudit() { var result = this.Save(); if (result.Item1) { result = this.Submit(result.Item2); } if (result.Item1) { this.Audit(result.Item2); } } /// <summary> /// 保存接口 /// </summary> public Tuple<bool, long> Save() { var data = "{\"Model\": { \"F_KKK_Text1\": \"111\"}"; var result = this._kdClient.Client.Save(this._formId, data); return ParseResult(result, this._kdClient.Client.Save, new string[2] { this._formId, data }); } /// <summary> /// 提交接口 /// </summary> public Tuple<bool, long> Submit(long pkId) { var data = "{"+string.Format("Ids:{0}", pkId)+"}"; var result = this._kdClient.Client.Submit(this._formId, data); return ParseResult(result, this._kdClient.Client.Submit, new string[2] { this._formId, data }); } /// <summary> /// 审核接口 /// </summary> public Tuple<bool, long> Audit(long pkId) { var data = "{" + string.Format("Ids:{0}", pkId) + "}"; var result = this._kdClient.Client.Audit(this._formId, data); return ParseResult(result, this._kdClient.Client.Audit, new string[2] { this._formId, data }); } /// <summary> /// 解析结果 /// </summary> /// <param name="result"></param> /// <param name="func"></param> /// <returns></returns> private Tuple<bool,long> ParseResult(string result,Func<string,string,string> func, params string[] prams) { JObject joResult = JObject.Parse(result); var sessionMsgCode = joResult["Result"]["ResponseStatus"]["MsgCode"].Value<int>(); if (sessionMsgCode == 1) //代表会话丢失重新登陆 { this._kdClient.Login2(); joResult = JObject.Parse(func(prams[0], prams[1])); } var isSuccuess = joResult["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>(); var pkId = joResult["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"].Value<long>(); return Tuple.Create<bool, long>(isSuccuess, pkId); } } }
3. 控制台调用程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleTest.WebApiClient; namespace ConsoleTest { public class Program { public static void Main(string[] args) { BillAClient billCilent = new BillAClient(); if (billCilent.IsLoginSuccess) { billCilent.SaveWithSubmitAndAudit(); } Console.ReadLine(); } } }
赞 27
27人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
1人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读