u-verification-code.vue 5.4 KB

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