|  | @@ -27,7 +27,6 @@ import { CustomerServiceComponent } from '../customer-service/customer-service.c
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  export class Tab2Page {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  
 | 
	
		
			
				|  |  |    isModalOpen: boolean = false; // 定义 isModalOpen 属性
 | 
	
		
			
				|  |  |    placeholderText: string = '';
 | 
	
		
			
				|  |  |    placeholderIndex: number = 0;
 | 
	
	
		
			
				|  | @@ -47,8 +46,6 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |      return await modal.present();
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |    private modalCtrl: ModalController;
 | 
	
		
			
				|  |  |    constructor(private router: Router,modalCtrl: ModalController) {
 | 
	
		
			
				|  |  |      this.modalCtrl = modalCtrl;
 | 
	
	
		
			
				|  | @@ -56,6 +53,8 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |      this.cyclePlaceholder();
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |    async clickToConsult(chatpartner:CloudObject) {
 | 
	
		
			
				|  |  | +    let consult: CloudObject | null = null;
 | 
	
		
			
				|  |  | +    let messages: string[] = [];
 | 
	
		
			
				|  |  |      // 验证用户登录
 | 
	
		
			
				|  |  |      let currentUser = new CloudUser();
 | 
	
		
			
				|  |  |      let userPrompt = ``
 | 
	
	
		
			
				|  | @@ -63,8 +62,12 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |        console.log("用户未登录,请登录后重试")
 | 
	
		
			
				|  |  |        let user = await openUserLoginModal(this.modalCtrl)
 | 
	
		
			
				|  |  |        if(!user?.id){
 | 
	
		
			
				|  |  | +        console.log("用户登录失败");
 | 
	
		
			
				|  |  |          return
 | 
	
		
			
				|  |  | -      }     
 | 
	
		
			
				|  |  | +      } 
 | 
	
		
			
				|  |  | +      else {
 | 
	
		
			
				|  |  | +        console.log("当前用户ID:", currentUser.id);
 | 
	
		
			
				|  |  | +      }    
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      if(currentUser?.get("username")){
 | 
	
		
			
				|  |  |        userPrompt += `当前来访的用户,姓名:${currentUser?.get("username")}`
 | 
	
	
		
			
				|  | @@ -78,8 +81,10 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      // 弹窗形式聊天:开始聊天
 | 
	
		
			
				|  |  |      localStorage.setItem("company","E4KpGvTEto")
 | 
	
		
			
				|  |  | -    let consult = new CloudObject("ChatRecord")
 | 
	
		
			
				|  |  | -    let messages: string[] = [];
 | 
	
		
			
				|  |  | +    // 创建聊天记录对象
 | 
	
		
			
				|  |  | +    consult = new CloudObject("ChatRecord")
 | 
	
		
			
				|  |  | +    messages = [];// 清空之前的消息记录
 | 
	
		
			
				|  |  | +    // 设置聊天记录的基本信息
 | 
	
		
			
				|  |  |      let now = new Date();
 | 
	
		
			
				|  |  |      let dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`
 | 
	
		
			
				|  |  |      //对象权限的精确制定
 | 
	
	
		
			
				|  | @@ -89,12 +94,16 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |      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
 | 
	
		
			
				|  |  | +      ACL:ACL,
 | 
	
		
			
				|  |  | +      content: "", // 初始内容为空
 | 
	
		
			
				|  |  | +      createdAt: now, // 可以加上创建时间
 | 
	
		
			
				|  |  |      })
 | 
	
		
			
				|  |  | +    // 进入聊天面板
 | 
	
		
			
				|  |  |      let options:ChatPanelOptions = {
 | 
	
		
			
				|  |  |        roleId:"2DXJkRsjXK",
 | 
	
		
			
				|  |  |        onChatInit: (chat: FmodeChat) => {
 | 
	
	
		
			
				|  | @@ -104,7 +113,6 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |          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")},需要为用户提供陪伴和支持等积极情绪。
 | 
	
	
		
			
				|  | @@ -118,18 +126,18 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |        onMessage:(chat:FmodeChat,message:FmodeChatMessage)=>{
 | 
	
		
			
				|  |  |          console.log("onMessage",message)
 | 
	
		
			
				|  |  |          let content:any = message?.content
 | 
	
		
			
				|  |  | -        if(typeof content == "string"){
 | 
	
		
			
				|  |  | +        if(typeof content === "string"){
 | 
	
		
			
				|  |  |            messages.push(content); // 将新消息添加到 messages 数组中
 | 
	
		
			
				|  |  |              consult.set({
 | 
	
		
			
				|  |  |                content: messages.join("\n"), // 将所有消息合并为一个字符串
 | 
	
		
			
				|  |  |              });
 | 
	
		
			
				|  |  | -            //consult.save();
 | 
	
		
			
				|  |  | -            consult.save().then(() => {
 | 
	
		
			
				|  |  | +          // 在此之前检查 consult 对象是否有效
 | 
	
		
			
				|  |  | +          console.log("准备保存聊天记录:", consult);
 | 
	
		
			
				|  |  | +          consult.save().then(() => {
 | 
	
		
			
				|  |  |                console.log("聊天记录已保存");
 | 
	
		
			
				|  |  |            }).catch((error) => {
 | 
	
		
			
				|  |  |                console.error("保存聊天记录时出错:", error);
 | 
	
		
			
				|  |  |            });
 | 
	
		
			
				|  |  | -          //}
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |        },
 | 
	
		
			
				|  |  |          // chat?.chatSession?.id 本次会话的 chatId
 | 
	
	
		
			
				|  | @@ -138,6 +146,7 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      openChatPanelModal(this.modalCtrl,options)
 | 
	
		
			
				|  |  | +    
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |    matchedCounselor: { name: string; specialty: string } | null = null;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -190,7 +199,6 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |          chat.role.set("bio",chatcompanion.get("bio"));
 | 
	
		
			
				|  |  |          chat.role.set("expertise", chatcompanion.get("expertise"));
 | 
	
		
			
				|  |  |          chat.role.set("avatar", chatcompanion.get("avatar") || "/assets/img/2.png")//设置聊天伙伴头像
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          chat.role.set("prompt", `
 | 
	
		
			
				|  |  |          # 角色设定
 | 
	
		
			
				|  |  |          您是${chatcompanion.get("name")},一位${chatcompanion.get("bio")}的聊天伙伴,需要为用户提供陪伴和支持等积极情绪。
 | 
	
	
		
			
				|  | @@ -209,7 +217,6 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |              consult.set({
 | 
	
		
			
				|  |  |                content: messages.join("\n"), // 将所有消息合并为一个字符串
 | 
	
		
			
				|  |  |              });
 | 
	
		
			
				|  |  | -            //consult.save();
 | 
	
		
			
				|  |  |              consult.save().then(() => {
 | 
	
		
			
				|  |  |                console.log("聊天记录已保存");
 | 
	
		
			
				|  |  |            }).catch((error) => {
 | 
	
	
		
			
				|  | @@ -224,9 +231,10 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      openChatPanelModal(this.modalCtrl,options)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |    questions = [
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |        title: '如何应对焦虑?',
 | 
	
	
		
			
				|  | @@ -245,7 +253,6 @@ export class Tab2Page {
 | 
	
		
			
				|  |  |      },
 | 
	
		
			
				|  |  |    ];
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  //matchCounselor() {
 | 
	
		
			
				|  |  |    ngOnInit() {
 | 
	
		
			
				|  |  |      this.loadChatPartnerList(),
 | 
	
		
			
				|  |  |      this.loadChatCompanionList()
 |