微信小程序實現快遞查詢功能(界面傳值、JSON數據請求和解析、radio-group的使用...)

運行效果:

這裏寫圖片描述

請求數據之前需要首先在小程序平臺設置服務器域名

這裏寫圖片描述
這裏寫圖片描述

第一個界面的實現:界面傳值、radio-group的使用

first.wxml

<!--first.wxml-->
<view class="first_view">
  <text>請選擇快遞公司:</text>

  <!-- 單項選擇器控件 -->
  <radio-group class="radio_group" bindchange="listenRadioGroup">
  <!--用view包含每個實現垂直排列  -->
    <view wx:for="{{items}}" wx:key="key">
      <radio value='{{item.value}}' checked='{{false}}'>{{item.name}}</radio>
    </view>
  </radio-group>

  <!-- 輸入框控件 -->
  <input class="input_view" type="number" bindinput="getInputText" cursor-spacing='10' placeholder="請輸入快遞單號" auto-focus/>

  <button bindtap="jump">查詢快遞</button>

</view>

first.wxss

/* first.wxss */

.first_view {
  display: flex;
  flex-direction: column;
}

.input_view {
  margin-top: 30rpx;
  margin-bottom: 30rpx;
  background-color: rgb(250, 250, 250);
}

first.js

// first.js
//創建兩個變量,保存快遞單號和快遞類型
var kd_type
var kd_num

Page({

  /**
   * 頁面的初始數據
   */
  data: {
    items: [
      { name: "申通", value: "shentong" },
      { name: "EMS", value: "ems" },
      { name: "順豐", value: "shunfeng" },
      { name: "圓通", value: "yuantong" },
      { name: "韻達", value: "yunda" },
      { name: "天天", value: "tiantian" },
      { name: "匯通", value: "huitongkuaidi" },
      { name: "全峯", value: "quanfengkuaidi" },
      { name: "德邦", value: "debangkuaidi" },
      { name: "宅急送", value: "zhaijisong" },
    ]
  },
  // 監聽單項選擇器radio_group的選中情況
  listenRadioGroup: function (value) {
    console.log(value)
    kd_type = value.detail.value
  },
  // 獲取文本框input的輸入內容
  getInputText: function (inputText) {
    console.log(inputText.detail.value)
    kd_num = inputText.detail.value
  },
  // “查詢快遞”按鈕點擊事件
  jump: function () {
    wx.navigateTo({
      // 參數拼接傳到下一個界面
      url: '../second/second?postid=' + kd_num + '&type=' + kd_type,
    })
  }
})

第二個界面的實現:JSON數據請求和解析

JSON數據格式如下:

這裏寫圖片描述

second.wxml

<!--second.wxml-->
<view class="container">

  <text class="title">
    快遞單號:{{result.nu}} 
    快遞公司:{{result.com}}
  </text>

  <block wx:for="{{result.data}}" wx:key="key">
    <text>
      {{item.time}}
      {{item.context}}
    </text>
  </block>
</view>

second.wxss

/**second.wxss**/

.title {
  font-size: 40rpx;
}

text {
  font-size: 30rpx;
}

second.js

//second.js
//獲取應用實例
var app = getApp()
Page({
  onLoad: function (option) {
    console.log('界面跳轉成功')
    var that = this

    // 數據請求
    wx.request({
      url: 'http://www.kuaidi100.com/query?',
      // 參數請求所需的參數
      data: { type: option.type, postid: option.postid },
      // 數據請求方式
      method: 'GET',
      // 請求成功
      success: function (res) {
        console.log("返回的數據爲" + res)
        // 設置數據
        that.setData({
          result: res.data
        })
      },
      // 請求失敗
      fail: function () {
        console.log('fail');
      },
      // 請求完成
      complete: function () {
        console.log('complete');
      }
    })
    //調用應用實例的方法獲取全局數據
    app.getUserInfo(function (userInfo) {
      //更新數據
      that.setData({
        userInfo: userInfo
      })
    })
  }
})
發佈了194 篇原創文章 · 獲贊 239 · 訪問量 76萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章