正在阅读:
- 首页 » 开发运维 » 后端 » 用友U8API接口webService开发(一)
用友U8API接口webService开发(一)
开发背景:
有外部仓储管理系统在对产品入库后的数据,推送给U8,形成产成品入库单。
开发语言:C#
开发工具:vs2010
开发环境:需要在开发时,安装U8客户端(以12.5版本为例),.Net Framework 4.0
1. 获取和准备API接口基础代码
在安装玩U8客户端之后,其中有一个程序为【API资源管理器】,打开,打开时,会和普通U8一样,需要登录。

以产成品入库单为例,点开左侧菜单,库存管理、产成品入库单,在服务接口下面,有很多接口事件,这里以新增为例
右击【添加新单据】,点击接口描述,可以看到当前接口中的各种信息,包括一些代码中的变量的解释,以及相关字段说明等。
点击代码示例,可以看到使用该接口的基础代码示例(包括vb版和C#版本),这里以c#为例。
2.创建webService项目
打开vs软件,新建一个空的c#web项目

创建项目完成后,在项目中,右击,点击一个新的项,

选择web服务(asmx后缀名)

添加完成后,此时,如果进行运行测试,正常会弹出web,如下图,说明基本的webService搭建成功

点击上图中的 【HelloWorld】,可以进行接口调用测试,而这里的【HelloWorld】即接口名称,在实际开发中,可以进行更换或者创建,如:

补充:
在配置文件【web.config】中增加如下配置信息:
否则,可能会出现接口调用失败等问题。
<configuration> <system.web> <webServices> <protocols> <add name="HttpSoap"/> <add name="HttpPost"/> <add name="HttpGet"/> <add name="Documentation"/> </protocols> </webServices> <compilation debug="true" targetFramework="4.0" /> </system.web> </configuration>
3.创建接口代码
根据实际业务需求,结合webService,将第一部分的代码整合到第二部分的web项目中
创建两个实体类,用来对应单据的表头表体部分,如下:

Headlist.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Headlist
{
public string ccode { get; set; } // 共同 入库/出库单号
public string ddate { get; set; } // 共同 出入库日期
public string cwhname { get; set; } // 入库 仓库
public string csysbarcode { get; set; } // 入库 单据条码
public string iproorderid { get; set; } // 入库 生产订单ID
public string cmpocode { get; set; } // 入库 生产订单号
public string cprobatch { get; set; } // 入库 生产批号
public string cbustype { get; set; } // 共同 业务类型
public string cvouchtype { get; set; } // 共同 单据类型
public string cwhcode { get; set; } // 共同 仓库编码
public string crdcode { get; set; } // 共同 出入库 类别编码
public string cdepcode { get; set; } // 共同 部门编码
public string cmaker { get; set; } // 共同 制单人
public string csource { get; set; } // 共同 来源单据
public string vt_id { get; set; } // 共同 模版号
public string brdflag { get; set; } // 出库 收发标志
public string ccuscode { get; set; } // 出库 客户编码
public string cpersoncode { get; set; } // 出库 业务员
public string bscanexpress { get; set; } // 出库 是否确认快递单号
public string cinvoicecompany { get; set; } // 出库 开票单位
public string cbuscode { get; set; } // 出库 业务号
public string cmemo { get; set; } // 出库 备注
public string cstcode { get; set; } // 出库 销售类型编码
public string cdlcode { get; set; } // 出库 发货单ID
public string cshipaddress { get; set; } // 出库 发货地址
public string caddcode { get; set; } // 出库 发货地址编码
public string cdefine2 { get; set; } // 出库 表头自定义项2,string类型,单包皮重:默认=0
public string cdefine3 { get; set; } // 出库 表头自定义项3,string类型
public string bscanexpress1 { get; set; } // 出库
public Bodylist[] bodylist { get; set; }
}
}Bodylist.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Bodylist
{
public string cinvcode { get; set; } // 共同 产品编码
public string editprop { get; set; } // 共同 编辑属性
public string cinvouchtype { get; set; } // 入库 产成品入库类型
public string cbsysbarcode { get; set; } // 共同 单据行条码
public string cbmemo { get; set; }
public string irowno { get; set; } // 共同 行号
public string cposition { get; set; } // 共同 货位编码
public string cbatch { get; set; } // 共同 批号
public string iquantity { get; set; } // 共同 数量
public string cdefine22 { get; set; } // 共同 自定义项
public string cmolotcode { get; set; }
public string impoids { get; set; }
public string cmocode { get; set; }
public string imoseq { get; set; }
public string brelated { get; set; }
public string bvmiused { get; set; }
public string iexpiratdatecalcu { get; set; }
public string inquantity { get; set; } // 共同 应发/收数量
public string iorderdid { get; set; } // 共同
public string iordertype { get; set; } // 共同 订单类别
public string isotype { get; set; } // 共同
public string cbarcode { get; set; }
public string ipesotype { get; set; } // 出库 需求跟踪方式
public string ipesodid { get; set; } // 出库 销售订单子表id
public string cpesocode { get; set; } // 出库 销售订单
public string ipesoseq { get; set; } // 出库 需求行号
public string cbdlcode { get; set; } // 出库 发货单号
public string iordercode { get; set; } // 出库 销售订单号
public string iorderseq { get; set; } // 出库 销售订单行号
public string bcosting { get; set; } // 出库 是否核算
public string cdefine26 { get; set; } // 出库 自定义项
public string idlsid { get; set; } // 出库 发货单子表ID
public string iposflag { get; set; } // 出库 是否已分配货位
}
}完善第二部分webService接口代码
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using System.ServiceModel;
using System.Text;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </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 WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
//产成品入库接口
[WebMethod]
public String productstockin(String mes)
{
string[] tr2 = new string[2];
tr2[0] = "success";
tr2[1] = "";
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Headlist>));
// json字符串转对象
JObject jsonstr = JObject.Parse(mes);
// 将json对象中的 Headlist 取出,并转换成字符串
String newheadlist = jsonstr["Headlist"].ToString();
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(newheadlist));
object ob = serializer.ReadObject(stream);
List<Headlist> jsonlist = (List<Headlist>)ob;
String tr = jsonlist[0].ccode;
// 调用接口
Productapi api = new Productapi();
tr2 = api.apisendinfo(jsonlist);
}
catch
{
tr2[0] = "error";
tr2[1] = "数据传递错误!";
}
String returnstr = "{\"state\":\"" + tr2[0] + "\",\"ErrorMsg\":\"" + tr2[1] + "\"}";
return returnstr;
}
}
}该日志由 bemender 于 2022年12月01日 发表
转载请注明文本地址:http://www.bemhome.com/post/172.html