WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-03-29 1bd2c493c9cbe47d10e4ba045d5c4635fd4eccef
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * CInputPhoneCode - 表单项,手机验证码输入框
 * @author Tevin
 */
 
<template>
    <view class="c-input-phone-code">
        <AtInput
            :name="itemRes.name"
            :title="itemRes.label"
            type="text"
            :placeholder="placeholder"
            :required="itemRes.required"
            :error="itemRes.error"
            :value="itemRes.formData[itemRes.name]"
            :onChange="evt=>itemRes.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: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 占位提示
        placeholder: String,
        // 验证码计时器开始方式
        //   true 点击按钮后立即自动开始
        //   false 后台返回为成功时才开始
        autoStart: {
            type: Boolean,
            default: true,
        },
        // 发送请求,通知后端给手机发验证码
        onCallPhoneCode: Function,
    },
    data() {
        return {
            cd: 60,
            holding: false,
        };
    },
    computed: {
        btnText() {
            if (this.holding) {
                return this.cd + '秒后重新获取';
            } else {
                return '重新获取验证码';
            }
        },
    },
    methods: {
        handleClick() {
            if (this.holding) {
                return;
            }
            const startCD = () => {
                this.holding = true;
                this.cd = 60;
                setInterval(() => {
                    this.cd -= 1;
                    if (this.cd === 0) {
                        this.holding = false;
                        this.cd = 60;
                    }
                }, 1000);
            };
            if (this.autoStart) {
                startCD();
                this.onCallPhoneCode(() => null);
            } else {
                this.onCallPhoneCode(() => {
                    startCD();
                });
            }
        },
    },
};
</script>