index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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" placeholder="手机号码" disabled />
  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 type="primary" size="large" :disabled="isDisabled" @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 @click="cancelClick">
  17. <HcIcon name="close" />
  18. <span>取消</span>
  19. </el-button>
  20. <el-button type="primary" hc-btn :loading="isLoading" @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 projectId = ref(userStore.getProjectId)
  51. const phoneVal = ref(config.smsPhone + '' || userInfo.value.phone + '')
  52. const showModal = ref(props.show)
  53. const isLoading = ref(props.loading)
  54. //监听
  55. watch(() => [
  56. props.show,
  57. props.loading,
  58. userStore.getUserInfo,
  59. ], ([show, loading, user]) => {
  60. userInfo.value = user
  61. showModal.value = show
  62. isLoading.value = loading
  63. })
  64. //返回的验证码
  65. const resCode = ref('')
  66. //表单
  67. const reportFormRef = ref(null)
  68. const reportModel = ref({ code: null })
  69. const reportRules = ref({
  70. code: {
  71. required: true,
  72. validator: (rule, value, callback) => {
  73. const code = resCode.value ?? ''
  74. if (!code) {
  75. callback(new Error('请先获取验证码'))
  76. } else if (!value) {
  77. callback(new Error('请输入验证码'))
  78. } else if (code !== value) {
  79. callback(new Error('验证码错误'))
  80. } else {
  81. callback()
  82. }
  83. },
  84. trigger: 'blur',
  85. },
  86. })
  87. //短信验证码
  88. const isDisabled = ref(false) //是否开启倒计时
  89. const totalTime = 60 //总时间,单位秒
  90. const recordingTime = ref(0) //记录时间变量
  91. const currentTime = ref(0) //显示时间变量
  92. //获取短信验证码
  93. const getCodeClick = async () => {
  94. const { error, code, msg } = await sendNotice({
  95. phone: phoneVal.value,
  96. }, false)
  97. //处理数据
  98. if (!error && code === 200 && msg) {
  99. resCode.value = msg
  100. //把显示时间设为总时间
  101. currentTime.value = totalTime
  102. //开始倒计时
  103. isDisabled.value = true
  104. //执行倒计时
  105. checkingTime()
  106. window?.$message?.success('发送成功')
  107. } else {
  108. resCode.value = ''
  109. window?.$message?.error(msg || '请求异常')
  110. }
  111. }
  112. //倒计时
  113. const checkingTime = () => {
  114. //判断是否开启
  115. if (isDisabled.value) {
  116. //判断显示时间是否已到0,判断记录时间是否已到总时间
  117. if (currentTime.value > 0 && recordingTime.value <= totalTime) {
  118. //记录时间增加 1
  119. recordingTime.value ++
  120. //显示时间,用总时间 - 记录时间
  121. currentTime.value = totalTime - recordingTime.value
  122. //1秒钟后,再次执行本方法
  123. setTimeout(() => {
  124. checkingTime()
  125. }, 1000)
  126. } else {
  127. //时间已完成,还原相关变量
  128. isDisabled.value = false //关闭倒计时
  129. recordingTime.value = 0 //记录时间为0
  130. currentTime.value = totalTime //显示时间为总时间
  131. }
  132. } else {
  133. //倒计时未开启,初始化默认变量
  134. isDisabled.value = false
  135. recordingTime.value = 0
  136. currentTime.value = totalTime
  137. }
  138. }
  139. //取消
  140. const cancelClick = () => {
  141. emit('cancel')
  142. }
  143. //确认
  144. const confirmClick = async () => {
  145. const validate = await formValidate(reportFormRef.value)
  146. if (validate) {
  147. saveSmsTimeoutApi()
  148. emit('confirm')
  149. }
  150. }
  151. //验证码过期时间
  152. const saveSmsTimeoutApi = async () => {
  153. await saveSmsTimeout({
  154. code: resCode.value,
  155. projectId: projectId.value,
  156. })
  157. }
  158. </script>