|
@@ -134,13 +134,66 @@ export class InterestSearchComponent implements OnInit {
|
|
|
|
|
|
alertButtons = ['确定'];
|
|
|
|
|
|
- getRandomQuestionnaire() {
|
|
|
- const questionnaires = ['q1', 'q2', 'q3']; // List of your questionnaires
|
|
|
- const randomIndex = Math.floor(Math.random() * questionnaires.length); // Generate a random index
|
|
|
- return questionnaires[randomIndex]; // Return the randomly selected questionnaire ID
|
|
|
+ async ngOnInit() {
|
|
|
+ try {
|
|
|
+ // 重置所有状态
|
|
|
+ this.resetAllData();
|
|
|
+
|
|
|
+ // 加载新问卷
|
|
|
+ await this.loadQuestionnaireData('q1');
|
|
|
+
|
|
|
+ // 显示欢迎提示
|
|
|
+ const toast = document.createElement('ion-toast');
|
|
|
+ toast.message = '欢迎参与兴趣调查';
|
|
|
+ toast.duration = 2000;
|
|
|
+ toast.position = 'top';
|
|
|
+ toast.color = 'success';
|
|
|
+ document.body.appendChild(toast);
|
|
|
+ await toast.present();
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('初始化问卷失败:', error);
|
|
|
+ const toast = document.createElement('ion-toast');
|
|
|
+ toast.message = '加载问卷失败,请重试';
|
|
|
+ toast.duration = 2000;
|
|
|
+ toast.position = 'top';
|
|
|
+ toast.color = 'danger';
|
|
|
+ document.body.appendChild(toast);
|
|
|
+ await toast.present();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ // 添加重置所有数据的方法
|
|
|
+ private resetAllData() {
|
|
|
+ // 重置基本信息
|
|
|
+ this.name = '';
|
|
|
+ this.gender = '';
|
|
|
+ this.age = null;
|
|
|
+ this.birthday = '';
|
|
|
+ this.occupation = '';
|
|
|
+
|
|
|
+ // 重置问卷相关数据
|
|
|
+ this.answers = {};
|
|
|
+ this.questionsWithOptions = [];
|
|
|
+ this.questionnaire = null;
|
|
|
+
|
|
|
+ // 重置其他状态
|
|
|
+ this.modalIsOpen = false;
|
|
|
+ this.modalContent = '';
|
|
|
+ this.isAnalyzing = false;
|
|
|
+ this.aiAnalysisResult = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保留这个新的 getRandomQuestionnaire 方法
|
|
|
+ getRandomQuestionnaire(): string {
|
|
|
+ // 随机选择一个问卷ID
|
|
|
+ const randomIndex = Math.floor(Math.random() * this.questionnaires.length);
|
|
|
+ const selectedId = this.questionnaires[randomIndex];
|
|
|
+ console.log('随机选择的问卷索引:', randomIndex, '问卷ID:', selectedId);
|
|
|
+ return selectedId;
|
|
|
+ }
|
|
|
|
|
|
+ // 保留这个新的 loadQuestionnaireData 方法
|
|
|
async loadQuestionnaireData(questionnaireId: string) {
|
|
|
try {
|
|
|
console.log('开始加载问卷:', questionnaireId);
|
|
@@ -148,6 +201,7 @@ export class InterestSearchComponent implements OnInit {
|
|
|
// 清空现有数据
|
|
|
this.questionsWithOptions = [];
|
|
|
this.answers = {};
|
|
|
+ this.questionnaire = null;
|
|
|
|
|
|
const questionnaireQuery = new CloudQuery("Questionnaire");
|
|
|
questionnaireQuery.equalTo("QuestionnaireId", questionnaireId);
|
|
@@ -172,9 +226,52 @@ export class InterestSearchComponent implements OnInit {
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error("加载问卷数据时出错:", error);
|
|
|
- throw error; // 向上传递错误,让调用者处理
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保留这个新的 saveProgress 方法
|
|
|
+ async saveProgress() {
|
|
|
+ try {
|
|
|
+ const surveyProgress = new CloudObject("SurveyProgress");
|
|
|
+
|
|
|
+ surveyProgress.set({
|
|
|
+ userId: { __type: "Pointer", className: "_User", objectId: "user1" },
|
|
|
+ questionnaireId: this.questionnaire?.QuestionnaireId, // 保存当前问卷ID
|
|
|
+ answers: this.answers,
|
|
|
+ personalInfo: {
|
|
|
+ name: this.name,
|
|
|
+ gender: this.gender,
|
|
|
+ age: this.age,
|
|
|
+ birthday: this.birthday,
|
|
|
+ occupation: this.occupation
|
|
|
+ },
|
|
|
+ lastUpdated: new Date(),
|
|
|
+ isCompleted: false
|
|
|
+ });
|
|
|
+
|
|
|
+ await surveyProgress.save();
|
|
|
+
|
|
|
+ const toast = document.createElement('ion-toast');
|
|
|
+ toast.message = '进度保存成功';
|
|
|
+ toast.duration = 2000;
|
|
|
+ toast.position = 'top';
|
|
|
+ toast.color = 'success';
|
|
|
+ document.body.appendChild(toast);
|
|
|
+ await toast.present();
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存进度失败:', error);
|
|
|
+ const toast = document.createElement('ion-toast');
|
|
|
+ toast.message = '保存失败,请重试';
|
|
|
+ toast.duration = 2000;
|
|
|
+ toast.position = 'top';
|
|
|
+ toast.color = 'danger';
|
|
|
+ document.body.appendChild(toast);
|
|
|
+ await toast.present();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
async loadQuestions(questionIds: string[]) {
|
|
|
this.questionsWithOptions = []; // 初始化问题列表
|
|
|
|
|
@@ -247,7 +344,7 @@ export class InterestSearchComponent implements OnInit {
|
|
|
// 创建一个数组保存选的 OptionId
|
|
|
const answersArray: string[] = [];
|
|
|
|
|
|
- // 遍历每个问题,获取用户选择的选项
|
|
|
+ // 遍历每个问题,获取用户选择的选��
|
|
|
for (const question of this.questionsWithOptions) {
|
|
|
const selectedOptionId = this.answers[question.QuestionId];
|
|
|
if (selectedOptionId) {
|
|
@@ -366,7 +463,7 @@ export class InterestSearchComponent implements OnInit {
|
|
|
this.isComplete = true;
|
|
|
}
|
|
|
|
|
|
- // 如果消息完成且内容符合 JSON 格式,则解
|
|
|
+ // 如果消息完成且内容符合 JSON 格式,则解析
|
|
|
if (this.isComplete) {
|
|
|
const cleanedContent = fullContent.trim();
|
|
|
|
|
@@ -390,7 +487,7 @@ export class InterestSearchComponent implements OnInit {
|
|
|
if (typeof content === 'string') {
|
|
|
contentStr = content; // 如果已经是字符串,直接使用
|
|
|
} else if (typeof content === 'object') {
|
|
|
- // 如果对象,转换为 JSON 字符串
|
|
|
+ // 如果对,转换为 JSON 字符串
|
|
|
contentStr = JSON.stringify(content, null, 2); // 美化 JSON 字符串格式
|
|
|
count = 0;
|
|
|
this.isComplete = false;
|
|
@@ -439,66 +536,6 @@ export class InterestSearchComponent implements OnInit {
|
|
|
throw new Error("AI 模型调用失败");
|
|
|
}
|
|
|
}
|
|
|
- /*
|
|
|
- // 显示分析结果
|
|
|
- showAnalysisResult() {
|
|
|
- if (this.aiAnalysisResult) {
|
|
|
- this.showAlert(this.aiAnalysisResult.content); // 弹窗显示 AI 分析的内容
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- async showAlert(content: string) {
|
|
|
- //const formattedContent = this.formatContent(content); // 格式化内容
|
|
|
-
|
|
|
- const alert = await this.alertController.create({
|
|
|
- header: '兴趣分析结果',
|
|
|
- message: `${content}`, // 用 pre 标签来保持格式
|
|
|
- buttons: ['确定']
|
|
|
- });
|
|
|
-
|
|
|
- await alert.present();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 格式化 content 为可读的文本格式
|
|
|
- formatContent(content: string): string {
|
|
|
- try {
|
|
|
- // 尝试 content 解析为 JSON 对象
|
|
|
- const contentObj = JSON.parse(content);
|
|
|
-
|
|
|
- // 构建格式化后的文本
|
|
|
- let formattedContent = '';
|
|
|
-
|
|
|
- // 遍历 JSON 象,生成类似 "标签: 描述" 格式
|
|
|
- for (const [tag, description] of Object.entries(contentObj)) {
|
|
|
- formattedContent += `${tag}: \n${description}\n\n`;
|
|
|
- }
|
|
|
-
|
|
|
- // 将换行符转换为 <br/>
|
|
|
- return formattedContent.replace(/\n/g, '<br/>');
|
|
|
- } catch (e) {
|
|
|
- console.error('格式化 AI 响应时出错:', e);
|
|
|
- return '分析结果格式错误。';
|
|
|
- }
|
|
|
- }
|
|
|
-*//*
|
|
|
- // 格式化 AI 响应内容
|
|
|
- formatContent(content: string): string {
|
|
|
- try {
|
|
|
- const contentObj = JSON.parse(content);
|
|
|
- return Object.entries(contentObj)
|
|
|
- .map(
|
|
|
- ([key, value]) =>
|
|
|
- `<strong>${key}:</strong><br>${value}<br><br>`
|
|
|
- )
|
|
|
- .join('');
|
|
|
- } catch (error) {
|
|
|
- console.error('格式化 AI 响应时出错:', error);
|
|
|
- return '分析结果格式错误。';
|
|
|
- }
|
|
|
- }
|
|
|
- */
|
|
|
// 格式化 AI 响应内容
|
|
|
formatContent(content: string): string {
|
|
|
try {
|
|
@@ -556,7 +593,7 @@ export class InterestSearchComponent implements OnInit {
|
|
|
return '分析结果格式错误。';
|
|
|
}
|
|
|
}
|
|
|
- // 显示分析结果
|
|
|
+ // 显示分析��果
|
|
|
showAnalysisResult() {
|
|
|
if (this.aiAnalysisResult) {
|
|
|
this.modalContent = this.formatContent(
|
|
@@ -603,60 +640,6 @@ export class InterestSearchComponent implements OnInit {
|
|
|
await this.datetime.confirm(true);
|
|
|
}
|
|
|
|
|
|
- async saveProgress() {
|
|
|
- try {
|
|
|
- // 创建一个新的对象来保存问卷进度
|
|
|
- const surveyProgress = new CloudObject("SurveyProgress");
|
|
|
-
|
|
|
- // 保存当前的问卷状态
|
|
|
- surveyProgress.set({
|
|
|
- userId: { __type: "Pointer", className: "_User", objectId: "user1" }, // 替换为实际的用户ID
|
|
|
- questionnaireId: this.questionnaire?.QuestionnaireId,
|
|
|
- answers: this.answers,
|
|
|
- personalInfo: {
|
|
|
- name: this.name,
|
|
|
- gender: this.gender,
|
|
|
- age: this.age,
|
|
|
- birthday: this.birthday,
|
|
|
- occupation: this.occupation
|
|
|
- },
|
|
|
- lastUpdated: new Date(),
|
|
|
- isCompleted: false
|
|
|
- });
|
|
|
-
|
|
|
- await surveyProgress.save();
|
|
|
-
|
|
|
- // 显示保存成功提示
|
|
|
- const toast = document.createElement('ion-toast');
|
|
|
- toast.message = '进度保存成功';
|
|
|
- toast.duration = 2000;
|
|
|
- toast.position = 'top';
|
|
|
- toast.color = 'success';
|
|
|
- document.body.appendChild(toast);
|
|
|
- await toast.present();
|
|
|
-
|
|
|
- } catch (error) {
|
|
|
- console.error('保存进度失败:', error);
|
|
|
-
|
|
|
- // 显示错误提示
|
|
|
- const toast = document.createElement('ion-toast');
|
|
|
- toast.message = '保存失败,请重试';
|
|
|
- toast.duration = 2000;
|
|
|
- toast.position = 'top';
|
|
|
- toast.color = 'danger';
|
|
|
- document.body.appendChild(toast);
|
|
|
- await toast.present();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- async ngOnInit() {
|
|
|
- // 加载问卷数据
|
|
|
- await this.loadQuestionnaireData('q1');
|
|
|
-
|
|
|
- // 尝试加载保存的进度
|
|
|
- await this.loadSavedProgress();
|
|
|
- }
|
|
|
-
|
|
|
async loadSavedProgress() {
|
|
|
try {
|
|
|
const progressQuery = new CloudQuery("SurveyProgress");
|
|
@@ -696,44 +679,13 @@ export class InterestSearchComponent implements OnInit {
|
|
|
document.body.appendChild(loadingToast);
|
|
|
await loadingToast.present();
|
|
|
|
|
|
- await this.getNewQuestionnaire();
|
|
|
- } catch (error) {
|
|
|
- console.error('更新问卷失败:', error);
|
|
|
- const toast = document.createElement('ion-toast');
|
|
|
- toast.message = '更新问卷失败,请重试';
|
|
|
- toast.duration = 2000;
|
|
|
- toast.position = 'top';
|
|
|
- toast.color = 'danger';
|
|
|
- document.body.appendChild(toast);
|
|
|
- await toast.present();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 修改获取新问卷的方法
|
|
|
- private async getNewQuestionnaire() {
|
|
|
- try {
|
|
|
- // 获取当前问卷ID
|
|
|
- const currentId = this.questionnaire?.QuestionnaireId || 'q1';
|
|
|
-
|
|
|
- // 从问卷列表中随机选择一个不同的问卷
|
|
|
- let availableQuestionnaires = this.questionnaires.filter(id => id !== currentId);
|
|
|
- if (availableQuestionnaires.length === 0) {
|
|
|
- availableQuestionnaires = this.questionnaires;
|
|
|
- }
|
|
|
-
|
|
|
- const randomIndex = Math.floor(Math.random() * availableQuestionnaires.length);
|
|
|
- const newQuestionnaireId = availableQuestionnaires[randomIndex];
|
|
|
-
|
|
|
- console.log('当前问卷ID:', currentId);
|
|
|
- console.log('新问卷ID:', newQuestionnaireId);
|
|
|
-
|
|
|
// 重置状态
|
|
|
this.answers = {};
|
|
|
this.questionsWithOptions = [];
|
|
|
this.questionnaire = null;
|
|
|
|
|
|
- // 加载新问卷
|
|
|
- await this.loadQuestionnaireData(newQuestionnaireId);
|
|
|
+ // 重新加载 q1 问卷
|
|
|
+ await this.loadQuestionnaireData('q1');
|
|
|
|
|
|
// 显示成功提示
|
|
|
const toast = document.createElement('ion-toast');
|
|
@@ -745,8 +697,14 @@ export class InterestSearchComponent implements OnInit {
|
|
|
await toast.present();
|
|
|
|
|
|
} catch (error) {
|
|
|
- console.error('获取新问卷失败:', error);
|
|
|
- throw error; // 向上传递错误,让调用者处理
|
|
|
+ console.error('更新问卷失败:', error);
|
|
|
+ const toast = document.createElement('ion-toast');
|
|
|
+ toast.message = '更新问卷失败,请重试';
|
|
|
+ toast.duration = 2000;
|
|
|
+ toast.position = 'top';
|
|
|
+ toast.color = 'danger';
|
|
|
+ document.body.appendChild(toast);
|
|
|
+ await toast.present();
|
|
|
}
|
|
|
}
|
|
|
}
|