code.vue 3.59 KB
<template>
  <view class="f-page">
    <view class="f-title">{{ t('rules.codeY') }}</view>
    <view class="f-desc">{{ t('app.resetPassword.sendCodeTips').replace('{value}',verifyText) }}</view>

    <uv-code-input v-model="code" space="88rpx" color="#202328" mode="line" :hairline="true" size="96rpx"
      fontSize="44rpx" :maxlength="4" :bold="true" @finish="handleFinishVerify" />

      <view class="re-send" :class="{'disabled-send':waitTime!==0}" @tap="handleReSendMsg">
      {{ t('app.resetPassword.reSendCode') }}
      <text v-if="waitTime!==0">
        ({{waitTime}}s)
        </text>
        </view>
    <!-- <uv-input 
      placeholder="请输入用户名"
      placeholder-style="font-size: 30rpx;color: #656a72;font-weight:400"
      custom-style="padding: 0;height: 96rpx;font-size:30rpx;padding-left:32rpx;border: 2rpx solid #E1E4EA;border-radius: 16rpx"
      v-model.trim="loginName"
      @change="change"
    /> -->
  </view>
</template>
<script setup>
  import {
    ref
  } from 'vue'
  import {
    onLoad,
    onShow,
    onHide
  } from '@dcloudio/uni-app'
  import{
    sendVerifyCodeApi,
    checkVerifyCodeApi
  } from '@/api'
  import { t } from '@/plugins/index.js'

  // 获取用户名
  const verifyInfo = ref({})
  const verifyText = ref('')
  onLoad((options) => {
    if (options.loginName) {
      verifyInfo.value = {
        ...options
      }
      verifyText.value = (options.selectType === 'email' ? t('maintenance.monitor.panel.email') : t('format.phoneNumber')) + options.val
    }
  })

  // 发送验证码
  const waitTime = ref(60)
  const timer = ref(null)

  onShow(() => {
    clearInterval(timer.value)
    timer.value = setInterval(() => {
      if (waitTime.value) {
        waitTime.value -= 1
      } else {
        // 结束计时
        clearInterval(timer)
      }
    }, 1000)
  })
  
  onHide(() => {
    clearInterval(timer.value)
  })
  // 重新发送验证码
  const handleReSendMsg = async () =>{
    try{
      if(waitTime.value){
        return false
      }
      const res = await sendVerifyCodeApi({
        unid: verifyInfo.value.unid,
        verifyType: 'email'
      })
      if(res.code === 200){
        waitTime.value = 60
        uni.showToast({
          title: t('table.sendSuccess')
        })
      }
    }catch(e){
      console.log(e);
    }
  }
  
  const code = ref('')
  const handleFinishVerify = async ()=>{
    try{
      const res = await checkVerifyCodeApi({
        loginName: verifyInfo.value.loginName,
        code:code.value
      })
      if(res.code === 200){
        // 跳转到重置密码页面
        uni.redirectTo({
          url:`/subPackages/accountGroup/pages/login/forget/reset?loginName=${verifyInfo.value.loginName}&code=${res.data}`
        })
      }
    }catch(e){
      console.log(e);
    }
  }


  const loginName = ref('')

  const goNext = () => {
    if (!loginName.value) {
      uni.showToast({
        title: t('rules.loginName')
      })
      return
    }
    uni.navigateTo({
      url: '/subPackages/accountGroup/pages/login/forget/verify?loginName=' + loginName.value
    })
  }
</script>
<style lang="scss" scoped>
  .f-page {
    padding: 64rpx 50rpx 0;

    .f-title {
      font-weight: bold;
      font-size: 46rpx;
      color: #202328;
      line-height: 64rpx;
    }

    .f-desc {
      font-weight: 400;
      font-size: 28rpx;
      color: #90949D;
      line-height: 40rpx;
      margin: 16rpx 0 80rpx;
    }
    .re-send{
      width: 100%;
      text-align: right;
      margin-top: 52rpx;
      font-size: 26rpx;
      color: #387CF5;
      line-height: 36rpx;
    }
    .disabled-send{
      color: #90949D;
    }
  }
</style>