标签:
先定义SortList数组用于存放快递公司的简称和全称
public SortedList Ht = new SortedList() { {"zhongyouwuliu", "中邮物流"}, {"zhongtong", "中通速递"}, {"zhongtiewuliu", "中铁快运"}, {"zhaijisong", "宅急送"}, {"yuntongkuaidi", "运通快递"}, {"yuananda", "源安达"}, {"yunda", "韵达快运"}, {"yuefengwuliu", "越丰物流"}, {"yuanzhijiecheng", "元智捷诚快递"}, {"yuanweifeng", "源伟丰快递"}, {"yuantong", "圆通速递"}, {"yuanchengwuliu", "远成物流"}, {"youshuwuliu", "优速物流"}, {"yibangwuliu", "一邦速递"}, {"yafengsudi", "亚风速递"}, //........ }
原始模型:
public class Detail { public DateTime time; public string context; public DateTime ftime; public Detail() { } } public class Info { public string message; public string nu; public int ischeck; public string com; public string status; public string condition; public int state; public Detail[] data; public Info() { } }
改良后模型:
public class Kuaididetail { public string Time; public string Context; public Kuaididetail() { } public Kuaididetail(string time,string context) { this.Time = time; this.Context = context; } } public class KuaidiInfo { public string No; //快递单号 public string Name; //快递公司名称 public string ShortName; //快递公司简称 public string Message; //状态信息,ok为有快递信息 public string HtmlStatus; //html状态码 详见:http://yige.org/tags/ref_httpmessages.php public int State; /*快递单当前的状态 : 0:在途,即货物处于运输过程中; 1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息; 2:疑难,货物寄送过程出了问题; 3:签收,收件人已签收; 4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收; 5:派件,即快递正在进行同城派件; 6:退回,货物正处于退回发件人的途中;*/ public string Days; //快递在途时间 public List<Kuaididetail> Detail; //快递详情 public KuaidiInfo() { } }
1.根据快递单号模糊搜索出快递公司名称
Demo图:
输入数字动态加载匹配度最高的快递公司名称:
$("#search_input").keyup(function () { var value = $("#search_input").val(); $("#search_list").html(""); $.ajax({ type: "GET", url: "/KuaiDi/GetData", data: {no:value}, dataType: ‘json‘, timeout: 5000, cache: false, success: function (result) { var html = ""; $.each(result, function (k, v) { html += "<li>"+v+"</li>"; }); $("#search_list").html(html); $("#search_list li").click(function (e) { e.stopPropagation(); var _text = $(this).text(); $("#search_input").val(_text); $("#search_list").hide(); }); }, error: function () { } }); });
json请求代码:
public JsonResult GetData(string no) { List<string> list = new List<string>(); try { string apiurl = "http://www.kuaidi100.com/autonumber/auto?num="+no; WebClient webClient = new WebClient(); byte[] result = webClient.DownloadData(apiurl); string tip = Encoding.GetEncoding("utf-8").GetString(result); JavaScriptSerializer jss = new JavaScriptSerializer(); var info = jss.Deserialize<List<KuaiduTip>>(tip); if (info.Count > 0) { foreach (var item in info) { string temp =no + " " + (Ht.ContainsKey(item.comCode) ? Ht[item.comCode].ToString() : item.comCode); list.Add(temp); } } } catch (Exception) { } return Json(list, JsonRequestBehavior.AllowGet); }
2.从快递100那里拿取数据
/// <summary> /// 从快递100那里拿取数据 /// </summary> /// <param name="name">快递公司简称</param> /// <param name="no">快递单号</param> /// <returns></returns> public Info GetInfo(string name, string no) { string apiurl = "http://www.kuaidi100.com/query?id=1&type=" + name + "&postid=" + no + "&valicode=11&temp=12"; WebClient webClient = new WebClient(); byte[] result = webClient.DownloadData(apiurl); string wuliu = Encoding.GetEncoding("utf-8").GetString(result); JavaScriptSerializer jss = new JavaScriptSerializer(); Info info = jss.Deserialize<Info>(wuliu); return info; }
3.判断快递是否已被签收
/// <summary> /// 判断快递是否已被签收 /// </summary> /// <param name="name">快递公司简称</param> /// <param name="no">快递单号</param> /// <returns></returns> public bool IsSigned(string name, string no) { Info info = GetInfo(name, no); if (info.message == "ok") { Detail[] details = info.data; if (details[0].context.Contains("签收") || info.state == 3) { return true; } } return false; }
4.快递接口调用(可实现json,html,xml及text等格式及单行多行,降序升序输出)
//快递接口调用 /// <summary> /// /// </summary> /// <param name="name">快递公司简称</param> /// <param name="no">快递单号</param> /// <param name="type">0表示json,1表示html,2表示xml,3表示文本</param> /// <param name="muti">true表示显示多行,false表示只显示一行</param> /// <param name="isDesc">true表示降序,false表示升序</param> /// <returns></returns> public ActionResult Index(string name, string no,int type,bool muti,bool isDesc) { try { Info info = GetInfo(name,no); string expressName = Ht[name].ToString(); KuaidiInfo kuaidiInfo = new KuaidiInfo(); kuaidiInfo.No = no; kuaidiInfo.Name = expressName; kuaidiInfo.ShortName = name; kuaidiInfo.State = info.state; kuaidiInfo.HtmlStatus = info.status; if (info.message == "ok") { Detail[] details = info.data; DateTime starTime = details[0].time; DateTime endTime = details[details.Length - 1].time; TimeSpan ts = starTime.Subtract(endTime); double ss = ts.TotalHours; kuaidiInfo.Days = ((int)ss / 24).ToString() + "天" + ((int)ss % 24).ToString() + "小时"; kuaidiInfo.Message = "OK"; List<Detail> dd = details.ToList(); if (muti) { if (isDesc) { List<Kuaididetail> kdList = new List<Kuaididetail>(); foreach (Detail t in dd) { Kuaididetail kd = new Kuaididetail(t.time.ToString(), t.context); kdList.Add(kd); } kuaidiInfo.Detail = kdList; } else { dd = dd.OrderBy(p => p.time).ToList(); List<Kuaididetail> kdList = new List<Kuaididetail>(); foreach (Detail t in dd) { Kuaididetail kd = new Kuaididetail(t.time.ToString(), t.context); kdList.Add(kd); } kuaidiInfo.Detail = kdList; } } else { if (isDesc) { Kuaididetail kd = new Kuaididetail(dd[0].time.ToString(), dd[0].context); List<Kuaididetail> kdList = new List<Kuaididetail>(); kdList.Add(kd); kuaidiInfo.Detail = kdList; } else { dd = dd.OrderBy(p => p.time).ToList(); Kuaididetail kd = new Kuaididetail(dd[0].time.ToString(), dd[0].context); List<Kuaididetail> kdList = new List<Kuaididetail>(); kdList.Add(kd); kuaidiInfo.Detail = kdList; } } } else { kuaidiInfo.Message = info.message.Split(‘:‘)[1]; } ///////////////////////////////////////////////////////////////////////////// switch (type) { case 0: { JavaScriptSerializer jss = new JavaScriptSerializer(); string json = jss.Serialize(kuaidiInfo); //序列化成JSON return Content(json); } case 1: return View(kuaidiInfo); case 2: CreateXmlFromBy(kuaidiInfo); return null; case 3: string result = String.Empty; foreach (var item in kuaidiInfo.Detail) { result += item.Time + "\t" + item.Context + "\n"; } return Content(result); default: return null; } } catch { return null; } } }
5.将数据导出到xml并显示在页面
public void CreateXmlFromBy(KuaidiInfo info) { var m =info.Detail.Select(p => new XElement("Data", new XElement("Time", p.Time), new XElement("Context", p.Context))); XElement x = new XElement("xml",new XElement("No",info.No),new XElement("Name",info.Name),new XElement("Message",info.Message),new XElement("HtmlStatus",info.HtmlStatus),new XElement("State",info.State),new XElement("Days",info.Days),m); Response.Charset = "utf-8";//格式 Response.ContentType = "text/xml";//类型 Response.Write(x); }
6.输出Demo演示
json格式:
{"No":"1900916254100","Name":"韵达快运","ShortName":"yunda","Message":"OK","HtmlStatus":"200","State":3,"Days":"2天19小时","Detail":[{"Time":"2014/10/27 16:33:31","Context":"到达:山东烟台莱山区公司大学分部 由 图片 签收"},{"Time":"2014/10/27 16:27:24","Context":"到达:山东烟台莱山区公司大学分部 指定:王师傅(15165747011) 派送"},{"Time":"2014/10/27 10:37:09","Context":"到达:山东烟台莱山区公司大学分部 上级站点:山东烟台莱山区公司 发往:"},{"Time":"2014/10/27 8:42:04","Context":"到达:山东烟台分拨中心 发往:山东烟台莱山区公司"},{"Time":"2014/10/27 8:40:26","Context":"到达:山东烟台分拨中心 上级站点:山东济南分拨中心"},{"Time":"2014/10/26 18:30:31","Context":"到达:山东济南分拨中心 发往:山东烟台分拨中心"},{"Time":"2014/10/26 18:28:53","Context":"到达:山东济南分拨中心 上级站点:江西南昌分拨中心"},{"Time":"2014/10/25 1:16:43","Context":"到达:江西南昌分拨中心 发往:山东济南分拨中心"},{"Time":"2014/10/25 1:13:51","Context":"到达:江西南昌分拨中心"},{"Time":"2014/10/24 20:38:26","Context":"到达:江西南昌青山湖区罗家集公司 已收件"}]}
html格式:
XML格式:
文本格式:
2014/10/27 16:33:31 到达:山东烟台莱山区公司大学分部 由 图片 签收 2014/10/27 16:27:24 到达:山东烟台莱山区公司大学分部 指定:王师傅(15165747011) 派送 2014/10/27 10:37:09 到达:山东烟台莱山区公司大学分部 上级站点:山东烟台莱山区公司 发往: 2014/10/27 8:42:04 到达:山东烟台分拨中心 发往:山东烟台莱山区公司 2014/10/27 8:40:26 到达:山东烟台分拨中心 上级站点:山东济南分拨中心 2014/10/26 18:30:31 到达:山东济南分拨中心 发往:山东烟台分拨中心 2014/10/26 18:28:53 到达:山东济南分拨中心 上级站点:江西南昌分拨中心 2014/10/25 1:16:43 到达:江西南昌分拨中心 发往:山东济南分拨中心 2014/10/25 1:13:51 到达:江西南昌分拨中心 2014/10/24 20:38:26 到达:江西南昌青山湖区罗家集公司 已收件
标签:
原文地址:http://www.cnblogs.com/xuhang/p/4242452.html