手(shou)(shou)機歸(gui)(gui)屬地查(cha)詢(xun)本(ben)文(wen)從一個簡單的手(shou)(shou)機歸(gui)(gui)屬地查(cha)詢(xun)實(shi)現入(ru)手(shou)(shou),來幫助(zhu)你入(ru)門(men)小程序開發。源碼基(ji)本(ben)功能如(ru)下:查(cha)詢(xun)手(shou)(shou)機歸(gui)(gui)屬地根據歷史記錄查(cha)詢(xun)手(shou)(shou)機位(wei)數校驗界面預(yu)覽初始化創建(jian)空白(bai)項(xiang)目(mu)新(xin)建(jian)一個空白(bai)項(xiang)目(mu)AppID 可選擇(ze)無不選擇(ze) ...
本文(wen)從一個簡單的(de)手機(ji)歸屬地查詢實現入手,來幫助你入門
小程序
開發。
源碼
基本功(gong)能如下:
界面預覽

新(xin)建一個空白項目
首先,創建全局配置文件 app.json
/app.json
{
"pages":[
"pages/index/index"
]
}
在全局配置文件中,定義了一個頁面。保存之后,將會自動生成 index 頁(ye)面的基本(ben)目(mu)錄(lu)。
pages
└── index
├── index.js // 頁(ye)面業務(wu)邏輯(ji)
├── index.json // 頁(ye)面(mian)配置
├── index.wxml // 頁面視圖(tu)
└── index.wxss // 頁面(mian)樣式
現在,還缺少一個(ge)應(ying)用的入(ru)口文件,用來(lai)注(zhu)冊小程序
/app.js
App({
})
App() 方法用來(lai)注冊(ce)一(yi)個(ge)小程序。到這(zhe)一(yi)步,小程序初始(shi)化就(jiu)完成了。
首先(xian),我們(men)來為頁面添(tian)加頂部導航文字
/pages/index/index.json
{
"navigationBarTitleText": "手機(ji)歸(gui)屬地(di)查(cha)詢(xun)"
}
接下來是查詢模塊(kuai)
/pages/index/index.wxml
<view>
<text>請輸入查詢內容text>
<input type="number" bindinput="bindPhoneInput" value="{{ phoneNumber }}"/>
<button type="primary" bindtap="queryPhoneInfo" disabled="{{ disabled }}">查詢button>
view>
說明
bindinput 用于綁定鍵盤輸入事件 - 用戶輸入時,將會調用綁定bindPhoneInput 函數bindtap 用于綁定點擊事件 - 用戶點擊按鈕后,將會調用綁定的 queryPhoneInfo 函數disabled 的值;
接下里是具體的功能實現,首先,我們把手機歸屬地查詢的功能封裝到全局業務文件 app.js 中,方便不(bu)同頁面使用
/app.js
App({
/**
* 獲取手機歸屬地信息
*/
getPhoneInfo(phoneNum, callback) {
wx.request({
url:
'//www.iteblog.com/api/mobile.php?mobile=' + phoneNum,
header: {
'content-type': 'application/json'
},
success: function (res) {
callback(res.data);
}
})
}
})
說明
wx.request 發送請求;在頁面里面實現剛才定義的兩個事件
/pages/index/index.js
var app = getApp();
Page({
/**
* 頁面(mian)的初始數據
*/
data: {
phoneNumber: null, // 查(cha)找的手機號
phoneInfo: null, // 查詢結(jie)果
disabled: true // 默認不可查詢(xun)
},
/**
* 鍵盤輸入手機號事件處(chu)理(li)
*/
bindPhoneInput(event){
this.setData({
phoneNumber: event.detail.value, // 綁定用戶輸入的(de)手機號
phoneInfo: null // 清空(kong)過往查詢結果
})
this.setDisabled();
},
/**
* 驗證手機(ji)是否為 11 位
*/
setDisabled() {
this.setData({
disabled: (this.data.phoneNumber && this.data.phoneNumber.toString().length === 11) ? false : true
})
},
/**
* 用戶點擊查詢處理
*/
queryPhoneInfo() {
app.getPhoneInfo(this.data.phoneNumber, data => this.setData({
phoneInfo: data
}));
}
})
說明
this.setData() 方法用于設置 data 的屬性,如果直接使用 this.data.phoneInfo 無法改變頁面狀態;app 的方法,需要先使用 getApp 進行實例化,然后通過實例來訪問方法;接下來在(zai)視圖里面顯示查(cha)詢結果
/pages/index/index.wxml
<view>
<view wx:if="{{ phoneInfo }}">
<text>查詢結果為:text>
<text wx:if="{{phoneInfo.ret === 0}}">
{{phoneInfo.operator}}{{phoneInfo.province}}{{phoneInfo.city}}
text>
<text wx:else> {{phoneInfo.msg}} text>
view>
view>
說明 - 使用 wx:if 與 wx:else 可以方便(bian)的根據(ju)查詢結果來切(qie)換視圖
最后是最近功(gong)(gong)能記錄(lu)的(de)功(gong)(gong)能實現,首先是視圖(tu)
<view>
<text>最近搜索text>
<view>
<view wx:for="{{ historyList }}" bindtap="selectHistory" data-number="{{item}}">
{{item}}
view>
view>
view>
說明:
historyList 數組selectHistory 事件data-number 中,selectHistory 就可以獲取對應的手機號了業務邏輯
// pages/index/index.js
var app = getApp();
Page({
/**
* 頁面的初始數據
*/
data: {
phoneNumber: null, // 查找的手機號
phoneInfo: null, // 查詢結果
historyList: [], // 查詢歷史
disabled: true
},
/**
* 鍵盤(pan)輸(shu)入手機號(hao)事(shi)件處理
*/
bindPhoneInput(event){
this.setData({
phoneNumber: event.detail.value, // 綁定用戶輸入的手機號
phoneInfo: null // 清空過往(wang)查詢結果
})
this.setDisabled();
},
/**
* 驗證(zheng)手機是否(fou)為 11 位(wei)
*/
setDisabled() {
this.setData({
disabled: (this.data.phoneNumber && this.data.phoneNumber.toString().length === 11) ? false : true
})
},
/**
* 用戶點擊查詢(xun)處理
*/
queryPhoneInfo() {
app.getPhoneInfo(this.data.phoneNumber, data => this.setData({
phoneInfo: data
}));
this.addQueryHistory(this.data.phoneNumber); // 添加(jia)搜索(suo)記(ji)錄
},
/**
* 將搜索記(ji)錄添加到緩(huan)存
*/
addQueryHistory(phoneNumber) {
var historyList = wx.getStorageSync('historyList') || [];
if (historyList.indexOf(phoneNumber) === -1) {
historyList.unshift(phoneNumber);
wx.setStorageSync('historyList', historyList);
}
this.setData({
historyList: historyList
})
},
/**
* 頁面加載后,從(cong)緩存中讀取最近搜索列(lie)表
*/
onLoad: function () {
this.setData({
historyList: wx.getStorageSync('historyList') || []
})
},
/**
* 用戶(hu)點擊記錄(lu)之后,將(jiang)其(qi)添加(jia)到輸(shu)入框中
*/
selectHistory(event) {
this.setData({
phoneNumber: event.currentTarget.dataset.number,
disabled: false
})
}
})
最后,只需要美化(hua)下界面(mian)即(ji)可。
視圖
<view class="querySection">
<text class="help-text">請輸入查詢內容text>
<input class="queryInput" type="number" bindinput="bindPhoneInput" value="{{ phoneNumber }}"/>
<button class="queryBtn" type="primary" bindtap="queryPhoneInfo" disabled="{{ disabled }}">查詢button>
view>
<view>
<view wx:if="{{ phoneInfo }}">
<text class="help-text">查詢結果為:text>
<text wx:if="{{phoneInfo.ret === 0}}">
{{phoneInfo.operator}}{{phoneInfo.province}}{{phoneInfo.city}}
text>
<text wx:else> {{phoneInfo.msg}} text>
view>
view>
<view>
<text class="help-text">最近搜索text>
<view class="items">
<view class="item" wx:for="{{ historyList }}" bindtap="selectHistory" data-number="{{item}}">
{{item}}
view>
view>
view>
樣式
/* pages/index/index.wxss */
page {
background-color: #EFEFF4;
font-family: -apple-system-font,Helvetica Neue,Helvetica,sans-serif;
}
.querySection {
display: flex;
flex-direction: column;
margin-top: 35px;
}
.help-text {
font-size:14pt;
color:#888888;
margin-left:15px;
}
.queryInput {
width:100%;
background-color: #FFFFFF;
height: 75px;
margin:10px auto;
}
.queryBtn {
margin:15px;
}
.items {
display: flex;
flex-wrap: wrap;
}
.item {
margin:20px;
background-color: #D2D2D2;
padding: 13px;
color:#FFFFFF;
border-radius:20px;
}
.queryRst {
margin:20px;
}