1、在entry / oh-package.json5目录下,增加依赖:
"dependencies": {'@hw-agconnect/cloud': "^1.0.0",'@hw-agconnect/hmcore': "^1.0.0",'@hw-agconnect/auth-component': "^1.0.0",'long': '^5.2.1'}
整体效果
{"license": "","devDependencies": {},"author": "","name": "entry","description": "Please describe the basic information.","main": "","version": "1.0.0","dependencies": {'@hw-agconnect/cloud': "^1.0.0",'@hw-agconnect/hmcore': "^1.0.0",'@hw-agconnect/auth-component': "^1.0.0",'long': '^5.2.1'}
}
2、初始化、在应用初始化阶段使用context初始化SDK,推荐在EntryAbility的onCreate中进行。
async initSDK(){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));}
3、在界面中首先导入一个包:
import {VerifyCodeAction} from '@hw-agconnect/cloud'
4.1、认证服务,在使用时调用:
sendCode(){if (this.checkPhone('17610176666')){// cloudcloud.auth().requestVerifyCode({action:VerifyCodeAction.REGISTER_LOGIN,//注册登录lang:'zh_CN',sendInterval:60,//多少秒发一次verifyCodeType:{phoneNumber:'17610176666',countryCode:'86',//中国手机号前面+86kind:'phone'}})}}checkPhone(number:string){return !!(number&&new RegExp('^1[3-9]\\d{9}$').test(number))}
4.2、端云存储上传照片
let file=util.generateRandomUUID()+'.jpg'await cloud.storage().upload({localPath:'',//本地地址cloudPath:file})// 下载后的地址let imgUrl=await cloud.storage().getDownloadURL(file)