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

// 云存储
"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: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大众筹码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值