1. 程式人生 > 其它 >金蝶雲星空自定義WebApi介面開發和呼叫

金蝶雲星空自定義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;
}
}
}