小程序模板網(wǎng)

小程序?qū)崙?zhàn)入門 - 手機(jī)歸屬地查詢

發(fā)布時(shí)間:2017-12-19 10:30 所屬欄目:小程序開發(fā)教程

手機(jī)歸屬地查詢本文從一個(gè)簡(jiǎn)單的手機(jī)歸屬地查詢實(shí)現(xiàn)入手,來幫助你入門小程序開發(fā)。源碼基本功能如下:查詢手機(jī)歸屬地根據(jù)歷史記錄查詢手機(jī)位數(shù)校驗(yàn)界面預(yù)覽初始化創(chuàng)建空白項(xiàng)目新建一個(gè)空白項(xiàng)目AppID 可選擇無不選擇 ...

 
 
 

手機(jī)歸屬地查詢

本文從一個(gè)簡(jiǎn)單的手機(jī)歸屬地查詢實(shí)現(xiàn)入手,來幫助你入門

小程序

開發(fā)。

 

 

源碼

 

基本功能如下:

  • 查詢手機(jī)歸屬地
  • 根據(jù)歷史記錄查詢
  • 手機(jī)位數(shù)校驗(yàn)

界面預(yù)覽

 

 

 

 

初始化

創(chuàng)建空白項(xiàng)目

新建一個(gè)空白項(xiàng)目

  • AppID 可選擇無
  • 不選擇中創(chuàng)建 quick start 項(xiàng)目,而是創(chuàng)建空白項(xiàng)目,加深對(duì)小程序結(jié)構(gòu)的理解。

基本配置

首先,創(chuàng)建全局配置文件 app.json

/app.json
{
  "pages":[
    "pages/index/index"
  ]
}

在全局配置文件中,定義了一個(gè)頁面。保存之后,將會(huì)自動(dòng)生成 index 頁面的基本目錄。

pages
└── index
    ├── index.js    // 頁面業(yè)務(wù)邏輯
    ├── index.json  // 頁面配置
    ├── index.wxml  // 頁面視圖
    └── index.wxss  // 頁面樣式

現(xiàn)在,還缺少一個(gè)應(yīng)用的入口文件,用來注冊(cè)小程序

/app.js
App({
})

App() 方法用來注冊(cè)一個(gè)小程序。到這一步,小程序初始化就完成了。

功能實(shí)現(xiàn)

頁面配置

首先,我們來為頁面添加頂部導(dǎo)航文字

/pages/index/index.json
{
  "navigationBarTitleText": "手機(jī)歸屬地查詢"
}

查詢模塊

接下來是查詢模塊

/pages/index/index.wxml

<view>
  <text>請(qǐng)輸入查詢內(nèi)容text>
  <input type="number" bindinput="bindPhoneInput" value="{{ phoneNumber }}"/>
  <button type="primary" bindtap="queryPhoneInfo" disabled="{{ disabled }}">查詢button>
view>

說明

  • bindinput 用于綁定鍵盤輸入事件 - 用戶輸入時(shí),將會(huì)調(diào)用綁定bindPhoneInput 函數(shù)
  • bindtap 用于綁定點(diǎn)擊事件 - 用戶點(diǎn)擊按鈕后,將會(huì)調(diào)用綁定的 queryPhoneInfo 函數(shù)
  • 按鈕是否可點(diǎn)擊取決于 disabled 的值;

接下里是具體的功能實(shí)現(xiàn),首先,我們把手機(jī)歸屬地查詢的功能封裝到全局業(yè)務(wù)文件 app.js 中,方便不同頁面使用

