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.
 
 
 

165 lines
5.6 KiB

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