webservice
1. 氚雲后端代碼,做個中間層webservice,通過webservice去調用第三方返回數據,在將數據轉義為JSON字符串,以SOAP協議回傳給氚雲。
總體業務流程圖如下:
2. webservice規范性:只能有4個同名接口,大小寫都要一致,返回類型都為JSON字符串。對應關系如下表:
序號 |
函數 |
1 |
GetSchema(string schemaCode) |
2 |
GetSchemaList() |
3 |
GetList(string userCode, string schemaCode, string filter) |
4 |
Invoke(string userCode, string schemaCode, string methodName, string param)
|
3.webservice示例:
H3chuanyun氚雲輔助包
鏈接: https://pan.baidu.com/s/11Kjd3z1qRRvTnC5kWVR5cw 提取碼: 2aqr
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using System.Web.Services; using Common; using H3.BizBus; using WebApplication2.model; using Newtonsoft.Json; using static WebApplication2.Class2; using Newtonsoft.Json.Linq; using System.Web.Script.Serialization; using static WebApplication2.Class1; using System.Runtime.Serialization.Json; using Microsoft.Ajax.Utilities; namespace WebApplication2 { /// <summary> /// WebDemo 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消注釋以下行。 // [System.Web.Script.Services.ScriptService] public class WebDemo : System.Web.Services.WebService { [WebMethod] //定義氚雲表單結構 public string GetSchema(string schemaCode) { string strSchemaJson = string.Empty; BizStructureSchema schema = new BizStructureSchema();//創建一個業務結構 string errorMessage = string.Empty; schema.Code = schemaCode; schema.Add(new ItemSchema("Status", "返回結果", BizDataType.String)); schema.Add(new ItemSchema("Msg", "返回結果", BizDataType.String)); schema.Add(new ItemSchema("KHName", "KHName", BizDataType.String)); schema.Add(new ItemSchema("KHNage", "KHNage", BizDataType.Int)); schema.Add(new ItemSchema("KHNGender", "KHNGender", BizDataType.String)); schema.Add(new ItemSchema("KHNbithday", "KHNbithday", BizDataType.DateTime)); strSchemaJson = BizStructureUtility.SchemaToJson(schema); //string jsStr = Newtonsoft.Json.JsonConvert.SerializeObject(schemaCode); return strSchemaJson;//返回JSON字符串 } [WebMethod] /// <summary> /// 獲取第三方數據至氚雲(整個表單) /// </summary> /// <param name="userCode">氚雲平台用戶Id</param> /// <param name="schemaCode">氚雲平台表單編碼</param> /// <param name="filter">查詢條件,josn格式數據</param> /// <returns></returns> //獲取數據庫數據將數據庫數據轉json格式傳入氚雲 public string GetList(string userCode, string schemaCode, string filter) { //數據庫 DSFWSEntities1 db = new DSFWSEntities1(); //對filter數據進行value獲取 string value = FJLdata(filter); if (value == "getcode") { int resultCodein = 0; //全局返回狀態 0為成功 否則失敗 string resultMsg = ""; //全局返回信息 int dataCount = 0; //返回數據條數 H3.BizBus.BizStructureSchema schema = null; string errorMessage = string.Empty; //獲取BizStructureSchema字段結構 BizStructureUtility.JsonToSchema(this.GetSchema(schemaCode), out schema, out errorMessage); //定義返回的數據集合,這里以返回多條數據為例 List<BizStructure> list = new List<BizStructure>(); var json = db.AddInside.SingleOrDefault(d=>d.dbsave==1); if (json != null&&schemaCode!=json.schema) { string jsoninside = json.json; JObject obj = JObject.Parse(jsoninside); KHNcode khn = new KHNcode() { KHName = obj["KHName"].ToString(), KHNage = int.Parse(obj["KHNage"].ToString()), KHNGender = obj["KHNGender"].ToString(), KHNbirthday = DateTime.Parse(obj["KHNbirthday"].ToString()) }; Console.WriteLine(khn); BizStructure bizStructure = new H3.BizBus.BizStructure(schema); bizStructure["Status"] = "true"; bizStructure["Msg"] = "msg"; bizStructure["KHName"] = khn.KHName; bizStructure["KHNage"] = int.Parse(khn.KHNage.ToString()); bizStructure["KHNGender"] = khn.KHNGender; string khnco = khn.KHNGender; bizStructure["KHNbithday"] = DateTime.Parse(khn.KHNbirthday.ToString()); list.Add(bizStructure); resultMsg = "inset"; } else { //如果數據為空,創建一個空對象 BizStructure bizStructure = new H3.BizBus.BizStructure(schema); list.Add(bizStructure); } ListResult listResult = new ListResult(resultCodein, resultMsg, list.ToArray(), dataCount); //var listbiz= BizStructureUtility.ListResultToJson(listResult); //JObject objlistbiz = JObject.Parse(listbiz); //JArray objjsonbiz = (JArray)objlistbiz["Data"];//拿值 if (resultMsg=="inset") { var redbsave = db.AddInside.SingleOrDefault(d=>d.dbsave==1); db.AddInside.Remove(redbsave); if (db.SaveChanges()>0) { return BizStructureUtility.ListResultToJson(listResult); } } //返回序列化之后的數據 return BizStructureUtility.ListResultToJson(listResult); } if (value == "delcode" ) { int resultCodein = 0; //全局返回狀態 0為成功 否則失敗 string resultMsg = ""; //全局返回信息 int dataCount = 0; //返回數據條數 H3.BizBus.BizStructureSchema schema = null; string errorMessage = string.Empty; //獲取BizStructureSchema字段結構 BizStructureUtility.JsonToSchema(this.GetSchema(schemaCode), out schema, out errorMessage); //定義返回的數據集合,這里以返回多條數據為例 List<BizStructure> list = new List<BizStructure>(); var dbremove = db.RemoveCode.SingleOrDefault(d=>d.dbre==1); if (dbremove != null && schemaCode != dbremove.reschema) { string jsoninside = dbremove.rejson; JObject obj = JObject.Parse(jsoninside); KHNcode khn = new KHNcode() { KHName = obj["KHName"].ToString(), KHNage = int.Parse(obj["KHNage"].ToString()), KHNGender = obj["KHNGender"].ToString(), KHNbirthday = DateTime.Parse(obj["KHNbirthday"].ToString()) }; BizStructure bizStructure = new H3.BizBus.BizStructure(schema); bizStructure["Status"] = "true"; bizStructure["Msg"] = "msg"; bizStructure["KHName"] = khn.KHName; bizStructure["KHNage"] = int.Parse(khn.KHNage.ToString()); bizStructure["KHNGender"] = khn.KHNGender; bizStructure["KHNbithday"] = DateTime.Parse(khn.KHNbirthday.ToString()); list.Add(bizStructure); resultMsg = "deletecode"; } else { //如果數據為空,創建一個空對象 BizStructure bizStructure = new H3.BizBus.BizStructure(schema); list.Add(bizStructure); } ListResult listResult = new ListResult(resultCodein, resultMsg, list.ToArray(), dataCount); if (resultMsg== "deletecode") { var redbremove = db.RemoveCode.SingleOrDefault(d => d.dbre == 1); db.RemoveCode.Remove(redbremove); if (db.SaveChanges() > 0) { return BizStructureUtility.ListResultToJson(listResult); } } //返回序列化之后的數據 return BizStructureUtility.ListResultToJson(listResult); } return ""; } ///filter進行轉化獲取value值,進行判斷,返回到getlist /// public string FJLdata(string json) { JObject obj = JObject.Parse(json); JArray jsonstr = (JArray)obj["Matcher"]["Matchers"]; string value = jsonstr[0]["Value"].ToString(); return value; } [WebMethod] //氚雲后端調用的方法名 public string Invoke(string userCode, string schemaCode, string methodName, string param) { DSFWSEntities1 db = new DSFWSEntities1(); int i = 0; string resultCode = "{\"resultCode\":0}"; //是否成功 0是成功 否則失敗 string resultMessage = "全局信息添加到數據庫失敗"; //全局返回消息 //氚雲調用webservice將數據存儲到數據庫,成功返回json格式 if (methodName == "submit") { KHNcode kh = new KHNcode(); JObject obj = JObject.Parse(param); kh.KHName = obj["KHName"].ToString(); kh.KHNage = int.Parse(obj["KHNage"].ToString()); kh.KHNGender = obj["KHNGender"].ToString(); kh.KHNbirthday = DateTime.Parse(obj["KHNbirthday"].ToString()); //返回數據 Console.WriteLine(resultCode); db.KHNcode.Add(kh); if (db.SaveChanges() > 0) { AddInside add = new AddInside(); add.dbsave = 1; add.json = param; add.schema = schemaCode; db.AddInside.Add(add); if (db.SaveChanges()>0) { return resultCode; } } else { return resultMessage; } } //這是全部數據 //第三方調用氚雲數據,將數據存儲到數據庫,存儲json轉list else if (methodName == "alldata") { i++; if (i<2) { string apiAddress = @"https://www.h3yun.com/OpenApi/Invoke"; HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(apiAddress); request.Method = "POST"; request.ContentType = "application/json"; request.Headers.Add("EngineCode", "i8f8rhfz68zbdfukhhz48kiw4"); request.Headers.Add("EngineSecret", "yhqF1mujLZtpa+iDHmAvzsfHJjQpWp8AuAhX/HxCy8W5FpRfDWa20Q=="); //身份認證參數 //request.Headers.Add("EngineCode", "fj854kdi05x0ur8ua4za9m766"); //request.Headers.Add("EngineSecret", "TiX3emcu3h0jN+SBtKDo6nfmb7jHD6ksM1ut961D56a/jdmKUGm62w=="); //參數 Dictionary<string, object> dicParams = new Dictionary<string, object>(); dicParams.Add("ActionName", "LoadBizObjects"); dicParams.Add("SchemaCode", "D000685KHNametwo"); dicParams.Add("Filter", "{\"FromRowNum\": 0,\"RequireCount\": false,\"ReturnItems\": [], \"SortByCollection\": [],\"ToRowNum\": 500, \"Matcher\": { \"Type\": \"And\", \"Matchers\": []}}"); string jsonData = JsonConvert.SerializeObject(dicParams); byte[] bytes; bytes = System.Text.Encoding.UTF8.GetBytes(jsonData); request.ContentLength = bytes.Length; using (Stream writer = request.GetRequestStream()) { writer.Write(bytes, 0, bytes.Length); writer.Close(); } string strValue = string.Empty; using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse()) { using (System.IO.Stream s = response.GetResponseStream()) { string StrDate = string.Empty; using (StreamReader Reader = new StreamReader(s, Encoding.UTF8)) { while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } } } } return DSFWs(strValue); } } //進行刪除,獲取methodName,拿到param,查詢數據庫對數據進行判定,進行對數據庫刪除 else if (methodName=="delete") { List<KHNcode> khncode = new List<KHNcode>(); JObject obj = JObject.Parse(param); if (obj.Count==4) { string khname = obj["KHName"].ToString(); var value = db.KHNcode.SingleOrDefault(d => d.KHName == khname); if (value!=null) { db.KHNcode.Remove(value); if (db.SaveChanges()>0) { RemoveCode remove = new RemoveCode(); remove.dbre = 1; remove.rejson = param; remove.reschema = schemaCode; db.RemoveCode.Add(remove); if (db.SaveChanges() > 0) { return resultCode; } } else { return resultMessage; } } } } return "none"; } } }
H3chuanyun氚雲輔助包
鏈接: https://pan.baidu.com/s/11Kjd3z1qRRvTnC5kWVR5cw 提取碼: 2aqr