因为疫情的影响,每天只能待在家里。鉴于一个多月没有写博客,今天就写一篇吧!
之前做过一个商品购物小程序的后台,里面有一个查询物流信息的功能,当时也是踩了几个坑。今天再写一个Dome例子,就把代码和步骤分享给大家。水平有限,但是希望能够帮到各位同学。
首先,毋庸置疑,肯定要借助第三方的api。这里的话选取的是快递100平台,登录快递100官网:
地址:https://sso.kuaidi100.com/sso/authorize.do
登陆后选择接口(API)下的接口申请
我当时选择的是企业版
然后选择立即开通
把信息填好后提交审核,好像要等待一会儿,之后会有消息提醒
之后会给我们分配key,要把key带到接口中做请求(这个下面会说到)
点击我的信息,企业信息,查看我们的key
如果开通的是企业版的话快递100会免费送我们100次的查询单量
到这里准备工作就做完了
接下来就是code部分
新建一个.net core mvc的项目(这个我就不贴图片做演示了 emm......)
引用一个NuGet程序包:HttpSender 用于后台发送请求
新建一个控制器及视图
Controller中的代码:
////// 查询物流,返回Json /// /// 快递单号 /// public async Task > queryInformation(string Number = "") { string res = string.Empty; string msg = string.Empty; bool success; var requestList = new testDate(); try { if (string.IsNullOrEmpty(Number)) { return new MessageModel () { msg = "单号为空", success = false }; } //将快递单号作为参数请求是那个快递 var Response = Sender.Get($"http://www.kuaidi100.com/autonumber/autoComNum?text={Number}"); Express express = JsonConvert.DeserializeObject (Response);
string code = string.Empty;
if (express.auto.Any())
code = express.auto[0].comCode;
else
goto action;
/* //参数 String param = "{\"com\":\"\",\"num\":\"\",\"from\":\"\",\"to\":\"\"}"; //反序列化 Param p = JsonConvert.DeserializeObject(param);*/ //string param = string.Empty; Param p = new Param(); p.com = code; p.num = Number; //序列化 var param = JsonConvert.SerializeObject(p);
#region 你的customer与key值 String customer = ""; String key = "";
#endregion MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
//将参数转为字节数组 byte[] InBytes = Encoding.GetEncoding("UTF-8").GetBytes(param + key + customer);
#region 加密 byte[] OutBytes = md5.ComputeHash(InBytes); string OutString = ""; for (int i = 0; i < OutBytes.Length; i++) { OutString += OutBytes[i].ToString("x2"); } String sign = OutString.ToUpper();
#endregion //带入参数,查询物流信息 res = Sender.Post("http://poll.kuaidi100.com/poll/query.do", $"customer={customer}&sign={sign}¶m={param}");
/*这里会返回多个参数字段,我只选取了data部分
{
"message":"",
"nu":"",
"ischeck":"",
"condition":"",
"com":"",
"status":"",
"state":"",
"data":[{}]
}
*/ msg = "物流信息已获取"; success = true; var list = JsonConvert.DeserializeObject(res); if (list.data.Any()) { var rList = new List (); list.data.ForEach((m) => { rList.Add(new HttpData { createDate = m.time, jobName = m.ftime, jobOther = m.context }); }); requestList = new testDate() { data = rList }; } } catch (Exception) { msg = "单号有误"; success = false; } action: return new MessageModel { msg = msg, success = success, response = requestList }; }
Model中我新建了几个类:
public class Express { public string comCode { get; set; } public string num { get; set; } public Listauto { get; set; } } public class MessageModel { public bool success { get; set; } = false; public string msg { get; set; } = "请求异常"; public T response { get; set; }
} ////// 物流信息参数字段 /// public class Param { public string com { get; set; } public string num { get; set; } public string from { get; set; } public string to { get; set; } } public class data { public string time { get; set; } public string ftime { get; set; } public string context { get; set; } } public class single { public string comCode { get; set; } public string id { get; set; } public string noCount { get; set; } public string noPre { get; set; } public string startTime { get; set; } } public class testDate { public List data { get; set; } } public class HttpData { public string createDate { get; set; } public string jobName { get; set; } public string jobOther { get; set; } }
前端的展示我在网上找了一个插件
@{ Layout = null; } "viewport" content="width=device-width" />Index "stylesheet" type="text/css" href="css\flowplugin.css" />"text-align:center;margin-top:100px;margin-bottom:-150px"> "font-size:20px;">快递单号:"border:1px solid black;width:300px;height:35px" id="Num" /> "position:relative;top:2px;">"height:270px;">class="flowtest">
前端效果展示:
代码我已上传到我的github,有需要的同学可以下载下来看一下
https://github.com/Davenever/logistics.git
最后给大家拜个晚年,新年快乐,大吉大利!