Add phone contacts

更新时间:
复制 MD 格式

my.addPhoneContact()

Note

Supported in base library 1.10.0 or later and mPaaS 10.1.60 or later. For compatibility with earlier versions, see Mini Program Base Library Description.

Users can save the form information to their phone's address book by choosing to create a new contact or add to an existing contact.

Input parameters

Parameter

Type

Required

Description

photoFilePath

String

No

Local file path of the profile picture.

nickName

String

No

Nickname.

lastName

String

No

Last name.

middleName

String

No

Middle name.

firstName

String

No

First name.

remark

String

No

Remarks.

mobilePhoneNumber

String

No

Mobile phone number.

alipayAccount

String

No

Alipay account.

addressCountry

String

No

Contact address country.

addressState

String

No

Contact address state/province.

addressCity

String

No

Contact address city.

addressStreet

String

No

Contact address street.

addressPostalCode

String

No

Contact address postal code.

organization

String

No

Company.

title

String

No

Job title.

workFaxNumber

String

No

Work fax number.

workPhoneNumber

String

No

Work phone number.

hostNumber

String

No

Company phone number.

email

String

No

Email.

url

String

No

Website.

workAddressCountry

String

No

Work address country.

workAddressState

String

No

Work address state/province.

workAddressCity

String

No

Work address city.

workAddressStreet

String

No

Work address street.

workAddressPostalCode

String

No

Work address postal code.

homeFaxNumber

String

No

Home fax number.

homePhoneNumber

String

No

Home phone number.

homeAddressCountry

String

No

Home address country.

homeAddressState

String

No

Home address state/province.

homeAddressCity

String

No

Home address city.

homeAddressStreet

String

No

Home address street.

homeAddressPostalCode

String

No

Home address postal code.

success

Function

No

Callback function for a successful API call.

fail

Function

No

Callback function for a failed API call.

complete

Function

No

Callback function that runs when the API call completes, regardless of success or failure.

Support for contact fields varies by device. Some phones may not support emojis or kaomoji. Unsupported fields are ignored.

Return value

Success: {"success":true}

Error codes

Error code

Error message

Description

Solution

3

fail ${detail}

The API call failed. ${detail} provides additional information.

-

11

fail cancel

The user canceled the operation.

Normal user interaction. No special handling is required.

Code samples

// API-DEMO page/API/contact/contact.json
{
   "defaultTitle": "Contact"
}
<!-- API-DEMO page/API/contact/contact.axml-->
<view class="page">

  <view class="page-description">Contact API</view>

  <view class="page-section">
    <view class="page-section-title">my.choosePhoneContact</view>
    <view class="page-section-demo">
      <button type="primary" onTap="choosePhoneContact">Open local address book</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">Open Alipay address book</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">Choose contact</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">Basic information</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Nickname</view>
        <view class="form-row-content">
          <input id="nickName" onInput="onInput" class="input" value="July" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Last name</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">Middle name</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">First name</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">Remarks</view>
        <view class="form-row-content">
          <input id="remark" onInput="onInput" class="input" value="These are the remarks" />
        </view>
      </view>

      <view class="form-row">
        <view class="form-row-label">Mobile phone number</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">Alipay account</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">WeChat ID</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">Contact address</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Country</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">State/Province</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">City</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">Street</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">Postal code</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">Work</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Company</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">Job title</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">Work fax</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">Work phone</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">Company phone</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">Email</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">Website</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">Work address</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Country</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">State/Province</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">City</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">Street</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">Postal code</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">Home</text>
      </view>

      <view class="form-row">
        <view class="form-row-label">Fax</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">Phone</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">Country</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">State/Province</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">City</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">Street</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">Postal code</view>
        <view class="form-row-content">
          <input id="homeAddressPostalCode" onInput="onInput" class="input" value="123456" />
        </view>
      </view>

      <button type="primary" onTap="addPhoneContact">Add to phone contacts</button>

    </view>
  </view>

</view>
// API-DEMO page/API/contact/contact.js
Page({
  data:{
      "photoFilePath": "/sdcard/DCIM/Camera/a.jpg",
      "nickName": "July",
      "lastName": "Last",
      "middleName": "Middle",
      "firstName": "First",
      "remark": "These are the remarks",
      "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', // Multi-selection mode
      includeMe: true,     // Include self
      includeMobileContactMode: 'known',// Include only bidirectional mobile contacts, meaning contacts where both parties have each other's number saved.
      multiChooseMax: 3,  // Select a maximum of three contacts
      multiChooseMaxTips: 'You have exceeded the maximum number of selections.',
      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: 'Client version too low',
        content: 'my.addPhoneContact() requires version 10.1.32 or later.'
      });
    }
  }
});