
import {smsCodeSend,emailCodeSend} from '@/api/auth.js'
import {ElMessage} from 'element-plus'


/**
 * 
 * @param {*} param -调用发送验证码接口的参数
 * @param {*} type -类型 1-邮箱 2-手机号码
 * @param {*} codeInfo -倒计时相关信息 timeout-倒计时 isRequesting-是否发送了验证码
 * @param {*} time -总共几秒倒计时 默认一分钟
 */
export const sendVerificationCode=async (param,type,codeInfo,time=60)=>{
  let codeTimer=null
  let resp={}
  if(type==1){
    // 邮箱
    resp=await emailCodeSend(param)
  }else{
    // 短信验证码
    resp=await smsCodeSend(param)
  }

  if(resp.code==200){
    clearInterval(codeTimer)
    codeInfo.timeout=time
    codeInfo.isRequesting=true
    codeInfo.timeout--
    codeTimer=setInterval(()=>{
      if(codeInfo.timeout==1){
        codeInfo.isRequesting=false
        clearInterval(codeTimer)
      }
      codeInfo.timeout--
    },1000)
    ElMessage.success('Verification code sent')
  }
}