重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Api接口开发实例

目的:第三方系统可以调用k3cloud系统中的库存数据与表单数据、报表数据进行数据分析;
实验研究:本来打算使用金蝶协同开发平台进行测试开发api接口,测试没有成功,然后就开始使用早期开发使用 的方法进行测试,然后成功了!时间宝贵,潦草写了以下的代码步骤,大家可以作为参考!
步骤:1 vs中新建类 ,引用需要的命名空间;
2 vs中新建控制台,为了测试第三方平台调用测试成功与否;
代码如下:
新建类
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.App.Data;
using System.ComponentModel;
using Kingdee.BOS.WebApi.ServicesStub;

成都创新互联是一家从事企业网站建设、网站制作、网站设计、行业门户网站建设、网页设计制作的专业网络公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点成百上千。

namespace api
{
[Description("接口测试")]
public class Class1 : Kingdee.BOS.WebApi.ServicesStub.AbstractWebApiBusinessService
{
public Class1(Kingdee.BOS.ServiceFacade.KDServiceFx.KDServiceContext context)
: base(context)
{ }

private DynamicObjectCollection GetInven()
{
StringBuilder str = new StringBuilder();
str.Append(@" /dialect/ SELECT t6.FNUMBER,t7.fname,m.FNUMBER, ml.fname,w.fname,isnull(t14.fname,'无') ,TSL.fname,tsl1.FNUMBER ,
sum(TI.FBASEQTY) FBASEQTY,st01.FNAME
FROM (select FBASEQTY,FSTOCKID,FMATERIALID,FSTOCKSTATUSID,FSTOCKORGID from T_STK_INVENTORY where FMATERIALID>1 and FSTOCKSTATUSID=10000 and FOBJECTTYPEID= 'STK_Inventory' and FKEEPERTYPEID='BD_KeeperOrg'and FOWNERTYPEID='BD_OwnerOrg' and FISEFFECTIVED=1 and FBASEQTY<>0) TI
inner join (select FSTOCKSTATUSID,FNAME From T_BD_STOCKSTATUS_L where FSTOCKSTATUSID>1 and FLOCALEID=2052) st01 on st01.FSTOCKSTATUSID=TI.FSTOCKSTATUSID
inner JOIN (select fnumber,FMASTERID,FMATERIALID,FMATERIALGROUP from T_BD_MATERIAL where FMATERIALID>1 and FDOCUMENTSTATUS='C' and FFORBIDSTATUS='A' ) M ON M.FMATERIALID = TI.FMATERIALID
LEFT JOIN (select FMATERIALID,FLOCALEID,fname from T_BD_MATERIAL_L where FMATERIALID>1 and FLOCALEID=2052) ML ON (M.FMASTERID = ML.FMATERIALID)
LEFT JOIN (select FSTOCKID,fname,FLOCALEID From T_BD_STOCK_L where FSTOCKID>1 and FLOCALEID=2052) TSL ON (TSL.FSTOCKID = TI.FSTOCKID)
LEFT JOIN (select fstockid,FGROUP,fnumber from T_BD_STOCK where fstockid>1 and FDOCUMENTSTATUS='C' and FFORBIDSTATUS='A') tsl1 on TSL.FSTOCKID=tsl1.fstockid
LEFT join (select FID,fname from T_BD_STOCKGROUP_L where FID>1 and FLOCALEID=2052) t14 on tsl1.FGROUP=t14.fid
inner join (select FMATERIALID,FCATEGORYID From T_BD_MATERIALBASE where FCATEGORYID>1) u0 on m.FMATERIALID=u0.FMATERIALID
left join (select FCATEGORYID,fname From T_BD_MATERIALCATEGORY_L where FCATEGORYID>1 and FLOCALEID=2052) w on u0.FCATEGORYID=w.FCATEGORYID
left join (select fid,FPARENTID From T_BD_MATERIALGROUP where fid>1) t4 on m.FMATERIALGROUP=t4.fid
left join (select fid,FNUMBER From T_BD_MATERIALGROUP where fid>1 ) t6 on t4.FPARENTID=t6.fid
left join (select fid,fname From T_BD_MATERIALGROUP_L where fid>1 and FLOCALEID=2052) t7 on t4.FPARENTID=t7.fid
inner join (select FORGID,fname From T_ORG_ORGANIZATIONS_L where FORGID>1 and FLOCALEID=2052) cc on TI.FSTOCKORGID=cc.FORGID
group by t6.FNUMBER,t7.fname,ml.fname,m.FNUMBER,tsl.fname,w.fname,isnull(t14.fname,'无'),TSL.fname,tsl1.FNUMBER,st01.FNAME");
return DBUtils.ExecuteDynamicObject(this.KDContext.Session.AppContext, str.ToString());
}
public JSONArray ExecuteService()
{

        DynamicObjectCollection list = GetInven() as DynamicObjectCollection;
        JSONArray jsonArray = new JSONArray();
        foreach (DynamicObject dynamicObject in list)
        {
            jsonArray.Add(ConvertDynamicObject2Json(dynamicObject));
        }
        returnjsonArray;
    }

    private JSONObject ConvertDynamicObject2Json(DynamicObject dynamicObject)
    {
        DynamicPropertyCollection dynamicPropertyCollection = dynamicObject.DynamicObjectType.Properties;
        JSONObject obj = new JSONObject();
        foreach (DynamicProperty property in dynamicPropertyCollection)
        {
            obj.Put(property.Name, dynamicObject[property.Name]);
        }
        return obj;
    }
}

}

新建控制台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.JSON;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{

        ApiClient client1 = new Kingdee.BOS.WebApi.Client.ApiClient("http://localhost/k3cloud/");
        bool result1 = client1.Login("数据中心id", "用户名", "密码", 2052);
        if (result1)
        {

         object   responseOut1 = client1.Execute("api.Class1.ExecuteService,api",null);
        }
        Console.Write(responseOut1);
        Console.ReadLine();

    }
}

}

生成解决方案后,打断点进行测试结果如下:
Api接口开发实例


文章名称:Api接口开发实例
分享链接:http://cqcxhl.cn/article/jjiods.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP