phone.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div>
  3. <v-form @submit.prevent>
  4. <v-text-field v-model="loginData.query.phone" placeholder="请输入手机号" color="#00897B" variant="outlined" density="compact" :rules="phoneRules">
  5. <template v-slot:prepend-inner>
  6. <span class="d-flex">
  7. <v-icon icon="mdi-cellphone" size="20"></v-icon>
  8. <span class="d-flex" id="menu-activator">
  9. <span class="phone-number">{{ currentAear }}</span>
  10. <v-icon size="20">mdi-chevron-down</v-icon>
  11. </span>
  12. <v-menu activator="#menu-activator">
  13. <v-list>
  14. <v-list-item v-for="(item, index) in items" :key="index" :value="index" @click="handleChangeCurrentAear">
  15. <v-list-item-title>{{ item.title }}</v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. </span>
  20. </template>
  21. </v-text-field>
  22. <v-text-field v-model="loginData.query.code" placeholder="请输入验证码" color="#00897B" variant="outlined" density="compact" prepend-inner-icon="mdi-security" :rules="[v=> !!v || '请填写验证码']">
  23. <template #append-inner>
  24. <span v-if="showCode" class="login-code" @click="handleCode">获取验证码</span>
  25. <span v-else class="disable">重新获取{{ count }}s</span>
  26. </template>
  27. </v-text-field>
  28. </v-form>
  29. </div>
  30. </template>
  31. <script setup>
  32. import { ref, reactive } from 'vue'
  33. import { setCodeTime } from '@/utils/code'
  34. defineOptions({ name: 'phone-index' })
  35. const phoneRules = ref([
  36. value => {
  37. if (value) return true
  38. return '请输入手机号'
  39. },
  40. value => {
  41. if (value?.length <= 11) return true
  42. return 'Name must be less than 10 characters.'
  43. }
  44. ])
  45. // 手机号区域
  46. const currentAear = ref('中国大陆')
  47. const items = [
  48. { title: '中国大陆', value: '86' },
  49. { title: '中国香港', value: '852' },
  50. { title: '中国澳门', value: '853' },
  51. { title: '西班牙', value: '34' }
  52. ]
  53. const handleChangeCurrentAear = (e) => {
  54. currentAear.value = e.target.innerText
  55. }
  56. // 获取验证码
  57. const showCode = ref(true)
  58. const count = ref(0)
  59. const timer = ref(null)
  60. const handleCode = () => {
  61. // 先校验手机号
  62. count.value = 60
  63. setTime()
  64. }
  65. const setTime = () => {
  66. showCode.value = false
  67. timer.value = setInterval(() => {
  68. let number = count.value
  69. if (number > 0 && number <= 60) {
  70. count.value--
  71. setCodeTime(number - 1)
  72. } else {
  73. showCode.value = true
  74. clearInterval(timer.value)
  75. timer.value = null
  76. }
  77. }, 1000)
  78. }
  79. const autoTimer = () => {
  80. count.value = 0
  81. if(!count.value) return
  82. setTime()
  83. }
  84. autoTimer()
  85. const loginData = reactive({
  86. query: {
  87. phone: '',
  88. code: ''
  89. }
  90. })
  91. </script>
  92. <style lang="scss" scoped>
  93. .login-code {
  94. width: 62px;
  95. color: #00897B;
  96. font-size: 12px;
  97. cursor: pointer;
  98. }
  99. .disable {
  100. width: 72px;
  101. color: grey;
  102. font-size: 12px;
  103. }
  104. ::v-deep .phone-number {
  105. font-size: 12px;
  106. width: 50px;
  107. }
  108. </style>