综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-code.vue 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="uv-code">
  3. <!-- 此组件功能由js完成,无需写html逻辑 -->
  4. </view>
  5. </template>
  6. <script>
  7. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  8. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  9. import props from './props.js';
  10. /**
  11. * Code 验证码倒计时
  12. * @description 考虑到用户实际发送验证码的场景,可能是一个按钮,也可能是一段文字,提示语各有不同,所以本组件不提供界面显示,只提供倒计时文本,由用户将文本嵌入到具体的场景
  13. * @tutorial https://www.uvui.cn/components/code.html
  14. * @property {String | Number} seconds 倒计时所需的秒数(默认 60 )
  15. * @property {String} startText 开始前的提示语,见官网说明(默认 '获取验证码' )
  16. * @property {String} changeText 倒计时期间的提示语,必须带有字母"x",见官网说明(默认 'X秒重新获取' )
  17. * @property {String} endText 倒计结束的提示语,见官网说明(默认 '重新获取' )
  18. * @property {Boolean} keepRunning 是否在H5刷新或各端返回再进入时继续倒计时( 默认false )
  19. * @property {String} uniqueKey 为了区分多个页面,或者一个页面多个倒计时组件本地存储的继续倒计时变了
  20. *
  21. * @event {Function} change 倒计时期间,每秒触发一次
  22. * @event {Function} start 开始倒计时触发
  23. * @event {Function} end 结束倒计时触发
  24. * @example <uv-code ref="uCode" @change="codeChange" seconds="20"></uv-code>
  25. */
  26. export default {
  27. name: "uv-code",
  28. mixins: [mpMixin, mixin, props],
  29. data() {
  30. return {
  31. secNum: this.seconds,
  32. timer: null,
  33. canGetCode: true, // 是否可以执行验证码操作
  34. }
  35. },
  36. mounted() {
  37. this.checkKeepRunning()
  38. },
  39. watch: {
  40. seconds: {
  41. immediate: true,
  42. handler(n) {
  43. this.secNum = n
  44. }
  45. }
  46. },
  47. methods: {
  48. checkKeepRunning() {
  49. // 获取上一次退出页面(H5还包括刷新)时的时间戳,如果没有上次的保存,此值可能为空
  50. let lastTimestamp = Number(uni.getStorageSync(this.uniqueKey + '_$uCountDownTimestamp'))
  51. if (!lastTimestamp) return this.changeEvent(this.startText)
  52. // 当前秒的时间戳
  53. let nowTimestamp = Math.floor((+new Date()) / 1000)
  54. // 判断当前的时间戳,是否小于上一次的本该按设定结束,却提前结束的时间戳
  55. if (this.keepRunning && lastTimestamp && lastTimestamp > nowTimestamp) {
  56. // 剩余尚未执行完的倒计秒数
  57. this.secNum = lastTimestamp - nowTimestamp
  58. // 清除本地保存的变量
  59. uni.removeStorageSync(this.uniqueKey + '_$uCountDownTimestamp')
  60. // 开始倒计时
  61. this.start()
  62. } else {
  63. // 如果不存在需要继续上一次的倒计时,执行正常的逻辑
  64. this.changeEvent(this.startText)
  65. }
  66. },
  67. // 开始倒计时
  68. start() {
  69. // 防止快速点击获取验证码的按钮而导致内部产生多个定时器导致混乱
  70. if (this.timer) {
  71. clearInterval(this.timer)
  72. this.timer = null
  73. }
  74. this.$emit('start')
  75. this.canGetCode = false
  76. // 这里放这句,是为了一开始时就提示,否则要等setInterval的1秒后才会有提示
  77. this.changeEvent(this.changeText.replace(/x|X/, this.secNum))
  78. this.timer = setInterval(() => {
  79. if (--this.secNum) {
  80. // 用当前倒计时的秒数替换提示字符串中的"x"字母
  81. this.changeEvent(this.changeText.replace(/x|X/, this.secNum))
  82. } else {
  83. clearInterval(this.timer)
  84. this.timer = null
  85. this.changeEvent(this.endText)
  86. this.secNum = this.seconds
  87. this.$emit('end')
  88. this.canGetCode = true
  89. }
  90. }, 1000)
  91. this.setTimeToStorage()
  92. },
  93. // 重置,可以让用户再次获取验证码
  94. reset() {
  95. this.canGetCode = true
  96. clearInterval(this.timer)
  97. this.secNum = this.seconds
  98. this.changeEvent(this.endText)
  99. },
  100. changeEvent(text) {
  101. this.$emit('change', text)
  102. },
  103. // 保存时间戳,为了防止倒计时尚未结束,H5刷新或者各端的右上角返回上一页再进来
  104. setTimeToStorage() {
  105. if (!this.keepRunning || !this.timer) return
  106. // 记录当前的时间戳,为了下次进入页面,如果还在倒计时内的话,继续倒计时
  107. // 倒计时尚未结束,结果大于0;倒计时已经开始,就会小于初始值,如果等于初始值,说明没有开始倒计时,无需处理
  108. if (this.secNum > 0 && this.secNum <= this.seconds) {
  109. // 获取当前时间戳(+ new Date()为特殊写法),除以1000变成秒,再去除小数部分
  110. let nowTimestamp = Math.floor((+new Date()) / 1000)
  111. // 将本该结束时候的时间戳保存起来 => 当前时间戳 + 剩余的秒数
  112. uni.setStorage({
  113. key: this.uniqueKey + '_$uCountDownTimestamp',
  114. data: nowTimestamp + Number(this.secNum)
  115. })
  116. }
  117. }
  118. },
  119. // #ifdef VUE2
  120. // 组件销毁的时候,清除定时器,否则定时器会继续存在,系统不会自动清除
  121. beforeDestroy() {
  122. this.setTimeToStorage()
  123. clearTimeout(this.timer)
  124. this.timer = null
  125. },
  126. // #endif
  127. // #ifdef VUE3
  128. // 组件销毁,兼容vue3
  129. unmounted() {
  130. this.setTimeToStorage()
  131. clearTimeout(this.timer)
  132. this.timer = null
  133. }
  134. // #endif
  135. }
  136. </script>