You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
26 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using NetLibrary.Data;
namespace NetLibrary.Express
{
public class WishYouApi
{
//用户网站上生成的API密钥
public string api_key { get; set; }
//物流渠道标识
public int zx_type { get; set; }
//用户提供的单次提交唯一标识
public int bid { get; set; }
//用户提供的运单唯一标识,将跟随运单号返回
public string guid { get; set; }
//是否挂号件(0=否1=是)
public string otype { get; set; }
//寄件人姓名(留空将使用地址簿默认信息)
public string from { get; set; }
//寄件人省名(留空将使用地址簿默认信息)
public string sender_addres { get; set; }
//寄件人城市名(留空将使用地址簿默认信息)
public string sender_province { get; set; }
//寄件人地址(留空将使用地址簿默认信息)
public string sender_city { get; set; }
//寄件人电话(留空将使用地址簿默认信息)
public string sender_phone { get; set; }
//收件人姓名
public string CustomName { get; set; }
//收件人姓名(到达国语言,留空将使用英文信息)
public string to_local { get; set; }
//收件人地址(英文)
public string recipient_addres { get; set; }
//收件人地址(到达国语言,留空将使用英文信息)
public string recipient_addres_local { get; set; }
//收件人国家代码
public string recipient_country_short { get; set; }
//收件人国家(到达国语言,留空将使用英文信息)
public string recipient_country_local { get; set; }
//收件人州名(英文)
public string recipient_province { get; set; }
//收件人国家(到达国语言,留空将使用英文信息)
public string recipient_province_local { get; set; }
//收件人城市名(英文)
public string recipient_city { get; set; }
//收件人城市名(到达国语言,留空将使用英文信息)
public string recipient_city_local { get; set; }
//收件人邮编
public string recipient_postcode { get; set; }
//收件人电话
public string recipient_phone { get; set; }
//内件物品的详细名称(英文描述,多个品种请合并)
public string content { get; set; }
//内件类型代码(1=礼品,2=文件,3=商品货样,4=其他)
public int type_no { get; set; }
//货品重量千克2KG以内三位小数多个品种请合计
public string weight { get; set; }
//内件数量(多个品种请合计)
public int num { get; set; }
//货品申报价值
public string price { get; set; }
//发件国家
public string from_country { get; set; }
//用户 自定义信息,例如商户订单号品编号,库位等
public string user_desc { get; set; }
//在电商平台的订单号或交易号
public string trande_no { get; set; }
//订单的金额, 单位为美元,包含物品价值和物流费用
public string trade_amount { get; set; }
public string ToXml()
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<orders>");
sb.Append("<api_key>"+this.api_key+"</api_key>");
sb.Append("<zx_type>"+this.zx_type+"</zx_type>");
sb.Append("<bid>"+this.bid+"</bid>");
sb.Append("<order>");
sb.Append("<guid>"+this.guid+"</guid>");
sb.Append("<otype>"+this.otype+"</otype>");
sb.Append("<from>"+this.from+"</from>");
sb.Append("<sender_addres>"+this.sender_addres+"</sender_addres>");
sb.Append("<sender_province>"+this.sender_province+"</sender_province>");
sb.Append("<sender_city>"+this.sender_city+"</sender_city>");
sb.Append("<sender_phone>"+this.sender_phone+"</sender_phone>");
sb.Append("<to>"+this.CustomName+"</to>");
sb.Append("<to_local>"+this.to_local+"</to_local>");
sb.Append("<recipient_addres>"+this.recipient_addres+"</recipient_addres>");
sb.Append("<recipient_addres_local>"+this.recipient_addres_local+"</recipient_addres_local>");
sb.Append("<recipient_country_short>"+this.recipient_country_short+"</recipient_country_short>");
sb.Append("<recipient_country_local>"+this.recipient_country_local+"</recipient_country_local>");
sb.Append("<recipient_province>"+this.recipient_province+"</recipient_province>");
sb.Append("<recipient_province_local>"+this.recipient_province_local+"</recipient_province_local>");
sb.Append("<recipient_city>"+this.recipient_city+"</recipient_city>");
sb.Append("<recipient_city_local>"+this.recipient_city_local+"</recipient_city_local>");
sb.Append("<recipient_postcode>"+this.recipient_postcode+"</recipient_postcode>");
sb.Append("<recipient_phone>"+this.recipient_phone+"</recipient_phone>");
sb.Append("<content>"+this.content+"</content>");
sb.Append("<type_no>"+this.type_no+"</type_no>");
sb.Append("<weight>"+this.weight+"</weight>");
sb.Append("<num>"+this.num+"</num>");
sb.Append("<price>"+this.price+"</price>");
sb.Append("<from_country>China</from_country>");
sb.Append("<user_desc>"+this.user_desc+"</user_desc>");
sb.Append("<trande_no>"+this.trande_no+"</trande_no>");
sb.Append("<trade_amount>"+this.trade_amount+"</trade_amount>");
sb.Append("</order>");
sb.Append("</orders>");
return sb.ToString();
}
public string PostData(string output, string Url)
{
string strReturn = "";
byte[] obtPostData = Encoding.UTF8.GetBytes(output.ToString());
HttpWebRequest orqRequest = (HttpWebRequest)WebRequest.Create(Url);
orqRequest.Timeout = 5000;
orqRequest.Method = "POST";
orqRequest.ContentType = "text/xml";
orqRequest.ContentLength = obtPostData.Length;
Stream stream;
using (stream = orqRequest.GetRequestStream())
{
stream.Write(obtPostData, 0, obtPostData.Length);
}
HttpWebResponse orsResponse = (HttpWebResponse)orqRequest.GetResponse();
using (Stream responseStream = orsResponse.GetResponseStream())
{
using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8))
{
strReturn = streamRead.ReadToEnd();
}
}
return strReturn;
}
public string SendGood(WishYouApi wya)
{
string outdata = wya.ToXml().ToString();
string str = PostData(outdata, "http://www.shwise.cn/api_order.asp");
XElement track = XElement.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str)));
try
{
if (track.Element("status").Value == "0")
{
string TrackingNumber = track.Element("barcode").Value;
str = TrackingNumber;
}
else
{
str = track.Element("error_message").Value+" 失败";
}
}
catch
{
str = track.Element("barcode").Attribute("error_message").Value + " 失败";
}
return str;
}
}
public class WishYouApi2
{
public string client_id { get; set; }
public string client_secret { get; set; }
public string code { get; set; }
public string grant_type { get; set; }
public string redirect_uri { get; set; }
public DateTime? GetTime { get; set; }
public string refresh_token { get; set; }
public Int32? ExpressId { get; set; }
//用户网站上生成的API密钥
public string access_token { get; set; }
//如果需要多个标签在一个PDF文件中请ᨀ交前一次返回的mark
public string mark { get; set; }
//用户提供的单次提交唯一标识
public int bid { get; set; }
//用户提供的运单唯一标识,将跟随运单号返回
public string guid { get; set; }
//是否挂号件(0=否1=是)
public string otype { get; set; }
//寄件人姓名(留空将使用地址簿默认信息)
public string from { get; set; }
//寄件人省名(留空将使用地址簿默认信息)
public string sender_addres { get; set; }
//寄件人城市名(留空将使用地址簿默认信息)
public string sender_province { get; set; }
//寄件人地址(留空将使用地址簿默认信息)
public string sender_city { get; set; }
//寄件人电话(留空将使用地址簿默认信息)
public string sender_phone { get; set; }
//收件人姓名
public string to { get; set; }
//收件人国家
public string recipient_country { get; set; }
//收件人国家简称
public string recipient_country_short { get; set; }
//收件人州名(英文)
public string recipient_province { get; set; }
//收件人城市名(英文)
public string recipient_city { get; set; }
//收件人地址(英文)
public string recipient_addres { get; set; }
//收件人邮编
public string recipient_postcode { get; set; }
//收件人电话
public string recipient_phone { get; set; }
public string to_local { get; set; }
//收件人国家(到达国语言,留空将使用英文信息)
public string recipient_country_local { get; set; }
public string recipient_province_local { get; set; }
//收件人城市名(到达国语言,留空将使用英文信息)
public string recipient_city_local { get; set; }
//收件人地址(到达国语言,留空将使用英文信息)
public string recipient_addres_local { get; set; }
//内件类型代码(1=礼品,2=文件,3=商品货样,4=其他)
public int type_no { get; set; }
//内件物品的详细名称(中文)
public string content_chinese { get; set; }
//内件物品的详细名称(英文)
public string content { get; set; }
public string ProductDetailedName{ get; set; }
public string ProductNameinChinese { get; set; }
//发件国家
public string from_country { get; set; }
//货品重量千克2KG以内三位小数多个品种请合计
public string weight { get; set; }
//内件数量(多个品种请合计)
public int num { get; set; }
//货品申报价值
public string single_price { get; set; }
//在电商平台的订单号或交易号
public string trande_no { get; set; }
//订单的金额, 单位为美元,包含物品价值和物流费用
public string trade_amount { get; set; }
//用户 自定义信息,例如商户订单号品编号,库位等
public string user_desc { get; set; }
//揽收信息联系人(必须中文)
public string receive_from { get; set; }
//揽收信息省名(必须中文)
public string receive_province { get; set; }
//揽收信息联系人(必须中文)
public string receive_city { get; set; }
//揽收信息联系人(必须中文)
public string receive_addres { get; set; }
//揽收信息电话
public string receive_phone { get; set; }
//分仓代码1=上海仓2=广州仓3=深圳仓4=义乌仓5=北京仓6=南京仓7=福州仓13=宁波
public string warehouse_code { get; set; }
//是否上门揽收0=自送1=上门揽收
public string doorpickup { get; set; }
/// <summary>
/// 是否带电, 0=不带电, 1=带电
/// </summary>
public string has_battery { get; set; }
public string ToXml()
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<orders>");
sb.Append("<access_token>" + this.access_token + "</access_token>");
sb.Append("<mark>" + this.mark + "</mark>");
sb.Append("<bid>" + this.bid + "</bid>");
sb.Append("<order>");
sb.Append("<guid>" + this.guid + "</guid>");
sb.Append("<otype>" + this.otype + "</otype>");
sb.Append("<from>" + this.from + "</from>");
sb.Append("<sender_addres>" + this.sender_addres + "</sender_addres>");
sb.Append("<sender_province>" + this.sender_province + "</sender_province>");
sb.Append("<sender_city>" + this.sender_city + "</sender_city>");
sb.Append("<sender_phone>" + this.sender_phone + "</sender_phone>");
sb.Append("<to>" + this.to + "</to>");
sb.Append("<to_local>" + this.to_local + "</to_local>");
sb.Append("<recipient_addres>" + this.recipient_addres + "</recipient_addres>");
sb.Append("<recipient_addres_local>" + this.recipient_addres_local + "</recipient_addres_local>");
sb.Append("<recipient_country_short>" + this.recipient_country_short + "</recipient_country_short>");
sb.Append("<recipient_country>" + this.recipient_country + "</recipient_country>");
sb.Append("<recipient_country_local>" + this.recipient_country_local + "</recipient_country_local>");
sb.Append("<recipient_province>" + this.recipient_province + "</recipient_province>");
sb.Append("<recipient_province_local>" + this.recipient_province_local + "</recipient_province_local>");
sb.Append("<recipient_city>" + this.recipient_city + "</recipient_city>");
sb.Append("<recipient_city_local>" + this.recipient_city_local + "</recipient_city_local>");
sb.Append("<recipient_postcode>" + this.recipient_postcode + "</recipient_postcode>");
sb.Append("<warehouse_code>" + warehouse_code + "</warehouse_code>");
sb.Append("<recipient_phone>" + this.recipient_phone + "</recipient_phone>");
sb.Append("<content>" + this.content + "</content>");
sb.Append("<content_chinese>" + this.content_chinese + "</content_chinese>");
sb.Append("<type_no>" + this.type_no + "</type_no>");
sb.Append("<weight>" + this.weight + "</weight>");
sb.Append("<num>" + this.num + "</num>");
sb.Append("<single_price>" + this.single_price + "</single_price>");
sb.Append("<from_country>China</from_country>");
sb.Append("<user_desc>" + this.user_desc + "</user_desc>");
if (this.receive_addres != null && this.receive_addres != "")
{
sb.Append("<receive_from>" + this.receive_from + "</receive_from>");
sb.Append("<receive_province>" + this.receive_province + "</receive_province>");
sb.Append("<receive_city>" + this.receive_city + "</receive_city>");
sb.Append("<receive_addres>" + this.receive_addres + "</receive_addres>");
sb.Append("<receive_phone>" + this.receive_phone + "</receive_phone>");
}
if (this.doorpickup!=null&&this.doorpickup != "")
sb.Append("<doorpickup>" + this.doorpickup + "</doorpickup>");
if (this.has_battery != null && this.has_battery != "")
sb.Append("<has_battery>" + this.has_battery + "</has_battery>");
sb.Append("<trande_no>" + this.trande_no + "</trande_no>");
sb.Append("<trade_amount>" + this.trade_amount + "</trade_amount>");
sb.Append("</order>");
sb.Append("</orders>");
return sb.ToString();
}
public string PostData(string output, string Url)
{
string strReturn = "";
byte[] obtPostData = Encoding.UTF8.GetBytes(output.ToString());
HttpWebRequest orqRequest = (HttpWebRequest)WebRequest.Create(Url);
orqRequest.Timeout = 5000;
orqRequest.Method = "POST";
orqRequest.ContentType = "text/xml";
orqRequest.ContentLength = obtPostData.Length;
Stream stream;
using (stream = orqRequest.GetRequestStream())
{
stream.Write(obtPostData, 0, obtPostData.Length);
}
HttpWebResponse orsResponse = (HttpWebResponse)orqRequest.GetResponse();
using (Stream responseStream = orsResponse.GetResponseStream())
{
using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8))
{
strReturn = streamRead.ReadToEnd();
}
}
return strReturn;
}
public string SendGood(WishYouApi2 wya)
{
if (wya.access_token == null || wya.access_token == "")
{
string ErrorMessage = "";
GetToken(out ErrorMessage);
wya.access_token = access_token;
if (ErrorMessage != "")
return ErrorMessage + "失败";
}
else if (GetTime == null || GetTime.Value < DateTime.Now.AddDays(-29))
{
string ErrorMessage = "";
GetReshToken(out ErrorMessage);
wya.access_token = access_token;
if (ErrorMessage != "")
return ErrorMessage + "失败";
}
if (Convert.ToDecimal(wya.single_price) > 10)
wya.single_price = "8";
string outdata = wya.ToXml().ToString();
string str = PostData(outdata, "https://wishpost.wish.com/api/v2/create_order");
XElement track = XElement.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str)));
try
{
if (track.Element("status").Value == "0")
{
string TrackingNumber = track.Element("barcode").Value;
str = TrackingNumber;
}
else
{
str = track.ToString() + "失败";
}
}
catch
{
str = track.ToString() + "失败";
}
return str;
}
public string GetLabel(List<WishPostTrack> list)
{
if (access_token == null || access_token == "")
{
string ErrorMessage = "";
GetToken(out ErrorMessage);
access_token = access_token;
if (ErrorMessage != "")
return ErrorMessage;
}
else if (GetTime == null || GetTime.Value < DateTime.Now.AddDays(-29))
{
string ErrorMessage = "";
GetReshToken(out ErrorMessage);
access_token = access_token;
if (ErrorMessage != "")
return ErrorMessage;
}
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<root>");
sb.Append("<access_token>" + this.access_token + "</access_token>");
sb.Append("<printlang>1</printlang>");
sb.Append("<printcode>2</printcode>");
sb.Append("<barcodes>");
foreach (var md in list)
{
sb.Append("<barcode>" + md.barcode+ "</barcode>");
}
sb.Append("</barcodes>");
sb.Append("</root>");
string outdata = sb.ToString();
string str = PostData(outdata, "https://wishpost.wish.com/api/v2/generate_label");
XElement track = XElement.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str)));
try
{
if (track.Element("status").Value == "0")
{
string PDF_URL = track.Element("PDF_URL").Value;
str = PDF_URL;
}
else
{
str = track.ToString() + "失败";
}
}
catch
{
str = track.ToString()+"失败";
}
return str;
}
#region
public string GetToken(out string ErrorMessage)
{
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
string grant_type = "authorization_code";
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<root>");
sb.Append("<client_id>" + client_id + "</client_id>");
sb.Append("<client_secret>" + client_secret + "</client_secret>");
sb.Append("<redirect_uri>" + redirect_uri + "</redirect_uri>");
sb.Append("<grant_type>" + grant_type + "</grant_type>");
sb.Append("<code>" + code + "</code>");
sb.Append("</root>");
ErrorMessage = "";
try
{
string str = PostData(sb.ToString(), "https://wishpost.wish.com/api/v2/oauth/access_token");
XElement track = XElement.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str)));
if (track.Element("status").Value == "0")
{
string access_token2 = track.Element("access_token").Value;
string refresh_token2 = track.Element("refresh_token").Value;
access_token = access_token2;
refresh_token = refresh_token2;
GetTime = DateTime.Now;
SaveWishCode();
return access_token;
}
else
{
ErrorMessage = track.Element("error_message").Value + " 失败";
}
}
catch(Exception ex)
{
ErrorMessage = ex.Message;
}
return ErrorMessage;
}
#endregion
#region 当长时令牌超过30天那么重新请求
public string GetReshToken(out string ErrorMessage)
{
string grant_type = "refresh_token";
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<root>");
sb.Append("<client_id>" + client_id + "</client_id>");
sb.Append("<client_secret>" + client_secret + "</client_secret>");
sb.Append("<refresh_token>" + refresh_token + "</refresh_token>");
sb.Append("<grant_type>" + grant_type + "</grant_type>");
sb.Append("</root>");
ErrorMessage = "";
try
{
string str = PostData(sb.ToString(), "https://wishpost.wish.com/api/v2/oauth/refresh_token");
XElement track = XElement.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str)));
if (track.Element("status").Value == "0")
{
string access_token2 = track.Element("access_token").Value;
string refresh_token2 = track.Element("refresh_token").Value;
access_token = access_token2;
refresh_token = refresh_token2;
GetTime = DateTime.Now;
SaveWishCode();
return access_token;
}
else
{
ErrorMessage = track.Element("error_message").Value + " 失败";
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
return ErrorMessage;
}
#endregion
#region 保存wish邮授权
public int SaveWishCode()
{
string tsql = @"
Update [JC_ExpressPost] set [UserCode]=@UserCode,[refresh_token]=@refresh_token,[GetTime]=@GetTime where redirect_uri='https://www.merchant.wish.com' or redirect_uri='https://www.baidu.com/' --ExpressID in (80,81,136,228,229,230,231,252)
";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.AddInParameter(cmd, "@UserCode", DbType.String, access_token);
db.AddInParameter(cmd, "@ExpressID", DbType.Int32, ExpressId);
db.AddInParameter(cmd, "@refresh_token", DbType.String, refresh_token);
db.AddInParameter(cmd, "@GetTime", DbType.DateTime, GetTime);
int a = Convert.ToInt32(db.ExecuteScalar(cmd));
return a;
}
#endregion
}
public class WishPostResult
{
public string user_id { get; set; }
public string token_type { get; set; }
public string access_token { get; set; }
public int expires_in { get; set; }
public int expiry_time { get; set; }
public string refresh_token { get; set; }
}
public class WishPostTrack
{
public string barcode { get; set; }
}
}