index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <hc-new-dialog v-model="showModal" title="短信认证" widths="26rem" @close="cancelClick">
  3. <el-form ref="reportFormRef" :model="reportModel" :rules="reportRules" label-width="auto" size="large">
  4. <el-form-item label="手机号码">
  5. <el-input v-model="phoneVal" disabled placeholder="手机号码" />
  6. </el-form-item>
  7. <el-form-item class="hc-input-button-group" label="验证码" prop="code">
  8. <el-input v-model="reportModel.code" placeholder="请输入验证码" />
  9. <el-button :disabled="isDisabled" size="large" type="primary" @click="getCodeClick">
  10. {{ isDisabled ? `倒计时${currentTime}s` : '获取验证码' }}
  11. </el-button>
  12. </el-form-item>
  13. </el-form>
  14. <template #footer>
  15. <div class="dialog-footer">
  16. <el-button size="large" @click="cancelClick">
  17. <HcIcon name="close" />
  18. <span>取消</span>
  19. </el-button>
  20. <el-button :loading="isLoading" hc-btn type="primary" @click="confirmClick">
  21. <HcIcon name="check" />
  22. <span>确认</span>
  23. </el-button>
  24. </div>
  25. </template>
  26. </hc-new-dialog>
  27. </template>
  28. <script setup>
  29. import { ref, watch } from 'vue'
  30. import { useAppStore } from '~src/store'
  31. import { saveSmsTimeout, sendNotice } from '~api/other'
  32. import config from '~src/config/index'
  33. import { formValidate } from 'js-fast-way'
  34. //参数
  35. const props = defineProps({
  36. show: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. loading: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. })
  45. //事件
  46. const emit = defineEmits(['cancel', 'confirm'])
  47. //变量
  48. const userStore = useAppStore()
  49. const userInfo = ref(userStore.getUserInfo)
  50. const phoneVal = ref(config.smsPhone + '' || userInfo.value.phone + '')
  51. const showModal = ref(props.show)
  52. const isLoading = ref(props.loading)
  53. //监听
  54. watch(() => [
  55. props.show,
  56. props.loading,
  57. userStore.getUserInfo,
  58. ], ([show, loading, user]) => {
  59. userInfo.value = user
  60. showModal.value = show
  61. isLoading.value = loading
  62. })
  63. //返回的验证码
  64. const resCode = ref('')
  65. //表单
  66. const reportFormRef = ref(null)
  67. const reportModel = ref({ code: null })
  68. const reportRules = ref({
  69. code: {
  70. required: true,
  71. validator: (rule, value, callback) => {
  72. const code = resCode.value ?? ''
  73. if (!code) {
  74. callback(new Error('请先获取验证码'))
  75. } else if (!value) {
  76. callback(new Error('请输入验证码'))
  77. } else if (code !== value) {
  78. callback(new Error('验证码错误'))
  79. } else {
  80. callback()
  81. }
  82. },
  83. trigger: 'blur',
  84. },
  85. })
  86. //短信验证码
  87. const isDisabled = ref(false) //是否开启倒计时
  88. const totalTime = 60 //总时间,单位秒
  89. const recordingTime = ref(0) //记录时间变量
  90. const currentTime = ref(0) //显示时间变量
  91. //获取短信验证码
  92. const getCodeClick = async () => {
  93. const { error, code, msg } = await sendNotice({
  94. phone: phoneVal.value,
  95. }, false)
  96. //处理数据
  97. if (!error && code === 200 && msg) {
  98. resCode.value = msg
  99. //把显示时间设为总时间
  100. currentTime.value = totalTime
  101. //开始倒计时
  102. isDisabled.value = true
  103. //执行倒计时
  104. checkingTime()
  105. window?.$message?.success('发送成功')
  106. } else {
  107. resCode.value = ''
  108. window?.$message?.error(msg || '请求异常')
  109. }
  110. }
  111. //倒计时
  112. const checkingTime = () => {
  113. //判断是否开启
  114. if (isDisabled.value) {
  115. //判断显示时间是否已到0,判断记录时间是否已到总时间
  116. if (currentTime.value > 0 && recordingTime.value <= totalTime) {
  117. //记录时间增加 1
  118. recordingTime.value++
  119. //显示时间,用总时间 - 记录时间
  120. currentTime.value = totalTime - recordingTime.value
  121. //1秒钟后,再次执行本方法
  122. setTimeout(() => {
  123. checkingTime()
  124. }, 1000)
  125. } else {
  126. //时间已完成,还原相关变量
  127. isDisabled.value = false //关闭倒计时
  128. recordingTime.value = 0 //记录时间为0
  129. currentTime.value = totalTime //显示时间为总时间
  130. }
  131. } else {
  132. //倒计时未开启,初始化默认变量
  133. isDisabled.value = false
  134. recordingTime.value = 0
  135. currentTime.value = totalTime
  136. }
  137. }
  138. //取消
  139. const cancelClick = () => {
  140. emit('cancel')
  141. }
  142. //确认
  143. const confirmClick = async () => {
  144. const validate = await formValidate(reportFormRef.value)
  145. if (validate) {
  146. saveSmsTimeoutApi().then()
  147. emit('confirm')
  148. }
  149. }
  150. //验证码过期时间
  151. const saveSmsTimeoutApi = async () => {
  152. await saveSmsTimeout({
  153. code: resCode.value,
  154. })
  155. }
  156. </script>