鸿蒙验证码封装


  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')
    }
  }

}

你可能感兴趣的:(harmonyos,华为)