微信小程序实例 - 手机号归属地查询

读万卷书 不如行一里路, 上手才是王道

码云地址: https://git.oschina.net/GreenLittleApple/wx_xiaochengxu01.git

先上图

代码解析
很简单, 一个js就能搞定。
1、 显示页面

<!--index.wxml--> <view class="body"> // 输入框 <input type="number" maxlength="11" auto-focus class="phoneInput" bindinput="listenPhoneInput" /> // 查询按钮(bindtap点击事件, 相当于onclick) <button type="primary" bindtap="queryHome4Phone" class="btnQuery"> 查询 </button> // 结果显示 <text class="result" wx:if="{{province != ''}}"> {{message}} </text> </view>

2、 控制代码 js

// 监听手机号输入 listenPhoneInput: function(e) { this.data.phoneNumber = e.detail.value
  }, // 查询手机号归属地 queryHome4Phone: function() { var page = this console.log("手机号是"+ this.data.phoneNumber) // 网络请求 wx.request({
      url: 'http://apis.juhe.cn/mobile/get', // 接口地址 data: { // 参数 phone: this.data.phoneNumber,
        key: '自己申请key(用的聚合数据)' },
      header: { 'Content-Type': 'application/json',
      },
      success: function(res) { // 成功回调函数 var result = res.data console.log(res.data) if(result.error_code == 201101) {
            page.setData({
              message: '手机号不能为空' })
        } else if(result.error_code == 201102) {
            page.setData({
              message: '错误的手机号码' })
        } else if(result.error_code == 201103) {
            page.setData({
              message: '查询无结果' })
        } else {
          page.setData({ // 组合结果 message: result.result.province + " " + result.result.city + " " + result.result.company
          })
        }
      }
    })
  },

至此, 手机号归属地就OK了。 如有疑问, 加群(214975625)讨论 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绿色的小苹果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值