|
|
@@ -1,58 +1,290 @@
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
-import { IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonItem, IonList } from '@ionic/angular/standalone';
|
|
|
-import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
+import { IonContent,IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonItem, IonList, IonButton } from '@ionic/angular/standalone';
|
|
|
+import { CloudObject, CloudQuery,CloudUser } from 'src/lib/ncloud';
|
|
|
+import { Router } from '@angular/router';
|
|
|
+
|
|
|
+// 定义 Option 接口,扩展 CloudObject
|
|
|
+interface Option extends CloudObject {
|
|
|
+ selected?: boolean; // 添加 selected 属性
|
|
|
+}
|
|
|
+
|
|
|
+// 定义 DimensionWeight 接口
|
|
|
+interface DimensionWeight {
|
|
|
+ dimension: string; // 存储维度
|
|
|
+ totalWeight: number; // 存储权重总和
|
|
|
+}
|
|
|
+
|
|
|
+// 定义 Question 接口,扩展 CloudObject
|
|
|
+interface Question extends CloudObject {
|
|
|
+ selectedOptionWeight?: number | null; // 选项权重
|
|
|
+ dimensionWeights?: DimensionWeight[]; // 存储每个维度的权重总和
|
|
|
+}
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-personality-test',
|
|
|
templateUrl: './personality-test.component.html',
|
|
|
styleUrls: ['./personality-test.component.scss'],
|
|
|
standalone: true,
|
|
|
- imports: [IonCard, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCardContent, IonList, IonItem, CommonModule],
|
|
|
+ imports: [IonContent,IonCard, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCardContent, IonList, IonItem, IonButton, CommonModule],
|
|
|
})
|
|
|
export class PersonalityTestComponent implements OnInit {
|
|
|
+ questionsList: Array<Question> = []; // 使用 Question 类型
|
|
|
+ optionList: Array<Option> = []; // 使用 Option 类型
|
|
|
|
|
|
- constructor() { }
|
|
|
+ constructor(private router: Router) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
- // 生命周期:页面加载后,运行列表加载函数
|
|
|
this.loadQuestionsList();
|
|
|
}
|
|
|
|
|
|
- questionsList: Array<CloudObject> = [];
|
|
|
- optionList: Array<CloudObject> = [];
|
|
|
-
|
|
|
// 查询并加载问题列表的函数
|
|
|
async loadQuestionsList() {
|
|
|
let query = new CloudQuery("Questions");
|
|
|
- this.questionsList = await query.find();
|
|
|
-
|
|
|
- // 打印加载的问题列表
|
|
|
+ this.questionsList = await query.find() as Question[]; // 确保问题列表为 Question 类型
|
|
|
console.log("加载的问题列表:", this.questionsList);
|
|
|
-
|
|
|
- await this.loadOptionsList(); // 调用加载选项的函数
|
|
|
+ await this.loadOptionsList(); // 加载选项列表
|
|
|
}
|
|
|
|
|
|
// 查询并加载选项列表的函数
|
|
|
async loadOptionsList() {
|
|
|
let query = new CloudQuery("option");
|
|
|
- this.optionList = await query.find();
|
|
|
-
|
|
|
- // 打印加载的选项列表
|
|
|
+ this.optionList = await query.find() as Option[]; // 确保选项列表为 Option 类型
|
|
|
console.log("加载的选项列表:", this.optionList);
|
|
|
}
|
|
|
|
|
|
// 根据问题 ID 获取相关选项
|
|
|
- getOptionsForQuestion(questionId: string) {
|
|
|
+ getOptionsForQuestion(questionId: string): Option[] {
|
|
|
return this.optionList.filter(option =>
|
|
|
option.get('question')?.objectId === questionId
|
|
|
- );
|
|
|
+ ) as Option[]; // 强制转换为 Option[]
|
|
|
}
|
|
|
|
|
|
+ // 处理选项按钮点击事件
|
|
|
+ selectOption(option: Option, question: Question) {
|
|
|
+ // 切换选项的选中状态
|
|
|
+ option.selected = !option.selected;
|
|
|
+
|
|
|
+ // 从问题中获取 dimension
|
|
|
+ const dimension = question.get('dimension'); // 从 Questions 表中获取 dimension
|
|
|
+ const weightString = option.get('weight'); // 从 option 表中获取 weight
|
|
|
+
|
|
|
+ // 将 weight 从 string 转换为 number
|
|
|
+ const weight = parseFloat(weightString); // 使用 parseFloat 进行转换
|
|
|
+
|
|
|
+ // 更新问题的 dimensionWeights
|
|
|
+ if (!question.dimensionWeights) {
|
|
|
+ question.dimensionWeights = []; // 初始化 dimensionWeights
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果选项被选中,则增加权重
|
|
|
+ if (option.selected) {
|
|
|
+ // 查找当前维度是否已存在
|
|
|
+ const existingDimension = question.dimensionWeights.find(dw => dw.dimension === dimension);
|
|
|
+ if (existingDimension) {
|
|
|
+ existingDimension.totalWeight += weight; // 增加权重
|
|
|
+ } else {
|
|
|
+ // 如果不存在,则添加新的维度
|
|
|
+ question.dimensionWeights.push({ dimension, totalWeight: weight });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果选项被取消选择,则减少权重
|
|
|
+ const existingDimension = question.dimensionWeights.find(dw => dw.dimension === dimension);
|
|
|
+ if (existingDimension) {
|
|
|
+ existingDimension.totalWeight -= weight; // 减少权重
|
|
|
+ if (existingDimension.totalWeight <= 0) {
|
|
|
+ // 如果总权重小于等于 0,则移除该维度
|
|
|
+ question.dimensionWeights = question.dimensionWeights.filter(dw => dw.dimension !== dimension);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存问题的更改
|
|
|
+ question.save().then(() => {
|
|
|
+ console.log('问题选项权重已保存:', question.dimensionWeights);
|
|
|
+ }).catch(error => {
|
|
|
+ console.error('保存问题时出错:', error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提交所有选项的处理方法
|
|
|
+ submitAll() {
|
|
|
+ // 遍历所有问题,准备提交的数据
|
|
|
+ const submissionData = this.questionsList.map(question => {
|
|
|
+ return {
|
|
|
+ questionId: question.id,
|
|
|
+ dimensionWeights: question.dimensionWeights,
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ // 打印每个相同 dimension 的总权重及对应的 dimension 的值
|
|
|
+ const dimensionTotals: { [key: string]: number } = {}; // 用于存储每个 dimension 的总权重
|
|
|
+
|
|
|
+ // 遍历每个问题的 dimensionWeights
|
|
|
+ submissionData.forEach(data => {
|
|
|
+ data.dimensionWeights?.forEach(dw => {
|
|
|
+ if (dimensionTotals[dw.dimension]) {
|
|
|
+ dimensionTotals[dw.dimension] += dw.totalWeight; // 累加权重
|
|
|
+ } else {
|
|
|
+ dimensionTotals[dw.dimension] = dw.totalWeight; // 初始化权重
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ // 打印结果
|
|
|
+ console.log('每个相同 dimension 的总权重及对应的 dimension 的值:');
|
|
|
+ for (const dimension in dimensionTotals) {
|
|
|
+ console.log(`Dimension: ${dimension}, Total Weight: ${dimensionTotals[dimension]}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成 MBTI 类型
|
|
|
+ let mbtiType = '';
|
|
|
+
|
|
|
+ // 根据维度和总权重确定 MBTI 类型字符
|
|
|
+ mbtiType += dimensionTotals['E/I'] >= 0 ? 'E' : 'I'; // E/I
|
|
|
+ mbtiType += dimensionTotals['S/N'] >= 0 ? 'S' : 'N'; // S/N
|
|
|
+ mbtiType += dimensionTotals['T/F'] >= 0 ? 'T' : 'F'; // T/F
|
|
|
+ mbtiType += dimensionTotals['J/P'] >= 0 ? 'J' : 'P'; // J/P
|
|
|
+
|
|
|
+ console.log('合成的 MBTI 类型:', mbtiType);
|
|
|
+ let currentUser = new CloudUser();
|
|
|
+ // 假设你有一个 API 方法来保存 UserResponse
|
|
|
+ const userResponse = new CloudObject("Userresponse"); // 创建 UserResponse 对象
|
|
|
+ let ACL:any = { // 公开访客 不可读 不可写
|
|
|
+ "*":{read:false,write:false}
|
|
|
+ }
|
|
|
+ if(currentUser?.id){ // 当前用户 可读 可写
|
|
|
+ ACL[currentUser?.id] = {read:true,write:true}
|
|
|
+ }
|
|
|
+
|
|
|
+ let now = new Date();
|
|
|
+ let dateStr = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
|
|
|
+ userResponse.set({
|
|
|
+ // ACL:ACL,
|
|
|
+ mbtiType: mbtiType,
|
|
|
+ createdAt: dateStr, // 创建时间
|
|
|
+ user:currentUser.toPointer(),
|
|
|
+ });
|
|
|
+
|
|
|
+ // 保存 UserResponse 对象
|
|
|
+ userResponse.save().then(() => {
|
|
|
+ console.log('MBTI 类型已成功保存:', mbtiType);
|
|
|
+
|
|
|
+ // 导航到 InterlocutionComponent,并传递 mbtiType
|
|
|
+ this.router.navigate(['/tabs/interlocution', { message: mbtiType }]);
|
|
|
+ }).catch(error => {
|
|
|
+ console.error('保存 MBTI 类型时出错:', error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
-}
|
|
|
+
|
|
|
+// import { CommonModule } from '@angular/common';
|
|
|
+// import { Component, OnInit } from '@angular/core';
|
|
|
+// import { IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonItem, IonList } from '@ionic/angular/standalone';
|
|
|
+// import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
+
|
|
|
+// @Component({
|
|
|
+// selector: 'app-personality-test',
|
|
|
+// templateUrl: './personality-test.component.html',
|
|
|
+// styleUrls: ['./personality-test.component.scss'],
|
|
|
+// standalone: true,
|
|
|
+// imports: [IonCard, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCardContent, IonList, IonItem, CommonModule],
|
|
|
+// })
|
|
|
+// export class PersonalityTestComponent implements OnInit {
|
|
|
+
|
|
|
+// constructor() { }
|
|
|
+
|
|
|
+// ngOnInit() {
|
|
|
+// this.loadQuestionsList();
|
|
|
+// }
|
|
|
+
|
|
|
+// questionsList: Array<CloudObject> = [];
|
|
|
+// optionList: Array<CloudObject> = [];
|
|
|
+
|
|
|
+// // 查询并加载问题列表的函数
|
|
|
+// async loadQuestionsList() {
|
|
|
+// let query = new CloudQuery("Questions");
|
|
|
+// this.questionsList = await query.find();
|
|
|
+// console.log("加载的问题列表:", this.questionsList);
|
|
|
+// await this.loadOptionsList(); // 加载选项列表
|
|
|
+// }
|
|
|
+
|
|
|
+// // 查询并加载选项列表的函数
|
|
|
+// async loadOptionsList() {
|
|
|
+// let query = new CloudQuery("option");
|
|
|
+// this.optionList = await query.find();
|
|
|
+// console.log("加载的选项列表:", this.optionList);
|
|
|
+// }
|
|
|
+
|
|
|
+// // 根据问题 ID 获取相关选项
|
|
|
+// getOptionsForQuestion(questionId: string) {
|
|
|
+// return this.optionList.filter(option =>
|
|
|
+// option.get('question')?.objectId === questionId
|
|
|
+// );
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// import { CommonModule } from '@angular/common';
|
|
|
+// import { Component, OnInit } from '@angular/core';
|
|
|
+// import { IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonItem, IonList } from '@ionic/angular/standalone';
|
|
|
+// import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
+
|
|
|
+// @Component({
|
|
|
+// selector: 'app-personality-test',
|
|
|
+// templateUrl: './personality-test.component.html',
|
|
|
+// styleUrls: ['./personality-test.component.scss'],
|
|
|
+// standalone: true,
|
|
|
+// imports: [IonCard, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCardContent, IonList, IonItem, CommonModule],
|
|
|
+// })
|
|
|
+// export class PersonalityTestComponent implements OnInit {
|
|
|
+
|
|
|
+// constructor() { }
|
|
|
+
|
|
|
+// ngOnInit() {
|
|
|
+// // 生命周期:页面加载后,运行列表加载函数
|
|
|
+// this.loadQuestionsList();
|
|
|
+// }
|
|
|
+
|
|
|
+// questionsList: Array<CloudObject> = [];
|
|
|
+// optionList: Array<CloudObject> = [];
|
|
|
+
|
|
|
+// // 查询并加载问题列表的函数
|
|
|
+// async loadQuestionsList() {
|
|
|
+// let query = new CloudQuery("Questions");
|
|
|
+// this.questionsList = await query.find();
|
|
|
+
|
|
|
+// // 打印加载的问题列表
|
|
|
+// console.log("加载的问题列表:", this.questionsList);
|
|
|
+
|
|
|
+// await this.loadOptionsList(); // 调用加载选项的函数
|
|
|
+// }
|
|
|
+
|
|
|
+// // 查询并加载选项列表的函数
|
|
|
+// async loadOptionsList() {
|
|
|
+// let query = new CloudQuery("option");
|
|
|
+// this.optionList = await query.find();
|
|
|
+
|
|
|
+// // 打印加载的选项列表
|
|
|
+// console.log("加载的选项列表:", this.optionList);
|
|
|
+// }
|
|
|
+
|
|
|
+// // 根据问题 ID 获取相关选项
|
|
|
+// getOptionsForQuestion(questionId: string) {
|
|
|
+// return this.optionList.filter(option =>
|
|
|
+// option.get('question')?.objectId === questionId
|
|
|
+// );
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// }
|
|
|
|
|
|
|
|
|
|