HarmonyOS--AGC(认证服务/云函数/云存储/云数据库)

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"
  }

在这里插入图片描述

八、修改构建配置文件: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: string, password: string, verifyCode: string): Promise<SignInResult> {
  return cloud.auth().createUser({
    kind: 'phone',
    phoneNumber: phoneNumber, // 手机号
    countryCode: '86',
    password: password, // 确认密码
    verifyCode: verifyCode // 验证码
  })
}


/*
 * 修改密码
 * 其中user: cloud.auth().getCurrentUser()的结果
 * */
export function phoneUpdatePassword(user: AuthUser, password: string, verifyCode: string): Promise<void> {
  return user.updatePassword(
    {
      password: password,
      providerType: 'phone',
      verifyCode: verifyCode
    })
}


/*
 * 重置密码/忘记密码
 * */
export function phoneResetPassword(phoneNumber: string, password: string, verifyCode: string): Promise<void> {
  return cloud.auth().resetPassword({
    kind: 'phone',
    countryCode: '86',
    phoneNumber: phoneNumber,
    password: password,
    verifyCode: verifyCode
  })
}

/*
 * 登出
 * */
export function logOut(): Promise<void> {
  return cloud.auth().signOut()
}


/**
 * 销户
 */
export function deleteUser(): Promise<void> {
  return cloud.auth().deleteUser()
}

/**
 * 修改手机号
 */
export function phoneUpdatePhone(user: AuthUser, newPhone: string, newVerifyCode: string): Promise<void> {
  return user.updatePhone({
    phoneNumber: newPhone,
    countryCode: '86',
    verifyCode: newVerifyCode,
    lang: 'zh_CN'
  })
}

/**
 * 账号重认证(密码认证)
 * 报:203818081 则需要重认证
 */
export function phoneUserReauthenticateByPassWord(user: AuthUser, passWord: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'phone',
      countryCode: '86',
      phoneNumber: user.getPhone().replace('+86-', ''),
      password: passWord
      // verifyCode?: string;
    }
  })
}


/**
 * 账号重认证(验证码认证)
 * 报:203818081 则需要重认证
 */
export function phoneUserReauthenticateByVerifyCode(user: AuthUser, verifyCode: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'phone',
      countryCode: '86',
      phoneNumber: user.getPhone().replace('+86-', ''),
      // password: passWord
      verifyCode: verifyCode
    }
  })
}

/**
 * 账号重认证(全)
 * 报:203818081 则需要重认证
 */
export function phoneUserReauthenticateByAll(user: AuthUser, passWord: string, verifyCode: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'phone',
      countryCode: '86',
      phoneNumber: user.getPhone().replace('+86-', ''),
      password: passWord,
      verifyCode: verifyCode
    }
  })
}



/**
 * 匿名/手机关联邮箱(验证码方式)
 * 可以使用密码--暂未封装函数
 * 关联用户或取消关联都必须限定时间在5分钟以内,超时需重新登录或重认证
 *
 * 手机不可以关联手机
 * 匿名也是转为实名的操作
 */
export function phoneLink(authUser: AuthUser,email:string,emailVerifyCode:string):Promise<SignInResult> {
  return authUser.link({
    kind: 'email',
    // password: password, // 给这个关联设置的登录密码
    email: email,
    verifyCode: emailVerifyCode
  })
}

/**
 * 手机取消关联邮箱
 * 关联用户或取消关联都必须限定时间在5分钟以内,超时需重新登录或重认证
 */
export function phoneUnLink(authUser: AuthUser):Promise<SignInResult> {
  return authUser.unlink('email')
}


/***
 * 匿名登录
 */
export function anonymous(): Promise<SignInResult> {
  return cloud.auth().signInAnonymously()
}

//提示信息
export function messageInfo(string: string) {
  console.info('system===>', string)
}

export function messageError(string: string) {
  console.error('system===>', string)
}

十一、邮箱认证–工具包

import cloud, { AuthUser, SignInResult, VerifyCodeAction, VerifyCodeResult } from '@hw-agconnect/cloud';
// 邮箱验证
// class EmailAgcUtils.ets
/**
 * 获取当前登录用户
 * */
export function getCurrentUser(): Promise<AuthUser | null> {
  return cloud.auth().getCurrentUser()
}

/*
 * 获取验证码(登录设置使用,并需要传参使用)
 * REGISTER_LOGIN: 登录相关使用/注册
 * RESET_PASSWORD: 修改密码相关使用
 * */
export function emailGetVerifyCode(action: VerifyCodeAction, email: string): Promise<VerifyCodeResult> {
  return cloud.auth().requestVerifyCode({
    action: action,
    lang: `zh_CN`,
    sendInterval: 60,
    verifyCodeType: {
      kind: 'email',
      email: email
    }
  })
}

/*
 * 验证码登录
 * */
export async function emailSignInByVerifyCode(email: string, verifyCode: string, autoCreateUser: boolean = false): Promise<SignInResult> {
  // 登录之前要先登出
  await logOut()
  return cloud.auth().signIn({
    autoCreateUser: autoCreateUser, // 自己创建用户
    credentialInfo: {
      kind: 'email',
      email: email,
      verifyCode: verifyCode
    }
  })
}


/*
 * 密码登录
 * */
