123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <view
- class="tui-countdown__verify"
- :class="{ 'tui-verify__opacity': status > 1 && isOpacity }"
- :style="{ width: width, height: height, padding: padding, margin: margin, borderRadius: radius, fontSize: size + 'rpx', color: color, background: background }"
- :hover-class="hover && status == 1 ? 'tui-verify__opacity' : ''"
- :hover-stay-time="150"
- @tap.stop="sendCode"
- >
- {{ showText }}
- <view class="tui-verify__line" :style="{ borderWidth: borderWidth, borderColor: borderColor, borderRadius: radius }"></view>
- </view>
- </template>
- <script>
- export default {
- name: 'tuiCountdownVerify',
- emits: ['send','countdown','end'],
- props: {
-
- text: {
- type: String,
- default: '发送验证码'
- },
-
- sendText: {
- type: String,
- default: '请稍候...'
- },
-
- countdownText: {
- type: String,
- default: 's后获取'
- },
-
- seconds: {
- type: Number,
- default: 60
- },
-
- width: {
- type: String,
- default: '182rpx'
- },
-
- height: {
- type: String,
- default: '56rpx'
- },
- padding: {
- type: String,
- default: '0'
- },
- margin: {
- type: String,
- default: '0'
- },
-
- radius: {
- type: String,
- default: '6rpx'
- },
-
- size: {
- type: Number,
- default: 24
- },
-
- color: {
- type: String,
- default: '#5677fc'
- },
-
- background: {
- type: String,
- default: 'transparent'
- },
-
- borderWidth: {
- type: String,
- default: '1px'
- },
-
- borderColor: {
- type: String,
- default: '#5677fc'
- },
-
- isOpacity: {
- type: Boolean,
- default: true
- },
-
- hover: {
- type: Boolean,
- default: true
- },
-
- successVal: {
- type: Number,
- default: 0
- },
-
- resetVal: {
- type: Number,
- default: 0
- },
-
- start:{
- type:Boolean,
- default:false
- },
-
- params: {
- type: [Number, String],
- default: 0
- }
- },
- data() {
- return {
- showText: '',
-
- status: 1,
- countdownTimer: null
- };
- },
- created() {
- if(this.start){
- this.doLoop();
- }else{
- this.showText = this.text;
- this.clearTimer();
- }
- },
-
- beforeDestroy() {
- this.clearTimer();
- },
-
-
- beforeUnmount() {
- this.clearTimer();
- },
-
- watch: {
- successVal(val) {
- if (val && val > 0) {
- this.doLoop();
- }
- },
- resetVal(val) {
- if (val && val > 0) {
- this.reset();
- }
- }
- },
- methods: {
- sendCode() {
- if (this.status > 1) return;
- this.clearTimer();
- this.status = 2;
- this.showText = this.sendText;
- this.$emit('send', {
- params: this.params
- });
- },
- doLoop: function() {
- this.clearTimer();
- this.status = 3;
- let seconds = this.seconds || 60;
- this.showText = seconds + this.countdownText;
- this.countdownTimer = setInterval(() => {
- if (seconds > 1) {
- --seconds;
- this.showText = seconds + this.countdownText;
-
- this.$emit('countdown', {
- seconds: seconds,
- params: this.params
- });
- } else {
- this.reset();
-
- this.$emit('end', {
- params: this.params
- });
- }
- }, 1000);
- },
-
- success() {
- this.doLoop();
- },
-
- reset() {
- this.clearTimer();
- this.showText = this.text;
- this.status = 1;
- },
- clearTimer() {
- clearInterval(this.countdownTimer);
- this.countdownTimer = null;
- }
- }
- };
- </script>
- <style scoped>
- .tui-countdown__verify {
- position: relative;
- display: flex;
- align-items: center;
- justify-content: center;
- white-space: nowrap;
- box-sizing: border-box;
- }
- .tui-verify__opacity {
- opacity: 0.5;
- }
- .tui-verify__line {
- position: absolute;
- width: 200%;
- height: 200%;
- transform-origin: 0 0;
- transform: scale(0.5, 0.5) translateZ(0);
- box-sizing: border-box;
- border-style: solid;
- left: 0;
- top: 0;
- pointer-events: none;
- }
- </style>
|