一.u9部分
1.编写U9服务
比如发货单创建等等业务。
里面的业务编写要按照下面的套路来比较省力。2.申请到阿里开发者账号,选择商家后台应用开发,获得appkey,下载c#的sdk,然后生成,直接拷贝到项目下面
2.对sdk做进一步的改进
增加上面的[XmlElement("...")],后面我要用一中方法序列化它,把它变成符合奇门格式的xml。做法是把第一个大写的换成小写的。
3.赋值
EntryorderCreateRequest req = new EntryorderCreateRequest();
.......(省略赋值的代码)
string xmlReq = XmlUtil.Serializer(typeof(EntryorderCreateRequest), req);
xmlReq = CommonUtils.XmlCommonReplace(xmlReq).Replace("EntryorderCreateRequest>", "request>");
CommonUtils.WriteLog("WMSSystemAutoSV", "ReceiveTaskAutoSendExtend", "request", xmlReq);
string res = WMSAPIUserExtend.CreateWMSItemMasteInfoExe(xmlReq, 2003);//这里发送请求出去
CommonUtils.WriteLog("WMSSystemAutoSV", "ReceiveTaskAutoSendExtend", "center", res);
4.发送请求的方法 阿里的开发文档有。照着配就可以了。
二.编写回调接口
1.新建一个wcf作为中间件,对外提供http请求(阿里的发货确认等接口需要回调你的接口)
2.编写代码(只展示关键代码)
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace U9API.Interface
{
[ServiceContract]
public interface IWMSAPI
{
/// <summary>
/// 测试Get请求
/// </summary>
/// <returns></returns>
[WebGet(UriTemplate = "Hello?id={id}")]
Stream Hello(string id);
/// <summary>
/// 测试POST请求
/// </summary>
/// <returns></returns>
[WebInvoke(UriTemplate = "HelloPost", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
Stream HelloPost(Stream xmlResponse);
/// <summary>
/// WMS入库确认回调此接口
/// </summary>
/// <param name="xmlResponse"></param>
/// <returns></returns>
[WebInvoke(UriTemplate = "EntryorderConfirmResponse", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
Stream EntryorderConfirmResponse(Stream xmlResponse);
/// <summary>
/// WMS发货确认回调此接口
/// </summary>
/// <param name="xmlResponse"></param>
/// <returns></returns>
[WebInvoke(UriTemplate = "DeliveryorderConfirmResponse", Method = "POST")]
Stream DeliveryorderConfirmResponse(Stream xmlResponse);
/// <summary>
/// WMS退货入库确认回调此接口
/// </summary>
/// <param name="xmlResponse"></param>
/// <returns></returns>
[WebInvoke(UriTemplate = "ReturnorderConfirmResponse", Method = "POST",BodyStyle = WebMessageBodyStyle.Bare)]
Stream ReturnorderConfirmResponse(Stream xmlResponse);
}
}
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.Text;
using U9API.Interface;
using U9API.Utils;
using System;
using System.Configuration;
using System.Web.Script.Serialization;
namespace U9API.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class WMSAPI : IWMSAPI
{
Config _config = new Config();
#region 测试接口
public Stream Hello(string id)
{
string result = "你好 GET!参数 id=" + id;
CommonUtils.WriteLog("WMSAPI","Hello", "request", id);
//string filePath = CommonUtils.CreateLog("Hello", id);
byte[] array = Encoding.UTF8.GetBytes(result);//将字符串转化为字节数组
MemoryStream memory = new MemoryStream(array);//初始化MemoryStream类
CommonUtils.WriteLog("WMSAPI","Hello", "response", result);
return memory;
}
public Stream HelloPost(Stream xmlResponse)
{
StreamReader sr = new StreamReader(xmlResponse, Encoding.GetEncoding("UTF-8"));
string response = sr.ReadToEnd();
sr.Close();
string result = string.Empty;
CommonUtils.WriteLog("WMSAPI","HelloPost", "request", response);
result = "你好 POST!参数 " + response;
byte[] array = Encoding.UTF8.GetBytes(result);//将字符串转化为字节数组
MemoryStream memory = new MemoryStream(array);//初始化MemoryStream类
CommonUtils.WriteLog("WMSAPI","HelloPost", "response", result);
return memory;
}
#endregion
public Stream EntryorderConfirmResponse(Stream xmlResponse)
{
StreamReader sr = new StreamReader(xmlResponse, Encoding.GetEncoding("UTF-8"));
string response = sr.ReadToEnd();
string result = string.Empty;
sr.Close();
try
{
CommonUtils.WriteLog("WMSAPI","EntryorderConfirmResponse", "request", response);
ItemPartAutoReceive.ItemPartAutoReceiveStub client = new ItemPartAutoReceive.ItemPartAutoReceiveStub();
ItemPartAutoReceive.ISVContext thContext = new ItemPartAutoReceive.ISVContext();
thContext.OrgID = Convert.ToInt64(_config.OrgID);
thContext.EntCode = _config.EntCode;
thContext.OrgCode = _config.OrgCode;
thContext.UserCode = _config.UserCode;
thContext.CultureName = _config.CultureName;
//string url = "http://@/SV.API.ErpAPISV.WMSSystemAutoSV.IItemPartAutoReceive.svc";
//client.Url = url.Replace("@", _config.SVUrl);
result = client.Do(thContext, response);
}
catch (Exception ex)
{
result = CommonUtils.GetXmlResponse(false, ex.ToString());
}
CommonUtils.WriteLog("WMSAPI","EntryorderConfirmResponse", "response", result);
byte[] array = Encoding.UTF8.GetBytes(result);//将字符串转化为字节数组
MemoryStream memory = new MemoryStream(array);//初始化MemoryStream类
return memory;
}
public Stream DeliveryorderConfirmResponse(Stream xmlResponse)
{
StreamReader sr = new StreamReader(xmlResponse, Encoding.GetEncoding("UTF-8"));
string request = sr.ReadToEnd();
string result = string.Empty;
sr.Close();
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
dynamic resObject = javaScriptSerializer.DeserializeObject(request) as dynamic;
string loginUserCode = resObject["loginUserCode"];
string password = resObject["password"];
string data = System.Web.HttpUtility.UrlDecode(resObject["data"]);
//string data = resObject["data"];
bool isAuthentication = CommonUtils.Authentication(loginUserCode, password);
if (isAuthentication == false)
{
result = CommonUtils.GetXmlResponse(false, "isAuthentication==false");
}
else
{
CommonUtils.WriteLog("WMSAPI", "DeliveryorderConfirmResponse", "request", data);
ItemPartAutoSend.ItemPartAutoSendStub client = new ItemPartAutoSend.ItemPartAutoSendStub();
ItemPartAutoSend.ISVContext thContext = new ItemPartAutoSend.ISVContext();
thContext.EntCode = _config.EntCode;
thContext.OrgCode = _config.OrgCode;
thContext.UserCode = _config.UserCode;
thContext.CultureName = _config.CultureName;
thContext.OrgID = Convert.ToInt64(_config.OrgID);
//string url = "http://@/SV.API.ErpAPISV.WMSSystemAutoSV.IItemPartAutoSend.svc";
//client.Url = url.Replace("@", _config.SVUrl);
result = client.Do(thContext, data);
}
}
catch (Exception ex)
{
result = CommonUtils.GetXmlResponse(false, ex.ToString());
}
CommonUtils.WriteLog("WMSAPI","DeliveryorderConfirmResponse", "response", result);
byte[] array = Encoding.UTF8.GetBytes(result);//将字符串转化为字节数组
MemoryStream memory = new MemoryStream(array);//初始化MemoryStream类
return memory;
}
/// <summary>
/// 退货入库
/// </summary>
/// <param name="xmlResponse"></param>
/// <returns></returns>
public Stream ReturnorderConfirmResponse(Stream xmlResponse)
{
StreamReader sr = new StreamReader(xmlResponse, Encoding.GetEncoding("UTF-8"));
string response = sr.ReadToEnd();
sr.Close();
string result = string.Empty;
try
{
CommonUtils.WriteLog("WMSAPI","ReturnorderConfirmResponse", "request", response);
ItemPartAutoReturn.ItemPartAutoReturnStub client = new ItemPartAutoReturn.ItemPartAutoReturnStub();
ItemPartAutoReturn.ISVContext thContext = new ItemPartAutoReturn.ISVContext();
thContext.EntCode = _config.EntCode;
thContext.OrgCode = _config.OrgCode;
thContext.UserCode = _config.UserCode;
thContext.CultureName = _config.CultureName;
thContext.OrgID = Convert.ToInt64(_config.OrgID);
string url = "http://@/SV.API.ErpAPISV.WMSSystemAutoSV.IItemPartAutoReturn.svc";
client.Url = url.Replace("@", _config.SVUrl);
result = client.Do(thContext, response);
}
catch (Exception ex)
{
result = CommonUtils.GetXmlResponse(false, ex.ToString());
}
CommonUtils.WriteLog("WMSAPI","ReturnorderConfirmResponse", "response", result);
byte[] array = Encoding.UTF8.GetBytes(result);//将字符串转化为字节数组
MemoryStream memory = new MemoryStream(array);//初始化MemoryStream类
return memory;
}
}
}
using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;
using U9API.Service;
using U9API.Utils;
namespace U9API
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("WMSAPI", new WebServiceHostFactory(), typeof(WMSAPI)));
}
}
}