Skip to content

Commit 1a27697

Browse files
committed
[dev] 利用id:内容的方式将other选项传递到后端
1 parent b082846 commit 1a27697

File tree

4 files changed

+111
-8
lines changed

4 files changed

+111
-8
lines changed

prompto-lab-app/src/main/java/io/github/timemachinelab/core/session/infrastructure/web/dto/UnifiedAnswerRequest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import lombok.AllArgsConstructor;
88

99
import javax.validation.constraints.NotBlank;
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
1012
import java.util.List;
1113
import java.util.Map;
1214

@@ -136,7 +138,12 @@ public String getAnswerString() {
136138

137139
switch (questionType.toLowerCase()) {
138140
case "single":
141+
// 单选直接返回字符串(已经是id:content格式)
142+
String singleChoice = getInputAnswer();
143+
return singleChoice != null ? singleChoice : "";
144+
139145
case "multi":
146+
// 多选返回逗号分隔的字符串(每个都是id:content格式)
140147
List<String> choices = getChoiceAnswer();
141148
return choices != null && !choices.isEmpty() ? String.join(", ", choices) : "";
142149

@@ -175,6 +182,12 @@ public String toReadableText() {
175182

176183
switch (questionType.toLowerCase()) {
177184
case "single":
185+
String singleChoice = getInputAnswer();
186+
if (singleChoice != null) {
187+
sb.append("选择了:").append(singleChoice);
188+
}
189+
break;
190+
178191
case "multi":
179192
List<String> choices = getChoiceAnswer();
180193
if (choices != null && !choices.isEmpty()) {
@@ -203,4 +216,79 @@ public String toReadableText() {
203216

204217
return sb.toString();
205218
}
219+
220+
/**
221+
* 解析选项字符串,提取ID和内容
222+
* @param optionString 格式为"id:content"的字符串
223+
* @return 包含id和content的Map
224+
*/
225+
public static Map<String, String> parseOptionString(String optionString) {
226+
Map<String, String> result = new HashMap<>();
227+
if (optionString != null && optionString.contains(":")) {
228+
String[] parts = optionString.split(":", 2);
229+
result.put("id", parts[0]);
230+
result.put("content", parts[1]);
231+
}
232+
return result;
233+
}
234+
235+
/**
236+
* 获取选择题的所有选项ID
237+
* @return 选项ID列表
238+
*/
239+
public List<String> getChoiceIds() {
240+
List<String> ids = new ArrayList<>();
241+
242+
if ("single".equals(questionType)) {
243+
String singleChoice = getInputAnswer();
244+
if (singleChoice != null) {
245+
Map<String, String> parsed = parseOptionString(singleChoice);
246+
if (!parsed.isEmpty()) {
247+
ids.add(parsed.get("id"));
248+
}
249+
}
250+
} else if ("multi".equals(questionType)) {
251+
List<String> choices = getChoiceAnswer();
252+
if (choices != null) {
253+
for (String choice : choices) {
254+
Map<String, String> parsed = parseOptionString(choice);
255+
if (!parsed.isEmpty()) {
256+
ids.add(parsed.get("id"));
257+
}
258+
}
259+
}
260+
}
261+
262+
return ids;
263+
}
264+
265+
/**
266+
* 获取选择题的所有选项内容
267+
* @return 选项内容列表
268+
*/
269+
public List<String> getChoiceContents() {
270+
List<String> contents = new ArrayList<>();
271+
272+
if ("single".equals(questionType)) {
273+
String singleChoice = getInputAnswer();
274+
if (singleChoice != null) {
275+
Map<String, String> parsed = parseOptionString(singleChoice);
276+
if (!parsed.isEmpty()) {
277+
contents.add(parsed.get("content"));
278+
}
279+
}
280+
} else if ("multi".equals(questionType)) {
281+
List<String> choices = getChoiceAnswer();
282+
if (choices != null) {
283+
for (String choice : choices) {
284+
Map<String, String> parsed = parseOptionString(choice);
285+
if (!parsed.isEmpty()) {
286+
contents.add(parsed.get("content"));
287+
}
288+
}
289+
}
290+
}
291+
292+
return contents;
293+
}
206294
}

prompto-lab-ui/src/components/Chat/MultipleChoiceOptions.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@click="toggleOption(option.id)"
99
class="option-item"
1010
:class="{
11-
selected: selectedValues.includes(option.id),
11+
selected: selectedValues.some(value => value.startsWith(`${option.id}:`)),
1212
disabled: disabled
1313
}"
1414
>
@@ -28,7 +28,7 @@
2828
@click="toggleOption(customOption.id)"
2929
class="option-item custom-option"
3030
:class="{
31-
selected: selectedValues.includes(customOption.id),
31+
selected: selectedValues.some(value => value.startsWith(`${customOption.id}:`)),
3232
disabled: disabled
3333
}"
3434
>
@@ -127,8 +127,9 @@ const customOptionCounter = ref(1)
127127
const toggleOption = (optionId: string) => {
128128
if (props.disabled) return
129129
130+
const allOptions = [...props.options, ...customOptions.value]
130131
const currentValues = [...props.selectedValues]
131-
const index = currentValues.indexOf(optionId)
132+
const index = currentValues.findIndex(value => value.startsWith(`${optionId}:`))
132133
133134
if (index > -1) {
134135
// 取消选择
@@ -138,7 +139,13 @@ const toggleOption = (optionId: string) => {
138139
if (props.maxSelections && currentValues.length >= props.maxSelections) {
139140
return // 达到最大选择数量
140141
}
141-
currentValues.push(optionId)
142+
143+
const selectedOption = allOptions.find(option => option.id === optionId)
144+
if (selectedOption) {
145+
// 格式化为 "id:content" 字符串
146+
const formattedValue = `${selectedOption.id}:${selectedOption.label}`
147+
currentValues.push(formattedValue)
148+
}
142149
}
143150
144151
emit('update:selectedValues', currentValues)

prompto-lab-ui/src/components/Chat/SingleChoiceOptions.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,16 @@ const customOptionCounter = ref(1)
125125
const selectOption = (optionId: string) => {
126126
if (props.disabled) return
127127
128-
emit('update:selectedValue', optionId)
129-
emit('change', optionId)
128+
// 查找选中的选项
129+
const allOptions = [...props.options, ...customOptions.value]
130+
const selectedOption = allOptions.find(option => option.id === optionId)
131+
132+
if (selectedOption) {
133+
// 格式化为 "id:content" 字符串
134+
const formattedValue = `${selectedOption.id}:${selectedOption.label}`
135+
emit('update:selectedValue', formattedValue)
136+
emit('change', formattedValue)
137+
}
130138
}
131139
132140
const startAddOption = () => {

prompto-lab-ui/src/services/userInteractionApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export const createAnswerRequest = {
221221
nodeId,
222222
userId,
223223
questionType: 'single',
224-
answer: [selectedOption]
224+
answer: selectedOption // 直接传递格式化后的字符串
225225
}),
226226

227227
/**
@@ -232,7 +232,7 @@ export const createAnswerRequest = {
232232
nodeId,
233233
userId,
234234
questionType: 'multi',
235-
answer: selectedOptions
235+
answer: selectedOptions // 直接传递格式化后的字符串数组
236236
}),
237237

238238
/**

0 commit comments

Comments
 (0)