<!--pages/index.wxml--> <view class='box'> <view class='title'>掃碼與打電話</view> <button type='primary' bindtap='scanCode'>開始掃碼</button> <view class='txtLayout'> <text>字符集:{{resCode.charSet}}</text> <text>掃碼類型:{{resCode.scanType}}</text> <text>掃碼結果:{{resCode.result}}</text> </view> <view class='txtLayout'> <input placeholder='請輸入聯系人姓名' bindblur='inputName'></input> <input placeholder='請輸入聯系人電話' bindblur='inputPhone' type='number'></input> </view> <view class='btnLayout'> <button type='primary' bindtap='makeCall' style='width:45%'>撥打電話</button> <button type='primary' bindtap='addPerson' style='width:45%'>添加聯系人</button> </view> </view>
/* pages/API/index.wxss */ .txtLayout { /*text組件布局*/ display: flex; flex-direction: column; margin: 20rpx 0rpx; border: 1px solid burlywood; padding: 10rpx; } text { margin: 10rpx 0; } .btnLayout { /* 設置button組件的布局 */ display: flex; flex-direction: row; justify-content: space-around; /* 設置主軸方向組件的排列方式 */ } input { margin: 20rpx 0; border-bottom: 1px solid blue; }
// pages/index.js Page({ name:'', //定義聯系人姓名 phone: '', //定義聯系人電話 scanCode: function() { var that = this; wx.scanCode({ //調用掃碼API函數 onlyFromCamera: false, //通過攝像頭和調用相冊圖片都可以進行掃碼 scanType: [], //不指定碼的類型 success: function(res) { that.setData({ resCode: res //獲取掃碼結果 }) }, }) }, inputName: function(e) { this.name = e.detail.value; //獲取聯系人姓名 }, inputPhone: function(e) { this.phone = e.detail.value; //獲取聯系人電話 }, makeCall: function() { let phone = this.phone; wx.makePhoneCall({ //調用打電話API函數 phoneNumber: phone }) }, addPerson: function() { let name = this.name; let phone = this.phone; if (name == '' || phone == '') { wx.showToast({ title: '姓名和電話不能為空', icon: 'none', duration: 2000 }) } else { wx.addPhoneContact({ //調用添加聯系人API函數 firstName: name, mobilePhoneNumber: phone }) } } })



掃碼
wx.scanCode(Object object)用於調用客戶端 掃碼界面進行掃碼,其參數屬性如下:

scanType 的合法值

打電話
wx.makePhoneCall(Object object) 用於撥打電話
| 屬性 | 說明 |
| phoneNumber | 需要撥打的電話號碼 |
| success | 接口調用成功的回調函數 |
| fail | 接口調用失敗的回調函數 |
| complete | 接口調用結束的回調函數 |
添加聯系人
wx.addPhoneContact(Object object) 用於添加手機 通訊錄聯系人。可以通過「新增聯系人」或 「添加到已有聯系人」的方式添加。
wx.addPhoneContact(Object object) 參數主要屬性

