|
@@ -1,12 +1,11 @@
|
|
|
-import { Component, OnInit,OnDestroy } from '@angular/core';
|
|
|
-import { NavController } from '@ionic/angular';
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+import { AlertController, NavController } from '@ionic/angular';
|
|
|
import * as Parse from 'parse';
|
|
|
-import { interval, Subscription } from 'rxjs';
|
|
|
|
|
|
interface ChatHistoryItem {
|
|
|
- chatId: number; // Adjust chatId to number type
|
|
|
+ chatId: number;
|
|
|
username: string;
|
|
|
- userChatOrder: number; // Add userChatOrder field
|
|
|
+ userChatOrder: number;
|
|
|
userChat: string;
|
|
|
aiChat: string;
|
|
|
}
|
|
@@ -18,55 +17,95 @@ interface ChatHistoryItem {
|
|
|
})
|
|
|
export class AiChatPage implements OnInit {
|
|
|
chatHistory: ChatHistoryItem[] = [];
|
|
|
- private loadChatHistorySubscription: Subscription | undefined;
|
|
|
+ username: string = ''; // 用户名
|
|
|
+ load: boolean | undefined;
|
|
|
|
|
|
- constructor(private navCtrl: NavController) {}
|
|
|
+ constructor(private navCtrl: NavController, private alertController: AlertController) {}
|
|
|
|
|
|
- ngOnInit() {
|
|
|
+ ngOnInit() {}
|
|
|
+
|
|
|
+ ionViewDidEnter() {
|
|
|
+ this.loadUserData();
|
|
|
this.loadChatHistory();
|
|
|
- //每隔一秒检测登录状况
|
|
|
- this.loadChatHistorySubscription = interval(1000).subscribe(() => {
|
|
|
- this.loadChatHistory();
|
|
|
- //console.log(this.username);
|
|
|
- });
|
|
|
}
|
|
|
- ngOnDestroy() {
|
|
|
- if (this.loadChatHistorySubscription) {
|
|
|
- this.loadChatHistorySubscription.unsubscribe();
|
|
|
+
|
|
|
+ async loadUserData() {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (currentUser) {
|
|
|
+ this.username = currentUser.getUsername()!;
|
|
|
+ } else {
|
|
|
+ this.username = '未登录';
|
|
|
}
|
|
|
+ console.log("tab-history:" + this.username);
|
|
|
+ this.CheckUser();
|
|
|
+ }
|
|
|
+
|
|
|
+ async CheckUser() {
|
|
|
+ if (this.username == '未登录') {
|
|
|
+ this.load = false;
|
|
|
+ this.presentLoginAlert();
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ this.load = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async presentLoginAlert() {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '未登录',
|
|
|
+ message: '请先登录以继续操作',
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '去登录',
|
|
|
+ handler: () => {
|
|
|
+ this.navCtrl.navigateForward('/user/login');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
}
|
|
|
|
|
|
async loadChatHistory() {
|
|
|
try {
|
|
|
const Chat = Parse.Object.extend('ai_chat');
|
|
|
const query = new Parse.Query(Chat);
|
|
|
+ query.equalTo('username', this.username); // 只获取当前用户名的聊天记录
|
|
|
query.select('chatId', 'username', 'userChatOrder', 'userChat', 'aiChat');
|
|
|
- query.descending('createdAt'); // Ensure latest chat is fetched first
|
|
|
+ query.descending('createdAt');
|
|
|
const results = await query.find();
|
|
|
|
|
|
- const groupedChats: { [key: number]: ChatHistoryItem } = results.reduce((acc: { [key: number]: ChatHistoryItem }, chat: any) => {
|
|
|
- const chatId = chat.get('chatId');
|
|
|
- if (!acc[chatId]) {
|
|
|
- acc[chatId] = {
|
|
|
- chatId,
|
|
|
- username: chat.get('username'),
|
|
|
- userChatOrder: chat.get('userChatOrder'),
|
|
|
- userChat: chat.get('userChat'),
|
|
|
- aiChat: chat.get('aiChat')
|
|
|
- };
|
|
|
- }
|
|
|
- return acc;
|
|
|
- }, {});
|
|
|
+ const groupedChats: { [key: number]: ChatHistoryItem } = results.reduce(
|
|
|
+ (acc: { [key: number]: ChatHistoryItem }, chat: any) => {
|
|
|
+ const chatId = chat.get('chatId');
|
|
|
+ if (!acc[chatId]) {
|
|
|
+ acc[chatId] = {
|
|
|
+ chatId,
|
|
|
+ username: chat.get('username'),
|
|
|
+ userChatOrder: chat.get('userChatOrder'),
|
|
|
+ userChat: chat.get('userChat'),
|
|
|
+ aiChat: chat.get('aiChat'),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return acc;
|
|
|
+ },
|
|
|
+ {}
|
|
|
+ );
|
|
|
|
|
|
this.chatHistory = Object.values(groupedChats).map(chat => {
|
|
|
- // Filter to only keep the first message for each chatId
|
|
|
- const firstMessage = results.find(item => item.get('chatId') === chat.chatId && item.get('userChatOrder') === 1);
|
|
|
+ const firstMessage = results.find(
|
|
|
+ item => item.get('chatId') === chat.chatId && item.get('userChatOrder') === 1
|
|
|
+ );
|
|
|
return {
|
|
|
chatId: chat.chatId,
|
|
|
username: firstMessage?.get('username') || chat.username,
|
|
|
userChatOrder: firstMessage?.get('userChatOrder') || chat.userChatOrder,
|
|
|
userChat: firstMessage?.get('userChat') || chat.userChat,
|
|
|
- aiChat: firstMessage?.get('aiChat') || chat.aiChat
|
|
|
+ aiChat: firstMessage?.get('aiChat') || chat.aiChat,
|
|
|
};
|
|
|
});
|
|
|
|
|
@@ -77,19 +116,23 @@ export class AiChatPage implements OnInit {
|
|
|
|
|
|
goToChat(chatId: number) {
|
|
|
this.navCtrl.navigateForward(`/tabs/tab2`, {
|
|
|
- queryParams: { chatId: chatId.toString() } // Ensure chatId is passed as string
|
|
|
+ queryParams: { chatId: chatId.toString() },
|
|
|
});
|
|
|
}
|
|
|
|
|
|
async startNewChat() {
|
|
|
- const Chat = Parse.Object.extend('ai_chat');
|
|
|
- const query = new Parse.Query(Chat);
|
|
|
- query.descending('chatId');
|
|
|
- const latestChat = await query.first();
|
|
|
- const newChatId = latestChat ? latestChat.get('chatId') + 1 : 1;
|
|
|
+ this.loadUserData();
|
|
|
+ if (this.load) {
|
|
|
+ const Chat = Parse.Object.extend('ai_chat');
|
|
|
+ const query = new Parse.Query(Chat);
|
|
|
+ query.descending('chatId');
|
|
|
+ query.equalTo('username', this.username); // 确保chatId生成基于当前用户的历史记录
|
|
|
+ const latestChat = await query.first();
|
|
|
+ const newChatId = latestChat ? latestChat.get('chatId') + 1 : 1;
|
|
|
|
|
|
- this.navCtrl.navigateForward(`/tabs/tab2`, {
|
|
|
- queryParams: { chatId: newChatId.toString() } // Ensure chatId is passed as string
|
|
|
- });
|
|
|
+ this.navCtrl.navigateForward(`/tabs/tab2`, {
|
|
|
+ queryParams: { chatId: newChatId.toString() },
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
}
|