form-field-input.js 3.06 KB
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
			})
		}
	}
})