|
@@ -1,98 +1,37 @@
|
|
import { Component,OnInit } from '@angular/core';
|
|
import { Component,OnInit } from '@angular/core';
|
|
import { AlertController } from '@ionic/angular';
|
|
import { AlertController } from '@ionic/angular';
|
|
|
|
+// 引用FmodeChatCompletion类
|
|
|
|
+import { TestChatCompletion, TestChatMessage } from './class-chat-completion';
|
|
@Component({
|
|
@Component({
|
|
selector: 'app-tab2',
|
|
selector: 'app-tab2',
|
|
templateUrl: './tab2.page.html',
|
|
templateUrl: './tab2.page.html',
|
|
styleUrls: ['./tab2.page.scss'],
|
|
styleUrls: ['./tab2.page.scss'],
|
|
})
|
|
})
|
|
export class Tab2Page implements OnInit {
|
|
export class Tab2Page implements OnInit {
|
|
- segment: string = 'memo';
|
|
|
|
- memos: string[] = [];
|
|
|
|
- newMemo: string = '';
|
|
|
|
messages: { sender: string, text: string }[] = [];
|
|
messages: { sender: string, text: string }[] = [];
|
|
newMessage: string = '';
|
|
newMessage: string = '';
|
|
|
|
+ messageList:Array<TestChatMessage> = []
|
|
|
|
+ userInput:string = ""
|
|
|
|
|
|
- constructor(private alertController: AlertController) {}
|
|
|
|
|
|
+ completion:TestChatCompletion
|
|
|
|
|
|
- ngOnInit() {
|
|
|
|
- this.loadMemos();
|
|
|
|
- }
|
|
|
|
- //加载备忘录
|
|
|
|
- loadMemos() {
|
|
|
|
- const memos = localStorage.getItem('memos');
|
|
|
|
- if (memos) {
|
|
|
|
- this.memos = JSON.parse(memos);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- //保存备忘录
|
|
|
|
- saveMemos() {
|
|
|
|
- localStorage.setItem('memos', JSON.stringify(this.memos));
|
|
|
|
- }
|
|
|
|
|
|
+ constructor(private alertController: AlertController) {this.completion = new TestChatCompletion(this.messageList)}
|
|
|
|
|
|
- //获取时间戳
|
|
|
|
- getCurrentTimestamp(): string {
|
|
|
|
- const now = new Date();
|
|
|
|
- const year = now.getFullYear();
|
|
|
|
- const month = (now.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
- const day = now.getDate().toString().padStart(2, '0');
|
|
|
|
- const hours = now.getHours().toString().padStart(2, '0');
|
|
|
|
- const minutes = now.getMinutes().toString().padStart(2, '0');
|
|
|
|
- return `${year}年${month}月${day}日${hours}:${minutes}`;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- //异步函数,添加备忘录
|
|
|
|
- async addMemo() {
|
|
|
|
- if (this.newMemo.trim().length > 0) {
|
|
|
|
- this.memos.push(this.newMemo);
|
|
|
|
- this.newMemo = '';
|
|
|
|
- this.saveMemos();
|
|
|
|
- const alert = await this.alertController.create({
|
|
|
|
- header: '提示',
|
|
|
|
- message: '添加成功',
|
|
|
|
- buttons: ['确定']
|
|
|
|
- });
|
|
|
|
- await alert.present();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- //异步函数,确认删除
|
|
|
|
- async confirmDeleteMemo(memo: string) {
|
|
|
|
- const alert = await this.alertController.create({
|
|
|
|
- header: '确认删除',
|
|
|
|
- message: `你确定要删除 "${memo}" 吗?`,
|
|
|
|
- buttons: [
|
|
|
|
- {
|
|
|
|
- text: '取消',
|
|
|
|
- role: 'cancel'
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- text: '删除',
|
|
|
|
- handler: () => {
|
|
|
|
- this.memos = this.memos.filter(m => m !== memo);
|
|
|
|
- this.saveMemos();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- ]
|
|
|
|
- });
|
|
|
|
- await alert.present();
|
|
|
|
|
|
+ ngOnInit() {
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
|
|
- //删除备忘录
|
|
|
|
- deleteMemo(memo: string) {
|
|
|
|
- this.memos = this.memos.filter(m => m !== memo);
|
|
|
|
- this.saveMemos();
|
|
|
|
- }
|
|
|
|
|
|
+ //ai对话
|
|
|
|
+ sendMessage(){
|
|
|
|
+ this.messageList.push({
|
|
|
|
+ role:"user",
|
|
|
|
+ content: this.userInput
|
|
|
|
+ })
|
|
|
|
+ this.userInput = ""
|
|
|
|
+ this.completion.createCompletionByStream()
|
|
|
|
|
|
- //
|
|
|
|
- sendMessage() {
|
|
|
|
- if (this.newMessage.trim().length > 0) {
|
|
|
|
- this.messages.push({ sender: 'user', text: this.newMessage });
|
|
|
|
- this.newMessage = '';
|
|
|
|
-
|
|
|
|
- // 模拟AI回复
|
|
|
|
- setTimeout(() => {
|
|
|
|
- this.messages.push({ sender: 'ai', text: 'AI的回复' });
|
|
|
|
- }, 1000);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|