鸿蒙验证码封装


  sendCode(){
    if (this.checkPhone(this.phone)){
      CloudUserManager.sendVerifyCode(this.phone,()=>{
        Toast.show('验证码发送成功')
      })
    }
  }

CloudUserManager:


import cloud, { AGCAuthError, AuthUser } from '@hw-agconnect/cloud'
import {VerifyCodeAction} from '@hw-agconnect/cloud'
import { Toast } from '../utils/system'
import { error } from '@hw-agconnect/hmcore/src/main/ets/error/CoreErrorCode'
import { AGCError } from '@hw-agconnect/hmcore'

export class CloudUserManager{


   static sendVerifyCode(number:string,event: () => void){
    //      Auth.requestVerifyCode
    //      Auth.requestVerifyCode//申请手机登录验证码。
    cloud.auth().requestVerifyCode({
      action:VerifyCodeAction.REGISTER_LOGIN,
      lang:'zh_CN',
      sendInterval:60,//多少秒发一次

      verifyCodeType:{
        phoneNumber:number,
        countryCode:'86',//中国手机号前面+86
        kind:'phone'
      }
    }).then(verifyCodeResult => {
      //验证码申请成功
      event()
    }).catch(()=>{
      Toast.show(error.toString())
    })
  }

  /**
   * 暂时有问题
   * */
  static createUser(number:string,code:string,event:()=>void){

    cloud.auth().createUser({
      kind: 'phone',
      countryCode: '86',
      phoneNumber:number,
      password: '123456',//可以给用户设置初始密码,后续可以用密码来登录
      verifyCode: code
    }).then(()=>{
      event()
    }).catch(()=>{

    })

  }

  static loginWithCode(number:string,code:string){

    cloud.auth().signIn({
      credentialInfo: {
        kind: 'phone',
        phoneNumber: number,
        countryCode: '86',
        verifyCode: code
      }
    }).then(user => {
      //登录成功
      Toast.show('登录成功')
    }).catch(() => {
      //登录失败
      Toast.show('登录失败')
    });
  }

  static isLoginCloud(event:(user:AuthUser)=>void){
    cloud.auth().getCurrentUser().then(user=>{
      if(user){
        //业务逻辑
        event(user)
      }
    });
  }

  static getUserInfo(event?:(user:AuthUser)=>void){
    let res=cloud.auth().getCurrentUser()
    if (res!=null) {
      console.log('23')
    }
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jian11058

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

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

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

打赏作者

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

抵扣说明:

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

余额充值