authPhoneDialog.vue 4.49 KB
<template>
  <baseDialog
    :is-show.sync="dialogShow"
    :dialog-config="dialogConfig.config"
    class="detail-content-warn"
    @handleClick="submit"
  >
    <el-form
      class="form"
      label-width="150px"
      size="small"
    >
      <el-form-item
        label="手机号:"
        required
        :error="phoneError"
      >
        <el-input
          v-model="phone"
          class="input-width"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item
        label="验证码:"
        required
        :error="smsError"
      >
        <div class="sms-code flex">
          <el-input
            v-model="smsCode"
            class="input-width"
            :maxlength="6"
            placeholder="请输入验证码"
            @input="handleInput"
          >
            clearable
            >
          </el-input>
          <el-button
            ref="smsCodeBtn"
            type="primary"
            :plain="countDownStatus"
            :disabled="countDownStatus"
            class="get-sms-code"
            @click="getSmsCode"
          >{{smsBtnText}}</el-button>
        </div>
      </el-form-item>
    </el-form>
  </basedialog>
</template>

<script>
import { sendSmsCode, verifySmsCode } from "@api/usedCarManagement"
export default {
  name: "AuthPhoneDialog",
  inject: ['userInfo'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
  },
  computed: {
    dialogShow: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('dialogClose')
      }
    },
    phone () {
      return this.userInfo.loginAccount.phone || ''
    }
  },
  watch: {
    visible: {
      handler (val) {
        if (val) {
          this.phoneError = ''
          this.smsError = ''
          this.smsCode = ''
        }
      },
      immediate: true,
      deep: true
    }
  },

  mounted () {
  },
  data () {
    return {
      phoneError: '',
      smsError: '',
      smsBtnText: '获取验证码',
      count: 60,
      smsCode: '',
      countDownStatus: false,
      dialogConfig: {
        config: {
          title: "短信验证",
          width: "640px"
        }
      },
    }
  },
  methods: {
    handleInput (value) {
      this.smsCode = value.replace(/\D/g, '')
    },
    async getSmsCode (e) {
      try {
        let { phone } = this
        if (!phone) return this.phoneError = '手机号不能为空'
        if (!/^1[3456789]\d{9}$/.test(phone)) return this.phoneError = '手机号格式错误'
        this.countDownStatus = true
        let data = await sendSmsCode({
          phone
        })
        if (data) {
          this.countDown()
          let target = e.target
          if (target.nodeName == 'SPAN') target = e.target.parentNode
          target.blur()
          this.notice.okTips('发送短信成功')
        } else {
          this.countDownStatus = false
          this.notice.errorTips('发送短信失败')
        }
      } catch (error) {
        this.countDownStatus = false
      }
    },
    countDown () {
      this.count--
      let count = this.count
      this.smsBtnText = `获取验证码(${count})`
      if (count <= 0 || !this.countDownStatus) {
        this.count = 60
        this.countDownStatus = false
        this.smsBtnText = '获取验证码'
        return
      }
      setTimeout(() => {
        this.countDown()
      }, 1e3)
    },
    async submit () {
      this.smsError = ''
      this.phoneError = ''
      let { phone, smsCode } = this
      if (!phone) return this.phoneError = '手机号不能为空'
      if (!/^1[3456789]\d{9}$/.test(phone)) return this.phoneError = '手机号格式错误'
      if (!smsCode) return this.smsError = '验证码不能为空'
      if (smsCode.length < 6) return this.smsError = '验证码错误,请重试!'
      let { code } = await verifySmsCode({
        phone,
        smsCode
      })
      if (code == 0) {
        this.$emit('loginSuccess')
        this.count = 60
        this.countDownStatus = false
        this.smsBtnText = '获取验证码'
        this.smsCode = ''
      } else this.smsError = '验证码错误,请重试!'
    }
  }
}
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  align-items: center;
}
.detail-content-warn {
  ::v-deep .el-dialog {
    height: 360px;
    .el-dialog__body {
      border-bottom: 1px solid #e6e6e6;
    }
  }
  .form {
    height: 195px;
    padding: 17px 22px 0 0;
    box-sizing: border-box;
  }
  .sms-code {
    gap: 8px;
    .get-sms-code {
      width: 160px;
    }
  }
}
</style>