|
|
@@ -1,24 +1,30 @@
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
-import { Cloudmy } from 'src/lib/ncloud';
|
|
|
+import { Cloudmy,Cloudget } from 'src/lib/ncloud';
|
|
|
import { CloudUser } from 'src/lib/ncloud';
|
|
|
-import Sentiment from 'sentiment'; // 导入 Sentiment.js
|
|
|
+import Sentiment from 'sentiment'; // 导入 Sentiment.js 用于情感分析
|
|
|
import axios from 'axios'; // 导入 axios 进行 HTTP 请求
|
|
|
-import md5 from 'md5'; // 导入 md5 库
|
|
|
+import md5 from 'md5'; // 导入 md5 库用于生成签名
|
|
|
+import { FmodeChatCompletion } from 'fmode-ng'; // 导入 FmodeChatCompletion 以进行聊天完成请求
|
|
|
+import { IonicModule } from '@ionic/angular';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-report',
|
|
|
templateUrl: './report.component.html',
|
|
|
styleUrls: ['./report.component.scss'],
|
|
|
standalone: true,
|
|
|
+ imports: [IonicModule],
|
|
|
})
|
|
|
export class ReportComponent implements OnInit {
|
|
|
data: any; // 用于存储接收到的数据
|
|
|
extractedContents: string[] = []; // 用于存储提取的内容
|
|
|
translatedText: string = ''; // 用于存储翻译后的文本
|
|
|
- data1: any; // 用于存储最终的数据
|
|
|
- data2: any;
|
|
|
- data3: any;
|
|
|
+ data1: any; // 用于存储 MBTI 类型
|
|
|
+ data2: any; // 用于存储情感得分
|
|
|
+ data3: any; // 用于存储合并后的内容
|
|
|
+ data4: any;
|
|
|
+ data5: any;
|
|
|
+ responseMsg: string = ""; // 用于展示消息内容的变量
|
|
|
|
|
|
constructor(private route: ActivatedRoute) {}
|
|
|
|
|
|
@@ -29,18 +35,30 @@ export class ReportComponent implements OnInit {
|
|
|
try {
|
|
|
this.data = JSON.parse(params['data']); // 尝试解析数据
|
|
|
console.log('成功返回数据:', this.data); // 打印成功消息和接收到的数据
|
|
|
-
|
|
|
+
|
|
|
+ const query = new Cloudget('psychotherapy');
|
|
|
+ query.descending('createdAt'); // 按照 createdAt 降序排列
|
|
|
+ const latestResponse = await query.first(); // 获取最近一次的记录
|
|
|
+
|
|
|
+ // 如果找到记录,赋值给 data5
|
|
|
+ if (latestResponse && latestResponse.content) {
|
|
|
+ this.data5 = latestResponse.content;
|
|
|
+ console.log('data5:', this.data5);
|
|
|
+ } else {
|
|
|
+ console.log('没有找到心理疏通文件');
|
|
|
+ }
|
|
|
+
|
|
|
// 检查第一个数据是否为 'yes'
|
|
|
if (this.data.data.data1 === 'yes') {
|
|
|
// 如果是 'yes',查询 Userresponse 表获取 mbtiType
|
|
|
const currentUser = new CloudUser();
|
|
|
const userId = currentUser.toPointer(); // 获取当前用户 ID
|
|
|
-
|
|
|
+
|
|
|
const query = new Cloudmy('Userresponse');
|
|
|
query.equalTo('user', userId);
|
|
|
query.descending('createdAt'); // 按照 createdAt 降序排列
|
|
|
const latestResponse = await query.first(); // 获取最近一次的记录
|
|
|
-
|
|
|
+
|
|
|
// 如果找到记录,赋值给 data1
|
|
|
if (latestResponse && latestResponse.mbtiType) {
|
|
|
this.data1 = latestResponse.mbtiType;
|
|
|
@@ -53,6 +71,30 @@ export class ReportComponent implements OnInit {
|
|
|
this.data1 = this.data.data.data1;
|
|
|
console.log('赋值给 data1 的MBTI 类型:', this.data1);
|
|
|
}
|
|
|
+
|
|
|
+ // 检查第二个数据是否为 'yes'
|
|
|
+ if (this.data.data.data2 === 'yes') {
|
|
|
+ let currentUser = new CloudUser();
|
|
|
+ let userId = currentUser.toPointer(); // 替换为实际的用户ID
|
|
|
+
|
|
|
+ const query = new Cloudmy('Chat');
|
|
|
+ query.equalTo('user', userId);
|
|
|
+ query.descending('chattime'); // 按照 chattime 降序排列
|
|
|
+ const latestChat = await query.first(); // 获取最新聊天记录
|
|
|
+
|
|
|
+ if (latestChat && latestChat.content) {
|
|
|
+ this.extractOddIndexedContents(latestChat.content);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果不是 'yes'
|
|
|
+ this.data2 = "没数据";
|
|
|
+ this.data3 = "没数据"
|
|
|
+ this.data4 = "没数据"// 确保 data1 被赋予一个空字符串
|
|
|
+ console.log('否');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待数据处理完成后再发送消息
|
|
|
+ await this.waitForData();
|
|
|
} catch (error) {
|
|
|
console.error('解析数据时发生错误:', error); // 捕获解析错误
|
|
|
}
|
|
|
@@ -61,44 +103,113 @@ export class ReportComponent implements OnInit {
|
|
|
console.log('没有接收到数据。'); // 打印没有接收到数据的消息
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待数据处理完成的方法
|
|
|
+ private async waitForData() {
|
|
|
+ while (this.data2 === undefined || this.data3 === undefined) {
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 100)); // 暂停100ms再检查
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送消息
|
|
|
+ this.sendMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送消息的方法
|
|
|
+ sendMessage() {
|
|
|
+ console.log("创建消息");
|
|
|
+ console.log("data1:", this.data1);
|
|
|
+ console.log("data2:", this.data2);
|
|
|
+ console.log("data3:", this.data3);
|
|
|
+ console.log("data4:", this.data4);
|
|
|
+ console.log("data5:", this.data5);
|
|
|
+
|
|
|
+ // 检查所有需要的值是否已赋值
|
|
|
+ if (this.data1 && this.data2 !== undefined && this.data3 && this.data4 && this.data5) {
|
|
|
+ let PromptTemplate = `
|
|
|
+ 你是一名专业的心理疏导方向的专家,
|
|
|
+ 用户的ti性格是${this.data1},
|
|
|
+ 用户当前的情感得分为${this.data2},
|
|
|
+ 情感分析结果是${this.data3},
|
|
|
+ 用户与AI的中用户聊天内容是${this.data4},
|
|
|
+ 请你根据他的性格,情感得分,情感分析结果以及用户的聊天内容,导出一份独属于用户的个人心理状态评估研究报告。
|
|
|
+ 以下是模板{
|
|
|
+个人心理状态评估研究报告
|
|
|
|
|
|
- // 检查第二个数据是否为 'yes'
|
|
|
- if ( this.data.data.data2 === 'yes') {
|
|
|
- console.log('否');
|
|
|
- let currentUser = new CloudUser();
|
|
|
- let userId = currentUser.toPointer(); // 替换为实际的用户ID
|
|
|
+一、评估方法
|
|
|
|
|
|
- const query = new Cloudmy('Chat');
|
|
|
- query.equalTo('user', userId);
|
|
|
- query.descending('chattime'); // 按照chattime降序排列
|
|
|
- const latestChat = await query.first(); // 获取最新聊天记录
|
|
|
+性格测试:通过进行性格测试,了解您的大致性格,****
|
|
|
|
|
|
- if (latestChat && latestChat.content) {
|
|
|
- this.extractOddIndexedContents(latestChat.content);
|
|
|
- }
|
|
|
+聊天对话:(如果聊天内容没数据就显示无数据)
|
|
|
+(有数据的话显示
|
|
|
+通过你与AI的对话对您的情绪进行分析,察觉到您的情绪不太好,(不要显示用户表达的话,和情感得分))***
|
|
|
+
|
|
|
+二、评估结果
|
|
|
+
|
|
|
+性格偏向:你是一个***人,***
|
|
|
+
|
|
|
+情绪状态:*****
|
|
|
+
|
|
|
+认知与情绪调节:****
|
|
|
+
|
|
|
+三、建议
|
|
|
+
|
|
|
+1.增强自我认知***
|
|
|
+
|
|
|
+2.专业咨询***
|
|
|
+
|
|
|
+3.情绪调节训练****
|
|
|
+
|
|
|
+}
|
|
|
+ 如果用户当前的情感得分为${this.data2}<0,参考${this.data5},再推荐1~2个心理治疗方法给用户
|
|
|
+ {
|
|
|
+ 四、推荐心理治疗方法
|
|
|
+
|
|
|
+ 1.***
|
|
|
+
|
|
|
+ 2.****}。
|
|
|
+ 不要显示用户基本信息`
|
|
|
+ ;
|
|
|
+
|
|
|
+ let completion = new FmodeChatCompletion([
|
|
|
+ { role: "user", content: PromptTemplate }
|
|
|
+ ]);
|
|
|
+
|
|
|
+ completion.sendCompletion().subscribe((message: any) => {
|
|
|
+ // 打印消息体
|
|
|
+ console.log(message.content);
|
|
|
+ // 赋值消息内容给组件内属性
|
|
|
+ this.responseMsg = message.content;
|
|
|
+ });
|
|
|
} else {
|
|
|
- // 如果不是 'yes'
|
|
|
- this.data1 = ""; // 确保 data1 被赋予一个空字符串
|
|
|
- console.log('否');
|
|
|
+ console.warn("发送消息失败: 数据未完全赋值", {
|
|
|
+ data1: this.data1,
|
|
|
+ data2: this.data2,
|
|
|
+ data3: this.data3,
|
|
|
+ data4: this.data4,
|
|
|
+ data5: this.data5
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
|
|
|
- private extractOddIndexedContents(contentArray: any[]) {
|
|
|
+ // 提取聊天记录中奇数索引的内容
|
|
|
+ private extractOddIndexedContents(contentArray: any[]) {
|
|
|
this.extractedContents = contentArray
|
|
|
.filter((_, index) => index % 2 === 0) // 过滤出奇数索引的对象
|
|
|
.slice(1) // 去掉第一个对象
|
|
|
- .map(item => item.content); // 提取content的值
|
|
|
+ .map(item => item.content); // 提取 content 的值
|
|
|
|
|
|
- // 明确指定 combinedContent 的类型为 string
|
|
|
+ // 合并内容
|
|
|
const combinedContent: string = this.extractedContents.join(' ');
|
|
|
-
|
|
|
- // 打印合并后的字符串
|
|
|
console.log('合并后的内容:', combinedContent);
|
|
|
+ this.data4 = combinedContent;
|
|
|
|
|
|
// 调用翻译函数
|
|
|
this.translateContent(combinedContent);
|
|
|
}
|
|
|
|
|
|
+ // 翻译内容
|
|
|
private async translateContent(content: string) {
|
|
|
const appid = '20241224002237024'; // 替换为您的百度翻译API的appid
|
|
|
const key = 'sNvEvziEi9iu8QxbjjoV'; // 替换为您的百度翻译API的key
|
|
|
@@ -131,6 +242,7 @@ export class ReportComponent implements OnInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 情感分析
|
|
|
private analyzeSentiment(text: string) {
|
|
|
console.log('进行情感分析的文本:', text);
|
|
|
const sentiment = new Sentiment();
|
|
|
@@ -142,8 +254,9 @@ export class ReportComponent implements OnInit {
|
|
|
this.data2 = result.score;
|
|
|
this.data3 = result;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
// this.translatedText = this.translateContent(combinedContent); // 翻译合并后的内容
|
|
|
// console.log('翻译结果:', this.translatedText);
|