verify.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="verify-container">
  3. <view class="title-wrapper">
  4. <view class="title">输入验证码</view>
  5. <view class="sub-title">验证码已发送到{{ countryCode }} {{ phoneNum }}</view>
  6. </view>
  7. <view class="verify-wrapper">
  8. <u-message-input
  9. :maxlength="6"
  10. active-color="#f2f2f2"
  11. inactive-color="#f2f2f2"
  12. :breathe="false"
  13. @finish="handleFinish"
  14. ></u-message-input>
  15. </view>
  16. <view class="count-down" :class="{ disabled: disabled }" @click="getVerifyCode">重新获取 <template v-if="disabled">{{ countdown }}s</template></view>
  17. </view>
  18. </template>
  19. <script>
  20. import { phoneLogin } from '@/utils/login'
  21. export default {
  22. data() {
  23. return {
  24. phoneNum: '',
  25. countryCode: '+86',
  26. disabled: true,
  27. countdown: 60,
  28. timer: null,
  29. loading: false
  30. };
  31. },
  32. onLoad(options) {
  33. if (options.phoneNum) {
  34. this.phoneNum = options.phoneNum
  35. }
  36. if (options.countryCode) {
  37. this.countryCode = decodeURIComponent(options.countryCode)
  38. }
  39. this.startCountdown()
  40. },
  41. onUnload() {
  42. this.clearCountdownTimer()
  43. },
  44. methods: {
  45. getVerifyCode() {
  46. if (this.disabled || this.loading) return
  47. uni.showLoading({
  48. title: '正在发送验证码'
  49. })
  50. this.loading = true
  51. this.$Request
  52. .getT('/app/Login/sendMsg/' + this.phoneNum + '/bind')
  53. .then((res) => {
  54. if (res.code === 0) {
  55. this.startCountdown()
  56. } else {
  57. uni.showModal({
  58. showCancel: false,
  59. title: '短信发送失败',
  60. content: res.msg ? res.msg : '请一分钟后再获取验证码',
  61. });
  62. }
  63. })
  64. .finally(() => {
  65. this.loading = false
  66. uni.hideLoading();
  67. })
  68. },
  69. startCountdown() {
  70. this.disabled = true
  71. this.timer = setInterval(() => {
  72. this.countdown--
  73. if (this.countdown <= 0) {
  74. this.clearCountdownTimer()
  75. this.countdown = 60
  76. this.disabled = false
  77. }
  78. }, 1000)
  79. },
  80. clearCountdownTimer() {
  81. if (this.timer) {
  82. clearInterval(this.timer)
  83. this.timer = null // 重置定时器实例
  84. }
  85. },
  86. handleFinish(value) {
  87. phoneLogin({
  88. phone: this.phoneNum,
  89. msg: value
  90. })
  91. },
  92. }
  93. }
  94. </script>
  95. <style lang="scss" scoped>
  96. .verify-container {
  97. padding: 0 40rpx;
  98. .title-wrapper {
  99. padding-top: 132rpx;
  100. .title {
  101. font-size: 40rpx;
  102. font-weight: 700;
  103. line-height: 52rpx;
  104. color: rgba(51, 51, 51, 1);
  105. margin-bottom: 12rpx;
  106. }
  107. .sub-title {
  108. font-size: 28rpx;
  109. line-height: 36rpx;
  110. color: rgba(153, 153, 153, 1);
  111. }
  112. }
  113. .verify-wrapper {
  114. margin: 120rpx auto 40rpx;
  115. ::v-deep.u-char-flex {
  116. justify-content: space-between;
  117. .u-box {
  118. width: 40rpx;
  119. height: 40rpx;
  120. box-sizing: border-box;
  121. border-radius: 12rpx;
  122. background: rgba(250, 250, 250, 1);
  123. color: rgba(51, 51, 51, 1) !important;
  124. }
  125. }
  126. }
  127. .count-down {
  128. color: rgba(1, 107, 246, 1);
  129. font-size: 24rpx;
  130. line-height: 32rpx;
  131. &.disabled {
  132. color: rgba(1, 107, 246, 0.6);
  133. }
  134. }
  135. }
  136. </style>