index.vue
5.96 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!--
* @Date: 2022-08-29 14:31:20
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-04-03 13:17:42
* @FilePath: /custom_form/src/components/TextField/index.vue
* @Description: 单行文本输入框(微信扫描功能)
-->
<template>
<div v-if="HideShow" class="text-field-page">
<div class="label">
<text v-if="item.component_props.required"> *</text>
{{ item.component_props.label }}
</div>
<!-- <div class="note-wrapper" v-if="item.component_props.note" v-html="item.component_props.note" /> -->
<nut-form-item :required="item.required">
<nut-input
v-model="input_value"
:type="item.type"
:border="false"
:placeholder="item.component_props.placeholder ? item.component_props.placeholder : '请输入'"
: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"
style="border: 1px solid #eaeaea; border-radius: 0.25rem; padding: 0.25rem 0.5rem;"
>
<template #right v-if="item.component_props.is_camera_scan"><Scan @click="clickRightIcon()"></Scan></template>
</nut-input>
<div
v-if="show_error"
style="padding: 5px; color: red; font-size: 12px;"
>
{{ error_msg }}
</div>
</nut-form-item>
</div>
</template>
<script setup>
import { getUrlParams } from "@/utils/tools";
import Taro from '@tarojs/taro'
import { ref, computed, watch, onMounted, reactive } from "vue";
import { Scan } from '@nutui/icons-vue-taro';
// 初始化WX环境
import wx from 'weixin-js-sdk'
const props = defineProps({
item: Object,
});
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
})
const emit = defineEmits(["active"]);
const input_value = ref('');
// 错误提示
const show_error = ref(false);
const error_msg = ref('');
// 默认识别类型
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 = getUrlParams(location.href) ? getUrlParams(location.href).model : '';
const clickRightIcon = () => {
// 预览模式屏蔽微信功能
if (model === 'preview') return false;
if (process.env.TARO_ENV === 'h5') {
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];
Taro.showToast({
title: '扫描成功',
icon: 'success',
duration: 2000
})
} else {
console.warn(res);
Taro.showToast({
title: '扫描失败',
icon: 'error',
duration: 2000
})
}
},
error: function (res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本过低请升级')
}
alert(res.errMsg);
},
});
});
}
if (process.env.TARO_ENV === 'weapp') {
Taro.scanCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: scanType(props.item.camera_scan_type), // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
if (res.errMsg === 'scanCode:ok') {
let code = res.result;
input_value.value = code;
Taro.showToast({
title: '扫描成功',
icon: 'success',
duration: 2000
})
} else {
console.warn(res);
Taro.showToast({
title: '扫描失败',
icon: 'error',
duration: 2000
})
}
},
error: function (res) {
if (res.errMsg.indexOf('function_not_exist') > 0) {
alert('版本过低请升级')
}
alert(res.errMsg);
},
});
}
}
// watch(
// () => input_value.value,
// (newValue, oldValue) => {
// console.warn(newValue);
// // props.item.value = {
// // key: "input",
// // filed_name: props.item.key,
// // value: newValue,
// // };
// // emit("active", props.item.value);
// },
// { immediate: true }
// );
onMounted(() => {
})
const changeInput = (val) => {
// props.item.value = {
// key: "input",
// filed_name: props.item.key,
// value: val,
// };
// emit("active", props.item.value);
// validInput();
}
// 校验模块
const validInput = () => {
// 必填项
if (props.item.component_props.required && !input_value.value) {
show_error.value = true;
error_msg.value = '必填项不能为空'
} else {
show_error.value = false;
error_msg.value = ''
}
return !show_error.value;
};
defineExpose({ validInput });
</script>
<style lang="less">
.text-field-page {
.label {
padding: 30px 30px 0 30px;
font-size: 26px;
font-weight: bold;
text {
color: red;
}
.note-wrapper {
font-size: 24px;
margin-left: 30px;
color: gray;
padding-bottom: 15px;
padding-top: 7px;
white-space: pre-wrap;
}
}
}
// :deep(.nut-input) {
// border: 1px solid #eaeaea;
// border-radius: 0.25rem;
// padding: 0.25rem 0.5rem;
// }
// .nut-input--border {
// border: 1px solid #eaeaea;
// border-radius: 7px;
// padding: 7px 14px !important;
// }
input {
color: #000 !important;
}
</style>