目录

Obtaining the Shipping Address

Use Cases

Account Kit provides the ability to obtain a user's shipping address, which can be an existing one selected or a new one added by the user. The following describes the function of obtaining the shipping address provided by Account Kit. Additionally, the shipping address selection button provided by Scenario Fusion Kit can be used to obtain the shipping address.

Constraints

  1. Shipping addresses and associated mobile numbers must be from the Chinese mainland only.

  2. The capability of obtaining the shipping address is supported on phones, tablets, and PCs/2-in-1 devices. In addition, starting from version 26.0.0, the capability is also supported on TVs.

Service Process

The process is as follows:

  1. When a user needs to specify a shipping address, the app calls the shipping address selection API to show the shipping address management screen of Account Kit.

  2. The user can add a shipping address or select an existing one on the shipping address management screen. After the user confirms the operation, the added or selected shipping address is returned to the app.

API Description

The table below lists the key API for obtaining the shipping address of a user. For details about the APIs, please refer to the API Reference.

API Description
chooseAddress(context: common.Context): Promise<AddressInfo> Shows the shipping address management screen and returns the shipping address specified by a user.

The above APIs must be called within the lifecycle of the page or custom component.

Prerequisites

Before coding, make sure that the following preparations are complete:

  1. Request necessary scopes. If the scopes are not requested, calling the API for obtaining the shipping address will result in error 1008100005.

If you have configured signing information and added a fingerprint before requesting necessary scopes, you will need to request a debug profile and manually configure signing information again.

  1. Configure the signing information and fingerprint and configure the client ID. If the API for obtaining a user's shipping address is called when you have not completed the two configuration tasks, error 1008100004 App Certificate Fingerprint Verification Failure will be returned.

Development Procedure

  1. Import the shippingAddress module and related common modules.

    import { shippingAddress } from '@kit.AccountKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Call the chooseAddress method to show the shipping address management screen, on which the user can select or add an address. Then, the address finally confirmed by the user will be sent to the app.

    // Execute the request.
    try {
      // The following provides only code snippets. Use it in the custom component instance and pass a valid context object.
      shippingAddress.chooseAddress(this.getUIContext().getHostContext())
        .then((data: shippingAddress.AddressInfo) => {
          hilog.info(0x0000, 'testTag', 'Succeeded in choosing address.');
          const userName: string = data.userName;
          const mobileNumber: string = data.mobileNumber;
          const countryCode: string = data.countryCode;
          const provinceName: string = data.provinceName;
          const cityName: string = data.cityName;
          const districtName: string = data.districtName;
          const streetName: string = data.streetName;
          const detailedAddress: string = data.detailedAddress;
          // Process the obtained shipping address information.
          // ...
        }).catch((error: BusinessError) => {
        // ...
        dealAllError(error);
      });
    } catch (error) {
      // ...
      dealAllError(error);
    }
    // Handle the error.
    function dealAllError(error: BusinessError): void {
      hilog.error(0x0000, 'testTag', `Failed to chooseAddress. Code: ${error.code}, message: ${error.message}`);
    }