API reference

更新时间:
复制 MD 格式

This topic describes the APIs required to integrate the Captcha Harmony client.

Utility class APIs

Get the AlicomCaptcha4Client utility class object

For non-singleton implementations, use getClient() to obtain an AlicomCaptcha4Client object and save it for use in a Page or other lifecycles.

public static getClient(context UIContext): AlicomCaptcha4Client

Parameter

Type

Description

context

UIContext

UIContext

Initialize with a custom configuration

Initializes the client with a custom configuration.

public init(captchaId: string, config: AlicomCaptcha4Config): AlicomCaptcha4Client

Parameter

Type

Description

captchaId

string

The verification ID. Pass the appId that is generated after you create an authentication plan on the CAPTCHA Plan Management page in the console.

config

AlicomCaptcha4Config

The parameter configuration object.

Add a listener for verification results

Adds a listener object for verification results. The callback is invoked after the Captcha is loaded and the user completes the verification.

public addOnSuccessListener(response: OnSuccessListener): AlicomCaptcha4Client

Parameter

Type

Description

response

AlicomCaptcha4Client.OnSuccessListener

The listener object for verification results.

Add a listener for Captcha loading failures

Adds a listener object for Captcha loading failures. This is triggered if an error occurs while the Captcha is loading.

public addOnFailureListener(listener: OnFailureListener): AlicomCaptcha4Client

Parameter

Type

Description

listener

AlicomCaptcha4Client.OnFailureListener

The listener object for Captcha loading failures.

Add a listener for successful frontend loading

Add a listener object to receive a callback after the verification frontend is successfully loaded. If preloading is complete, the callback is triggered immediately after you call verifyWithCaptcha.

public addOnWebViewShowListener(listener: OnWebViewShowListener): AlicomCaptcha4Client

Parameter

Type

Description

listener

AlicomCaptcha4Client.OnWebViewShowListener

The listener object for successful frontend loading.

Start the verification flow

Start the verification flow to load the Captcha. You must call init(), addOnSuccessListener(), and addOnFailureListener before you call verifyWithCaptcha(). Otherwise, an error may occur.

public verifyWithCaptcha(): AlicomCaptcha4Client

Cancel the verification

Cancels the validation and closes the validation window. Before you call verifyWithCaptcha(), you must first call init(), addOnSuccessListener(), and addOnFailureListener. Otherwise, an error may occur.

public cancel()

Enable or disable log printing for monitoring

public setLogEnable(enable: boolean): AlicomCaptcha4Client

Destroy long-lifecycle resources

Call this method during the destroy lifecycle of the Captcha interface that the user is using.

public void destroy()

Get the version number

Returns the version number. The type is String.

public static getVersion(): string

Parameter configuration class APIs

Set the request timeout period

setTimeOut(timeOut: number): AlicomCaptcha4Config

Parameter

Type

Description

timeOut

number

The request timeout period. Unit: ms. The default value is 10000.

Set the resource address for WebView loading

setResourcePath(html: string): AlicomCaptcha4Config

Parameter

Type

Description

html

string

The address to load in the webView.

Control the language of the loaded WebView

Specifies the language setting to pass to the JS. This interface controls only the language of the loaded webView. The SDK's multilingual support follows the language of the current application. If you do not set this parameter or set it to null, the default language short code of the application is sent to the frontend.

setLanguage(language: string): AlicomCaptcha4Config

Parameter

Type

Description

language

string

The supported language short code.

The supported languages and their short codes are listed below.

Language

Language

Short code

Simplified Chinese

Chinese(Simplified)

zho

Traditional Chinese (hk)

Chinese(Hong Kong)

zho-hk

Traditional Chinese (tw)

Chinese(Taiwan)

zho-tw

American English

English

eng

British English

English

eng-gb

Japanese

Japanese

jpn

Indonesia

Indonesian

ind

Korean

Korean

kor

Russian

Russian

rus

Arabic

Arabic

ara

Spanish

Spanish

spa

French

French

fra

German

German

deu

Uyghur

Uyghur

udm

Brazilian Portuguese

Portuguese(Brazil)

pon

European Portuguese

Portuguese(Europe)

por

