微信小程序—查询快递
作者:秋名
撰写时间:2019 年 8 月21日
使用工具:微信开发者工具+聚合数据常用快递查询API
顺丰快递单号:https://www.kuaidi100.com/all/sf.shtml()
微信开放文档:https://developers.weixin.qq.com/miniprogram/dev/api/network/request/ wx.request.html
<!--index.wxml代码-->
<view class="container">
<input placeholder="请输入快递单号" bindinput="input"/>
<button type="primary" bindtap="btnClick" >查询</button>// bindtap绑定按钮
</view>
<!-- scroll-view可滚动视图区域。-->
<scroll-view scroll-y="true" style="height: 200px;" >
<!-- 循环物流信息获取到值,显示到页面 -->
<view wx:for="{{expressInfo.result.list}}">
<!-- item:每一行数据。显示每一行的物流信息和时间 -->
{{item.datetime}} || {{item.remark}}
</view>
</scroll-view>
<!--index.wxss代码-->
input{
border:1px solid black;
width:90%;
margin: 5%;
padding: 5px;
}
<!--index.js代码-->
//获取应用实例
const app = getApp()
Page({
data: {
expressNu: null,//定义值,保存输入框输入的数据
expressInfo: null,//将物流信息绑定到页面
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
getUserInfo: function(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
// 获取输入框的文本值
//e=event(数据)
input: function (e) {
console.log(e)
this.setData({ expressNu: e.detail.value })
},
// 点击按钮
btnClick: function () {
var thispage = this; //声明变量
app.getExpressInfo(this.data.expressNu, function (data) {//获取文本的值
console.log(data)
thispage.setData({expressInfo: data})//给页面赋值
});
}
})
打开微信开放文档,然后点击网络,复制连接API地址的代码
<!--app.js代码-->
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
getExpressInfo:function(nu,cb){
const requestTask = wx.request({
url: 'http://v.juhe.cn/exp/index?key=&com=sf&no='+nu,
// &com=sf顺丰、sto申通、yt圆通、yd韵达、tt天天(需要查询“聚合数据常用快递公司编号对照表”)
//&no='+nu(需要查询的快递单号)
//对应的Key值在个人中心—我的接口AppKey后面那一大串
data: {
x: '',
y: ''
},
header: {
'Key': '092838385d94284c30499eadc5c9bdbf'
},
success(res) {
//console.log(res.data)
cb(res.data)
}
})
},
globalData: {
userInfo: null
}
})
聚合数据常用快递公司编号对照表:
聚合数据Key值:
完成效果图:
注:如果网络地址报错可以点击项目—项目设置—把不校验合法域名的勾打上就可以了。