|
|
|
@ -55,6 +55,22 @@ |
|
|
|
originalPersonality: '' |
|
|
|
} |
|
|
|
}, |
|
|
|
watch: { |
|
|
|
// 监听输入框内容变化,同步快捷选择状态 |
|
|
|
personalityDescription: { |
|
|
|
handler(newVal) { |
|
|
|
this.syncSelectedPersonalities(); |
|
|
|
}, |
|
|
|
immediate: true |
|
|
|
}, |
|
|
|
// 监听快捷选择选项加载完成 |
|
|
|
personalityOptions: { |
|
|
|
handler() { |
|
|
|
this.syncSelectedPersonalities(); |
|
|
|
}, |
|
|
|
immediate: true |
|
|
|
} |
|
|
|
}, |
|
|
|
onLoad(options) { |
|
|
|
// 接收传入的性格数据 |
|
|
|
if (options.personality) { |
|
|
|
@ -66,6 +82,24 @@ |
|
|
|
this.getPersonalityDataList(); |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
// 同步快捷选择状态 |
|
|
|
syncSelectedPersonalities() { |
|
|
|
if (!this.personalityDescription || this.personalityOptions.length === 0) { |
|
|
|
this.selectedPersonalities = []; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 将输入框内容按逗号分割 |
|
|
|
const descriptionItems = this.personalityDescription.split(',') |
|
|
|
.map(item => item.trim()) |
|
|
|
.filter(item => item.length > 0); |
|
|
|
|
|
|
|
// 找出哪些是快捷选择项 |
|
|
|
this.selectedPersonalities = descriptionItems.filter(item => |
|
|
|
this.personalityOptions.includes(item) |
|
|
|
); |
|
|
|
}, |
|
|
|
|
|
|
|
// 解析传入的性格数据 |
|
|
|
parsePersonality(personality) { |
|
|
|
if (!personality) return; |
|
|
|
@ -85,42 +119,60 @@ |
|
|
|
this.personalityOptions.includes(item) |
|
|
|
); |
|
|
|
|
|
|
|
// 描述部分是数组中不在快捷选择里的项 |
|
|
|
const descriptions = personality.filter(item => |
|
|
|
!this.personalityOptions.includes(item) |
|
|
|
); |
|
|
|
this.personalityDescription = descriptions.join(','); |
|
|
|
// 将所有内容都显示在输入框中 |
|
|
|
this.personalityDescription = personality.join(','); |
|
|
|
} else { |
|
|
|
// 兼容字符串形式的数据 |
|
|
|
const parts = personality.split(','); |
|
|
|
if (parts.length > 1) { |
|
|
|
// 最后一部分是快捷选择 |
|
|
|
const quickSelections = parts[parts.length - 1]; |
|
|
|
this.selectedPersonalities = this.personalityOptions.filter(item => |
|
|
|
quickSelections.includes(item) |
|
|
|
); |
|
|
|
|
|
|
|
// 前面的部分是描述 |
|
|
|
this.personalityDescription = parts.slice(0, -1).join(','); |
|
|
|
} else { |
|
|
|
// 只有快捷选择 |
|
|
|
this.selectedPersonalities = this.personalityOptions.filter(item => |
|
|
|
personality.includes(item) |
|
|
|
); |
|
|
|
} |
|
|
|
const parts = personality.split(','); |
|
|
|
|
|
|
|
// 识别哪些是快捷选择项 |
|
|
|
this.selectedPersonalities = parts.filter(item => |
|
|
|
this.personalityOptions.includes(item.trim()) |
|
|
|
); |
|
|
|
|
|
|
|
// 将所有内容都显示在输入框中 |
|
|
|
this.personalityDescription = personality; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 切换性格选择 |
|
|
|
togglePersonality(personality) { |
|
|
|
const index = this.selectedPersonalities.indexOf(personality); |
|
|
|
if (index > -1) { |
|
|
|
this.selectedPersonalities.splice(index, 1); |
|
|
|
const isSelected = this.selectedPersonalities.includes(personality); |
|
|
|
if (isSelected) { |
|
|
|
// 取消选择:从输入框中移除对应文本 |
|
|
|
this.removeFromDescription(personality); |
|
|
|
} else { |
|
|
|
// 选择:添加到输入框中 |
|
|
|
this.addToDescription(personality); |
|
|
|
} |
|
|
|
// selectedPersonalities会通过watch自动更新 |
|
|
|
}, |
|
|
|
|
|
|
|
// 添加性格到描述中 |
|
|
|
addToDescription(personality) { |
|
|
|
if (this.personalityDescription.trim()) { |
|
|
|
// 如果输入框已有内容,用逗号分隔 |
|
|
|
if (!this.personalityDescription.includes(personality)) { |
|
|
|
this.personalityDescription += ',' + personality; |
|
|
|
} |
|
|
|
} else { |
|
|
|
this.selectedPersonalities.push(personality); |
|
|
|
// 如果输入框为空,直接添加 |
|
|
|
this.personalityDescription = personality; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 从描述中移除性格 |
|
|
|
removeFromDescription(personality) { |
|
|
|
if (!this.personalityDescription.includes(personality)) return; |
|
|
|
|
|
|
|
// 将描述按逗号分割 |
|
|
|
let parts = this.personalityDescription.split(',').map(item => item.trim()); |
|
|
|
// 移除指定的性格 |
|
|
|
parts = parts.filter(item => item !== personality); |
|
|
|
// 重新组合 |
|
|
|
this.personalityDescription = parts.join(','); |
|
|
|
}, |
|
|
|
|
|
|
|
// 获取性格数据列表 |
|
|
|
getPersonalityDataList() { |
|
|
|
getDictList('pet_personality').then(res => { |
|
|
|
@ -139,24 +191,21 @@ |
|
|
|
savePersonality() { |
|
|
|
this.loading = true; |
|
|
|
|
|
|
|
// 组合性格描述和快捷选择,保持数组形式 |
|
|
|
let finalPersonality = []; |
|
|
|
|
|
|
|
if (this.personalityDescription.trim()) { |
|
|
|
finalPersonality.push(this.personalityDescription.trim()); |
|
|
|
} |
|
|
|
|
|
|
|
if (this.selectedPersonalities.length > 0) { |
|
|
|
finalPersonality = finalPersonality.concat(this.selectedPersonalities); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证是否至少选择了一项 |
|
|
|
if (finalPersonality.length === 0) { |
|
|
|
// 验证是否至少有内容 |
|
|
|
if (!this.personalityDescription.trim()) { |
|
|
|
this.$modal.showToast('请至少选择一项性格特征'); |
|
|
|
this.loading = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 将输入框内容按逗号分割,去重并过滤空值 |
|
|
|
const personalityItems = this.personalityDescription.split(',') |
|
|
|
.map(item => item.trim()) |
|
|
|
.filter(item => item.length > 0); |
|
|
|
|
|
|
|
// 去重处理 |
|
|
|
const finalPersonality = Array.from(new Set(personalityItems)); |
|
|
|
|
|
|
|
// 延迟一下模拟保存过程 |
|
|
|
setTimeout(() => { |
|
|
|
this.loading = false; |
|
|
|
@ -166,7 +215,7 @@ |
|
|
|
|
|
|
|
// 安全检查:确保有足够的页面 |
|
|
|
if (!pages || pages.length < 2) { |
|
|
|
console.warn('页面栈不足,无法获取上一页') |
|
|
|
console.warn('页面栈不足,无法获取上一页') |
|
|
|
uni.navigateBack(); |
|
|
|
return; |
|
|
|
} |
|
|
|
@ -301,4 +350,4 @@ |
|
|
|
border-radius: 8px; |
|
|
|
} |
|
|
|
} |
|
|
|
</style> |
|
|
|
</style> |