金蝶云星空自定义WebApi接口开发和调用

1、金蝶云星空自定义WebApi开发

 

 

插件开发好之后,编译生成DLL文件,放到金蝶安装目录\WebSite\bin下,并重启IIS

2、客户端调用

第一种方式,引用Kingdee.BOS.WebApi.Client组件

自定义webapi的名称:接口工程命名空间.类名.方法名,接口工程命名空间

 

 第二种方式,不引用Kingdee.BOS.WebApi.Client组件

 

 

 

 

 

 

 

 

 

 

 

 

 

 3、工具调用自定义webapi示例

接口的URL地址:http://IP/k3cloud/接口工程命名空间.类名.方法名,接口工程命名空间

 

 4、自定义Webapi源码示例

using Kingdee.BOS.WebApi.ServicesStub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.App.Data;
using System.Data;
using Kingdee.BOS.ServiceHelper;

namespace GetCust
{
public class GetCust: AbstractWebApiBusinessService
{
//固定写法,方法名和类名一致
public GetCust(KDServiceContext context): base(context)
{

}
public JObject ExecuteService(JObject parameter)
{
string OrgNo = MyConvert.ToString(parameter["OrgNo"]);
            //返回客户信息
            JObject jsonRoot = new JObject();
            //正确
            JObject jsonRootTrue = new JObject();
            //错误
            JObject jsonRootErr = new JObject();
string Err = "";
if (OrgNo == "")
{
Err = "组织字段不可以为空";
}
else
{

//数据库查询,是否有这个组织
string sql = "/*dialect*/select FORGID from T_ORG_Organizations where FDOCUMENTSTATUS='C' AND FNUMBER = '" + OrgNo + "'";
DataSet ds = DBServiceHelper.ExecuteDataSet(this.KDContext.Session.AppContext, sql);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count == 1)
{
//查询组织下面的客户
string sql1 = @"/*dialect*/select b.FCUSTID,FNUMBER,a.FNAME from T_BD_CUSTOMER_L a inner join T_BD_CUSTOMER b on a.FCUSTID=b.FCUSTID where FUSEORGID=" + (int)dt.Rows[0]["FORGID"] + " and a.FLOCALEID=2052";

DataSet ds1 = DBServiceHelper.ExecuteDataSet(this.KDContext.Session.AppContext, sql1);
DataTable dt1 = ds1.Tables[0];
jsonRootTrue.Add("IsSuccess", "true");

//构造Json
JArray Rows = new JArray();
jsonRootTrue.Add("Cust", Rows);
for (int i = 0; i < dt1.Rows.Count; i++)
{
JObject Row = new JObject();
Row.Add("Custid", dt1.Rows[i]["FCUSTID"].ToString());
Row.Add("CustNumber", dt1.Rows[i]["FNUMBER"].ToString());
Row.Add("CustName", dt1.Rows[i]["FNAME"].ToString());
Rows.Add(Row);
}
}
else
{
Err = "组织字段没有找到对应的编码";
}
}
if (Err != "")
{
jsonRootErr.Add("IsSuccess", "false");
jsonRootErr.Add("Errors", Err);
jsonRoot = jsonRootErr;
}
else
{
jsonRoot = jsonRootTrue;
}
return jsonRoot;
}
}
}

5、客户端调用源码示例

using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
#region 引用Kingdee.BOS.WebApi.Client
//K3CloudApiClient client = new K3CloudApiClient("http://127.0.0.1/k3cloud/");
//var loginResult = client.ValidateLogin("6281cc6e2ae1d5", "Administrator", "qwe123!@#", 2052);

//var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();

//if (resultType == 1)
//{
// string sJson = textBox1.Text;
// textBox2.Text = client.Execute<JObject>("GetCust.GetCust.ExecuteService,GetCust", new object[] { sJson }).ToString();
// //string a = Query(sJson);
//}
#endregion

#region 不引用Kingdee.BOS.WebApi.Client
var result = Login();
var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
if (iResult == 1 || iResult == -5)
{
string sJson = textBox1.Text;
string a = Query(sJson);
textBox2.Text = a;
}
#endregion
}
public static string Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://127.0.0.1/k3cloud/",
"Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");

List<object> Parameters = new List<object>();
Parameters.Add("6281cc6e2ae1d5");//账套标示
Parameters.Add("Administrator");//用户名
Parameters.Add("qwe123!@#");//密码
Parameters.Add(2052);//2052代表中文
httpClient.Content = JsonConvert.SerializeObject(Parameters);

return httpClient.SysncRequest();
}
public static string Query(string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://127.0.0.1/k3cloud/", "GetCust.GetCust.ExecuteService,GetCust.common.kdsvc");

List<object> Parameters = new List<object>();
//业务对象Id
//Parameters.Add(formId);
//Json字串
Parameters.Add(content);
httpClient.Content = JsonConvert.SerializeObject(Parameters);
return httpClient.SysncRequest();
}
}

public class HttpClient
{
/// <summary>
/// Seivice URL
/// </summary>
public string Url { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// Cookie,保证登录后,所有访问持有一个Cookie;
/// </summary>
static CookieContainer Cookie = new CookieContainer();

/// <summary>
/// HTTP访问
/// </summary>
public string SysncRequest()
{
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.CookieContainer = Cookie;
httpRequest.Timeout = 1000 * 60 * 10;//10min

using (Stream reqStream = httpRequest.GetRequestStream())
{
JObject jObj = new JObject();
jObj.Add("format", 1);
jObj.Add("useragent", "ApiClient");
jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());
jObj.Add("parameters", Content);
jObj.Add("timestamp", DateTime.Now);
jObj.Add("v", "1.0");
string sContent = jObj.ToString();
var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Flush();
}
using (var repStream = httpRequest.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(repStream))
{
return ValidateResult(reader.ReadToEnd());
}
}
}

private static string ValidateResult(string responseText)
{
if (responseText.StartsWith("response_error:"))
{
var failText = responseText.TrimStart("response_error:".ToCharArray());
}
return responseText;
}
}
}

 

posted @ 2022-05-27 11:56  放放的Blog  阅读(6954)  评论(0编辑  收藏  举报