my.addPhoneContact()
基礎庫 1.10.0 及以上版本支持該接口,低版本需要做兼容處理,操作參見 小程序基礎庫說明,mPaaS 10.1.60 及以上版本支持該接口。
用戶可以選擇將該表單以 創建新聯系人 或 添加到現有聯系人 的方式,寫入到手機系統的通訊錄。
入參
參數 | 類型 | 必填 | 說明 |
photoFilePath | String | 否 | 頭像本地文件路徑 |
nickName | String | 否 | 昵稱 |
lastName | String | 否 | 姓氏 |
middleName | String | 否 | 中間名 |
firstName | String | 否 | 名字 |
remark | String | 否 | 備注 |
mobilePhoneNumber | String | 否 | 手機號 |
alipayAccount | String | 否 | 支付寶賬號 |
addressCountry | String | 否 | 聯系地址國家 |
addressState | String | 否 | 聯系地址省份 |
addressCity | String | 否 | 聯系地址城市 |
addressStreet | String | 否 | 聯系地址街道 |
addressPostalCode | String | 否 | 聯系地址郵政編碼 |
organization | String | 否 | 公司 |
title | String | 否 | 職位 |
workFaxNumber | String | 否 | 工作傳真 |
workPhoneNumber | String | 否 | 工作電話 |
hostNumber | String | 否 | 公司電話 |
String | 否 | 電子郵件 | |
url | String | 否 | 網站 |
workAddressCountry | String | 否 | 工作地址國家 |
workAddressState | String | 否 | 工作地址省份 |
workAddressCity | String | 否 | 工作地址城市 |
workAddressStreet | String | 否 | 工作地址街道 |
workAddressPostalCode | String | 否 | 工作地址郵政編碼 |
homeFaxNumber | String | 否 | 住宅傳真 |
homePhoneNumber | String | 否 | 住宅電話 |
homeAddressCountry | String | 否 | 住宅地址國家 |
homeAddressState | String | 否 | 住宅地址省份 |
homeAddressCity | String | 否 | 住宅地址城市 |
homeAddressStreet | String | 否 | 住宅地址街道 |
homeAddressPostalCode | String | 否 | 住宅地址郵政編碼 |
success | Function | 否 | 調用成功的回調函數 |
fail | Function | 否 | 調用失敗的回調函數 |
complete | Function | 否 | 調用結束的回調函數(調用成功、失敗都會執行) |
不同手機對應用聯系人的以上字段的支持程度不同, 可能不支持 Emoji 表情和顏文字, 當不支持時, 此項會被忽略。
返回值
成功:addPhoneContact response:{"success":true}
錯誤碼
錯誤碼 | 錯誤信息 | 描述 | 解決方案 |
3 | fail ${detail} | 調用失敗,detail 加上詳細信息 | - |
11 | fail cancel | 用戶取消操作 | 用戶正常交互流程分支,不需要特殊處理。 |
代碼示例
// API-DEMO page/API/contact/contact.json
{
"defaultTitle": "Contact"
}
<!-- API-DEMO page/API/contact/contact.axml-->
<view class="page">
<view class="page-description">聯系人 API</view>
<view class="page-section">
<view class="page-section-title">my.choosePhoneContact</view>
<view class="page-section-demo">
<button type="primary" onTap="choosePhoneContact">喚起本地通訊錄</button>
</view>
</view>
<view class="page-section">
<view class="page-section-title">my.chooseAlipayContact</view>
<view class="page-section-demo">
<button type="primary" onTap="chooseAlipayContact">喚起支付寶通訊錄</button>
</view>
</view>
<view class="page-section">
<view class="page-section-title">my.chooseContact</view>
<view class="page-section-demo">
<button type="primary" onTap="chooseContact">選擇聯系人</button>
</view>
</view>
<view class="page-section">
<view class="page-section-title">my.addPhoneContact</view>
<view class="page-section-demo">
<view style="font-size:18px;margin-top:18px;margin-bottom:18px">
<text style="font-size:18px;margin-top:18px;margin-bottom:18px">基本信息</text>
</view>
<view class="form-row">
<view class="form-row-label">昵稱</view>
<view class="form-row-content">
<input id="nickName" onInput="onInput" class="input" value="七月流火" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">姓氏</view>
<view class="form-row-content">
<input id="lastName" onInput="onInput" class="input" value="Last" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">中間名</view>
<view class="form-row-content">
<input id="middleName" onInput="onInput" class="input" value="Middle" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">名字</view>
<view class="form-row-content">
<input id="firstName" onInput="onInput" class="input" value="First" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">備注</view>
<view class="form-row-content">
<input id="remark" onInput="onInput" class="input" value="這里是備注" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">手機號</view>
<view class="form-row-content">
<input id="mobilePhoneNumber" onInput="onInput" class="input" value="138********" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">支付寶賬號</view>
<view class="form-row-content">
<input id="alipayAccount" onInput="onInput" class="input" value="alipay@alipay.com" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">微信號</view>
<view class="form-row-content">
<input id="weChatNumber" onInput="onInput" class="input" value="liuhuo" />
</view>
</view>
<view style="font-size:18px;margin-top:18px;margin-bottom:18px">
<text style="font-size:18px;margin-top:18px;margin-bottom:18px">聯系地址</text>
</view>
<view class="form-row">
<view class="form-row-label">國家</view>
<view class="form-row-content">
<input id="addressCountry" onInput="onInput" class="input" value="US" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">省份</view>
<view class="form-row-content">
<input id="addressState" onInput="onInput" class="input" value="California" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">城市</view>
<view class="form-row-content">
<input id="addressCity" onInput="onInput" class="input" value="San Francisco" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">街道</view>
<view class="form-row-content">
<input id="addressStreet" onInput="onInput" class="input" value="Mountain View" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">郵政編碼</view>
<view class="form-row-content">
<input id="addressPostalCode" onInput="onInput" class="input" value="94016" />
</view>
</view>
<view style="font-size:18px;margin-top:18px;margin-bottom:18px">
<text style="font-size:18px;margin-top:18px;margin-bottom:18px">工作</text>
</view>
<view class="form-row">
<view class="form-row-label">公司</view>
<view class="form-row-content">
<input id="organization" onInput="onInput" class="input" value="AntFin" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">職位</view>
<view class="form-row-content">
<input id="title" onInput="onInput" class="input" value="Developer" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">工作傳真</view>
<view class="form-row-content">
<input id="workFaxNumber" onInput="onInput" class="input" value="11111111" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">工作電話</view>
<view class="form-row-content">
<input id="workPhoneNumber" onInput="onInput" class="input" value="11111112" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">公司電話</view>
<view class="form-row-content">
<input id="hostNumber" onInput="onInput" class="input" value="11111113" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">電子郵件</view>
<view class="form-row-content">
<input id="email" onInput="onInput" class="input" value="liuhuo01@alipay.com" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">網站</view>
<view class="form-row-content">
<input id="url" onInput="onInput" class="input" value="www.alipay.com" />
</view>
</view>
<view style="font-size:18px;margin-top:18px;margin-bottom:18px">
<text style="font-size:18px;margin-top:18px;margin-bottom:18px">工作地址</text>
</view>
<view class="form-row">
<view class="form-row-label">國家</view>
<view class="form-row-content">
<input id="workAddressCountry" onInput="onInput" class="input" value="China" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">省份</view>
<view class="form-row-content">
<input id="workAddressState" onInput="onInput" class="input" value="Zhejiang" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">城市</view>
<view class="form-row-content">
<input id="workAddressCity" onInput="onInput" class="input" value="Hangzhou" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">街道</view>
<view class="form-row-content">
<input id="workAddressStreet" onInput="onInput" class="input" value="Tianmushan Road" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">郵政編碼</view>
<view class="form-row-content">
<input id="workAddressPostalCode" onInput="onInput" class="input" value="361005" />
</view>
</view>
<view style="font-size:18px;margin-top:18px;margin-bottom:18px">
<text style="font-size:18px;margin-top:18px;margin-bottom:18px">住宅</text>
</view>
<view class="form-row">
<view class="form-row-label">傳真</view>
<view class="form-row-content">
<input id="homeFaxNumber" onInput="onInput" class="input" value="11111114" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">電話</view>
<view class="form-row-content">
<input id="homePhoneNumber" onInput="onInput" class="input" value="11111115" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">國家</view>
<view class="form-row-content">
<input id="homeAddressCountry" onInput="onInput" class="input" value="Canada" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">省份</view>
<view class="form-row-content">
<input id="homeAddressState" onInput="onInput" class="input" value="Ontario" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">城市</view>
<view class="form-row-content">
<input id="homeAddressCity" onInput="onInput" class="input" value="Toronto" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">街道</view>
<view class="form-row-content">
<input id="homeAddressStreet" onInput="onInput" class="input" value="No.234 Road" />
</view>
</view>
<view class="form-row">
<view class="form-row-label">郵政編碼</view>
<view class="form-row-content">
<input id="homeAddressPostalCode" onInput="onInput" class="input" value="123456" />
</view>
</view>
<button type="primary" onTap="addPhoneContact">添加到手機聯系人</button>
</view>
</view>
</view>
// API-DEMO page/API/contact/contact.js
Page({
data:{
"photoFilePath": "/sdcard/DCIM/Camera/a.jpg",
"nickName": "七月流火",
"lastName": "Last",
"middleName": "Middle",
"firstName": "First",
"remark": "這里是備注",
"mobilePhoneNumber": "138********",
"homePhoneNumber": "11111115",
"workPhoneNumber": "11111112",
"homeFaxNumber": "11111114",
"workFaxNumber": "11111111",
"hostNumber": "11111113",
"weChatNumber": "liuhuo",
"alipayAccount": "alipay@alipay.com",
"addressCountry": "US",
"addressState": "California",
"addressCity": "San Francisco",
"addressStreet": "Mountain View",
"addressPostalCode": "94016",
"workAddressCountry": "China",
"workAddressState": "Zhejiang",
"workAddressCity": "Hangzhou",
"workAddressStreet": "Tianmushan Road",
"workAddressPostalCode": "361005",
"homeAddressCountry": "Canada",
"homeAddressState": "Ontairo",
"homeAddressCity": "Toronto",
"homeAddressStreet": "No.234 Road",
"homeAddressPostalCode": "123456",
"organization": "AntFin",
"title": "Developer",
"email": "liuhuo01@alipaydoc.com",
"url": "www.alipay.com",
success: (res) => {
my.alert({
content: 'addPhoneContact response: ' + JSON.stringify(res)
});
},
fail: (res) => {
my.alert({
content: 'addPhoneContact response: ' + JSON.stringify(res)
});
}
},
choosePhoneContact() {
my.choosePhoneContact({
success: (res) => {
my.alert({
content: 'choosePhoneContact response: ' + JSON.stringify(res)
});
},
fail: (res) => {
my.alert({
content: 'choosePhoneContact response: ' + JSON.stringify(res)
});
},
});
},
chooseAlipayContact() {
my.chooseAlipayContact({
count: 2,
success: (res) => {
my.alert({
content: 'chooseAlipayContact response: ' + JSON.stringify(res)
});
},
fail: (res) => {
my.alert({
content: 'chooseAlipayContact response: ' + JSON.stringify(res)
});
},
});
},
chooseContact() {
my.chooseContact({
chooseType: 'multi', // 多選模式
includeMe: true, // 包含自己
includeMobileContactMode: 'known',//僅包含雙向手機通訊錄聯系人,也即雙方手機通訊錄都存有對方號碼的聯系人
multiChooseMax: 3, // 最多能選擇三個聯系人
multiChooseMaxTips: '超過選擇的最大人數了',
success: (res) => {
my.alert({
content: 'chooseContact : ' + JSON.stringify(res)
});
},
fail: (res) => {
my.alert({
content: 'chooseContact : ' + JSON.stringify(res)
});
},
});
},
onInput(e) {
this.data[e.currentTarget.id] = e.detail.value;
},
addPhoneContact() {
if (my.canIUse('addPhoneContact')) {
my.addPhoneContact(this.data);
} else {
my.alert({
title: '客戶端版本過低',
content: 'my.addPhoneContact() 需要 10.1.32 及以上版本'
});
}
}
});