|
|
// Keep option identifiers in answers; labels and scores belong to the server schema.
|
|
|
export function blank(value) {
|
|
|
return (
|
|
|
value === null ||
|
|
|
value === undefined ||
|
|
|
(typeof value === 'string' && !value.trim()) ||
|
|
|
(Array.isArray(value) && !value.length)
|
|
|
)
|
|
|
}
|
|
|
export function decode(value, fallback) {
|
|
|
if (typeof value !== 'string') return value || fallback
|
|
|
try {
|
|
|
return JSON.parse(value)
|
|
|
} catch (e) {
|
|
|
return fallback
|
|
|
}
|
|
|
}
|
|
|
export function prepareQuestions(questions, answered = false) {
|
|
|
return (questions || []).map((q) => ({
|
|
|
...q,
|
|
|
config: decode(q.config, {}),
|
|
|
select_item: decode(q.select_item, []),
|
|
|
dimensions: decode(q.dimensions, []).map((d) => ({
|
|
|
...d,
|
|
|
edit_input: d.edit_input || 'radio',
|
|
|
need_fill: required(q),
|
|
|
rule: required(q) ? 'required' : '',
|
|
|
select_item: decode(d.select_item, []),
|
|
|
config: decode(d.config, {})
|
|
|
})),
|
|
|
value:
|
|
|
answered ? restoredValue(q) : q.edit_input === 'multi_dimension'
|
|
|
? {}
|
|
|
: ['checkbox', 'file'].includes(q.edit_input)
|
|
|
? []
|
|
|
: '',
|
|
|
other_text: answered ? (q.other_text || '') : '',
|
|
|
option_texts: answered ? decode(q.option_texts, {}) : {},
|
|
|
show_other: false
|
|
|
}))
|
|
|
}
|
|
|
// Prefill only a new response; saved answers always take precedence.
|
|
|
export function fillUserFields(questions, user) {
|
|
|
const allowed = ['name', 'mobile', 'no', 'sex', 'birthday', 'email', 'company_name', 'company_position', 'idcard', 'plate']
|
|
|
questions.forEach(q => {
|
|
|
const key = (q.config || {}).user_field
|
|
|
if (!allowed.includes(key) || !['text', 'textarea', 'date'].includes(q.edit_input) || !blank(q.value)) return
|
|
|
const value = user[key]
|
|
|
if (!blank(value)) q.value = Array.isArray(value) ? value.join('、') : String(value)
|
|
|
})
|
|
|
return questions
|
|
|
}
|
|
|
function restoredValue(field) {
|
|
|
if (field.edit_input === 'multi_dimension') {
|
|
|
const values = decode(field.value, {})
|
|
|
return decode(field.dimensions, []).reduce((result, dim) => {
|
|
|
result[dim.field] = restoredValue({ ...dim, value: values[dim.field] })
|
|
|
return result
|
|
|
}, {})
|
|
|
}
|
|
|
if (field.edit_input === 'checkbox') return (field.selected_keys || []).map(String)
|
|
|
if (field.edit_input === 'radio' || !field.edit_input) return String((field.selected_keys || [])[0] ?? '')
|
|
|
return field.value == null ? '' : field.value
|
|
|
}
|
|
|
export function conditionValues(condition) {
|
|
|
return (condition.values || [condition.value]).map(String)
|
|
|
}
|
|
|
export function required(field, questions) {
|
|
|
const condition = (field.config || {}).visible_when
|
|
|
if (questions && condition && Array.isArray(condition.required_values)) {
|
|
|
const shown = visibleQuestions(questions)
|
|
|
const parent = shown.find(q => q.field === condition.field)
|
|
|
if (!shown.includes(field) || !parent) return false
|
|
|
const selected = (Array.isArray(parent.value) ? parent.value : [parent.value]).map(String)
|
|
|
return condition.required_values.map(String).some(key => selected.includes(key))
|
|
|
}
|
|
|
return (
|
|
|
(field.need_fill === true || field.need_fill === 1 || field.need_fill === '1') ||
|
|
|
(field.rule || '').split(',').includes('required')
|
|
|
)
|
|
|
}
|
|
|
export function showOther(field, value) {
|
|
|
const key = (field.config || {}).other_key
|
|
|
if (blank(key)) return field.allow_input === true || field.allow_input === 1 || field.allow_input === '1'
|
|
|
return (Array.isArray(value) ? value : [value])
|
|
|
.map(String)
|
|
|
.includes(String(key))
|
|
|
}
|
|
|
export function validateField(field, value) {
|
|
|
if (blank(value)) return required(field) ? '请填写此项' : ''
|
|
|
if (field.edit_input === 'file') return !Array.isArray(value) || value.length > ((field.config || {}).max_files || 1) ? '上传文件数量不正确' : ''
|
|
|
if (field.edit_input === 'checkbox' && Array.isArray(value) && value.length < Number((field.config || {}).min_choices || 1)) return '至少选择' + ((field.config || {}).min_choices || 1) + '项'
|
|
|
if (field.edit_input === 'checkbox' && (field.config || {}).max_choices && Array.isArray(value) && value.length > Number(field.config.max_choices)) return '最多选择' + field.config.max_choices + '项'
|
|
|
if (field.edit_input === 'number') {
|
|
|
const n = Number(value)
|
|
|
const c = field.config || {}
|
|
|
if (!Number.isFinite(n)) return '请输入有效数字'
|
|
|
if (c.integer && !Number.isInteger(n)) return '请输入整数'
|
|
|
if (!blank(c.min) && n < Number(c.min)) return '不能小于 ' + c.min
|
|
|
if (!blank(c.max) && n > Number(c.max)) return '不能大于 ' + c.max
|
|
|
}
|
|
|
const rule = (field.rule || '').split(',')
|
|
|
if (rule.includes('integer') && !/^-?\d+$/.test(String(value)))
|
|
|
return '请输入整数'
|
|
|
if (rule.includes('numeric') && !Number.isFinite(Number(value)))
|
|
|
return '请输入数字'
|
|
|
if (rule.includes('mobile') && !/^1[3-9]\d{9}$/.test(String(value)))
|
|
|
return '手机号格式不正确'
|
|
|
if (
|
|
|
rule.includes('email') &&
|
|
|
!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(value))
|
|
|
)
|
|
|
return '邮箱格式不正确'
|
|
|
if (rule.includes('idcard') && !/^(\d{15}|\d{17}[\dXx])$/.test(String(value)))
|
|
|
return '身份证格式不正确'
|
|
|
return ''
|
|
|
}
|
|
|
export function visibleQuestions(questions) {
|
|
|
const shown = []
|
|
|
return questions.filter(q => {
|
|
|
const condition = (q.config || {}).visible_when
|
|
|
if (condition) {
|
|
|
const parent = shown.find(item => item.field === condition.field)
|
|
|
if (!parent || !conditionValues(condition).some(key => (Array.isArray(parent.value) ? parent.value : [parent.value]).map(String).includes(key))) return false
|
|
|
}
|
|
|
shown.push(q)
|
|
|
return true
|
|
|
})
|
|
|
}
|
|
|
export function selectedNotes(q) {
|
|
|
const values = (Array.isArray(q.value) ? q.value : [q.value]).map(String)
|
|
|
return (q.select_item || []).filter(o => values.includes(String(o.key)) && (q.config || {}).option_notes && q.config.option_notes[o.key])
|
|
|
}
|
|
|
export function firstError(questions) {
|
|
|
for (const q of visibleQuestions(questions)) {
|
|
|
if (q.edit_input === 'section') continue
|
|
|
const fields = q.edit_input === 'multi_dimension' ? q.dimensions : [q]
|
|
|
for (const field of fields) {
|
|
|
const value =
|
|
|
q.edit_input === 'multi_dimension' ? q.value[field.field] : q.value
|
|
|
const need = required(q, questions)
|
|
|
const rules = (field.rule || '').split(',').filter(rule => rule !== 'required')
|
|
|
const effective = { ...field, need_fill: need, rule: [...rules, ...(need ? ['required'] : [])].join(',') }
|
|
|
const error = validateField(effective, value)
|
|
|
if (error)
|
|
|
return {
|
|
|
id: q.id,
|
|
|
message:
|
|
|
q.name + (field !== q ? ' / ' + field.name : '') + ':' + error
|
|
|
}
|
|
|
}
|
|
|
for (const option of selectedNotes(q)) {
|
|
|
if (q.config.option_notes[option.key].required && blank((q.option_texts || {})[option.key])) return { id: q.id, message: q.name + ':请填写补充说明' }
|
|
|
}
|
|
|
if (
|
|
|
showOther(q, q.value) &&
|
|
|
(q.config || {}).other_required &&
|
|
|
blank(q.other_text)
|
|
|
)
|
|
|
return { id: q.id, message: q.name + ':请填写补充说明' }
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
|
export function progress(questions) {
|
|
|
let total = 0
|
|
|
let answered = 0
|
|
|
for (const q of visibleQuestions(questions)) {
|
|
|
if (q.edit_input === 'section') continue
|
|
|
const fields = q.edit_input === 'multi_dimension' ? q.dimensions : [q]
|
|
|
total++
|
|
|
const complete = fields.length > 0 && fields.every((f) => {
|
|
|
const value =
|
|
|
q.edit_input === 'multi_dimension' ? q.value[f.field] : q.value
|
|
|
return !blank(value) && !validateField(f, value)
|
|
|
})
|
|
|
const supplementComplete = !showOther(q, q.value) ||
|
|
|
!(q.config || {}).other_required || !blank(q.other_text)
|
|
|
if (complete && supplementComplete) answered++
|
|
|
}
|
|
|
return {
|
|
|
total,
|
|
|
answered,
|
|
|
percent: total ? Math.round((answered * 100) / total) : 0
|
|
|
}
|
|
|
}
|
|
|
export function submission(questions) {
|
|
|
return visibleQuestions(questions)
|
|
|
.filter((q) => q.edit_input !== 'section')
|
|
|
.map((q) => {
|
|
|
let value = q.value
|
|
|
if (q.edit_input === 'number' && !blank(value)) value = Number(value)
|
|
|
if (q.edit_input === 'multi_dimension') {
|
|
|
value = {}
|
|
|
q.dimensions.forEach((d) => {
|
|
|
const v = q.value[d.field]
|
|
|
value[d.field] = blank(v)
|
|
|
? null
|
|
|
: d.edit_input === 'number'
|
|
|
? Number(v)
|
|
|
: v
|
|
|
})
|
|
|
}
|
|
|
return {
|
|
|
id: q.id,
|
|
|
value,
|
|
|
option_texts: selectedNotes(q).reduce((texts, opt) => { texts[opt.key] = (q.option_texts || {})[opt.key] || ''; return texts }, {}),
|
|
|
other_text: showOther(q, q.value) ? q.other_text : ''
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// Normalize presentation without changing the saved editor content.
|
|
|
export function descriptionHtml(value) {
|
|
|
const text = String(value || '')
|
|
|
if (!/<[a-z][\s\S]*>/i.test(text)) {
|
|
|
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\n/g, '<br>')
|
|
|
}
|
|
|
return text.replace(/<([a-z][\w:-]*)(\s[^<>]*?)?\s*\/?>/gi, (tag, name, attributes) => {
|
|
|
let attrs = (attributes || '').replace(/\/\s*$/, '')
|
|
|
let styles = ''
|
|
|
attrs = attrs.replace(/\sstyle\s*=\s*("[^"]*"|'[^']*')/gi, (match, quoted) => {
|
|
|
styles = quoted.slice(1, -1).split(';').filter(part => !/^\s*(font|font-family|font-size|line-height)\s*:/i.test(part)).join(';')
|
|
|
return ''
|
|
|
})
|
|
|
if (name.toLowerCase() === 'font') attrs = attrs.replace(/\s(?:face|size)\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, '')
|
|
|
styles += ';font-family:-apple-system,BlinkMacSystemFont,Arial,sans-serif;font-size:14px;line-height:1.6'
|
|
|
return '<' + name + attrs + ' style="' + styles.replace(/"/g, '"') + '">'
|
|
|
})
|
|
|
}
|