/app.js
App({
  /**
   * 獲取手機(jī)歸屬地信息
   */
  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 發(fā)送請(qǐng)求;
  • 該函數(shù)接受兩個(gè)參數(shù),一個(gè)是手機(jī)號(hào),另外一個(gè)則是自定義函數(shù),用來處理查詢的結(jié)果。

在頁面里面實(shí)現(xiàn)剛才定義的兩個(gè)事件

/pages/index/index.js
var app = getApp();
Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    phoneNumber: null, // 查找的手機(jī)號(hào)
    phoneInfo: null, // 查詢結(jié)果
    disabled: true  // 默認(rèn)不可查詢
  },

   /**
   * 鍵盤輸入手機(jī)號(hào)事件處理
   */
  bindPhoneInput(event){
    this.setData({
      phoneNumber: event.detail.value,  // 綁定用戶輸入的手機(jī)號(hào)
      phoneInfo: null   //  清空過往查詢結(jié)果
    })
    this.setDisabled();
  },

  /**
   * 驗(yàn)證手機(jī)是否為 11 位
   */
  setDisabled() {
    this.setData({
      disabled: (this.data.phoneNumber && this.data.phoneNumber.toString().length === 11) ? false : true
    })
  },

  /**
   * 用戶點(diǎn)擊查詢處理
   */
  queryPhoneInfo() {
    app.getPhoneInfo(this.data.phoneNumber, data => this.setData({
      phoneInfo: data
    }));
  }
})

說明

  • data 用于管理該頁面的數(shù)據(jù);
  • this.setData() 方法用于設(shè)置 data 的屬性,如果直接使用 this.data.phoneInfo 無法改變頁面狀態(tài);
  • 在頁面中調(diào)用 app 的方法,需要先使用 getApp 進(jìn)行實(shí)例化,然后通過實(shí)例來訪問方法;

查詢結(jié)果顯示

接下來在視圖里面顯示查詢結(jié)果

/pages/index/index.wxml




<view>

  <view wx:if="{{ phoneInfo }}">
   <text>查詢結(jié)果為: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 可以方便的根據(jù)查詢結(jié)果來切換視圖

最近搜索功能實(shí)現(xiàn)

最后是最近功能記錄的功能實(shí)現(xiàn),首先是視圖


<view>
  <text>最近搜索text>
  <view>
    <view wx:for="{{ historyList }}"  bindtap="selectHistory" data-number="{{item}}">
       {{item}}
    view>
  view>
view>

說明:

  • 遍歷 historyList 數(shù)組
  • 用戶點(diǎn)擊某一記錄時(shí)候,觸發(fā) selectHistory 事件
  • 將每條手機(jī)號(hào)保存到 data-number 中,selectHistory 就可以獲取對(duì)應(yīng)的手機(jī)號(hào)了

業(yè)務(wù)邏輯

// pages/index/index.js
var app = getApp();
Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    phoneNumber: null, // 查找的手機(jī)號(hào)
    phoneInfo: null, // 查詢結(jié)果
    historyList: [],  // 查詢歷史
    disabled: true

  },

  /**
   * 鍵盤輸入手機(jī)號(hào)事件處理
   */
  bindPhoneInput(event){
    this.setData({
      phoneNumber: event.detail.value,  // 綁定用戶輸入的手機(jī)號(hào)
      phoneInfo: null   //  清空過往查詢結(jié)果
    })
    this.setDisabled();
  },

  /**
   * 驗(yàn)證手機(jī)是否為 11 位
   */
  setDisabled() {
    this.setData({
      disabled: (this.data.phoneNumber && this.data.phoneNumber.toString().length === 11) ? false : true
    })
  },


  /**
   * 用戶點(diǎn)擊查詢處理
   */
  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') || []
    })
  },

  /**
   * 用戶點(diǎn)擊記錄之后,將其添加到輸入框中
   */
  selectHistory(event) {
    this.setData({
      phoneNumber: event.currentTarget.dataset.number,
      disabled: false
    })
  }

})

界面美化

最后,只需要美化下界面即可。

視圖


<view class="querySection">
  <text class="help-text">請(qǐng)輸入查詢內(nèi)容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">查詢結(jié)果為: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;
}


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/18203.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×