全部产品
云市场
云游戏

添加手机联系人

更新时间:2020-07-07 19:42:16

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 公司电话
email 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 用户取消操作 用户正常交互流程分支,不需要特殊处理。

代码示例

  1. // API-DEMO page/API/contact/contact.json
  2. {
  3. "defaultTitle": "Contact"
  4. }
  1. <!-- API-DEMO page/API/contact/contact.axml-->
  2. <view class="page">
  3. <view class="page-description">联系人 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">my.choosePhoneContact</view>
  6. <view class="page-section-demo">
  7. <button type="primary" onTap="choosePhoneContact">唤起本地通讯录</button>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">my.chooseAlipayContact</view>
  12. <view class="page-section-demo">
  13. <button type="primary" onTap="chooseAlipayContact">唤起支付宝通讯录</button>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">my.chooseContact</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="chooseContact">选择联系人</button>
  20. </view>
  21. </view>
  22. <view class="page-section">
  23. <view class="page-section-title">my.addPhoneContact</view>
  24. <view class="page-section-demo">
  25. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  26. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">基本信息</text>
  27. </view>
  28. <view class="form-row">
  29. <view class="form-row-label">昵称</view>
  30. <view class="form-row-content">
  31. <input id="nickName" onInput="onInput" class="input" value="七月流火" />
  32. </view>
  33. </view>
  34. <view class="form-row">
  35. <view class="form-row-label">姓氏</view>
  36. <view class="form-row-content">
  37. <input id="lastName" onInput="onInput" class="input" value="Last" />
  38. </view>
  39. </view>
  40. <view class="form-row">
  41. <view class="form-row-label">中间名</view>
  42. <view class="form-row-content">
  43. <input id="middleName" onInput="onInput" class="input" value="Middle" />
  44. </view>
  45. </view>
  46. <view class="form-row">
  47. <view class="form-row-label">名字</view>
  48. <view class="form-row-content">
  49. <input id="firstName" onInput="onInput" class="input" value="First" />
  50. </view>
  51. </view>
  52. <view class="form-row">
  53. <view class="form-row-label">备注</view>
  54. <view class="form-row-content">
  55. <input id="remark" onInput="onInput" class="input" value="这里是备注" />
  56. </view>
  57. </view>
  58. <view class="form-row">
  59. <view class="form-row-label">手机号</view>
  60. <view class="form-row-content">
  61. <input id="mobilePhoneNumber" onInput="onInput" class="input" value="13800000000" />
  62. </view>
  63. </view>
  64. <view class="form-row">
  65. <view class="form-row-label">支付宝账号</view>
  66. <view class="form-row-content">
  67. <input id="alipayAccount" onInput="onInput" class="input" value="alipay@alipay.com" />
  68. </view>
  69. </view>
  70. <view class="form-row">
  71. <view class="form-row-label">微信号</view>
  72. <view class="form-row-content">
  73. <input id="weChatNumber" onInput="onInput" class="input" value="liuhuo" />
  74. </view>
  75. </view>
  76. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  77. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">联系地址</text>
  78. </view>
  79. <view class="form-row">
  80. <view class="form-row-label">国家</view>
  81. <view class="form-row-content">
  82. <input id="addressCountry" onInput="onInput" class="input" value="US" />
  83. </view>
  84. </view>
  85. <view class="form-row">
  86. <view class="form-row-label">省份</view>
  87. <view class="form-row-content">
  88. <input id="addressState" onInput="onInput" class="input" value="California" />
  89. </view>
  90. </view>
  91. <view class="form-row">
  92. <view class="form-row-label">城市</view>
  93. <view class="form-row-content">
  94. <input id="addressCity" onInput="onInput" class="input" value="San Francisco" />
  95. </view>
  96. </view>
  97. <view class="form-row">
  98. <view class="form-row-label">街道</view>
  99. <view class="form-row-content">
  100. <input id="addressStreet" onInput="onInput" class="input" value="Mountain View" />
  101. </view>
  102. </view>
  103. <view class="form-row">
  104. <view class="form-row-label">邮政编码</view>
  105. <view class="form-row-content">
  106. <input id="addressPostalCode" onInput="onInput" class="input" value="94016" />
  107. </view>
  108. </view>
  109. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  110. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">工作</text>
  111. </view>
  112. <view class="form-row">
  113. <view class="form-row-label">公司</view>
  114. <view class="form-row-content">
  115. <input id="organization" onInput="onInput" class="input" value="AntFin" />
  116. </view>
  117. </view>
  118. <view class="form-row">
  119. <view class="form-row-label">职位</view>
  120. <view class="form-row-content">
  121. <input id="title" onInput="onInput" class="input" value="Developer" />
  122. </view>
  123. </view>
  124. <view class="form-row">
  125. <view class="form-row-label">工作传真</view>
  126. <view class="form-row-content">
  127. <input id="workFaxNumber" onInput="onInput" class="input" value="11111111" />
  128. </view>
  129. </view>
  130. <view class="form-row">
  131. <view class="form-row-label">工作电话</view>
  132. <view class="form-row-content">
  133. <input id="workPhoneNumber" onInput="onInput" class="input" value="11111112" />
  134. </view>
  135. </view>
  136. <view class="form-row">
  137. <view class="form-row-label">公司电话</view>
  138. <view class="form-row-content">
  139. <input id="hostNumber" onInput="onInput" class="input" value="11111113" />
  140. </view>
  141. </view>
  142. <view class="form-row">
  143. <view class="form-row-label">电子邮件</view>
  144. <view class="form-row-content">
  145. <input id="email" onInput="onInput" class="input" value="liuhuo01@sina.com" />
  146. </view>
  147. </view>
  148. <view class="form-row">
  149. <view class="form-row-label">网站</view>
  150. <view class="form-row-content">
  151. <input id="url" onInput="onInput" class="input" value="www.alipay.com" />
  152. </view>
  153. </view>
  154. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  155. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">工作地址</text>
  156. </view>
  157. <view class="form-row">
  158. <view class="form-row-label">国家</view>
  159. <view class="form-row-content">
  160. <input id="workAddressCountry" onInput="onInput" class="input" value="China" />
  161. </view>
  162. </view>
  163. <view class="form-row">
  164. <view class="form-row-label">省份</view>
  165. <view class="form-row-content">
  166. <input id="workAddressState" onInput="onInput" class="input" value="Zhejiang" />
  167. </view>
  168. </view>
  169. <view class="form-row">
  170. <view class="form-row-label">城市</view>
  171. <view class="form-row-content">
  172. <input id="workAddressCity" onInput="onInput" class="input" value="Hangzhou" />
  173. </view>
  174. </view>
  175. <view class="form-row">
  176. <view class="form-row-label">街道</view>
  177. <view class="form-row-content">
  178. <input id="workAddressStreet" onInput="onInput" class="input" value="Tianmushan Road" />
  179. </view>
  180. </view>
  181. <view class="form-row">
  182. <view class="form-row-label">邮政编码</view>
  183. <view class="form-row-content">
  184. <input id="workAddressPostalCode" onInput="onInput" class="input" value="361005" />
  185. </view>
  186. </view>
  187. <view style="font-size:18px;margin-top:18px;margin-bottom:18px">
  188. <text style="font-size:18px;margin-top:18px;margin-bottom:18px">住宅</text>
  189. </view>
  190. <view class="form-row">
  191. <view class="form-row-label">传真</view>
  192. <view class="form-row-content">
  193. <input id="homeFaxNumber" onInput="onInput" class="input" value="11111114" />
  194. </view>
  195. </view>
  196. <view class="form-row">
  197. <view class="form-row-label">电话</view>
  198. <view class="form-row-content">
  199. <input id="homePhoneNumber" onInput="onInput" class="input" value="11111115" />
  200. </view>
  201. </view>
  202. <view class="form-row">
  203. <view class="form-row-label">国家</view>
  204. <view class="form-row-content">
  205. <input id="homeAddressCountry" onInput="onInput" class="input" value="Canada" />
  206. </view>
  207. </view>
  208. <view class="form-row">
  209. <view class="form-row-label">省份</view>
  210. <view class="form-row-content">
  211. <input id="homeAddressState" onInput="onInput" class="input" value="Ontario" />
  212. </view>
  213. </view>
  214. <view class="form-row">
  215. <view class="form-row-label">城市</view>
  216. <view class="form-row-content">
  217. <input id="homeAddressCity" onInput="onInput" class="input" value="Toronto" />
  218. </view>
  219. </view>
  220. <view class="form-row">
  221. <view class="form-row-label">街道</view>
  222. <view class="form-row-content">
  223. <input id="homeAddressStreet" onInput="onInput" class="input" value="No.234 Road" />
  224. </view>
  225. </view>
  226. <view class="form-row">
  227. <view class="form-row-label">邮政编码</view>
  228. <view class="form-row-content">
  229. <input id="homeAddressPostalCode" onInput="onInput" class="input" value="123456" />
  230. </view>
  231. </view>
  232. <button type="primary" onTap="addPhoneContact">添加到手机联系人</button>
  233. </view>
  234. </view>
  235. </view>
  1. // API-DEMO page/API/contact/contact.js
  2. Page({
  3. data:{
  4. "photoFilePath": "/sdcard/DCIM/Camera/a.jpg",
  5. "nickName": "七月流火",
  6. "lastName": "Last",
  7. "middleName": "Middle",
  8. "firstName": "First",
  9. "remark": "这里是备注",
  10. "mobilePhoneNumber": "13800000000",
  11. "homePhoneNumber": "11111115",
  12. "workPhoneNumber": "11111112",
  13. "homeFaxNumber": "11111114",
  14. "workFaxNumber": "11111111",
  15. "hostNumber": "11111113",
  16. "weChatNumber": "liuhuo",
  17. "alipayAccount": "alipay@alipay.com",
  18. "addressCountry": "US",
  19. "addressState": "California",
  20. "addressCity": "San Francisco",
  21. "addressStreet": "Mountain View",
  22. "addressPostalCode": "94016",
  23. "workAddressCountry": "China",
  24. "workAddressState": "Zhejiang",
  25. "workAddressCity": "Hangzhou",
  26. "workAddressStreet": "Tianmushan Road",
  27. "workAddressPostalCode": "361005",
  28. "homeAddressCountry": "Canada",
  29. "homeAddressState": "Ontairo",
  30. "homeAddressCity": "Toronto",
  31. "homeAddressStreet": "No.234 Road",
  32. "homeAddressPostalCode": "123456",
  33. "organization": "AntFin",
  34. "title": "Developer",
  35. "email": "liuhuo01@sina.com",
  36. "url": "www.alipay.com",
  37. success: (res) => {
  38. my.alert({
  39. content: 'addPhoneContact response: ' + JSON.stringify(res)
  40. });
  41. },
  42. fail: (res) => {
  43. my.alert({
  44. content: 'addPhoneContact response: ' + JSON.stringify(res)
  45. });
  46. }
  47. },
  48. choosePhoneContact() {
  49. my.choosePhoneContact({
  50. success: (res) => {
  51. my.alert({
  52. content: 'choosePhoneContact response: ' + JSON.stringify(res)
  53. });
  54. },
  55. fail: (res) => {
  56. my.alert({
  57. content: 'choosePhoneContact response: ' + JSON.stringify(res)
  58. });
  59. },
  60. });
  61. },
  62. chooseAlipayContact() {
  63. my.chooseAlipayContact({
  64. count: 2,
  65. success: (res) => {
  66. my.alert({
  67. content: 'chooseAlipayContact response: ' + JSON.stringify(res)
  68. });
  69. },
  70. fail: (res) => {
  71. my.alert({
  72. content: 'chooseAlipayContact response: ' + JSON.stringify(res)
  73. });
  74. },
  75. });
  76. },
  77. chooseContact() {
  78. my.chooseContact({
  79. chooseType: 'multi', // 多选模式
  80. includeMe: true, // 包含自己
  81. includeMobileContactMode: 'known',//仅包含双向手机通讯录联系人,也即双方手机通讯录都存有对方号码的联系人
  82. multiChooseMax: 3, // 最多能选择三个联系人
  83. multiChooseMaxTips: '超过选择的最大人数了',
  84. success: (res) => {
  85. my.alert({
  86. content: 'chooseContact : ' + JSON.stringify(res)
  87. });
  88. },
  89. fail: (res) => {
  90. my.alert({
  91. content: 'chooseContact : ' + JSON.stringify(res)
  92. });
  93. },
  94. });
  95. },
  96. onInput(e) {
  97. this.data[e.currentTarget.id] = e.detail.value;
  98. },
  99. addPhoneContact() {
  100. if (my.canIUse('addPhoneContact')) {
  101. my.addPhoneContact(this.data);
  102. } else {
  103. my.alert({
  104. title: '客户端版本过低',
  105. content: 'my.addPhoneContact() 需要 10.1.32 及以上版本'
  106. });
  107. }
  108. }
  109. });