Commit 7375f7ca by 张立顺

feat 自测功能修复

1 parent b5c20a66
...@@ -22,7 +22,7 @@ export const RECRUIT_MENU_ENABLED = false ...@@ -22,7 +22,7 @@ export const RECRUIT_MENU_ENABLED = false
* 4预发布 * 4预发布
* 5cdptest * 5cdptest
*/ */
let index = 1; let index = 2;
const accountInfo = wx.getAccountInfoSync(); const accountInfo = wx.getAccountInfoSync();
const systemInfo = getSystemInfoSync(); const systemInfo = getSystemInfoSync();
const envName = accountInfo.miniProgram.envVersion; const envName = accountInfo.miniProgram.envVersion;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
const { fetchFieldOptions } = require('../../api/fieldOptionsService.js') const { fetchFieldOptions } = require('../../api/fieldOptionsService.js')
const { coerceMultipleSelectValue } = require('../../form-engine/multiple-select-value.js') const { coerceMultipleSelectValue } = require('../../form-engine/multiple-select-value.js')
const { resolveMultipleSelectItemDisplayLabel } = require('../../form-engine/multiple-select-display.helpers.js')
const { normalizeOptionRecord } = require('../../form-engine/field-submit-value.js') const { normalizeOptionRecord } = require('../../form-engine/field-submit-value.js')
const { const {
shouldRefreshOptionsOnPopup, shouldRefreshOptionsOnPopup,
...@@ -113,15 +114,16 @@ Component({ ...@@ -113,15 +114,16 @@ Component({
? normalizedValue.find(item => this.resolveItemKey(item) === selectedKey) ? normalizedValue.find(item => this.resolveItemKey(item) === selectedKey)
: null : null
labels.push( labels.push(
(rawItem && resolveMultipleSelectItemDisplayLabel({
typeof rawItem === 'object' && selectedKey,
(rawItem.name || rawItem.label)) || rawItem,
matched?.label || matched,
selectedKey hasOptionsKey: !!field?.optionsKey
})
) )
}) })
this.setData({ this.setData({
displayLabel: labels.length ? labels.join('/') : '', displayLabel: labels.filter(part => part != null && part !== '').join('/'),
pickerValueIds: selectedValues pickerValueIds: selectedValues
}) })
}, },
......
...@@ -508,7 +508,9 @@ function extractMultipleSelectIdsAndLabels(raw, field = {}) { ...@@ -508,7 +508,9 @@ function extractMultipleSelectIdsAndLabels(raw, field = {}) {
if (id !== '') { if (id !== '') {
ids.push(String(id)) ids.push(String(id))
} }
names.push(name || String(id)) if (name && !isMultipleSelectIdLikeLabel(name, id)) {
names.push(name)
}
} }
return return
} }
...@@ -518,7 +520,10 @@ function extractMultipleSelectIdsAndLabels(raw, field = {}) { ...@@ -518,7 +520,10 @@ function extractMultipleSelectIdsAndLabels(raw, field = {}) {
} }
ids.push(str) ids.push(str)
const matched = findStaticOption(field, str) const matched = findStaticOption(field, str)
names.push(matched?.label ?? matched?.name ?? str) const label = matched?.label ?? matched?.name
if (label && !isMultipleSelectIdLikeLabel(label, str)) {
names.push(label)
}
}) })
return { ids, displayLabel: names.filter(part => part != null && part !== '').join('/') } return { ids, displayLabel: names.filter(part => part != null && part !== '').join('/') }
} }
...@@ -535,12 +540,59 @@ function splitMultipleSelectDisplayLabel(value) { ...@@ -535,12 +540,59 @@ function splitMultipleSelectDisplayLabel(value) {
return trimmed.split('/').map(part => part.trim()).filter(part => part !== '') return trimmed.split('/').map(part => part.trim()).filter(part => part !== '')
} }
/** 展示名是否实为 id(jsonContent 误把 uuid 当 value 时不作为 label) */
function isMultipleSelectIdLikeLabel(label, id = '') {
if (label == null || label === '') {
return true
}
return String(label) === String(id)
}
/** `/` 展示串是否含至少一段真实姓名(非 id) */
function hasRealMultipleSelectDisplayNames(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel)
if (!names.length) {
return false
}
return names.some((name, index) => !isMultipleSelectIdLikeLabel(name, ids[index]))
}
/** 保存前剔除与 id 相同的展示段 */
function sanitizeMultipleSelectSlashLabel(ids = [], displayLabel = '') {
if (typeof displayLabel !== 'string' || displayLabel === '') {
return ''
}
const parts = displayLabel
.trim()
.split('/')
.map(part => part.trim())
const sanitized = ids
.map((id, index) => {
const part = parts[index]
if (part == null || part === '' || isMultipleSelectIdLikeLabel(part, id)) {
return ''
}
return part
})
.filter(Boolean)
return sanitized.join('/')
}
function zipMultipleSelectIdsWithDisplayLabel(ids = [], displayLabel = '') { function zipMultipleSelectIdsWithDisplayLabel(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel) const names = splitMultipleSelectDisplayLabel(displayLabel)
return ids.map((id, index) => ({ return ids.map((id, index) => {
id: String(id), const idStr = String(id)
name: names[index] != null && names[index] !== '' ? names[index] : String(id) const namePart = names[index]
})) const entry = { id: idStr }
if (
namePart != null &&
namePart !== '' &&
!isMultipleSelectIdLikeLabel(namePart, idStr)
) {
entry.name = namePart
}
return entry
})
} }
function resolveMultipleSelectEngineValueFromAppField(field = {}) { function resolveMultipleSelectEngineValueFromAppField(field = {}) {
...@@ -555,7 +607,7 @@ function resolveMultipleSelectEngineValueFromAppField(field = {}) { ...@@ -555,7 +607,7 @@ function resolveMultipleSelectEngineValueFromAppField(field = {}) {
return coerceMultipleSelectValue(meta) return coerceMultipleSelectValue(meta)
} }
const displayNames = splitMultipleSelectDisplayLabel(field.value) const displayNames = splitMultipleSelectDisplayLabel(field.value)
if (displayNames.length) { if (displayNames.length && hasRealMultipleSelectDisplayNames(meta, field.value)) {
return zipMultipleSelectIdsWithDisplayLabel(meta, field.value) return zipMultipleSelectIdsWithDisplayLabel(meta, field.value)
} }
return meta.map(id => String(id)) return meta.map(id => String(id))
...@@ -1336,7 +1388,7 @@ export function formatFieldForAppJsonContent(field = {}, options = {}) { ...@@ -1336,7 +1388,7 @@ export function formatFieldForAppJsonContent(field = {}, options = {}) {
return next return next
} }
next[key] = ids next[key] = ids
next.value = displayLabel next.value = sanitizeMultipleSelectSlashLabel(ids, displayLabel)
return next return next
} }
......
/**
* multipleSelect 展示名解析 — 纯函数,供组件回显与 jsonContent 编解码共用
* @see packageDynamicForm/docs/field-options-api.md
*/
/**
* 展示名是否实为 id(jsonContent 误把 uuid 写入 value 时不应作为 label)
* @param {*} label 候选展示名
* @param {*} [id] 对应选项 id
* @returns {boolean}
*/
function isMultipleSelectIdLikeLabel(label, id = '') {
if (label == null || label === '') {
return true
}
return String(label) === String(id)
}
/**
* 解析 multipleSelect 单选项展示文案;optionsKey 未匹配时不回退 id
* @param {Object} [params]
* @param {string} params.selectedKey 选中 id
* @param {*} [params.rawItem] 引擎值中的原始项
* @param {Object} [params.matched] 已加载 options 中的匹配项
* @param {boolean} [params.hasOptionsKey] 是否远程 optionsKey 字段
* @returns {string}
*/
function resolveMultipleSelectItemDisplayLabel({
selectedKey,
rawItem,
matched,
hasOptionsKey = false
} = {}) {
if (rawItem != null && typeof rawItem === 'object') {
const name = rawItem.name || rawItem.label
if (name && !isMultipleSelectIdLikeLabel(name, selectedKey)) {
return String(name)
}
}
if (matched?.label) {
return String(matched.label)
}
return hasOptionsKey ? '' : String(selectedKey || '')
}
/**
* `/` 拼接展示串中剔除与 id 相同的段(保存入库前防御)
* @param {Array<string>} [ids=[]]
* @param {string} [displayLabel='']
* @returns {string}
*/
function sanitizeMultipleSelectSlashLabel(ids = [], displayLabel = '') {
if (typeof displayLabel !== 'string' || displayLabel === '') {
return ''
}
const parts = displayLabel
.trim()
.split('/')
.map(part => part.trim())
const sanitized = ids
.map((id, index) => {
const part = parts[index]
if (part == null || part === '' || isMultipleSelectIdLikeLabel(part, id)) {
return ''
}
return part
})
.filter(Boolean)
return sanitized.join('/')
}
/** App jsonContent 双 key:`value` 为 `/` 拼接展示串 */
function splitMultipleSelectDisplayLabel(value) {
if (typeof value !== 'string' || value === '') {
return []
}
const trimmed = value.trim()
if (trimmed.startsWith('[')) {
return []
}
return trimmed.split('/').map(part => part.trim()).filter(part => part !== '')
}
/** `/` 展示串是否含至少一段真实姓名(非 id) */
function hasRealMultipleSelectDisplayNames(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel)
if (!names.length) {
return false
}
return names.some((name, index) => !isMultipleSelectIdLikeLabel(name, ids[index]))
}
/** id 数组 + `/` 展示串 → `{ id, name? }[]`(不把 id 当 name) */
function zipMultipleSelectIdsWithDisplayLabel(ids = [], displayLabel = '') {
const names = splitMultipleSelectDisplayLabel(displayLabel)
return ids.map((id, index) => {
const idStr = String(id)
const namePart = names[index]
const entry = { id: idStr }
if (
namePart != null &&
namePart !== '' &&
!isMultipleSelectIdLikeLabel(namePart, idStr)
) {
entry.name = namePart
}
return entry
})
}
module.exports = {
isMultipleSelectIdLikeLabel,
resolveMultipleSelectItemDisplayLabel,
sanitizeMultipleSelectSlashLabel,
splitMultipleSelectDisplayLabel,
hasRealMultipleSelectDisplayNames,
zipMultipleSelectIdsWithDisplayLabel
}
...@@ -55,8 +55,10 @@ export function normalizeMultipleSelectValuesInMap(list = [], values = {}) { ...@@ -55,8 +55,10 @@ export function normalizeMultipleSelectValuesInMap(list = [], values = {}) {
} }
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
const displayHelpers = require('./multiple-select-display.helpers.js')
module.exports = { module.exports = {
coerceMultipleSelectValue, coerceMultipleSelectValue,
normalizeMultipleSelectValuesInMap normalizeMultipleSelectValuesInMap,
...displayHelpers
} }
} }
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
isMultipleSelectIdLikeLabel,
resolveMultipleSelectItemDisplayLabel,
sanitizeMultipleSelectSlashLabel,
hasRealMultipleSelectDisplayNames,
zipMultipleSelectIdsWithDisplayLabel
} = require('../form-engine/multiple-select-display.helpers.js')
describe('F2-1 staff echo (id-only jsonContent)', () => {
it('IT-F21-ECHO-001: dual key with uuid slash label resolves to id array not named objects', () => {
const meta = ['uuid-1', 'uuid-2']
const result = hasRealMultipleSelectDisplayNames(meta, 'uuid-1/uuid-2')
? zipMultipleSelectIdsWithDisplayLabel(meta, 'uuid-1/uuid-2')
: meta.map(id => String(id))
assert.deepEqual(result, ['uuid-1', 'uuid-2'])
})
it('IT-F21-ECHO-001b: dual key with real names still zips id and name', () => {
const meta = ['staff-001', 'staff-002']
assert.equal(hasRealMultipleSelectDisplayNames(meta, '张三/李四'), true)
assert.deepEqual(zipMultipleSelectIdsWithDisplayLabel(meta, '张三/李四'), [
{ id: 'staff-001', name: '张三' },
{ id: 'staff-002', name: '李四' }
])
})
it('IT-F21-ECHO-002: resolveMultipleSelectItemDisplayLabel uses options not uuid fallback', () => {
const options = [
{ label: '张三', value: 'uuid-1', id: 'uuid-1', name: '张三' },
{ label: '李四', value: 'uuid-2', id: 'uuid-2', name: '李四' }
]
const labels = ['uuid-1', 'uuid-2'].map(selectedKey => {
const matched = options.find(item => String(item.value) === selectedKey)
return resolveMultipleSelectItemDisplayLabel({
selectedKey,
rawItem: selectedKey,
matched,
hasOptionsKey: true
})
})
assert.deepEqual(labels, ['张三', '李四'])
assert.equal(
resolveMultipleSelectItemDisplayLabel({
selectedKey: 'uuid-1',
rawItem: 'uuid-1',
matched: null,
hasOptionsKey: true
}),
''
)
})
it('IT-F21-ECHO-002b: sanitizeMultipleSelectSlashLabel strips id-like segments', () => {
assert.equal(
sanitizeMultipleSelectSlashLabel(['uuid-1', 'uuid-2'], 'uuid-1/uuid-2'),
''
)
assert.equal(
sanitizeMultipleSelectSlashLabel(['staff-001', 'staff-002'], '张三/李四'),
'张三/李四'
)
assert.equal(isMultipleSelectIdLikeLabel('uuid-1', 'uuid-1'), true)
assert.equal(isMultipleSelectIdLikeLabel('张三', 'uuid-1'), false)
})
it('IT-F21-ECHO-003: sanitize id-only slash label for jsonContent save', () => {
const ids = ['uuid-1', 'uuid-2']
const displayLabel = sanitizeMultipleSelectSlashLabel(ids, 'uuid-1/uuid-2')
assert.equal(displayLabel, '')
assert.deepEqual(ids, ['uuid-1', 'uuid-2'])
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!