index.vue
3.65 KB
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
127
128
129
130
131
132
133
134
135
136
<!--
* @Date: 2022-08-29 14:31:20
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-05-28 10:05:08
* @FilePath: /data-table/src/components/TextField/index.vue
* @Description: 单行文本输入框(微信扫描功能)
-->
<template>
<div v-if="HideShow" class="text-field-page">
<div :class="[isGroup ? 'group-label' : 'label']">
<span v-if="item.component_props.required"> *</span>
{{ item.component_props.label }}
</div>
<div class="note-wrapper" v-if="item.component_props.note" v-html="item.component_props.note" />
<van-field v-model="item.value" :name="item.name" :type="item.type"
:placeholder="item.component_props.placeholder ? item.component_props.placeholder : '请输入'" :rules="item.rules"
:required="item.required"
:readonly="item.component_props.readonly || (item.component_props.is_camera_scan && !item.component_props.is_edit_camera_scan_result)"
:disabled="item.component_props.disabled" :input-align="item.component_props.align"
:right-icon="item.component_props.is_camera_scan ? 'scan' : ''" @click-right-icon="clickRightIcon" />
</div>
</template>
<script setup>
import { useRoute } from "vue-router";
import { showSuccessToast, showFailToast } from 'vant';
// 初始化WX环境
import wx from 'weixin-js-sdk'
const props = defineProps({
item: Object,
});
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
});
// 集合组标识
const isGroup = computed(() => {
return props.item.component_props.is_field_group
});
onMounted(() => {
props.item.value = props.item.component_props.default;
});
const $route = useRoute();
// 默认识别类型
const scan_type_code = ref('ALL_CODE');
// 微信二维码扫描功能判断
const scanType = (scan_type_code) => {
switch (scan_type_code) {
case 'ALL_CODE':
return ["qrCode", "barCode"]
case 'QR_CODE':
return ["qrCode"]
case 'BAR_CODE':
return ["barCode"]
}
}
// 预览模式
const model = $route.query.model;
const clickRightIcon = async () => {
// 预览模式屏蔽微信功能
if (model === 'preview') return false;
wx.ready(() => {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: scanType(props.item.camera_scan_type), // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
if (res.errMsg === 'scanQRCode:ok') {
let code = res.resultStr;
// let code_arr = code.split(",");
// props.item.value = code_arr[1];
props.item.value = code;
showSuccessToast('扫描成功')
} else {
console.warn(res);
showFailToast('扫描失败')
}
},
error: function (res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本过低请升级')
}
alert(res.errMsg);
},
});
});
}
</script>
<style lang="less" scoped>
.text-field-page {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
span {
color: red;
}
}
.group-label {
padding: 0.75rem 0 0.75rem 1rem;
font-size: 0.9rem;
font-weight: bold;
background-color: #f9f9f9;
color: #666;
border-top: 1px solid #eee;
border-bottom: 1px solid #eee;
span {
color: red;
}
}
.note-wrapper {
font-size: 0.9rem;
margin-left: 1rem;
color: gray;
padding-bottom: 0.5rem;
padding-top: 0.25rem;
white-space: pre-wrap;
}
}
:deep(.van-field__body) {
border: 1px solid #eaeaea;
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
}
</style>