export async function emailSignInByPassWord(email: string, password: string, autoCreateUser: boolean = false): Promise<SignInResult> {
  // 登录之前要先登出
  await logOut()
  return cloud.auth().signIn({
    autoCreateUser: autoCreateUser, // 自己创建用户
    credentialInfo: {
      kind: 'email',
      email: email,
      password: password
    }
  })
}


/*
 * 验证码和密码登录(全填)
 * */
export async function emailSignIn(email: string, password: string, verifyCode: string, autoCreateUser: boolean = false): Promise<SignInResult> {
  // 登录之前要先登出
  await logOut()
  return cloud.auth().signIn({
    autoCreateUser: autoCreateUser, // 自己创建用户
    credentialInfo: {
      kind: 'email',
      email: email,
      password: password,
      verifyCode: verifyCode
    }
  })
}


/*
 * 注册用户
 * */
export function emailUCreateUser(email: string, password: string, verifyCode: string): Promise<SignInResult> {
  return cloud.auth().createUser({
    kind: 'email',
    email: email,
    password: password, // 确认密码
    verifyCode: verifyCode // 验证码
  })
}


/*
 * 修改密码
 * 其中user: cloud.auth().getCurrentUser()的结果
 * */
export function emailUpdatePassword(user: AuthUser, password: string, verifyCode: string): Promise<void> {
  return user.updatePassword(
    {
      password: password,
      providerType: 'email',
      verifyCode: verifyCode
    })
}


/*
 * 重置密码/忘记密码
 * */
export function emailUResetPassword(email: string, password: string, verifyCode: string): Promise<void> {
  return cloud.auth().resetPassword({
    kind: 'email',
    email: email,
    password: password,
    verifyCode: verifyCode
  })
}


/*
 * 登出
 * */
export function logOut(): Promise<void> {
  return cloud.auth().signOut()
}


/**
 * 销户
 */
export function deleteUser(): Promise<void> {
  return cloud.auth().deleteUser()
}

/**
 * 修改邮箱
 */
export function emailUpdateEmail(user: AuthUser, newEmail: string, newVerifyCode: string): Promise<void> {
  return user.updateEmail({
    email: newEmail,
    verifyCode: newVerifyCode,
    lang: 'zh_CN'
  })
}

/**
 * 账号重认证(密码认证)
 */
export function emailUserReauthenticateByPassWord(user: AuthUser, passWord: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'email',
      email: user.getEmail(),
      password: passWord
      // verifyCode?: string;
    }
  })
}


/**
 * 账号重认证(验证码认证)
 */
export function emailUserReauthenticateByVerifyCode(user: AuthUser, verifyCode: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'email',
      email: user.getEmail(),
      // password: passWord
      verifyCode: verifyCode
    }
  })
}

/**
 * 账号重认证(全)
 */
export function emailUserReauthenticateByAll(user: AuthUser, passWord: string, verifyCode: string): Promise<SignInResult> {
  return user.userReauthenticate({
    credentialInfo: {
      kind: 'email',
      email: user.getEmail(),
      password: passWord,
      verifyCode: verifyCode
    }
  })
}

/**
 *匿名/邮箱关联手机(验证码方式)
 * 可以使用密码--暂未封装函数
 *
 * 邮箱不可以关联邮箱
 * 匿名也是转为实名的操作
 */
export function emailLink(authUser: AuthUser,phoneNumber:string,phoneVerifyCode:string):Promise<SignInResult> {
  return authUser.link({
    kind: 'phone',
    countryCode: '86',
    phoneNumber: phoneNumber,
    // password?: string; // 给这个关联设置的登录密码
    verifyCode:phoneVerifyCode
  })
}

/**
 * 邮箱取消关联手机
 */
export function emailUnLink(authUser: AuthUser):Promise<SignInResult> {
  return authUser.unlink('phone')
}

//提示信息
export function messageEmailInfo(string: string) {
  console.info('system===>', string)
}

export function messageEmailError(string: string) {
  console.error('system===>', string)
}

十二、匿名认证–工具包

/***
 * 匿名登录
 */
export function anonymous(): Promise<SignInResult> {
  return cloud.auth().signInAnonymously()
}

十三、认证服务FAQ

问题:{"code":203817988,"message":"third provider is disabled"}
查看服务配置,相关验证是否启动。

在这里插入图片描述

······················································ ······················································ ······················································ ······················································ ······················································ ······················································ ······················································ ······················································

十四、云函数

操作步骤

1.AGC中开启云函数
2.创建函数
3.在函数代码---代码文件中编辑并提交,格式见图片:(注意判断)
4.在IDE工具中进行调用此方法,见代码:在开发前要准备环境(认证服务一致):
		1.json文件的导入
		2.依赖
		3.构建文件
		4.集成SDK

在这里插入图片描述

在这里插入图片描述

导包:import cloud from '@hw-agconnect/cloud'
  async fun_sum() {
    const rep = await cloud.callFunction({
      name: 'fun-jisuan',  // 函数名
      version: '$latest', // 版本
      params: { // 参数
        "num1": 3,
        "num2": 3
      }
    })
    console.log('hmlog-->', JSON.stringify(rep.getValue()))
    this.result1 = rep.getValue().result + '';
  }
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大众筹码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值