原創(chuàng)聲明:本文為作者原創(chuàng),未經(jīng)允許不得轉(zhuǎn)載,經(jīng)授權(quán)轉(zhuǎn)載需注明作者和出處
碼云地址: https://git.oschina.net/GreenLittleApple/wx_xiaochengxu01.git
先上圖
代碼解析
很簡(jiǎn)單, 一個(gè)js就能搞定。
1、 顯示頁(yè)面
<!--index.wxml-->
<view class="body">
// 輸入框
<input type="number" maxlength="11" auto-focus class="phoneInput" bindinput="listenPhoneInput" />
// 查詢按鈕(bindtap點(diǎn)擊事件, 相當(dāng)于onclick)
<button type="primary" bindtap="queryHome4Phone" class="btnQuery"> 查詢 </button>
// 結(jié)果顯示
<text class="result" wx:if="{{province != ''}}">
{{message}}
</text>
</view>
2、 控制代碼 js
// 監(jiān)聽(tīng)手機(jī)號(hào)輸入
listenPhoneInput: function(e) {
this.data.phoneNumber = e.detail.value
},
// 查詢手機(jī)號(hào)歸屬地
queryHome4Phone: function() {
var page = this
console.log("手機(jī)號(hào)是"+ this.data.phoneNumber)
// 網(wǎng)絡(luò)請(qǐng)求
wx.request({
url: 'http://apis.juhe.cn/mobile/get', // 接口地址
data: { // 參數(shù)
phone: this.data.phoneNumber,
key: '自己申請(qǐng)key(用的聚合數(shù)據(jù))'
},
header: {
'Content-Type': 'application/json',
},
success: function(res) { // 成功回調(diào)函數(shù)
var result = res.data
console.log(res.data)
if(result.error_code == 201101) {
page.setData({
message: '手機(jī)號(hào)不能為空'
})
} else if(result.error_code == 201102) {
page.setData({
message: '錯(cuò)誤的手機(jī)號(hào)碼'
})
} else if(result.error_code == 201103) {
page.setData({
message: '查詢無(wú)結(jié)果'
})
} else {
page.setData({ // 組合結(jié)果
message: result.result.province + " " + result.result.city + " " + result.result.company
})
}
}
})
},