|
@@ -0,0 +1,126 @@
|
|
|
|
+import { Component,OnInit } from '@angular/core';
|
|
|
|
+import { TestChatCompletion, TestChatMessage,oldMessage } from './class-chat-completion';
|
|
|
|
+import {SharedService} from 'src/modules/user/service-user/Share.service'
|
|
|
|
+import {NavController } from '@ionic/angular';
|
|
|
|
+import * as Parse from "parse"
|
|
|
|
+(Parse as any).serverURL = "https://web2023.fmode.cn/parse"
|
|
|
|
+Parse.initialize("dev")
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+async function historyMessage(senderId: string,receiverId:string,historyMessages:Array<oldMessage>=[]){
|
|
|
|
+ await findMessagesByUserName(receiverId,senderId,historyMessages,"assistant")
|
|
|
|
+ await findMessagesByUserName(senderId,receiverId,historyMessages,"user")
|
|
|
|
+ historyMessages.sort((a, b) => Number(a.createdAt) - Number(b.createdAt));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function findMessagesByUserName(senderId: string,receiverId:string,historyMessages:Array<oldMessage>=[],role:string) {
|
|
|
|
+ const Gzpmessage = Parse.Object.extend('Gzpmessage');
|
|
|
|
+ const query = new Parse.Query(Gzpmessage);
|
|
|
|
+ query.equalTo("senderId", senderId);
|
|
|
|
+ query.equalTo("receiverId", receiverId);
|
|
|
|
+ try {
|
|
|
|
+ const messages = await query.find();
|
|
|
|
+ for (const message of messages) {
|
|
|
|
+ historyMessages.push({
|
|
|
|
+ role:role,
|
|
|
|
+ content:message.get("messageContent"),
|
|
|
|
+ createdAt:message.get("createdAt").getTime()
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error("查询消息时出错:", error);
|
|
|
|
+ }
|
|
|
|
+ return historyMessage
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function sendToParse(value:string,senderId:string,receiverId:string){
|
|
|
|
+ const Gzpmessage = Parse.Object.extend('Gzpmessage');
|
|
|
|
+ const message = new Gzpmessage();
|
|
|
|
+ message.set('senderId', senderId);
|
|
|
|
+ message.set('receiverId', receiverId);
|
|
|
|
+ message.set('messageContent', value);
|
|
|
|
+ message.set('createdAt', new Date());
|
|
|
|
+ // 设置其他字段...
|
|
|
|
+
|
|
|
|
+ // 保存对象到数据库
|
|
|
|
+ message.save().then(
|
|
|
|
+ (result: any) => {
|
|
|
|
+ console.log('对话消息已保存到数据库:', result);
|
|
|
|
+ },
|
|
|
|
+ (error: any) => {
|
|
|
|
+ console.error('保存对话消息时出错:', error);
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@Component({
|
|
|
|
+ selector: 'app-ai-chat',
|
|
|
|
+ templateUrl: './ai-chat.page.html',
|
|
|
|
+ styleUrls: ['./ai-chat.page.scss'],
|
|
|
|
+})
|
|
|
|
+export class AiChatPage implements OnInit {
|
|
|
|
+
|
|
|
|
+ back(){
|
|
|
|
+ this.navCtrl.back()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ oldMessage:boolean=true;
|
|
|
|
+ title: string ='小助手';
|
|
|
|
+ userId: string = '';
|
|
|
|
+ userImg: string = ''
|
|
|
|
+ public userInput: string = '';
|
|
|
|
+ messageList: Array<TestChatMessage> = [];
|
|
|
|
+ historyMessages:Array<oldMessage>=[]
|
|
|
|
+ completion: TestChatCompletion;
|
|
|
|
+
|
|
|
|
+ constructor(private sharedService: SharedService,private navCtrl:NavController) {
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role:'assistant',
|
|
|
|
+ content:"您好,我是您的小助手,请问现在有什么可以帮到您的。",
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ this.completion = new TestChatCompletion(this.messageList,this.sharedService);
|
|
|
|
+ this.userId = this.sharedService.getUserId();
|
|
|
|
+ this.userImg = this.sharedService.getUserImg();
|
|
|
|
+ historyMessage(this.userId,'1',this.historyMessages)
|
|
|
|
+ // findMessagesByUserName('Aichat',this.userId,this.messageList,"assistant")
|
|
|
|
+ // findMessagesByUserName(this.userId,'Aichat',this.messageList,"user")
|
|
|
|
+ }
|
|
|
|
+ send(value: string) {
|
|
|
|
+ if (this.sharedService.pdlogin()){
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role: 'user',
|
|
|
|
+ content: value,
|
|
|
|
+ });
|
|
|
|
+ sendToParse(value,this.userId,"1")
|
|
|
|
+ this.completion.createCompletionByStream();
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ if(value=='开始'){
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role:'assistant',
|
|
|
|
+ content:"请问",
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ this.sharedService.isLogin()
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role:'user',
|
|
|
|
+ content:value,
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role:'assistant',
|
|
|
|
+ content:"对不起,您还没有登入,暂时无法使用此功能",
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.userInput='';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ngOnInit() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|