wansfa 1 anno fa
parent
commit
14ac4660f5

+ 21 - 1
src/api/qw/im.js

@@ -16,7 +16,7 @@ export function getContactsByUser(userId) {
 }
 
 
-export function getConversation(userId) {
+export function getConversations(userId) {
   return request({
     url: '/qw/contact/conversationList/'+userId,
     method: 'get'
@@ -24,6 +24,16 @@ export function getConversation(userId) {
 }
 
 
+export function getConversation(query) {
+  return request({
+    url: '/qw/contact/conversation',
+    method: 'get',
+    params:query
+  })
+}
+
+
+
 export function getMessageList(query) {
   return request({
     url: '/qw/message/getList',
@@ -33,4 +43,14 @@ export function getMessageList(query) {
 }
 
 
+// 发送文本消息
+export function sendTextMsg(data) {
+  return request({
+    url: '/qw/message/text',
+    method: 'post',
+    data: data
+  })
+}
+
+
 

+ 45 - 8
src/components/LemonUI/components/index.vue

@@ -278,9 +278,7 @@ export default {
       this.$emit("pull-messages",
       this.currentContact,
         (messages = [], isEnd = false) => {
-        
           this._addMessage(messages, this.currentContactId, 0);
-
           this.CacheMessageLoaded.set(this.currentContactId, isEnd);
           if (isEnd == true) this.$refs.messages.loaded();
           this.updateCurrentMessages();
@@ -378,7 +376,7 @@ export default {
                 contact,
                 timeFormat: this.contactTimeFormat,
               },
-              () => this.changeContact(contact.id),
+              () => this.changeContact(contact.conversationId),
               this.$scopedSlots["sidebar-message"],
             );
           }),
@@ -411,7 +409,7 @@ export default {
       return (
         <lemon-contact
           class={{
-            "lemon-contact--active": this.currentContactId == props.contact.id,
+            "lemon-contact--active": this.currentContactId == props.contact.conversationId,
           }}
           v-lemon-contextmenu_contact={this.contactContextmenu}
           props={props}
@@ -438,7 +436,7 @@ export default {
                   simple: true,
                 },
                 () => {
-                  this.changeContact(contact.id);
+                  this.changeContact(contact.conversationId);
                 },
                 this.$scopedSlots["sidebar-contact"],
               ),
@@ -571,7 +569,7 @@ export default {
                       lastContent: " ",
                     });
                   }
-                  this.changeContact(curact.id, DEFAULT_MENU_LASTMESSAGES);
+                  this.changeContact(curact.conversationId, DEFAULT_MENU_LASTMESSAGES);
                 }}
               >
                 发送消息
@@ -641,7 +639,7 @@ export default {
       if (!allMessages[this.currentContactId]){
           allMessages[this.currentContactId] = [];
       }
-      console.log("qxj allMessages:"+JSON.stringify(allMessages));
+      //console.log("qxj allMessages:"+JSON.stringify(allMessages));
       this.currentMessages = allMessages[this.currentContactId];
     },
     /**
@@ -935,6 +933,29 @@ export default {
       );
       return true;
     },
+
+     appendConversation(conversation) {
+      if (isEmpty(conversation.conversationId) || isEmpty(conversation.displayName)) {
+        console.error("id | displayName cant be empty");
+        return false;
+      }
+      if (this.hasConversation(conversation.conversationId)) return true;
+      this.conversations.push(
+        Object.assign(
+          {
+            conversationId: "",
+            displayName: "",
+            avatar: "",
+            index: "",
+            unread: 0,
+            lastSendTime: "",
+            lastContent: "",
+          },
+          conversation,
+        ),
+      );
+      return true;
+    },
     removeContact(id) {
       const index = this.findContactIndexById(id);
       if (index === -1) return false;
@@ -950,7 +971,6 @@ export default {
     updateContact(data) {
       const contactId = data.id;
       delete data.id;
-
       const index = this.findContactIndexById(contactId);
       if (index !== -1) {
         const { unread } = data;
@@ -974,6 +994,10 @@ export default {
     findContactIndexById(contactId) {
       return this.contacts.findIndex(item => item.id == contactId);
     },
+    findConversationIndexById(conversationId) {
+      console.log("qxj conversationId:"+conversationId+", conversations:"+JSON.stringify(this.conversations));
+      return this.conversations.findIndex(item => item.conversationId == conversationId);
+    },
     /**
      * 根据 id 查找判断是否存在联系人
      * @param contactId 联系人 id
@@ -982,6 +1006,9 @@ export default {
     hasContact(contactId) {
       return this.findContactIndexById(contactId) !== -1;
     },
+    hasConversation(conversationId) {
+      return this.findConversationIndexById(conversationId) !== -1;
+    },
     findMessage(messageId) {
       for (const key in allMessages) {
         const message = allMessages[key].find(({ id }) => id == messageId);
@@ -991,12 +1018,22 @@ export default {
     findContact(contactId) {
       return this.getContacts().find(({ id }) => id == contactId);
     },
+    findConversations(conversationId) {
+      return this.getConversations().find(({ id }) => id == conversationId);
+    },
     /**
      * 返回所有联系人
      * @return {Array<Contact>}
      */
     getContacts() {
       return this.contacts;
+    },
+      /**
+     * 返回所有会话
+     * @return {Array<Contact>}
+     */
+    getConversations() {
+      return this.conversations;
     },
     //返回当前聊天窗口联系人信息
     getCurrentContact() {

+ 1 - 1
src/components/LemonUI/components/messages.vue

@@ -60,7 +60,7 @@ export default {
                 this.loadingText()
               )
             ) : (
-              <i class="lemon-icon-loading lemonani-spin" />
+                <i class="lemon-icon-loading lemonani-spin" />
             )}
           </span>
         </div>

+ 17 - 17
src/components/LemonUI/database/contacts1.js → src/components/LemonUI/database/conversations.js

@@ -1,7 +1,7 @@
 
 export default [
   {
-    id: 1,
+    conversationId: 1,
     displayName: "像梦一样自由",
     avatar: "https://p.qqan.com/up/2020-2/2020022821001845128.jpg",
     index: "X",
@@ -10,7 +10,7 @@ export default [
     lastContent: "你开心吗",
   },
   {
-    id: 2,
+    conversationId: 2,
     displayName: "zyp",
     avatar: "https://p.qqan.com/up/2021-1/20211301122243621.jpg",
     index: "Z",
@@ -19,7 +19,7 @@ export default [
     lastContent: "不错哟",
   },
   {
-    id: 3,
+    conversationId: 3,
     displayName: "凌云",
     avatar: "https://p.qqan.com/up/2021-1/2021129102387841.jpg",
     index: "L",
@@ -28,7 +28,7 @@ export default [
     lastContent: "",
   },
   {
-    id: 4,
+    conversationId: 4,
     displayName: "小郭",
     avatar: "https://p.qqan.com/up/2021-1/2021122135507881.jpg",
     index: "X",
@@ -37,7 +37,7 @@ export default [
     lastContent: "",
   },
   {
-    id: 5,
+    conversationId: 5,
     displayName: "杨玉泉",
     avatar: "https://p.qqan.com/up/2021-1/20211211131598147.jpg",
     index: "Y",
@@ -46,7 +46,7 @@ export default [
     lastContent: "",
   },
   {
-    id: 6,
+    conversationId: 6,
     displayName: "森系Style",
     avatar: "https://p.qqan.com/up/2021-1/2021113104111220.jpg",
     index: "S",
@@ -55,7 +55,7 @@ export default [
     lastContent: "",
   },
   {
-    id: 7,
+    conversationId: 7,
     displayName: "霸王花",
     avatar: "https://p.qqan.com/up/2021-1/20211411391666.jpg",
     index: "B",
@@ -64,7 +64,7 @@ export default [
     lastContent: "你怎么还不睡呀?",
   },
   {
-    id: 8,
+    conversationId: 8,
     displayName: "曾平",
     avatar: "https://p.qqan.com/up/2020-12/202012291044425822.jpg",
     index: "Z",
@@ -73,7 +73,7 @@ export default [
     lastContent: "",
   },
   {
-    id: 9,
+    conversationId: 9,
     displayName: "淡然",
     avatar: "https://p.qqan.com/up/2020-12/202012141813343503.jpg",
     index: "D",
@@ -82,7 +82,7 @@ export default [
     lastContent: "",
   },
   {
-    "id": 7881300287915556,
+    "conversationId": 7881300287915556,
     "displayName": "RつE",
     "avatar": "http://wx.qlogo.cn/mmhead/Q3auHgzwzM4qCB1MNA6PTO8MDXiavEEklbYx0BlX1uRP7KbleAg1rmw/0",
     "index": "R",
@@ -91,7 +91,7 @@ export default [
     "lastContent": "[视频]"
   },
   {
-    "id": 7881301063988639,
+    "conversationId": 7881301063988639,
     "displayName": "初衷",
     "avatar": "http://wx.qlogo.cn/mmhead/AVK3Oz5sfA6fLpRL1OrA42xflZXL6zY56I7joCVoX2pXP0ABs28QySOdria474v2grXSHibHShRP8/0",
     "index": "C",
@@ -100,7 +100,7 @@ export default [
     "lastContent": ""
 },
 {
-    "id": 7881303019934662,
+    "conversationId": 7881303019934662,
     "displayName": "AllMight",
     "avatar": "http://wx.qlogo.cn/mmhead/oYwP0cFmRU1Zd0HX9qibZiaIy78ibQDz3H7hlyicAFhUQGfldYYXfdoTeA/0",
     "index": "A",
@@ -109,7 +109,7 @@ export default [
     "lastContent": "[视频]"
 },
 {
-    "id": 7881303630026525,
+    "conversationId": 7881303630026525,
     "displayName": "咕咕咕",
     "avatar": "http://wx.qlogo.cn/mmhead/Q3auHgzwzM4RPPP6vuW2YyHnmJZOkEZMCNYdcMhkFu4q9MGz6iaul4g/0",
     "index": "G",
@@ -118,7 +118,7 @@ export default [
     "lastContent": ""
 },
 {
-    "id": 7881301068952363,
+    "conversationId": 7881301068952363,
     "displayName": "Ximi",
     "avatar": "http://wx.qlogo.cn/mmhead/icYBDgv87Hlam1ib5FZ9LurxDoCtBCevQHGo1Gugg0Cz4/0",
     "index": "X",
@@ -127,7 +127,7 @@ export default [
     "lastContent": "[视频]"
 },
   {
-    id: 13,
+    conversationId: 13,
     displayName: "Lemon-imui交流群",
     avatar: "https://p.qqan.com/up/2020-11/20201127157109035.jpg",
     index: "L",
@@ -137,7 +137,7 @@ export default [
     lastContent: "这个咋处理啊?",
   },
   {
-    id: 14,
+    conversationId: 14,
     displayName: "系统通知",
     avatar: "https://p.qqan.com/up/2020-6/2020061117234279854.jpg",
     index: "[1]系統通知",
@@ -156,7 +156,7 @@ export default [
     },
   },
   {
-    id: 15,
+    conversationId: 1688856679458971,
     displayName: "宁静致远。",
     avatar: "https://p.qqan.com/up/2020-6/2020060308522797777.jpg",
     index: "N",

+ 89 - 26
src/views/qw/qwChat/qq.vue

@@ -104,9 +104,10 @@ import store from "@/store";
 import { getQrCode } from '@/api/qw/login';
 import { getDeviceId} from '@/api/qw/account';
 import LemonMessageVoice from "@/components/LemonUI/components/message/voice";
-import { getContactsByUser,getConversation,getMessageList} from '@/api/qw/im';
+import { getContactsByUser,getConversations,getMessageList,getConversation,sendTextMsg} from '@/api/qw/im';
 import ContactsData from "@/components/LemonUI/database/contacts";
 import MessagesData from "@/components/LemonUI/database/messages";
+import Conversations from "@/components/LemonUI/database/conversations";
 import EmojiData from "@/components/LemonUI/database/emoji";
 import '@/components/LemonUI/index.css';
 let pages = {};
@@ -252,6 +253,7 @@ export default {
       },
       contactData:null,
       conversationData:null,
+      isFromLogin:false,
       windowWidth: document.documentElement.clientWidth*0.65,  //实时屏幕宽度
       windowHeight: document.documentElement.clientHeight*0.7,   //实时屏幕高度
       queryParams: {
@@ -262,7 +264,7 @@ export default {
     };
   },
   created(){
-     this.initSocket();
+    
     //  this.$store.dispatch('qwLoginOut').then(() => {
     //  });
   },
@@ -275,6 +277,11 @@ export default {
         }, 2000);
         return;
     }
+    console.log("qxj qwUserId:"+JSON.stringify(this.qwUser));
+    this.isFromLogin=this.$route.query.isFromLogin;
+    if(this.isFromLogin!=1){
+        this.initSocket(this.qwUser.deviceId);
+    }
     this.UserData.id=this.qwUser.userId;
     this.UserData.displayName=this.qwUser.nickName;
     this.UserData.avatar=this.qwUser.avatar;
@@ -315,21 +322,20 @@ export default {
     };
     const IMUI = this.$refs.IMUI;
     
-    var conversationData = ContactsData.filter(item => item.id<=1688856679458971);
+    var conversationData = Conversations.filter(item => item.conversationId<=1688856679458971);
     conversationData.sort((a1, a2) => {
       return a2.lastSendTime - a1.lastSendTime;
     });
 
-    this.getContacts();  //获取联系人信息
-    this.getConversation();   //获取会话信息
-   
-    //  IMUI.initContacts(ContactsData);
-
-    //  IMUI.initConversations(conversationData);
-
+  
+    // IMUI.initContacts(ContactsData);
+    // IMUI.initConversations(conversationData);
     // setTimeout(() => {
     //     IMUI.changeContact(1688856679458971);
     // }, 500);
+
+    this.getContacts();  //获取联系人信息
+    this.getConversation();   //获取会话信息
   
     IMUI.initMenus([
       {
@@ -391,13 +397,20 @@ export default {
   },
 
   methods: {
-    initSocket() {
+    initSocket(userId) {
                let that=this;
-              if(!!this.qwIm.socket){
+               console.log("qxj socket:"+this.qwIm.socket);
+               if(!!this.qwIm.socket){
+                  this.qwIm.initSocket(userId,true);
+                  // 监听socket连接
+                  this.qwIm.socket.onopen = function() {
+                          console.log("socket连接成功");
+                  };
                   this.qwIm.socket.onerror = function() {
                       console.log("连接错误")
                   };
                   // 监听socket消息
+                  const IMUI = this.$refs.IMUI;
                   this.qwIm.socket.onmessage = function(res) {
                        var data=JSON.parse(res.data);
                         console.log("收到服务端内容", JSON.stringify(data));
@@ -409,7 +422,21 @@ export default {
                             setTimeout(() => {
                                that.$router.push({path:'/qw/qwLogin'});
                             }, 2000);
-                            
+                        }
+                        if(data.cmd=="receiveMsg"){
+                             var msgData=JSON.parse(data.msg);
+                             if(!IMUI.hasConversation(msgData.toContactId)){  //收到新消息时是最新的会话,添加该会话
+                                    let queryParams={conversationId:msgData.toContactId,userId:that.qwUser.userId}
+                                    getConversation(queryParams).then(response => {
+                                          if(response.code==200){
+                                              var conversation=response.data;
+                                              IMUI.appendConversation(conversation); 
+                                              that.appendMessageAction(msgData);
+                                          }
+                                   });
+                             }else{
+                                  that.appendMessageAction(msgData);
+                             }
                         }
                   };
                     // 监听socket消息
@@ -418,6 +445,7 @@ export default {
                   };
               }
     },
+  
     getContacts(){  
       getContactsByUser(this.qwUser.userId).then(response => {
            this.contactData = response.data;
@@ -428,7 +456,7 @@ export default {
     getConversation(){
       let that=this;
       const IMUI = this.$refs.IMUI;
-      getConversation(this.qwUser.userId).then(response => {
+      getConversations(this.qwUser.userId).then(response => {
            this.conversationData = response.data;
            IMUI.initConversations(response.data);
            var fstConversation=this.conversationData[0];
@@ -443,8 +471,32 @@ export default {
       });
     },
 
+    appendMessageAction(msgData){
+          if(msgData.type=="text"){   //文本
+              const message = {
+                    id: msgData.id,
+                    status: msgData.status,
+                    type: msgData.type,
+                    sendTime: msgData.sendTime,
+                    content: msgData.content,
+                    params1: "1",
+                    params2: "2",
+                    toContactId: msgData.toContactId,
+                    fromUser: msgData.fromUser,
+              };
+              this.appendRemoteMessage(message);
+           }
+    },
+
+
+    sendTextMsg(){
+
+    },
+     
+    
+
     messageTimeFormat(time) {
-      console.log("qxj messageTimeFormat:"+time);
+      //console.log("qxj messageTimeFormat:"+time);
       return this.friendlyDate(time);
     },
     changeTheme() {
@@ -488,14 +540,15 @@ export default {
                }
           }
       });
+
       // setTimeout(() => {
       //       let isEnd = true;
-      //       next(MessagesData[contact.id], isEnd);
+      //       next(MessagesData[contact.conversationId], isEnd);
       // }, 2000);
 
     },
     handleChangeContact(contact, instance) {
-        console.log("qxj contact:"+JSON.stringify(contact));
+        //console.log("qxj contact:"+JSON.stringify(contact));
         if (!pages[contact.conversationId]){
             pages[contact.conversationId] =1;
         }
@@ -505,10 +558,20 @@ export default {
         instance.closeDrawer();
     },
     handleSend(message, next, file) {
-      console.log(message, next, file);
-      setTimeout(() => {
-        next();
-      }, 1000);
+      console.log(JSON.stringify(message));
+      var params={};
+      if(message.type=="text"){  //text   image   voice   video
+          params={"conversationId":message.toContactId,"deviceId":this.qwUser.deviceId,"userId":this.qwUser.userId,"content":message.content};
+          sendTextMsg(params).then(response => {
+              if(response.code==200){
+                  next();
+              }
+          });
+      }
+       
+      // setTimeout(() => {
+      //   next();
+      // }, 1000);
     },
     handleMenuAvatarClick() {
       console.log("Event:menu-avatar-click");
@@ -618,6 +681,11 @@ export default {
       };
       IMUI.appendMessage(message, true);
     },
+    appendRemoteMessage(message) {  //从服务端返回的消息
+        const { IMUI } = this.$refs;
+        IMUI.appendMessage(message, true);
+    },
+
     updateContact() {
       this.$refs.IMUI.updateContact({
         id: "contact-3",
@@ -633,11 +701,6 @@ export default {
     openCustomContainer() {
 
     }
-
-
-
-
-
   },
 };
 </script>

+ 1 - 1
src/views/qw/qwLogin/index.vue

@@ -155,7 +155,7 @@ export default {
                             that.$store.dispatch("qwUser", qwUserInfo);
                             that.showQRCode=false;
                             setTimeout(() => {
-                                that.$router.push({path:'/qw/chat'});
+                                that.$router.push({path:'/qw/chat',query:{"isFromLogin":1}});
                             }, 2000);
                         }
                         if(data.cmd=="verifyCode"){