计算机网络/计算机科学与应用/系统/运维/开发

微信小程序 复制/拨打/保存电话


一、WXML

 <view class="action-btn copy" bindtap="copyPhone" data-phone="{{user.mobile}}" data-name="{{user.name}}">
            <text class="icon">复制</text>
          </view>
          <view class="action-btn call" bindtap="callPhone" data-phone="{{user.mobile}}">
            <text class="icon">拨打</text>
          </view>
          <view class="action-btn save" bindtap="savePhone" data-name="{{user.name}}" data-phone="{{user.mobile}}">
            <text class="icon">保存</text>
          </view>

二、JS

// 拨打电话
  callPhone(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({ phoneNumber: phone });
  },
  // 复制姓名/手机号
  copyPhone(e) {
    const name = e.currentTarget.dataset.name;
    const phone = e.currentTarget.dataset.phone;
    // 拼接复制内容:姓名 手机号
    const copyText = `${name} ${phone}`;
    wx.setClipboardData({
      data: copyText,
      success() {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        });
      }
    });
  },
  // 保存通讯录
  savePhone(e) {
    const name = e.currentTarget.dataset.name;
    const phone = e.currentTarget.dataset.phone;
    wx.addPhoneContact({
      firstName: name,
      mobilePhoneNumber: phone,
      success() {
        wx.showToast({ title: '保存成功', icon: 'success' });
      },
      fail() {
        wx.showToast({ title: '保存失败', icon: 'none' });
      }
    });
  }


人生活在得失之间,得亦是失,失亦是得。

评论

^