码迷,mamicode.com
首页 > Windows程序 > 详细

Api接口开发实例

时间:2018-05-10 23:42:13      阅读:429      评论:0      收藏:0      [点我收藏+]

标签: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<JSONArray>("api.Class1.ExecuteService,api",null);
        }
        Console.Write(responseOut1);
        Console.ReadLine();

    }
}

}

生成解决方案后,打断点进行测试结果如下:
技术分享图片

Api接口开发实例

标签:api

原文地址:http://blog.51cto.com/yataigp/2115046

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!