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("");
sb.Append("");
sb.Append(""+this.api_key+"");
sb.Append(""+this.zx_type+"");
sb.Append(""+this.bid+"");
sb.Append("");
sb.Append(""+this.guid+"");
sb.Append(""+this.otype+"");
sb.Append(""+this.from+"");
sb.Append(""+this.sender_addres+"");
sb.Append(""+this.sender_province+"");
sb.Append(""+this.sender_city+"");
sb.Append(""+this.sender_phone+"");
sb.Append(""+this.CustomName+"");
sb.Append(""+this.to_local+"");
sb.Append(""+this.recipient_addres+"");
sb.Append(""+this.recipient_addres_local+"");
sb.Append(""+this.recipient_country_short+"");
sb.Append(""+this.recipient_country_local+"");
sb.Append(""+this.recipient_province+"");
sb.Append(""+this.recipient_province_local+"");
sb.Append(""+this.recipient_city+"");
sb.Append(""+this.recipient_city_local+"");
sb.Append(""+this.recipient_postcode+"");
sb.Append(""+this.recipient_phone+"");
sb.Append(""+this.content+"");
sb.Append(""+this.type_no+"");
sb.Append(""+this.weight+"");
sb.Append(""+this.num+"");
sb.Append(""+this.price+"");
sb.Append("China");
sb.Append(""+this.user_desc+"");
sb.Append(""+this.trande_no+"");
sb.Append(""+this.trade_amount+"");
sb.Append("");
sb.Append("");
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; }
///
/// 是否带电, 0=不带电, 1=带电
///
public string has_battery { get; set; }
public string ToXml()
{
StringBuilder sb = new StringBuilder();
sb.Append("");
sb.Append("");
sb.Append("" + this.access_token + "");
sb.Append("" + this.mark + "");
sb.Append("" + this.bid + "");
sb.Append("");
sb.Append("" + this.guid + "");
sb.Append("" + this.otype + "");
sb.Append("" + this.from + "");
sb.Append("" + this.sender_addres + "");
sb.Append("" + this.sender_province + "");
sb.Append("" + this.sender_city + "");
sb.Append("" + this.sender_phone + "");
sb.Append("" + this.to + "");
sb.Append("" + this.to_local + "");
sb.Append("" + this.recipient_addres + "");
sb.Append("" + this.recipient_addres_local + "");
sb.Append("" + this.recipient_country_short + "");
sb.Append("" + this.recipient_country + "");
sb.Append("" + this.recipient_country_local + "");
sb.Append("" + this.recipient_province + "");
sb.Append("" + this.recipient_province_local + "");
sb.Append("" + this.recipient_city + "");
sb.Append("" + this.recipient_city_local + "");
sb.Append("" + this.recipient_postcode + "");
sb.Append("" + warehouse_code + "");
sb.Append("" + this.recipient_phone + "");
sb.Append("" + this.content + "");
sb.Append("" + this.content_chinese + "");
sb.Append("" + this.type_no + "");
sb.Append("" + this.weight + "");
sb.Append("" + this.num + "");
sb.Append("" + this.single_price + "");
sb.Append("China");
sb.Append("" + this.user_desc + "");
if (this.receive_addres != null && this.receive_addres != "")
{
sb.Append("" + this.receive_from + "");
sb.Append("" + this.receive_province + "");
sb.Append("" + this.receive_city + "");
sb.Append("" + this.receive_addres + "");
sb.Append("" + this.receive_phone + "");
}
if (this.doorpickup!=null&&this.doorpickup != "")
sb.Append("" + this.doorpickup + "");
if (this.has_battery != null && this.has_battery != "")
sb.Append("" + this.has_battery + "");
sb.Append("" + this.trande_no + "");
sb.Append("" + this.trade_amount + "");
sb.Append("");
sb.Append("");
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 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("");
sb.Append("");
sb.Append("" + this.access_token + "");
sb.Append("1");
sb.Append("2");
sb.Append("");
foreach (var md in list)
{
sb.Append("" + md.barcode+ "");
}
sb.Append("");
sb.Append("");
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("");
sb.Append("");
sb.Append("" + client_id + "");
sb.Append("" + client_secret + "");
sb.Append("" + redirect_uri + "");
sb.Append("" + grant_type + "");
sb.Append("" + code + "
");
sb.Append("");
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("");
sb.Append("");
sb.Append("" + client_id + "");
sb.Append("" + client_secret + "");
sb.Append("" + refresh_token + "");
sb.Append("" + grant_type + "");
sb.Append("");
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; }
}
}