Set whether the window disappears when the gray area is clicked

setIsCanceledOnTouchOutside(isCanceledOnTouchOutside: boolean): AlicomCaptcha4Config

Parameter

Type

Description

isCanceledOnTouchOutside

boolean

Specifies whether the window disappears when the gray area is clicked.

  • true (default): The window disappears.

  • false: The item remains.

Set whether to enable debug mode

Specifies whether to enable debug mode. When you publish the application, you must set this to false or leave it unconfigured.

setIsDebug(isDebug: boolean): AlicomCaptcha4Config

Parameter

Type

Description

Debug

boolean

Specifies whether to enable debug mode.

  • true: Enables debug mode.

  • false (default): No

Set the WebView background color

Setting the webView background color controls the background color of the Harmony webView control, which is different from the background color of the frontend JS.

setBackgroundColor(backgroundColor: string): AlicomCaptcha4Config

Parameter

Type

Description

backgroundColor

string

The color resource.

Configure extra parameters - setParams(Map<string, Object>)

Additional parameter settings. These parameters are passed directly to the JS.

setParams(params: Map<string, Object>): AlicomCaptcha4Config

Parameter

Type

Description

params

JSONObject

Additional parameter settings. The parameters are passed to JS.

The supported key-value pairs are listed below.

Value

Type

Description

displayMode

number

The theme style. Valid values:

  • 0 (default): Follows the system setting.

  • 1: Normal

  • 2: Dark

protocol

string

The protocol prefix. You can use HTTPS:// or HTTP://. The default is HTTPS://.

bgColor

string

The color of the verification background mask. The value must be a CSS-compliant color code, where the last two digits represent the transparency. Default value: #00000000.

loading

string

The relative path of the resource for a custom loading effect when the WebView loads an HTML file. An empty string cancels the loading effect.

rem

number

Sets the overall scaling ratio of the Captcha.

nextWidth

string

The width of the Captcha pop-up window. You must include the unit, such as "300px".

riskType

string

If risk control integration is configured on the server-side, this field can specify the verification form.

Important

The risk control integration mode is not yet available. Calling this parameter does not take effect.

hideSuccess

boolean

Hides the success pop-up window.

Sample code:

let params = new Map<string, string>();
params.set("displayMode", 0);
params.set("protocol", "https://");
params.set("bgColor", "#CC0000FF");
...
let AlicomCaptcha4Config = new AlicomCaptcha4Config()
        .setParams(params);

Callback listener class APIs

Verification API callback

AlicomCaptcha4Client#OnSuccessListener: A callback listener class for verification results.

onSuccess: (status: boolean, response: string) => void;

Parameter

Type

Description

status

boolean

The result of the user's verification operation.

  • true: Success.

  • false: Failure. If the verification fails, the Captcha style is refreshed.

response

string

After a user action is successfully validated, you need to obtain the response data and send it to the server-side for validation.

Example of a successful verification:

{
  "lot_number":"b150e61198924eff9eaae121e9b67aa8",
  "pass_token":"6fcfba4c35cb57b873c99c5b3b02089c58a26244c18f7706aaa1e87aed3aa8f4",
  "gen_time":"1622020720",
  "captcha_output":"Tu6rW0dRNO0NLxMKftwdH00G5whF5HHddthoXSEA1G4="
}

Example of a failed verification:

{
  "captchaId":"647f5ed2ed8acb4be36784e01556bb71",
  "captchaType":"slide",
  "challenge":"95ed53b4-95c6-4c6b-9aac-769b29ef74a0"
}

Loading failure callback

AlicomCaptcha4Client#OnFailureListener: A callback listener class for load failures.

onFailure: (error: string) => void;

Parameter

Type

Description

error

string

The description of the verification loading failure. This includes cancellations by the user.

Example of a loading failure:

{
  "code":"-14460",
  "msg":"Verification session canceled",
  "desc":
       {"description":"User cancelled'Captcha'"}
}

Successful frontend loading

AlicomCaptcha4Client#OnWebViewShowListener: A listener class for the callback that is triggered when the frontend is loaded successfully.

This callback is invoked after the verification frontend is successfully loaded.

onWebViewShow: () => void;