鸿蒙HarmonyOS SDK
用户账号
手机和邮箱账号

手机和邮箱账号

手机短信图形验证需要 @gizwits/smart-sdk 1.0.4 或更高版本。

手机验证码登录

await userManager.sendMobileLoginVerifyCode('+86', phone, 'zh-CN');
await userManager.loginWithMobile('+86', phone, verificationCode);

邮箱验证码登录

await userManager.sendEmailLoginVerifyCode(email, 'zh-CN');
await userManager.loginWithEmail(email, verificationCode);

手机号注册

await userManager.sendMobileRegisterVerifyCode('+86', phone, 'zh-CN');
const mobileToken = await userManager.registerWithMobile(
  '+86',
  phone,
  verificationCode,
  password
);
userManager.updateAuthorizeData(mobileToken);

registerWithPhone(phoneCode, phone, password, verificationCode) 是参数顺序不同的兼容接口。

手机短信图形验证

发送登录、注册或找回密码短信验证码时,服务端可能要求用户先完成图形验证。首次调用保持不变;需要图形验证时,SDK 会抛出 GizCaptchaRequiredError。

import {
  GizCaptchaInfo,
  GizCaptchaInput,
  GizCaptchaRequiredError,
} from '@gizwits/smart-sdk';
 
let captchaInfo: GizCaptchaInfo | undefined = undefined;
 
try {
  await userManager.sendMobileLoginVerifyCode('+86', phone, 'zh-CN');
} catch (error) {
  if (error instanceof GizCaptchaRequiredError) {
    captchaInfo = error.captcha;
    // 将 captchaInfo.captchaImage 作为 JPEG Base64 图片展示给用户。
  } else {
    throw error;
  }
}

用户输入图片字符后,使用同一个验证码标识重试发送。appVersion 传入接入应用当前的 versionName:

if (captchaInfo !== undefined) {
  const captchaInput = new GizCaptchaInput(
    captchaInfo.captchaId,
    userEnteredCode,
    appVersion
  );
  await userManager.sendMobileLoginVerifyCode('+86', phone, 'zh-CN', captchaInput);
}

注册和找回密码分别通过 sendMobileRegisterVerifyCode() 与 sendMobileForgotPasswordVerifyCode() 的第四个参数传入 GizCaptchaInput。需要更换图片时,调用 getMobileCaptcha();如果重试后再次收到 GizCaptchaRequiredError,请使用异常中返回的新图片继续验证。

邮箱注册

await userManager.sendEmailRegisterVerifyCode(email, 'zh-CN');
const emailToken = await userManager.registerWithEmail(email, verificationCode, password);
userManager.updateAuthorizeData(emailToken);

验证码发送和校验可能受频率限制。请捕获 Promise 异常并转换为适合的 ArkUI 提示。