Browse Source

存储聊天消息

warrior 2 months ago
parent
commit
b6336fc0c3

+ 2 - 2
projects/live-app/src/modules/live/chat/chat.component.html

@@ -52,10 +52,10 @@
       msgServe.messageMapList[uid][$index-1].timestamp > 600) {
       <div class="time-box">
         @if (item.istoday) {
-        <div class="time">{{ item.timestamp | date : "HH:mm" }}</div>
+        <div class="time">{{ item.timestamp | showDate}}</div>
         }@else {
         <div class="time">
-          {{ item.timestamp | date : "yyyy年MM月dd日 HH:mm" }}
+          {{ item.timestamp | showDate}}
         </div>
         }
       </div>

+ 6 - 3
projects/live-app/src/modules/live/chat/chat.component.ts

@@ -3,6 +3,8 @@ import { HttpClient } from '@angular/common/http';
 import { Component, OnInit } from '@angular/core';
 import { FormsModule } from '@angular/forms';
 import { ActivatedRoute, Router } from '@angular/router';
+import { SharedModule } from '../../shared.module';
+
 import {
   IonicModule,
   LoadingController,
@@ -23,11 +25,12 @@ import { MessageService } from '../../../services/message.service';
   imports: [
     IonicModule,
     FormsModule,
-    DatePipe,
+    // DatePipe,
     GiftModalComponent,
     CommonModule,
+    SharedModule
   ],
-  providers: [DatePipe],
+  // providers: [DatePipe],
 })
 export class ChatComponent implements OnInit {
   uid: string = '';
@@ -45,7 +48,7 @@ export class ChatComponent implements OnInit {
   constructor(
     private router: Router,
     private http: HttpClient,
-    public datePipe: DatePipe,
+    // public datePipe: DatePipe,
     public toastController: ToastController,
     private loadingCtrl: LoadingController,
     private activateRoute: ActivatedRoute,

+ 1 - 1
projects/live-app/src/modules/tabs/notice/notice.component.html

@@ -83,7 +83,7 @@
       <div class="notice-list" (touchmove)="onMousemove($event)">
         <ion-list>
           @for (item of friends; track $index) {
-          <ion-item (click)="toUrl('/live/chat/' + item?.uid)">
+          <ion-item (click)="toUrl('/live/chat/' + item?.channel)">
             <ion-avatar slot="start">
               <img [src]="item?.avatar" alt="avatar" />
             </ion-avatar>

+ 10 - 4
projects/live-app/src/modules/user/profile/profile.component.html

@@ -51,11 +51,13 @@
         </div>
       </div>
       <div class="user-right">
+        @if (user?.id !== currentUser?.id) {
         <ion-icon
           (click)="onCollection()"
           name="heart-circle-sharp"
           [style.color]="isFollow ? '#fe454e' : '#b3b3b3'"
         ></ion-icon>
+        }
       </div>
     </div>
     <div class="user-fans">
@@ -137,19 +139,23 @@
       <div class="round" (click)="onEdit()">
         <ion-icon name="create-outline"></ion-icon>编辑资料
       </div>
-      }@else{
+      }@else{ @if(profile?.get('idcard') && profile?.get('identyType') ==
+      'anchor'){
       <div class="gift" (click)="gift.openModal()">
         <ion-icon name="gift-outline"></ion-icon>
       </div>
-      @if(profile?.get('idcard')){
       <div class="round" (click)="toLiveContact()">
         <ion-icon name="videocam-outline"></ion-icon>直播通话
       </div>
-      }
+      } @if(friends?.get('isPass')){
       <div class="round" (click)="toMsg()">
         <ion-icon name="chatbubble-outline"></ion-icon>私信
       </div>
-      }
+      }@else {
+      <div class="round" (click)="onFriend()">
+        <ion-icon name="person-add-outline"></ion-icon>加为好友
+      </div>
+      } }
     </div>
   </ion-toolbar>
 </ion-footer>

+ 66 - 32
projects/live-app/src/modules/user/profile/profile.component.ts

@@ -41,6 +41,7 @@ export class ProfileComponent implements OnInit {
   profiel?: Parse.Object; // 用户信息
   user?: Parse.Object; // 查看用户
   @ViewChild('preview') preview!: ImagePreviewComponent;
+  friends?: Parse.Object; // 好友
   profile?: Parse.Object;
   active: number = 0;
   currenImg: string = '';
@@ -56,7 +57,7 @@ export class ProfileComponent implements OnInit {
   @ViewChild('gift') gift!: GiftModalComponent;
   iscall: boolean = false;
   isLiveing: boolean = false; // 是否在直播通话中
-  userStatus:string = 'OFFLINE';
+  userStatus: string = 'OFFLINE';
   constructor(
     private activateRoute: ActivatedRoute,
     private router: Router,
@@ -71,25 +72,25 @@ export class ProfileComponent implements OnInit {
       this.inviteCallback(data);
     });
   }
-  get state(){
-    let map:any = {
-      "ONLINE":{
-        val:'在线',
-        color:'#28bb50',
+  get state() {
+    let map: any = {
+      ONLINE: {
+        val: '在线',
+        color: '#28bb50',
       },
-      "OFFLINE":{
-        val:'离线',
-        color:'#a1a1a1',
+      OFFLINE: {
+        val: '离线',
+        color: '#a1a1a1',
       },
-      "CONNECTING":{
-        val:'忙线',
-        color:'#cf1b24',
+      CONNECTING: {
+        val: '忙线',
+        color: '#cf1b24',
       },
-      "":{
-        val:'离线',
-        color:'#a1a1a1',
+      '': {
+        val: '离线',
+        color: '#a1a1a1',
       },
-    }
+    };
     return map?.[this.userStatus] || map['OFFLINE'];
   }
   ngOnInit() {
@@ -97,14 +98,13 @@ export class ProfileComponent implements OnInit {
       let id: any = params.get('id');
       this.uid = id;
       await this.refresh();
-      this.userStatus = await this.connectTask.getState(this.uid,this.uid)
-      this.getRoom();
+      this.userStatus = await this.connectTask.getState(this.uid, this.uid);
     });
   }
   ngOnDestroy(): void {
     //Called once, before the instance is destroyed.
     //Add 'implements OnDestroy' to the class.
-    if (this.isLiveing && this.uid !== this.currentUser?.id){
+    if (this.isLiveing && this.uid !== this.currentUser?.id) {
       console.log('断开连接');
       this.msgSer.unsubscribeMessage(this.uid);
     }
@@ -114,6 +114,7 @@ export class ProfileComponent implements OnInit {
       message: '加载中',
     });
     loading.present();
+    await this.getFriends();
     await this.getProfile();
     await this.getFollwState();
     let res = await this.aiChatServ.getFansAndFollow(this.uid);
@@ -122,17 +123,20 @@ export class ProfileComponent implements OnInit {
     let res1 = await this.aiChatServ.getGiftLogCount(this.uid);
     this.numsObject.gift = res1.data[0].gift ?? 0;
     this.giftList = await this.aiChatServ.getGiftList(this.uid, 16);
+    this.getRoom();
     loading.dismiss();
   }
-  async getRoom() {
-    let query = new Parse.Query('Room');
-    query.equalTo('company', this.aiChatServ.company);
-    query.equalTo('profile', this.profile?.id);
-    // query.equalTo('state', true);
+  async getFriends() {
+    let query1 = new Parse.Query('Friends');
+    query1.equalTo('user', this.currentUser?.id);
+    query1.equalTo('friend', this.uid);
+    let query2 = new Parse.Query('Friends');
+    query2.equalTo('user', this.uid);
+    query2.equalTo('friend', this.currentUser?.id);
+    let query = Parse.Query.or(query1,query2);
     query.notEqualTo('isDeleted', true);
-    // query.select('objectId')
-    let r = await query.first();
-    this.room = r;
+    query.select('objectId','isPass');
+    this.friends = await query.first();
   }
   async getProfile() {
     let queryProfile = new Parse.Query('Profile');
@@ -160,6 +164,16 @@ export class ProfileComponent implements OnInit {
     let r = await query.first();
     this.isFollow = r?.id ? true : false;
   }
+  async getRoom() {
+    let query = new Parse.Query('Room');
+    query.equalTo('company', this.aiChatServ.company);
+    query.equalTo('profile', this.profile?.id);
+    // query.equalTo('state', true);
+    query.notEqualTo('isDeleted', true);
+    query.select('objectId');
+    let r = await query.first();
+    this.room = r;
+  }
   /* 关注 */
   async onCollection() {
     let query = new Parse.Query('ProfileRadar');
@@ -196,6 +210,26 @@ export class ProfileComponent implements OnInit {
   onShow() {
     if (this.uid == this.currentUser?.id) this.isOpen = true;
   }
+  async onFriend() {
+    if (this.friends?.id && !this.friends?.get('isPass')) {
+      const toast = await this.toastController.create({
+        message: '已申请,等待对方同意',
+        color: 'warning',
+        duration: 1500,
+      });
+      toast.present();
+      return;
+    }
+    if (!this.friends?.id) {
+      let obj = Parse.Object.extend('Friends');
+      this.friends = new obj();
+      this.friends?.set('user', this.currentUser?.toPointer());
+      this.friends?.set('friend', this.user?.toPointer());
+      this.friends?.set('company', this.aiChatServ.company);
+      this.friends?.set('channel', this.user?.id + '-' + this.currentUser?.id);
+      await this.friends?.save();
+    }
+  }
 
   /* 更换背景 */
   async onSaveBackGround(e: any) {
@@ -239,18 +273,18 @@ export class ProfileComponent implements OnInit {
     });
   }
   toMsg() {
-    this.router.navigate(['live/chat/' + this.uid]);
+    this.router.navigate(['live/chat/' + this.friends?.get('channel')]);
   }
   async toLiveContact() {
-    this.userStatus = await this.connectTask.getState(this.uid,this.uid)
-    if(this.userStatus !== 'ONLINE'){
+    this.userStatus = await this.connectTask.getState(this.uid, this.uid);
+    if (this.userStatus !== 'ONLINE') {
       const toast = await this.toastController.create({
         message: '对方不在线或忙线中',
         color: 'warning',
         duration: 1500,
       });
       toast.present();
-      return
+      return;
     }
     console.log(this.userStatus);
     const alert = await this.alertController.create({
@@ -287,7 +321,7 @@ export class ProfileComponent implements OnInit {
     toast.present();
     if (event) {
       this.isLiveing = true;
-      this.userStatus = await this.connectTask.getState(this.uid,this.uid)
+      this.userStatus = await this.connectTask.getState(this.uid, this.uid);
       this.router.navigate(['live/link-room/' + this.room?.id]);
     }
   }

+ 9 - 6
projects/live-app/src/services/aichart.service.ts

@@ -59,12 +59,15 @@ export class AiChatService {
     return data.data;
   }
   getFriends(uid: string): Promise<any> {
-    let sql = `SELECT u.avatar,u.nickname,u.name,u."objectId" AS uid FROM 
-    (SELECT (CASE WHEN "friend" = '${uid}' THEN "user" ELSE "friend" END) fid
-    FROM "Friends"
-    WHERE "friend" = '${uid}'
-    OR "user" = '${uid}'
-    GROUP BY fid) AS f
+    let sql = `SELECT u.avatar,u.nickname,u.name,u."objectId" AS uid,f."channel"
+     FROM 
+      (SELECT (CASE WHEN "friend" = '${uid}' THEN "user" ELSE "friend" END) fid,"channel"
+      FROM "Friends"
+      WHERE "isDeleted" IS NOT TRUE
+      AND "isPass" = TRUE
+      AND ("friend" = '${uid}'
+      OR "user" = '${uid}')
+      GROUP BY fid,"channel") AS f
     LEFT JOIN "_User" AS u
     ON u."objectId" = f.fid
     `;

+ 1 - 1
projects/live-app/src/services/message.service.ts

@@ -352,7 +352,7 @@ export class MessageService {
       data['is_self'] = is_self;
       data['istoday'] = true;
       data['timestamp'] = new Date(data.timestamp)
-      this.messageMapList[channelName].push(data)
+      this.messageMapList[channelName].unshift(data)
     });
     // this.messageMapList[channelName].unshift(...msgList);
   }