feat:编辑资料页 form 增加库字段并对老用户兼容初始化
- form reactive 新增 personalityTagLibrary 和 hobbyLibrary 字段 - syncFromStore 增加库字段加载逻辑,后端有则用后端数据 - 老用户(后端无库字段)使用预设 + 已选合并去重初始化 - 新增 buildInitialLibrary 辅助函数处理数组合并去重
This commit is contained in:
@@ -225,6 +225,8 @@ const form = reactive({
|
||||
company: '',
|
||||
personalityTags: [],
|
||||
hobbies: [],
|
||||
personalityTagLibrary: [], // 新增
|
||||
hobbyLibrary: [], // 新增
|
||||
childhood: { date: '', text: '' },
|
||||
joy: { date: '', text: '' },
|
||||
low: { date: '', text: '' },
|
||||
@@ -243,8 +245,32 @@ const birthdayDisplay = computed(() => {
|
||||
return `${year}年${month}月${day}日`
|
||||
})
|
||||
|
||||
/**
|
||||
* 合并预设数组与已选数组,去重返回完整库
|
||||
*/
|
||||
const buildInitialLibrary = (preset, selected) => {
|
||||
const library = [...preset]
|
||||
if (Array.isArray(selected)) {
|
||||
selected.forEach((tag) => {
|
||||
if (!library.includes(tag)) library.push(tag)
|
||||
})
|
||||
}
|
||||
return library
|
||||
}
|
||||
|
||||
const syncFromStore = () => {
|
||||
const source = store.userProfile || store.registrationData || {}
|
||||
|
||||
// 性格标签库:后端有则用后端,否则用预设 + 已选合并(老用户兼容)
|
||||
const personalityTagLibrary = Array.isArray(source.personalityTagLibrary) && source.personalityTagLibrary.length > 0
|
||||
? [...source.personalityTagLibrary]
|
||||
: buildInitialLibrary(personalityTags, source.personalityTags)
|
||||
|
||||
// 兴趣爱好库:后端有则用后端,否则用预设 + 已选合并(老用户兼容)
|
||||
const hobbyLibrary = Array.isArray(source.hobbyLibrary) && source.hobbyLibrary.length > 0
|
||||
? [...source.hobbyLibrary]
|
||||
: buildInitialLibrary(hobbyOptions, source.hobbies)
|
||||
|
||||
Object.assign(form, {
|
||||
nickname: source.nickname || '',
|
||||
gender: source.gender || '',
|
||||
@@ -256,6 +282,8 @@ const syncFromStore = () => {
|
||||
company: source.company || '',
|
||||
personalityTags: Array.isArray(source.personalityTags) ? [...source.personalityTags] : [],
|
||||
hobbies: Array.isArray(source.hobbies) ? [...source.hobbies] : [],
|
||||
personalityTagLibrary,
|
||||
hobbyLibrary,
|
||||
childhood: { date: source.childhood?.date || '', text: source.childhood?.text || '' },
|
||||
joy: { date: source.joy?.date || '', text: source.joy?.text || '' },
|
||||
low: { date: source.low?.date || '', text: source.low?.text || '' },
|
||||
|
||||
Reference in New Issue
Block a user