手機歸屬地查詢本文從一個簡單的手機歸屬地查詢實現入手,來幫助你入門小程序開發。源碼基本功能如下:查詢手機歸屬地根據歷史記錄查詢手機位數校驗界面預覽初始化創建空白項目新建一個空白項目AppID 可選擇無不選擇 ...
本文從一個簡單的手機歸屬地查詢實現入手,來幫助你入門
小程序
開發。
源碼
基本功能如下:
界面預覽
新建一個空白項目
首先,創建全局配置文件 app.json
/app.json
{
"pages":[
"pages/index/index"
]
}
在全局配置文件中,定義了一個頁面。保存之后,將會自動生成 index
頁面的基本目錄。
pages
└── index
├── index.js // 頁面業務邏輯
├── index.json // 頁面配置
├── index.wxml // 頁面視圖
└── index.wxss // 頁面樣式
現在,還缺少一個應用的入口文件,用來注冊小程序
/app.js
App({
})
App()
方法用來注冊一個小程序。到這一步,小程序初始化就完成了。
首先,我們來為頁面添加頂部導航文字
/pages/index/index.json
{
"navigationBarTitleText": "手機歸屬地查詢"
}
接下來是查詢模塊
/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
中,方便不同頁面使用
/app.js
App({
/**
* 獲取手機歸屬地信息
*/
getPhoneInfo(phoneNum, callback) {
wx.request({
url:
'https://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({
/**
* 頁面的初始數據
*/
data: {
phoneNumber: null, // 查找的手機號
phoneInfo: null, // 查詢結果
disabled: true // 默認不可查詢
},
/**
* 鍵盤輸入手機號事件處理
*/
bindPhoneInput(event){
this.setData({
phoneNumber: event.detail.value, // 綁定用戶輸入的手機號
phoneInfo: null // 清空過往查詢結果
})
this.setDisabled();
},
/**
* 驗證手機是否為 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
進行實例化,然后通過實例來訪問方法;接下來在視圖里面顯示查詢結果
/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
可以方便的根據查詢結果來切換視圖
最后是最近功能記錄的功能實現,首先是視圖
<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
},
/**
* 鍵盤輸入手機號事件處理
*/
bindPhoneInput(event){
this.setData({
phoneNumber: event.detail.value, // 綁定用戶輸入的手機號
phoneInfo: null // 清空過往查詢結果
})
this.setDisabled();
},
/**
* 驗證手機是否為 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.addQueryHistory(this.data.phoneNumber); // 添加搜索記錄
},
/**
* 將搜索記錄添加到緩存
*/
addQueryHistory(phoneNumber) {
var historyList = wx.getStorageSync('historyList') || [];
if (historyList.indexOf(phoneNumber) === -1) {
historyList.unshift(phoneNumber);
wx.setStorageSync('historyList', historyList);
}
this.setData({
historyList: historyList
})
},
/**
* 頁面加載后,從緩存中讀取最近搜索列表
*/
onLoad: function () {
this.setData({
historyList: wx.getStorageSync('historyList') || []
})
},
/**
* 用戶點擊記錄之后,將其添加到輸入框中
*/
selectHistory(event) {
this.setData({
phoneNumber: event.currentTarget.dataset.number,
disabled: false
})
}
})
最后,只需要美化下界面即可。
視圖
<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;
}