HarmonyOS–AGC(认证服务/云函数/云存储/云数据库)
文章目录
一、注册华为账号开通认证服务
二、添加项目:*包名要与项目的包名保持一致
三、获取需要的文件
四、创建项目:*包名要与项目的包名保持一致
五、添加json文件
六、加入请求权限
在module.json5文件中添加:"requestPermissions": [{
"name": "ohos.permission.INTERNET"}],
七、加入依赖
// 这三类可以整合为一个,统一使用这几个依赖(合起来的,下面单独分开的)
"dependencies": {
"@hw-agconnect/crypto-ohos": "^1.0.10",
"@hw-agconnect/function-ohos": "^1.1.2",
"@hw-agconnect/auth-ohos": "^1.1.3",
"@hw-agconnect/cloudstorage-ohos": "^1.0.10",
"@hw-agconnect/api-ohos": "^1.1.2",
"@hw-agconnect/base-ohos": "^1.1.2",
"@hw-agconnect/core-ohos": "^1.1.2",
"@hw-agconnect/credential-ohos": "^1.1.2",
"@ohos/agconnect-auth-component": "^1.0.5",
"@hw-agconnect/auth-component": "^1.0.0",
"@hw-agconnect/cloud": "^1.0.1",
"@hw-agconnect/hmcore": "^1.0.1",
"long": "5.2.1"
}
// 认证服务
"dependencies": {
"@hw-agconnect/hmcore": "^1.0.1",
"@hw-agconnect/cloud": "^1.0.1",
"@hw-agconnect/auth-ohos": "^1.1.3"
}
// 下面的是端云项目依赖使用的
"dependencies": {
"@hw-agconnect/crypto-ohos": "^1.0.10",
"@hw-agconnect/function-ohos": "^1.0.10",
"@hw-agconnect/auth-ohos": "^1.0.10",
"@hw-agconnect/cloudstorage-ohos": "^1.0.10",
"@hw-agconnect/api-ohos": "^1.0.10",
"@hw-agconnect/base-ohos": "^1.0.10",
"@hw-agconnect/core-ohos": "^1.0.10",
"@hw-agconnect/credential-ohos": "^1.0.10",
"@ohos/agconnect-auth-component": "^1.0.5",
"@hw-agconnect/auth-component": "^1.0.0",
"@hw-agconnect/cloud": "^1.0.0",
"@hw-agconnect/hmcore": "^1.0.0",
"long": "5.2.1"
}
// 云函数使用
"dependencies": {
"@hw-agconnect/api-ohos": "^1.1.2",
"@hw-agconnect/core-ohos": "^1.1.2",
"@hw-agconnect/function-ohos": "^1.1.2",
"@hw-agconnect/credential-ohos": "^1.1.2",
"@hw-agconnect/base-ohos": "^1.1.2",
"@hw-agconnect/cloud": "^1.0.0",
"@hw-agconnect/hmcore": "^1.0.1"
}
// 云存储
"dependencies": {
"@hw-agconnect/hmcore": "^1.0.1",
"@hw-agconnect/cloud": "^1.0.1"
}
八、修改构建配置文件:build-profile.json5
{
"apiType": "stageMode",
"showInServiceCenter": true, // 新添加
"buildOption": {
// 新添加:内容
//配置筛选har依赖.so资源文件的过滤规则
"napiLibFilterOption": {
//按照.so文件的优先级顺序,打包最高优先级的.so文件
"pickFirsts": [
"**/1.so"
],
//按照.so文件的优先级顺序,打包最低优先级的.so 文件
"pickLasts": [
"**/2.so"
],
//排除的.so文件
"excludes": [
"**/3.so"
],
//允许当.so重名冲突时,使用高优先级的.so文件覆盖低优先级的.so文件
"enableOverride": true,
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}
九、集成SDK
导包:
import {
initialize } from '@hw-agconnect/hmcore';
// 方式一(不推荐)
导包: import {
initialize } from '@hw-agconnect/hmcore';
async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
// 根据管理读取文件,根据信息读取里面的数据
try {
let input = await this.context.resourceManager
.getRawFileContent('agconnect-services.json')
let jsonString = util.TextDecoder.create('utf-8',
{
ignoreBOM: true
}).decodeWithStream(input, {
stream: false
});
initialize(this.context, JSON.parse(jsonString));
console.info('system===>SDK集成成功')
} catch (e) {
console.info('system===>SDK集成失败')
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
}
方式二: // 直接获取json 文件
导包: import {
initialize } from '@hw-agconnect/hmcore';
import json from '../../resources/rawfile/agconnect-services.json'
onCreate(want, launchParam) {
// 方式二:引入
try {
initialize(this.context,json );
console.info('system===>SDK集成成功')
} catch (err){
console.info('system===>SDK集成失败')
}
}
十、手机号认证–工具包
import cloud, {
AuthUser, SignInResult, VerifyCodeAction, VerifyCodeResult } from '@hw-agconnect/cloud';
// 手机验证
// class PhoneAgcUtils.ets
/**
* 获取当前登录用户
* */
export function getCurrentUser(): Promise<AuthUser | null> {
return cloud.auth().getCurrentUser()
}
/*
* 获取验证码(登录设置使用,并需要传参使用)
* REGISTER_LOGIN: 登录相关使用
* RESET_PASSWORD: 修改密码相关使用
* */
export function phoneGetVerifyCode(action: VerifyCodeAction, phoneNumber: string): Promise<VerifyCodeResult> {
return cloud.auth().requestVerifyCode({
action: action,
lang: `zh_CN`,
sendInterval: 60,
verifyCodeType: {
phoneNumber: phoneNumber,
countryCode: '86',
kind: `phone`
}
})
}
/*
* 验证码登录
* */
export async function phoneSignInByVerifyCode(phoneNumber: string, verifyCode: string, autoCreateUser: boolean = false): Promise<SignInResult> {
// 登录之前要先登出
await logOut()
return cloud.auth().signIn({
autoCreateUser: autoCreateUser, // 自己创建用户
credentialInfo: {
kind: 'phone',
countryCode: '86', // 区号
phoneNumber: phoneNumber,
verifyCode: verifyCode
}
})
}
/*
* 密码登录
* */
export async function phoneSignInByPassWord(phoneNumber: string, password: string, autoCreateUser: boolean = false): Promise<SignInResult> {
// 登录之前要先登出
await logOut()
return cloud.auth().signIn({
autoCreateUser: autoCreateUser, // 自己创建用户
credentialInfo: {
kind: 'phone',
countryCode: '86', // 区号
phoneNumber: phoneNumber,
password: password
}
})
}
/*
* 验证码和密码登录(全填)
* */
export async function phoneSignIn(phoneNumber: string, password: string, verifyCode: string, autoCreateUser: boolean = false): Promise<SignInResult> {
// 登录之前要先登出
await logOut()
return cloud.auth().signIn({
autoCreateUser: autoCreateUser, // 自己创建用户
credentialInfo: {
kind: 'phone',
countryCode: '86', // 区号
phoneNumber: phoneNumber,
password: password,
verifyCode: verifyCode
}
})
}
/*
* 注册用户
* */
export function phoneCreateUser(phoneNumber: