WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2020-12-01 c4c9a03d3ba029b613d64a6514c132d28636d1c5
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * CInputPhoneCode
 * @author Tevin
 */
 
<template>
    <view class="c-input-phone-code">
        <AtInput
            :name="itemData.name"
            :title="itemData.label"
            type="text"
            :placeholder="placeholder"
            :required="itemData.required"
            :error="itemData.error"
            :value="itemData.formData[itemData.name]"
            :onChange="evt=>itemData.onChange(evt)"
        >
            <AtButton
                class="c-input-phone-btn"
                type="primary"
                size="small"
                :disabled="holding"
                :onClick="evt=>handleClick()"
            >{{btnText}}</AtButton>
        </AtInput>
    </view>
</template>
 
<script>
import { AtInput, AtButton } from 'taro-ui-vue';
import './cInputPhoneCode.scss';
 
export default {
    name: 'CInputPhoneCode',
    components: {
        AtInput,
        AtButton,
    },
    props: {
        placeholder: String,
        itemData: Object,
        onCallPhoneCode: Function,
    },
    data() {
        return {
            cd: 60,
            holding: false,
        };
    },
    computed: {
        btnText() {
            if (this.holding) {
                return this.cd + '秒后重新获取';
            } else {
                return '重新获取验证码';
            }
        },
    },
    methods: {
        handleClick() {
            if (this.holding) {
                return;
            }
            this.holding = true;
            this.cd = 60;
            this.onCallPhoneCode();
            const timer = setInterval(() => {
                this.cd -= 1;
                if (this.cd === 0) {
                    this.holding = false;
                    this.cd = 60;
                }
            }, 1000);
        },
    },
};
</script>