vue 实现获取验证码时间递减功能

男娘i 2022-05-20 00:55 317阅读 0赞

vue 实现获取验证码时间递减功能

  1. 关于获取验证码时间递减的情况,最常用的是出现在注册界面,当我们点击获取验证码之后,然后时间通常为60s递减,那么vue是怎么实现的呢?首先看一下效果图。获取验证码接口就自己去琢磨了哈

70 70 1

vue如下:

  1. <template>
  2. <transition name="bounce"
  3. enter-active-class="bounceInLeft"
  4. leave-active-class="bounceOutRight">
  5. <div class="bg" v-loading="loading" :style="{backgroundImage:`url(${backImgUrl})`}">
  6. <div class="login">
  7. <div class="left">
  8. <div class="loginTitle">
  9. <h3>用户注册</h3>
  10. </div>
  11. <el-form :model="loginForm" :rules="rules" ref="loginForm" style="margin-top: 40px;">
  12. <el-form-item prop="phone">
  13. <el-input v-model="loginForm.phone" placeholder="请输入手机号"></el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" :maxlength="16" :minlength="6"></el-input>
  17. </el-form-item>
  18. <el-form-item prop="password">
  19. <el-input v-model="loginForm.repassword" placeholder="再次请输入密码" type="password" :maxlength="16" :minlength="6"></el-input>
  20. </el-form-item>
  21. <!-- <el-form-item class="modifyVerification">
  22. <div class="verificationCode">
  23. <el-input v-model="loginForm.password" placeholder="请输入验证码"></el-input>
  24. <div class="verficationImage"></div>
  25. </div>
  26. </el-form-item> -->
  27. <el-form-item class="phoneVerification">
  28. <div class="phoneVerificationCode">
  29. <el-input v-model="loginForm.validateCode" placeholder="请输入收到的验证码"></el-input>
  30. <div class="phoneVerficationImage" @click="getValidate()">{
  31. {validateName}}</div>
  32. </div>
  33. </el-form-item>
  34. <div class="agreeText">
  35. <div>
  36. <el-checkbox v-model="checked">同意<span>注册协议</span>
  37. </el-checkbox>
  38. </div>
  39. <div>
  40. <a @click="goToLogin()"><span>已有账号登录</span></a>
  41. </div>
  42. </div>
  43. <el-form-item>
  44. <el-button type="primary" style="width: 100%; margin: 30px 0" @click="submitForm()">立即注册</el-button>
  45. </el-form-item>
  46. </el-form>
  47. </div>
  48. <div class="right">
  49. <img :src="imgCode"/>
  50. <p>扫一扫<br>下载嗨黔东南</p>
  51. </div>
  52. </div>
  53. </div>
  54. </transition>
  55. </template>
  56. <script>
  57. import imgCode from 'assets/images/code.jpg'
  58. import backImgUrl from 'assets/images/landr/login-bg.jpg'
  59. let countDown = 60
  60. export default {
  61. data () {
  62. return {
  63. validateName: '获取验证码',
  64. checked: false,
  65. backImgUrl,
  66. imgCode,
  67. loading: false,
  68. loginForm: {
  69. phone: '',
  70. password: '',
  71. repassword: '',
  72. validateCode: ''
  73. },
  74. rules: {
  75. phone: [
  76. { required: true, message: '请输入手机号', trigger: 'blur' },
  77. { pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  78. ],
  79. password: [
  80. { required: true, message: '请输入密码', trigger: 'blur' },
  81. { pattern: /^[0-9a-zA-Z]{6,16}$/, message: '密码6~16位由字母、数字、下划线组成', trigger: 'blur' }
  82. ],
  83. repassword: [
  84. { required: true, message: '请输入密码', trigger: 'blur' },
  85. { pattern: /^[0-9a-zA-Z]{6,16}$/, message: '密码6~16位由字母、数字、下划线组成', trigger: 'blur' }
  86. ]
  87. }
  88. }
  89. },
  90. created() {
  91. document.title = '登录-嗨黔东南'
  92. },
  93. methods: {
  94. submitForm () {
  95. const _self = this
  96. if (this.loginForm.password !== this.loginForm.repassword) {
  97. _self.$message({
  98. showClose: true,
  99. message: '两次输入的密码不一致',
  100. type: 'error'
  101. })
  102. return
  103. } else if (this.loginForm.validateCode === '' || this.loginForm.validateCode === null) {
  104. _self.$message({
  105. showClose: true,
  106. message: '未验证手机验证码',
  107. type: 'error'
  108. })
  109. return
  110. }
  111. this.$refs.loginForm.validate((valid) => {
  112. if (valid) {
  113. _self.loading = true
  114. _self.$http.post(`/v1/sms/${_self.loginForm.phone}/${_self.loginForm.validateCode}`, null).then(data => {
  115. _self.loading = false
  116. _self.$http.post('/v1/phoneRegister', {phone: _self.loginForm.phone, password: _self.loginForm.password}).then(data => {
  117. this.$store.commit('setUserInfo', data)
  118. _self.loading = false
  119. _self.$message({
  120. showClose: true,
  121. message: '注册成功',
  122. type: 'sucess'
  123. })
  124. _self.$router.push('/login')
  125. }).catch(errMsg => {
  126. _self.loading = false
  127. _self.$message({
  128. showClose: true,
  129. message: errMsg,
  130. type: 'error'
  131. })
  132. })
  133. }).catch(errMsg => {
  134. _self.loading = false
  135. _self.$message({
  136. showClose: true,
  137. message: errMsg,
  138. type: 'error'
  139. })
  140. })
  141. }
  142. })
  143. },
  144. goToLogin () {
  145. this.$router.push('/login')
  146. },
  147. // 手机验证码定时器
  148. setTimeDown () {
  149. if (countDown === 0) {
  150. this.validateDisabled = false
  151. this.validateName = '重新获取'
  152. countDown = 60
  153. return
  154. } else {
  155. this.validateDisabled = true
  156. this.validateName = countDown + 's'
  157. countDown--
  158. }
  159. const _self = this
  160. this.timer = setTimeout(() => {
  161. _self.setTimeDown()
  162. }, 1000)
  163. },
  164. getValidate () {
  165. if (this.validateDisabled) {
  166. return
  167. }
  168. if (this.loginForm.phone === null || this.loginForm.phone === '') {
  169. const _self = this
  170. _self.$message({
  171. showClose: true,
  172. message: '手机号不能为空',
  173. type: 'error'
  174. })
  175. return
  176. }
  177. if (countDown >= 60) {
  178. const _self = this
  179. this.$http.get(`/v1/sms/` + this.loginForm.phone, null).then(data => {
  180. _self.$message({
  181. showClose: true,
  182. message: '发送验证码成功',
  183. type: 'sucess'
  184. })
  185. }).catch(errMsg => {
  186. _self.$message({
  187. showClose: true,
  188. message: errMsg,
  189. type: 'error'
  190. })
  191. })
  192. }
  193. this.setTimeDown()
  194. }
  195. }
  196. }
  197. </script>
  198. <style lang="stylus" scoped>
  199. @import '~@/assets/css/variable.styl'
  200. .bg
  201. position fixed
  202. top 0
  203. left 0
  204. right 0
  205. bottom 0
  206. width 100%
  207. height 100%
  208. background-repeat: no-repeat;
  209. background-size: cover;
  210. background-position 50% 50%;
  211. z-index 500
  212. .login
  213. padding 18px
  214. background #ffffff
  215. border-radius 5px
  216. position absolute
  217. top 50%
  218. left 50%
  219. transform translate(-50%, -50%)
  220. display flex
  221. .forget
  222. text-align right
  223. margin-top -21px
  224. cursor pointer
  225. color $color-theme
  226. &:hover
  227. text-decoration underline
  228. .left
  229. width 300px
  230. padding 12px
  231. padding-right 24px
  232. .loginTitle
  233. left 0px
  234. top 0px
  235. position fixed
  236. width 100%
  237. line-height 50px
  238. height 50px
  239. background-color #58D0FF
  240. text-align center
  241. color white
  242. font-size 18px
  243. .right
  244. padding 70px 10px 0 20px
  245. border-left 1px solid $color-border
  246. text-align center
  247. img
  248. width 180px
  249. height 180px
  250. p
  251. font-size 15px
  252. color #797979
  253. line-height 24px
  254. margin-top 30px
  255. text-align center
  256. width 180px
  257. line-height 1.8
  258. letter-spacing 4px
  259. .divider
  260. display flex
  261. justify-content space-between
  262. align-items center
  263. white-space nowrap
  264. height auto
  265. overflow hidden
  266. line-height 1
  267. text-align center
  268. padding 10px 0
  269. color #666
  270. margin-top 24px
  271. &:before
  272. &:after
  273. content ''
  274. display block
  275. position relative
  276. width 100px
  277. height 1px
  278. background $color-border
  279. .third-login
  280. display flex
  281. justify-content space-around
  282. text-align center
  283. margin-top 15px
  284. i
  285. font-size 48px
  286. color $color-theme
  287. p
  288. color #666
  289. .modifyVerification {
  290. .verificationCode {
  291. display flex
  292. .verficationImage {
  293. background-color #79c757
  294. width 55%
  295. height 40px
  296. margin-left 10px
  297. }
  298. }
  299. }
  300. .phoneVerification {
  301. .phoneVerificationCode {
  302. display flex
  303. .phoneVerficationImage {
  304. cursor pointer
  305. background-color #ffad4d
  306. width 55%
  307. height 40px
  308. margin-left 10px
  309. color white
  310. text-align center
  311. }
  312. }
  313. }
  314. .agreeText {
  315. display flex
  316. justify-content space-between
  317. }
  318. a {
  319. text-decoration none
  320. color black
  321. }
  322. </style>

定时主要关注getValidate()和setTimeDown()方法即可,欢迎留言,项目要上线来不及时间解释。

使用的是elementUi 组件,需要引入,注重上面两个方法即可

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue实现验证

    在Web应用程序中,为了避免机器自动化或恶意攻击,很常见的做法是要求用户在表单提交之前输入验证码。验证码最常见的形式是图片验证码,因为图片验证码最大程度地防止了自动化机器调用A