WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2023-11-22 057e40f413c2ef93f6ec71dbb06c491d169c6c6f
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * CInputScanCode - 表单项,手机验证码输入框
 * @author Tevin
 */
 
<template>
    <view
        class="c-input-scan-code"
        :class="[readOnly?'read-only':'']"
    >
        <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)"
            :onBlur="evt => onBlur(evt)"
        >
            <view class="c-input-scan-space"></view>
            <AtButton
                class="c-input-scan-btn"
                size="small"
                type="primary"
                :disabled="readOnly"
                :onClick="evt => handleScan()"
            >
                <text v-show="!scaning">扫描</text>
                <AtIcon
                    v-show="scaning"
                    value="loading"
                />
            </AtButton>
        </AtInput>
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { AtInput, AtButton, AtIcon } from 'taro-ui-vue';
import { Tools } from '@components/common/Tools';
import './cInputScanCode.scss';
 
export default {
    name: 'CInputScanCode',
    components: {
        AtInput,
        AtIcon,
        AtButton,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 占位提示
        placeholder: String,
        // 只读模式
        readOnly: {
            type: Boolean,
            default: false,
        },
        // 由业务层调用app
        onScaning: Function,
        // 失去焦点回调
        onBlur: {
            type: Function,
            default: () => () => null,
        },
    },
    data() {
        return {
            scaning: false,
        };
    },
    methods: {
        handleScan() {
            // 小程序
            if (process.env.TARO_ENV === 'weapp') {
                this.handleScaning('taro');
            }
            // h5页面
            else if (process.env.TARO_ENV === 'h5') {
                const ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 微信公众号中
                    this.handleScaning('taro');
                } else {
                    // 浏览器中
                    this.handleScaning('app');
                }
            }
        },
        handleScaning(type) {
            this.scaning = true;
            if (type === 'taro') {
                Taro.scanCode({
                    onlyFromCamera: false,
                    scanType: 'barCode',
                    success: res => {
                        this.scaning = false;
                        this.itemRes.onChange(res.result);
                    },
                    fail: err => {
                        if (err.errMsg.indexOf('cancel') >= 0) {
                            Tools.toast('扫码已取消');
                        } else {
                            Tools.toast('扫码失败!');
                        }
                        this.scaning = false;
                    },
                });
            } else if (type === 'app') {
                if (this.onScaning) {
                    this.onScaning(result => {
                        this.scaning = false;
                        if (result) {
                            this.itemRes.onChange(result);
                        }
                    });
                }
            }
        },
    },
};
</script>