氚云第三方连接Webservice

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 

  

posted @ 2022-01-25 10:25  山君与见山  阅读(959)  评论(0编辑  收藏  举报