背景
华为云服务提供了统一认证的云服务,支持手机、邮箱等自定义登录服务,并且提供了免费使用的额度,这样子方便中小企业或者项目快速的开发工作。下面是支持的认证方式:

操作步骤
1.AGC(AppGallery Connect)创建项目
在AGC界面创建自己的云服务项目(详细可看上篇文章【HarmonyOS】端云一体化初始化项目),并开通认证服务,如下图:

启用手机号码和邮箱地址服务:

2.添加项目配置文件
在AGC的项目界面下载agconnect-services.json文件,并添加到本地的项目文件中。


3.添加与云服务相关的第三方库
- 在项目的终端中,输入 cd entry 进入entry目录
- 安装SDK
可以在entry->oh-package.json5文件中可以查看添加的第三方库和对应的版本号,可以看到添加了 @hw-agconnect/hmcore和 @hw-agconnect/cloud两个第三方库。

示例使用手机验证码实现登录功能
验证码的操作示意图

1.搭建初始化界面

2.云服务认证使用初始化
修改EntryAbility文件代码进行初始化
- 添加import { initialize } from ‘@hw-agconnect/hmcore’
- 添加配置的JSON文件路径 :import serciceJson from ‘…/…/resources/rawfile/agconnect-services.json’
- 在OnCreate方法中添加初始化代码
修改示意图:

整体代码如下:
import { abilityAccessCtrl, AbilityConstant, PermissionRequestResult, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { initialize } from '@hw-agconnect/hmcore'
import serciceJson from '../../resources/rawfile/agconnect-services.json'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
try {
initialize(this.context, serciceJson);
} catch (e) {
hilog.error(0x0000, 'AGConnectError', JSON.stringify(e));
}
let AtManager = abilityAccessCtrl.createAtManager();
AtManager.requestPermissionsFromUser(this.context, ['ohos.permission.READ_MEDIA', 'ohos.permission.MEDIA_LOCATION'])
.then((data: PermissionRequestResult) => {
hilog.info(0x0000, 'testTag', '%{public}s', 'request permissions from user success' + data);
})
.catch((err: Object) => {
hilog.error(0x0000, 'testTag', 'Failed to request permissions from user. Cause: %{public}s',
JSON.stringify(err) ?? '');
});
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}
onWindowStageDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
发送验证码
输入手机号码后点击获取验证码按钮,然后获取登录的验证码短信。短信截图如下:

完整代码:
主要调用requestVerifyCode方法去获取验证码信息,传入VerifyCodeParam类型对象。
VerifyCodeParam的属性解析:
- verifyCodeType :PhoneVerifyCode对象,主要输入phoneNumber和countryCode对象。
- action : 枚举值,选择获取验证码的类型
- lang : 语言
- sendInterval : 重复发送的时间间隔
根据验证码实现登录功能
实现登录效果

主要调用auth的signIn方法,并传入对应的电话号码和验证码。
SignInParam对象
- credentialInfo: CredentialInfo对象,设置电话号码和前缀,可以选择密码登录或者验证码登录
- autoCreateUser?: boolean; 设置当没有当前对象存在时,是否需要自动生成用户。
总结
以上示例仅仅对统一认证流程的讲解,其中完整的逻辑判断并没有添加,可以根据自己的需求来添加相应的逻辑判断