-setupchat
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
|
import { GeneralService } from './../../services/generalservice';
|
||||||
import { ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms';
|
import { ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms';
|
||||||
|
import { IChat } from '../../interfaces/main.interface'
|
||||||
import { Component, HostListener } from '@angular/core';
|
import { Component, HostListener } from '@angular/core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -11,6 +13,7 @@ export class ChatWidgetComponent {
|
|||||||
isOpen = false;
|
isOpen = false;
|
||||||
// newMessage = '';
|
// newMessage = '';
|
||||||
chatForm!: FormGroup;
|
chatForm!: FormGroup;
|
||||||
|
messages:IChat[]=[];
|
||||||
|
|
||||||
// State สำหรับจัดการขนาด
|
// State สำหรับจัดการขนาด
|
||||||
isMaximized = false;
|
isMaximized = false;
|
||||||
@@ -27,7 +30,9 @@ export class ChatWidgetComponent {
|
|||||||
private startHeight = 0;
|
private startHeight = 0;
|
||||||
|
|
||||||
|
|
||||||
constructor() {
|
constructor(
|
||||||
|
private generalService: GeneralService
|
||||||
|
) {
|
||||||
this.setupFormControl(); // เรียกใช้ตอนเริ่ม Component
|
this.setupFormControl(); // เรียกใช้ตอนเริ่ม Component
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,14 +43,42 @@ export class ChatWidgetComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
messages = [
|
|
||||||
{ text: 'สวัสดีครับ AI พร้อมช่วยเหลือครับ 👋', isUser: false },
|
|
||||||
{ text: 'ลองกดปุ่มขยายที่หัวมุม หรือลากมุมซ้ายบนของกล่องเพื่อปรับขนาดได้เลยครับ', isUser: false },
|
|
||||||
];
|
|
||||||
|
|
||||||
toggleChat() {
|
toggleChat() {
|
||||||
this.isOpen = !this.isOpen;
|
this.isOpen = !this.isOpen;
|
||||||
this.isMaximized = false; // Reset ขนาดเมื่อปิด
|
let body = {methods: 'ind'}
|
||||||
|
this.OnAiChat(body);
|
||||||
|
const isAlreadyHave = this.messages.some(sub => sub.text == 'รอAi ประมวณผลสักครู่');
|
||||||
|
if(isAlreadyHave === false){
|
||||||
|
this.messages.push({
|
||||||
|
text: 'รอAi ประมวณผลสักครู่',
|
||||||
|
isUser: false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
this.isMaximized = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnAiChat(value: any){
|
||||||
|
let url = 'https://n8n.nuttakit.work/webhook/Ai'
|
||||||
|
let request = {
|
||||||
|
methods: value.methods || 'cht',
|
||||||
|
message: value.message ?? ''
|
||||||
|
}
|
||||||
|
this.generalService.postUrl(url, request).subscribe({
|
||||||
|
next: (result: any) => {
|
||||||
|
if (result.code === 200) {
|
||||||
|
this.messages.push(result.data)
|
||||||
|
}else{
|
||||||
|
this.generalService.trowApi(result);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: (error: any) => {
|
||||||
|
|
||||||
|
},
|
||||||
|
complete: () => {
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleMaximize() {
|
toggleMaximize() {
|
||||||
@@ -57,12 +90,12 @@ export class ChatWidgetComponent {
|
|||||||
if (newMessage.trim()) {
|
if (newMessage.trim()) {
|
||||||
this.messages.push({ text: newMessage, isUser: true });
|
this.messages.push({ text: newMessage, isUser: true });
|
||||||
newMessage = '';
|
newMessage = '';
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
this.messages.push({
|
// this.messages.push({
|
||||||
text: 'รับทราบครับ ระบบกำลังประมวลผล...',
|
// text: 'รับทราบครับ ระบบกำลังประมวลผล...',
|
||||||
isUser: false
|
// isUser: false
|
||||||
});
|
// });
|
||||||
}, 1000);
|
// }, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,3 +25,9 @@ export interface IDropBdg {
|
|||||||
bdgnam?: string;
|
bdgnam?: string;
|
||||||
bdgcod?: number;
|
bdgcod?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export interface IChat {
|
||||||
|
text?: string;
|
||||||
|
isUser?: boolean
|
||||||
|
}
|
||||||
|
|||||||
@@ -73,6 +73,35 @@ export class GeneralService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postUrl(uri: string, body: any): Observable<any> {
|
||||||
|
let payload: any;
|
||||||
|
let isJson = true;
|
||||||
|
|
||||||
|
// เช็คว่าเป็น FormData หรือไม่?
|
||||||
|
if (body instanceof FormData) {
|
||||||
|
payload = body; // ส่งไปตรงๆ ไม่ต้องครอบ request
|
||||||
|
isJson = false; // บอก getHttpOptions ว่าไม่ต้องใส่ JSON Header
|
||||||
|
} else {
|
||||||
|
payload = this.wrapRequestBody(body); // ทำงานแบบเดิม
|
||||||
|
isJson = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fullUrl = `${uri}`;
|
||||||
|
|
||||||
|
return this.http.post(fullUrl, payload, this.getHttpOptions(isJson)).pipe(
|
||||||
|
map((res: any) => res),
|
||||||
|
catchError((error: any) => {
|
||||||
|
const response = error?.error;
|
||||||
|
// console.error('❌ [POST Request Error]:', error);
|
||||||
|
return throwError(() => ({
|
||||||
|
status: error.status,
|
||||||
|
code: response?.code ?? '500',
|
||||||
|
message: response?.message ?? 'Internal Server Error',
|
||||||
|
message_th: response?.message_th ?? 'เกิดข้อผิดพลาดภายในเซิร์ฟเวอร์'
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// ตัวอย่างใน Component ที่จะ Upload
|
// ตัวอย่างใน Component ที่จะ Upload
|
||||||
// onSave() {
|
// onSave() {
|
||||||
|
|||||||
Reference in New Issue
Block a user