import { Component,CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone'; import { ExploreContainerComponent } from '../explore-container/explore-container.component'; import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonItem, IonLabel, IonList,IonSelect, IonSelectOption } from '@ionic/angular/standalone'; import { CommonModule } from '@angular/common'; import { Router } from '@angular/router'; import { ChatPanelOptions, FmodeChat, FmodeChatMessage, openChatPanelModal } from 'fmode-ng'; import { ModalController } from '@ionic/angular/standalone'; import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud'; import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component'; @Component({ selector: 'app-tab2', templateUrl: 'tab2.page.html', styleUrls: ['tab2.page.scss'], standalone: true, imports: [IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent, IonCard,IonCardHeader,IonCardTitle,IonCardContent, IonItem,IonLabel, IonSelect,IonSelectOption, IonButton,IonList, CommonModule ], schemas: [CUSTOM_ELEMENTS_SCHEMA], }) export class Tab2Page { private modalCtrl: ModalController; constructor(private router: Router,modalCtrl: ModalController) { this.modalCtrl = modalCtrl; // 其他构造函数代码 } async clickToConsult(chatpartner:CloudObject) { // 验证用户登录 let currentUser = new CloudUser(); let userPrompt = `` if(!currentUser?.id){ console.log("用户未登录,请登录后重试") let user = await openUserLoginModal(this.modalCtrl) if(!user?.id){ return } } if(currentUser?.get("username")){ userPrompt += `当前来访的用户,姓名:${currentUser?.get("username")}` } if(currentUser?.get("gender")){ userPrompt += `,性别:${currentUser?.get("gender")}` } if(currentUser?.get("age")){ userPrompt += `,年龄:${currentUser?.get("age")}` } // 弹窗形式聊天:开始咨询 localStorage.setItem("company","E4KpGvTEto") let consult = new CloudObject("ChatRecord") let now = new Date(); let dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}` //对象权限的精确制定 let ACL:any = {//公开访客 不可读 不可写 "*":{read:false,write:false} } if(currentUser?.id){//当前用户 可读 可写 ACL[currentUser?.id] = {read:true,write:true} } consult.set({ title:`${chatpartner.get('expertise') || ""}领域聊天记录${dateStr}-${chatpartner.get('name')}`, chatpartner:chatpartner.toPointer(), user:currentUser.toPointer(), ACL:ACL }) let options:ChatPanelOptions = { roleId:"2DXJkRsjXK", onChatInit: (chat: FmodeChat) => { console.log("onChatInit"); console.log("预设角色", chat.role); chat.role.set("name", chatpartner.get("name")); chat.role.set("bio",chatpartner.get("bio")); chat.role.set("expertise", chatpartner.get("expertise")); chat.role.set("avatar", chatpartner.get("avatar") || "/assets/img/2.png") chat.role.set("prompt", ` # 角色设定 您是${chatpartner.get("name")},一位${chatpartner.get("bio")},${chatpartner.get("expertise")},需要为用户提供陪伴和支持等积极情绪。 # 开始话语 当您准备好了,可以以一个关心用户的朋友的身份,向来访的用户打招呼。 # 对话环节 每次回复用户消息都在消息结尾附带[祝你有愉快的一天] ${userPrompt} `); }, onMessage:(chat:FmodeChat,message:FmodeChatMessage)=>{ console.log("onMessage",message) let content:any = message?.content if(typeof content == "string"){ if(content?.indexOf("[祝你有愉快的一天]")>-1){ console.log("对话结束") consult.set({ content:content }) consult.save(); } } }, // chat?.chatSession?.id 本次会话的 chatId onChatSaved:(chat:FmodeChat)=>{ console.log("onChatSaved",chat,chat?.chatSession,chat?.chatSession?.id) } } openChatPanelModal(this.modalCtrl,options) } //selectedIssue:string=''; matchedCounselor: { name: string; specialty: string } | null = null; questions = [ { title: '如何应对焦虑?', advice: '尝试深呼吸和正念冥想,保持规律的作息,健康饮食,设定小目标,培养兴趣爱好,适当锻炼。', expanded: false, }, { title: '如何提高自信心?', advice: '设定小目标并逐步实现,进行积极自我对话,培养技能,保持健康的生活方式,多与他人社交。', expanded: false, }, { title: '如何改善人际关系?', advice: '积极多与他人沟通,倾听对方的感受,理解他人的感受,多参加社交活动,保持积极的态度,尊重他人。', expanded: false, }, ]; matchCounselor() { } goChat(){ this.router.navigateByUrl("/chat/session/role/2DXJkRsjXK") } ngOnInit() { this.loadChatPartnerList() } chatpartnerList:Array=[] async loadChatPartnerList(){ let query = new CloudQuery("ChatPartner"); this.chatpartnerList = await query.find() } }