uni-app Integration

更新时间:
复制 MD 格式

This document explains how to integrate the Phone Number Verification Service native plugin into the uni-app development framework and perform mobile number verification.

Background information

Uni-app native plugin

The Alibaba Cloud Phone Number Verification SDK is an authentication plugin developed using the uni-app native plugin extension capability of HBuilder. It lets you easily integrate Alibaba Cloud's phone number verification capabilities into your project and implement related features at the JavaScript (JS) layer.

Important

This plugin is developed with native code and is compatible only with Android and iOS applications built on the uni-app framework and is not compatible with other types of mini programs.

Add the plugin to your project

  1. Create a uni-app project.

    1. If you have not created a uni-app project, see Create a project by using the HBuilder visual interface.

    2. If you already have a project, proceed to the next step.

  2. Download the plugin.

    1. Go to the plugin page: Alibaba Cloud Phone Number Verification SDK.

    2. On the right side of the page, click Download for offline packaging to download the plugin.

  3. Import the plugin into your project.

    1. In the root directory of your project, create a new folder named nativeplugins. Unzip the downloaded AliCloud-NirvanaPns_x.x.x.zip package and add the extracted plugin folder to the nativeplugins folder.37

    2. Open the manifest.json file in the root directory and click App Native Plugin Configuration > Select Local Plugin.38

    3. In the Local Plugin Selection dialog box, select the Alibaba Cloud Phone Number Verification SDK checkbox.image

    4. Click OK to import the plugin.

Create a scheme code

See Create a verification scheme and obtain the Scheme Code.

Interaction flow

For the complete interaction flow, see Mobile number verification interaction flow.

Methods

Import method

In your vue or nvue file, import the native plugin by using the uni.requireNativePlugin method.

Example

const aLiSDKModule = uni.requireNativePlugin('AliCloud-NirvanaPns');

setAuthSDKInfo (Set secret)

This is a required method.

Before using one-click logon, you must set the secret and ensure that the secret matches your app's bundle ID or package name.

Parameters: setAuthSDKInfo(info)

Parameter

Type

Description

info

String

The secret generated in the Phone Number Verification Service console.

Example

aLiSDKModule.setAuthSDKInfo("<SDK_INFO>"); // Replace this with the secret you obtained from the console.

accelerateVerify

Accelerates obtaining a mobile number verification token.

  • We recommend calling this method 2 to 3 seconds before you call the method to get the mobile number verification token. This buffer allows the method to obtain a temporary credential, a process that typically takes 1 to 3 seconds.

  • Avoid calling this method multiple times.

  • Calling this method is unnecessary if you get the mobile number verification token immediately after the application starts.

Parameters

Parameter

Type

Description

timeout

Number

The interface timeout in milliseconds.

callback(args)

Function

Callback function. The structure of the input parameters is as follows:

  • resultCode: A value of 600000 indicates a successful call.

  • msg: A message providing information about the call.

Example

aLiSDKModule.accelerateVerify(5000, (result) => {
  console.log(JSON.stringify(result));
  if ("600000" == result.resultCode) {
    console.log("Successfully accelerated the token acquisition for mobile number verification");
  } else {
    console.log("Failed to accelerate token acquisition for mobile number verification, " + result.msg);
  }
});

getVerifyToken

Gets the token for mobile number verification.

Parameters

Parameter

Type

Description

timeout

Number

The interface timeout in milliseconds.

callback(args)

Function

Callback.The input parameters for the callback have the following structure:

  • resultCode: A value of 600000 indicates a successful call.

  • msg: A message providing information about the call.

  • token: The token for mobile number verification. This parameter is returned only when resultCode is 600000.

Example

aLiSDKModule.getVerifyToken(5000, (result) => {
  if ("600000" == result.resultCode) {
    console.log(
      "Successfully obtained the mobile number verification token. You must now use the mobile number and token to perform verification on your server. The SDK's role in the process is complete."
    );
  }
});

getVersion (Get SDK version)

Parameters:getVersion(callback)

Parameter

Type

Description

callback(version)

Function

The callback function. The input parameter is an object with the following properties:

  • version: The version number of the SDK.

Example

aLiSDKModule.getVersion((version) => {
  console.log("The current SDK version number is: " + version);
});

checkEnvAvailable (Environment check)

Checks whether the current environment supports one-click logon or local number verification.

Parameters:checkEnvAvailable(authType, callback)

Parameter

Type

Description

authType

Number

The type of check to perform:

  • 1: Checks the environment for local number verification.

  • 2: Checks the environment for one-click logon.

callback(args)

Function

The callback function. The input parameter is an object with the following properties:

  • resultCode: 600000 indicates that the method call was successful and the environment supports the specified feature.

  • msg: A message that provides additional information.

Example

aLiSDKModule.checkEnvAvailable(2, (result) => {
  if ("600000" == result.resultCode) {
    uni.navigateTo({
      animationDuration: 300,
      url: "../login/login",
    });
  } else {
    console.log("The current environment does not support one-click logon. result = ", JSON.stringify(result));
    uni.showToast({
      icon: "none",
      title: result.msg,
      duration: 3000,
    });
  }
});

getCurrentCarrierName (Get default SIM carrier)

Parameters:getCurrentCarrierName(callback)

Return value

Type

Description

callback(carrierName)

Function

The callback function. The input parameter is an object with the following properties:

  • carrierName: The name of the current carrier.

    China Mobile: CMCC

    China Unicom: CUCC

    China Telecom: CTCC

Example

aLiSDKModule.getCurrentCarrierName((carrierName) => {
  console.log("The current carrier is: " + carrierName);
});

setCheckboxIsChecked

Parameters

Parameter

Type

Description

isChecked

Boolean

Whether to select the checkbox by default.

  • true: The checkbox is selected.

  • false: The checkbox is not selected.

Example

aLiSDKModule.setCheckboxIsChecked(true)

closePrivactAlertView

Example

aLiSDKModule.closePrivactAlertView()