小程序拨打电话功能(数据是数组或字符串情况)

亲测实现过程没有任何问题

wxml:

<view class="phone" bindtap="callPhone" data-phone="{
   
   {item.tel}}">
	<image class="phone_icon" src="https://cdn.17wenhua.com/partyMiniProgram/vote/%E6%8B%A8%E6%89%93%E7%94%B5%E8%AF%9D.png"></image>
	<view class="icon_bottom_title">电话</view>
</view>

当获取到的电话列表是数组的情况:

js:

// 拨打电话 
callPhone(e){
  //获取点击的内容
  const phoneNumber = e.currentTarget.dataset.phone;
  //没有电话的情况
  if(e.currentTarget.dataset.phone.length==0){
    console.log('no');
    wx.showToast({
      title: '此商家暂无电话',
      icon:"none"
    })
  }
  //调起弹框用来显示多条内容
  wx.showActionSheet({
    itemList:phoneNumber, //电话数组
    success: (res) => {
      const selectedIndex = res.tapIndex;
      var selectedData = phoneNumber[selectedIndex];
      console.log(selectedData,'当前点击内容');
      //拨打电话的API
      wx.makePhoneCall({
        phoneNumber:selectedData,
        success: function () {
          console.log("拨打电话成功!")
        },
        fail: function () {
          console.log("拨打电话失败!")
        }
      })
    },
    fail (res) {
      console.log('取消选项')
    }
  })
},

当获取到的电话只有一条内容:

// 拨打电话 
callPhone(e){
  //获取点击的内容
  const phoneNumber = e.currentTarget.dataset.phone;
      wx.makePhoneCall({
        phoneNumber:phoneNumber,
        success: function () {
          console.log("拨打电话成功!")
        },
        fail: function () {
          console.log("拨打电话失败!")
        }
    })
},

猜你喜欢

转载自blog.csdn.net/m0_65465945/article/details/135913278