MrShi
2024-03-05 84b74d64ecc519882b89278f1011f7ac7525d190
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
97
98
99
100
101
<template>
    <GlobalWindow
        :title="title"
        width="40%"
        :visible.sync="visible"
        :confirm-working="isWorking"
        @confirm="confirm"
    >
        <el-form :model="form" ref="form" :rules="rules">
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入" disabled v-trim/>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
                <div style="width: 100%; display: flex; align-items: center;">
                    <el-input v-model="form.code" placeholder="请输入" v-trim/>
                    <el-button type="primary" style="margin-left: 15px;" @click="send" v-if="num === 0">发送验证码</el-button>
                    <el-button type="primary" style="margin-left: 15px;" v-else>{{num}}</el-button>
                </div>
            </el-form-item>
        </el-form>
    </GlobalWindow>
</template>
 
<script>
    import BaseOpera from '@/components/base/BaseOpera'
    import GlobalWindow from '@/components/common/GlobalWindow'
    import { sendSms, validateCode } from '@/api/business/smsEmail'
    import { mapState } from 'vuex'
    export default {
        name: 'operaverifyPhoneNumberWindew',
        extends: BaseOpera,
        components: { GlobalWindow },
        data () {
            return {
                form: {
                    phone: this.$store.state.userInfo.company.phone,
                    code: ''
                },
                rules: {
                    code: [
                        { required: true, message: '请输入验证码' }
                    ]
                },
                num: 0,
                timer: null
            }
        },
        computed: {
            ...mapState(['userInfo'])
        },
        methods: {
            open (title) {
                this.title = title
                this.num = 0
                clearInterval(this.timer)
                this.timer = null
                this.visible = true
                this.$nextTick(() => {
                    this.$refs.form.resetFields()
                })
            },
            send () {
                sendSms({
                    phone: this.form.phone
                }).then(res => {
                    this.num = 60
                    this.setTime()
                })
            },
            setTime () {
                this.timer = setInterval(() => {
                    if (this.num === 0) {
                        clearInterval(this.timer)
                        this.timer = null
                        return
                    }
                    this.num = this.num - 1
                }, 1000)
            },
            confirm () {
                this.$refs.form.validate((valid) => {
                    if (!valid) {
                        return
                    }
                    this.isWorking = true
                    validateCode({
                        code: this.form.code
                    }).then(res => {
                        this.visible = false
                        this.$tip.apiSuccess('操作成功')
                        this.$emit('success')
                    }).catch(e => {
                        this.$tip.apiFailed(e)
                    }).finally(() => {
                        this.isWorking = false
                    })
                })
            }
        }
    }
</script>