form-field-input.js
3.06 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
const DEFAULT_OCR_ICON =
'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/ocr_icon.png'
Component({
properties: {
field: {
type: Object,
value: {},
observer: 'observeField'
},
value: {
type: null,
observer: 'syncDisplayValue'
},
disabled: { type: Boolean, value: false },
error: { type: String, value: '' },
groupKey: { type: String, value: '' },
rowIndex: { type: Number, value: -1 }
},
data: {
displayValue: '',
showOcr: false,
scanIcon: '',
maxLength: 140
},
lifetimes: {
attached() {
const app = typeof getApp === 'function' ? getApp() : null
const imgUrl = app?.globalData?.IMG_URL || wx.$util?.IMG_URL || ''
this.setData({
scanIcon: imgUrl ? `${imgUrl}ocr_icon.png` : DEFAULT_OCR_ICON
})
}
},
methods: {
/**
* 字段配置变化时同步 OCR 展示态,并仅从模板 rules 读取输入长度。
* @param {Object} field 当前模板字段
*/
observeField(field = {}) {
const configuredMaxLength = Number(field.rules?.maxLength)
this.setData({
showOcr: field.prefixComponent === 'scan' && !this.data.disabled,
maxLength: configuredMaxLength > 0 ? configuredMaxLength : 140
})
},
syncDisplayValue(value) {
const displayValue =
value === null || value === undefined ? '' : String(value)
this.setData({ displayValue })
},
onInput(e) {
if (this.data.disabled) return
const rawValue = e.detail.value
const field = this.properties.field || {}
this.setData({ displayValue: rawValue })
const detail = { key: field.key, value: rawValue }
if (this.properties.groupKey) {
detail.groupKey = this.properties.groupKey
const rowIndex = Number(this.properties.rowIndex)
if (!Number.isNaN(rowIndex) && rowIndex >= 0) {
detail.rowIndex = rowIndex
}
}
this.triggerEvent('change', detail)
},
onBlur() {
const field = this.properties.field || {}
const detail = {
key: field.key,
value: this.data.displayValue
}
if (this.properties.groupKey) {
detail.groupKey = this.properties.groupKey
const rowIndex = Number(this.properties.rowIndex)
if (!Number.isNaN(rowIndex) && rowIndex >= 0) {
detail.rowIndex = rowIndex
}
}
this.triggerEvent('blur', detail)
},
onOcr() {
if (this.data.disabled) return
const pages = getCurrentPages()
const page =
pages.find(p => typeof p.openVinOcrForField === 'function') ||
pages[pages.length - 1]
if (!page || typeof page.openVinOcrForField !== 'function') {
return wx.$util?.showToast?.('当前页面不支持识别')
}
const field = this.properties.field || {}
const groupKey = this.properties.groupKey || undefined
let rowIndex = Number(this.properties.rowIndex)
if (groupKey && (Number.isNaN(rowIndex) || rowIndex < 0)) {
const active = page.data?.groupActiveIndex?.[groupKey]
rowIndex =
active != null && active !== '' && !Number.isNaN(Number(active))
? Number(active)
: 0
}
page.openVinOcrForField({
childKey: field.key,
groupKey,
rowIndex: groupKey ? rowIndex : undefined
})
}
}
})