1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<template>
<el-button @click="send" :disabled="leftTime > 0 && leftTime < 60">{{text}}</el-button>
</template>
<script>
import {sendSmsCode} from '@/api/ecw/orderCargoControl'
let timeout = null
export default {
props:{
orderId: [String, Number],
scene: [String, Number]
},
data(){
return {
leftTime: 60,
loading: false
}
},
computed:{
text(){
if(this.leftTime === 60){
return this.$t('发送验证码')
}
if(this.leftTime > 0){
return this.leftTime + this.$t('秒')
}
return this.$t('重新发送')
}
},
destroyed(){
if(timeout)clearTimeout(timeout)
},
methods:{
send(){
if(this.loading || (this.leftTime > 0 && this.leftTime < 60)) return false
this.loading = true
sendSmsCode({
orderId: this.orderId,
scene: this.scene
}).then(res => {
this.$message.success(this.$t('发送成功'))
this.countDown()
}).finally(res => {
this.loading = false
})
},
countDown(){
this.leftTime --
if(this.leftTime <= 0){
return false
}
timeout = setTimeout(this.countDown, 1000)
}
}
}
</script>