index.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <u-toast ref="uToast"></u-toast>
  7. <u-verification-code :keep-running="true" :seconds="seconds" @end="end" @start="start" ref="uCode"
  8. @change="codeChange" :startText="startText" :changeText="changeText"
  9. :endText="endText"></u-verification-code>
  10. <u-button @click="getCode">{{tips}}</u-button>
  11. <u-button :custom-style="{marginTop: '30rpx'}" @tap="reset" style="margin-top: 30rpx;">重置</u-button>
  12. </view>
  13. </view>
  14. <view class="u-config-wrap">
  15. <view class="u-config-title u-border-bottom">
  16. 参数配置
  17. </view>
  18. <view class="u-config-item">
  19. <view class="u-item-title">倒计时间</view>
  20. <u-subsection :current="0" :list="['60s', '10s', '5s']" @change="secondsChange"></u-subsection>
  21. </view>
  22. <view class="u-config-item">
  23. <view class="u-item-title">自定义提示语</view>
  24. <u-subsection :current="1" :list="['是', '否']" @change="textChange"></u-subsection>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. tips: '',
  34. seconds: 60,
  35. refCode: null,
  36. startText: '获取验证码',
  37. changeText: 'X秒重新获取',
  38. endText: '重新获取'
  39. }
  40. },
  41. onReady() {
  42. // 注意这里为错误示例,目前微信小程序最新稳定版开发工具如此
  43. // 赋值会报错,很诡异,其他端无此问题
  44. // this.refCode = this.$refs.uCode;
  45. },
  46. methods: {
  47. codeChange(text) {
  48. // console.log(text);
  49. this.tips = text;
  50. },
  51. getCode() {
  52. if(this.$refs.uCode.canGetCode) {
  53. // 模拟向后端请求验证码
  54. uni.showLoading({
  55. title: '正在获取验证码'
  56. })
  57. setTimeout(() => {
  58. uni.hideLoading();
  59. // 这里此提示会被this.start()方法中的提示覆盖
  60. this.$u.toast('验证码已发送');
  61. // 通知验证码组件内部开始倒计时
  62. this.$refs.uCode.start();
  63. }, 2000);
  64. } else {
  65. this.$u.toast('倒计时结束后再发送');
  66. }
  67. },
  68. secondsChange(index) {
  69. this.seconds = index == 0 ? 60 : index == 1 ? 10 : 5;
  70. },
  71. textChange(index) {
  72. if(index == 0) {
  73. this.startText = '点一下获取',
  74. this.changeText = '重新获取Xs',
  75. this.endText = '再次获取'
  76. } else {
  77. this.startText = '获取验证码',
  78. this.changeText = 'X秒重新获取',
  79. this.endText = '重新获取'
  80. }
  81. },
  82. end() {
  83. this.$u.toast('倒计时结束');
  84. },
  85. start() {
  86. this.$u.toast('倒计时开始');
  87. },
  88. reset() {
  89. this.$refs.uCode.reset();
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss" scoped>
  95. .u-demo {}
  96. </style>