Mr.Shi
2023-08-21 3d1a4b4bf3d37e29a3d3345e00f9f84c0d7959a6
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
<template>
    <div class="box">
    </div>
</template>
 
<script setup lang="ts">
    import { getCurrentInstance, watch } from 'vue'
    import { Toast, Dialog } from 'vant'
    const { proxy }: any = getCurrentInstance()
 
    const props = defineProps({
        openCode: { // 是否打开钉钉扫一扫
            type: Boolean,
            required: false,
            default: false
        },
        infos: { // 弹框提示字
            type: Array,
            required: true
        },
        promptOrNot: { // 是否需要弹框提示
            type: Boolean,
            required: false,
            default: true
        }
    })
 
    let arrCode: string[] = []  // 存放扫码数组
 
    const emit = defineEmits(['onDecode', 'closePopup'])
 
    // 核心方法
    const init = (index: number): void => {
        if (proxy.$dd.env.platform !== 'notInDingTalk') {
            if (props.promptOrNot) {
                Dialog.alert({
                    title: '扫码提示',
                    confirmButtonColor: '#4275FC',
                    message: `${props.infos[index]}`,
                }).then(() => {
                    proxy.$dd.biz.util.scan({
                        type: 'all',
                        onSuccess: function(data: any) {
                            arrCode.push(data.text)
                            // 最后一次返回父组件扫码数据
                            if (index === props.infos.length - 1) {
                                emit('onDecode', arrCode)
                            } else {
                                init(index + 1)
                            }
                        },
                        onFail : function(err: any) {
                            arrCode = []
                            emit('closePopup')
                        }
                    })
                }).catch(() => {
                    emit('closePopup')
                })
            } else {
                proxy.$dd.biz.util.scan({
                    type: 'all',
                    onSuccess: function(data: any) {
                        arrCode.push(data.text)
                        // 最后一次返回父组件扫码数据
                        if (index === props.infos.length - 1) {
                            emit('onDecode', arrCode)
                        } else {
                            init(index + 1)
                        }
                    },
                    onFail : function(err: any) {
                        arrCode = []
                        emit('closePopup')
                    }
                })
            }
        } else {
            Toast.fail({ message: '当前不是钉钉环境' })
            emit('closePopup')
        }
    }
 
    // 监听openCode触发扫码方法
    watch(() => props.openCode, (news: boolean) => {
        if (news) {
            init(0)
        } else {
            arrCode = []
        }
    })
</script>
 
<style lang="scss" scoped>
    .box_video {
 
    }
</style>