金蝶云星空自定义接口:查询数据库

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

一.项目需求

1.客户要求在“海柔”系统上调用“金蝶”自定义接口满足海柔系统获取金蝶数据的效果。

二.代码思路

1.自定义接口业务接口调用金蝶标准登陆接口http://域名/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc

2.将查询语句再单独封装成一个自定义接口。

3.在父级接口中调用查询语句封装的接口得到所需数据。

三.可能遇到的问题

1.如果不将“查询语句”业务单独封装成接口则会出现this.KDContext.Session.AppContext为null的情况找不到数据中心。

四.代码案例

1.业务需求代码自定义接口

using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.ServicesStub;
using System.ComponentModel;
using System;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.ServiceHelper;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
using NoticeOfReceipt;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.App.Data;
using System.Collections.Generic;

namespace NoticeOfReceipt
{
    [HotUpdate]
    [Description("自定义接口查询供应商对应仓库物料库存数据")]
    public class WarehouseStock_Interface : AbstractWebApiBusinessService
    {

        public WarehouseStock_Interface(KDServiceContext context) : base(context)
        {

        }

        //供应商编码物料编码
        public string SelectStockQtys(string AcctID, string Username, string Password, string FSUPPLIERNUMBER,string FNUMBER)
        {
            JArray arr = new JArray();

            string ServiceURL = "http://localhost/";

            string Logindata = "";
            Dictionary<String, Object> dic;

            try
            {
				//登陆参数
                Object str = new
                {
                    acctID = AcctID,	//账套ID
                    username = Username,	//用户名
                    password = Password,	//密码
                    lcid = 2052
                };
                string login = Posturl.Post(str.ToJson(), ServiceURL + "k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc", "");
                Dictionary<String, Object> jsonDict = JsonConvert.DeserializeObject<Dictionary<String, Object>>(login);
                Logindata = jsonDict["LoginResultType"].ToString();
                dic = JsonConvert.DeserializeObject<Dictionary<String, Object>>(jsonDict["Context"].ToString());
            }
            catch (Exception)
            {
                arr.Add("{\"Message\":\"登录失败了\"}");
                return arr.ToString();
            }

            if (Logindata != "1")
            {
                arr.Add("{\"Message\":\"登录失败了\"}");
                return arr.ToString();
            }
			//自定义查询接口参数
            Object parameter = new
            {
                FSUPPLIERNUMBER = FSUPPLIERNUMBER,
                FNUMBER = FNUMBER,
            };
			
			//调用查询接口返回结果。
            return Posturl.Post(parameter.ToJson(), ServiceURL + "/K3Cloud/NoticeOfReceipt.WarehouseStockSql.SelectData,NoticeOfReceipt.common.kdsvc", dic["SessionId"].ToString());

          
        }
    }
}

2.自定义数据库查询接口被1接口调用

using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.ServicesStub;
using System.ComponentModel;
using System;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.ServiceHelper;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
using NoticeOfReceipt;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.App.Data;
using System.Collections.Generic;

namespace NoticeOfReceipt
{
    [HotUpdate]
    [Description("自定义接口查询供应商对应仓库物料库存数据")]
    internal class WarehouseStockSql : AbstractWebApiBusinessService
    {
        public WarehouseStockSql(KDServiceContext context) : base(context)
        {

        }

        public string SelectData(string FSUPPLIERNUMBER, string FNUMBER)
        {
            string sql = string.Format(@"/*dialect*/   数据库查询语句根据自己的业务自行编码");

           // if (!string.IsNullOrEmpty(FSUPPLIERNUMBER))
            //{
                //sql += $" and ISNULL(FSUPPLIERNUMBER, '') = '{FSUPPLIERNUMBER}'";
            //}

            //if (!string.IsNullOrEmpty(FNUMBER))
           // {
               // sql += $" and FNUMBER = '{FNUMBER}'";
           // }
            DynamicObjectCollection data = DBUtils.ExecuteDynamicObject(this.KDContext.Session.AppContext, sql);

            return JsonConvert.SerializeObject(data);
        }
    }
}

3.Posturl类用于调用自定义接口

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace NoticeOfReceipt
{
    public class Posturl
    {
        public static string cke = "";
        public static string Post(string param, string requestUriString, string sessionid)
        {
            string result;
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = ((object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) => true);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
                httpWebRequest.Method = "POST";
                //string Connection = "keep-alive";
                httpWebRequest.ContentType = "application/json";
                if (!string.IsNullOrEmpty(sessionid))
                    httpWebRequest.Headers.Add("Sessionid", sessionid);
                httpWebRequest.Headers.Add("Cookie", cke);
                byte[] bytes = Encoding.UTF8.GetBytes(param);
                httpWebRequest.ContentLength = (long)bytes.Length;
                Stream requestStream = httpWebRequest.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                if (string.IsNullOrEmpty(sessionid))
                {
                    cke = httpWebResponse.Headers["Set-Cookie"];
                }
                string text = "";
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                //string str;
                //while ((str = streamReader.ReadLine()) != null)
                //{
                //    text = text + str + "\r\n";
                //}
                result = streamReader.ReadToEnd();
                responseStream.Close();
            }
            catch (WebException ex)
            {

                result = "调用失败";
            }

            return result;
        }
    }
}

五.ApiPost测试请求

ApiPost下载链接https://www.apipost.cn/?utm_source=10120
1.调用链接http://域名/K3Cloud/程序集.类名.方法名,程序集.common.kdsvc
2.Http请求方式Post
3.请求结果调用成功
在这里插入图片描述

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: 数据库