Ver código fonte

修改聊天列表和单聊展示聊天人员头像和昵称

xyg 3 anos atrás
pai
commit
d8f92e1c7f

+ 19 - 1
light-application-wx/comps/chat/chat.js

@@ -62,7 +62,25 @@ Component({
         },
 
         saveSendMsg(evt) {
-            msgStorage.saveMsg(evt.detail.msg, evt.detail.type);
+            console.info(evt);
+            let msg = evt.detail.msg;
+            let users = [msg.body.from, msg.body.to];
+            WebIM.conn.fetchUserInfoById(users).then((res) => {
+                console.log(res);
+                let fromInfo = {};
+                let toInfo = {};
+                for (let k in res.data) {
+                    if (k == msg.body.from) {
+                        fromInfo = res.data[k];
+                    } else {
+                        toInfo = res.data[k];
+                    }
+                }
+                msg.body.fromInfo = fromInfo;
+                msg.body.toInfo = toInfo;
+                console.info('from and to ', fromInfo, toInfo);
+                msgStorage.saveMsg(msg, evt.detail.type);
+            });
             // this.data.__comps__.inputbar.cancelEmoji();
         },
 

+ 4 - 4
light-application-wx/comps/chat/msglist/msglist.wxml

@@ -19,10 +19,10 @@
 <view scroll-y="true" class="{{view}} wrap {{isIPX?'scroll_view_X': ''}}" bind:tap="onTap" bindscroll="scrollmore" bindscrolltoupper="refresh" upper-threshold='-50' scroll-into-view="{{toView}}">
     <view class="message" wx:for="{{chatMsg}}" wx:key="mid" id="{{item.mid}}">
         <view class="{{item.style == 'self' ? 'row-right' : 'row-left'}}">
-            <image class="photo" src="{{imgPath}}/chat-user.png" wx:if="{{item.style != 'self'}}"></image>
+            <image class="photo" src="{{item.info.fromInfo.avatarurl || imgPath +'/chat-user.png'}}" wx:if="{{item.style != 'self'}}"></image>
             <view class="msgbox">
-                <text class="user-text" wx:if="{{item.style != 'self'}}">{{ item.yourname + ' ' + item.time}}</text>
-                <text class="user-text" wx:if="{{item.style == 'self'}}">{{ item.time + ' ' + item.yourname}}</text>
+                <text class="user-text" wx:if="{{item.style != 'self'}}">{{item.info.fromInfo.nickname + ' ' + item.time}}</text>
+                <text class="user-text" wx:if="{{item.style == 'self'}}">{{item.time + ' ' + item.info.fromInfo.nickname}}</text>
                 <view wx:if="{{ item.msg.type == 'img' || item.msg.type == 'video' }}">
                     <template is="{{ item.msg.type }}" data="{{ item }}" />
                 </view>
@@ -33,7 +33,7 @@
                     </view>
                 </view>
             </view>
-            <image class="photo" src="{{imgPath}}/chat-user.png" wx:if="{{item.style == 'self'}}"></image>
+            <image class="photo" src="{{item.info.fromInfo.avatarurl || imgPath +'/chat-user.png'}}" wx:if="{{item.style == 'self'}}"></image>
         </view>
     </view>
 </view>

+ 1 - 1
light-application-wx/pages/chat-detail/chat-detail.js

@@ -23,7 +23,7 @@ Page({
         console.log('username *****', username)
         this.setData({ username: username });
         wx.setNavigationBarTitle({
-            title: username.your
+            title: username.navigationTitle
         });
         disp.on('em.megList.refresh', function () {
             const pages = getCurrentPages();

+ 28 - 3
light-application-wx/pages/chat-room/chat-room.js

@@ -189,7 +189,7 @@ Page({
 
                 cul.call(that, newChatMsgKeys, historyChatMsgKeys);
             }
-        })
+        });
 
         function cul(newChatMsgKeys, historyChatMsgKeys) {
             let array = []
@@ -358,6 +358,9 @@ Page({
         });
     },
 
+    /**
+     * 进入聊天界面
+     */
     into_chatRoom: function (event) {
         let detail = event.currentTarget.dataset.item;
         console.info('detail:', detail);
@@ -374,9 +377,16 @@ Page({
      */
     into_singleChatRoom: function (detail) {
         var my = wx.getStorageSync("myUsername");
+        let title = '';
+        if (detail.info.from == my) {
+            title = detail.info.toInfo.nickname;
+        } else {
+            title = detail.info.fromInfo.nickname;
+        }
         var nameList = {
             myName: my,
-            your: detail.username
+            your: detail.username,
+            navigationTitle: title
         };
         wx.navigateTo({
             url: "../chat-detail/chat-detail?username=" + JSON.stringify(nameList)
@@ -458,9 +468,10 @@ Page({
         let timeArr = chatTime.split(' ')[1].split(':');
         let nowArr = util.formatDate(null).split(' ')[0].split('-');
         let month = dateArr[1] < 10 ? '0' + dateArr[1] : dateArr[1];
+        let day = dateArr[2] < 10 ? '0' + dateArr[2] : dateArr[2];
         console.info('dateArr', dateArr);
         console.info('nowArr', nowArr);
-        if (nowArr[0] == dateArr[0] && nowArr[1] == month && nowArr[2] == dateArr[2]) {
+        if (nowArr[0] == dateArr[0] && nowArr[1] == month && nowArr[2] == day) {
             return `${timeArr[0]}:${timeArr[1]}`;
         } else {
             return `${dateArr[0]}/${dateArr[1]}/${dateArr[2]}`;
@@ -468,6 +479,20 @@ Page({
     },
 
     /**
+     * 查询环信用户属性
+     */
+    getUserShowName: function (lastChatMsg) {
+        util.post({
+            url: '/api/easemob/getUpUserAttribute/' + lastChatMsg.username,
+            success: (res) => {
+                wx.hideLoading();
+                console.info(res);
+                return res.data.data.data.nickname;
+            }
+        });
+    },
+
+    /**
      * 扫一扫加入群聊
      */
     scanJoinGroup: function () {

+ 3 - 43
light-application-wx/pages/chat-room/chat-room.wxml

@@ -1,28 +1,13 @@
 <!--pages/chat-room/chat-room.wxml-->
-<!-- <scroll-view class="chat-room">
-    <view class="otherItem" bindtap="into_groups">
-        <image src="{{imgPath}}/chat-group.png"></image>
-        <text>群组</text>
-    </view>
-    <view class="chat" wx:for="{{list}}" wx:key="i" data-id="{{item.uuid}}" data-name="{{item.username}}" bindtap="chatDetail">
-        <image class="photo" src="{{ item.photo ? item.photo : imgPath + '/chat-user.png'}}" mode="cover"></image>
-        <view class="item-row">
-            <text class="chat-name">{{item.username}}</text>
-            <text class="chat-time">{{item.time}}</text>
-            <text class="chat-content">{{item.content}}</text>
-            <text class="unread-num" wx:if="{{item.num > 0}}">{{item.num}}</text>
-        </view>
-    </view>
-</scroll-view> -->
 <scroll-view scroll-y="true" class="chat-room">
     <view class="otherItem" bindtap="into_groups">
         <image src="{{imgPath}}/chat-group.png"></image>
         <text>群组</text>
     </view>
     <view class="chat" wx:for="{{arr}}" wx:key="i" data-item="{{item}}" catchtap="into_chatRoom" bindlongtap="del_chat">
-        <image class="photo" src="{{ (item.chatType == 'groupchat' || item.chatType == 'chatRoom') ? imgPath + '/chat-group2.png' : imgPath + '/chat-user.png'}}" mode="cover"></image>
+        <image class="photo" src="{{ (item.chatType == 'groupchat' || item.chatType == 'chatRoom') ? imgPath + '/chat-group2.png' : (item.info.from == item.username ? item.info.fromInfo.avatarurl : item.info.toInfo.avatarurl) || imgPath + '/chat-user.png'}}" mode="cover"></image>
         <view class="item-row">
-            <text class="chat-name">{{(item.chatType == 'groupchat' || item.chatType == 'chatRoom' || item.groupName) ? item.groupName : item.username}}</text>
+            <text class="chat-name">{{(item.chatType == 'groupchat' || item.chatType == 'chatRoom' || item.groupName) ? item.groupName : (item.info.from == item.username ? item.info.fromInfo.nickname : item.info.toInfo.nickname || item.username)}}</text>
             <text class="chat-time">{{item.time}}</text>
             <text class="chat-content" wx:if="{{item.msg.data[0].data}}">{{item.msg.data[0].data}}</text>
             <text class="chat-content" wx:if="{{item.msg.type == 'img'}}">[图片]</text>
@@ -31,36 +16,11 @@
             <text class="unread-num" wx:if="{{item.unReadCount > 0 || item.unReadCount == '99+'}}">{{item.unReadCount}}</text>
         </view>
     </view>
-    <!-- <view wx:for="{{ arr }}" wx:key="index" class="chat_list" data-item="{{ item }}" catchtap='del_chat'>
-        <swipe-delete>
-            <view class="tap_mask" catchtap="into_chatRoom" data-item="{{ item }}">
-                <view class="list_box">
-                    <view class="list_left" data-username="{{ item.username }}">
-                        <view class="list_pic">
-                            <view class="em-msgNum" wx:if="{{ item.unReadCount > 0 || item.unReadCount == '99+' }}">{{ item.unReadCount }}</view>
-
-                            <image src="{{(item.chatType == 'groupchat' || item.chatType == 'chatRoom')? '../../images/groupTheme.png':'../../images/theme@2x.png'}}"></image>
-                        </view>
-                        <view class="list_text">
-                            <text class="list_user">{{(item.chatType == 'groupchat' || item.chatType == 'chatRoom' || item.groupName)?item.groupName : item.username}}</text>
-                            <text class="list_word" wx:if="{{ item.msg.data[0].data }}">{{item.msg.data[0].data}}</text>
-                            <text class="list_word" wx:if="{{ item.msg.type == 'img' }}">[图片]</text>
-                            <text class="list_word" wx:if="{{ item.msg.type == 'audio' }}">[语音]</text>
-                            <text class="list_word" wx:if="{{ item.msg.type == 'video' }}">[视频]</text>
-                        </view>
-                    </view>
-                    <view class="list_right">
-                        <text data-username="{{ item.username }}">{{item.time}}</text>
-                    </view>
-                </view>
-            </view>
-        </swipe-delete>
-    </view> -->
     <view wx:if="{{arr.length == 0}}" class="chat_noChat">
         当前没有历史聊天,添加一个好友开始聊天吧
     </view>
-    <!-- </view> -->
 </scroll-view>
+
 <!-- bug: margin-bottom 不生效 需要加一个空标签-->
 <view style="height: 1px;"></view>
 

+ 0 - 2
light-application-wx/pages/login/login.js

@@ -10,8 +10,6 @@ Page({
      */
     data: {
         imgPath: app.globalData.imgPath,
-        app_title1: '重庆市城市管理局',
-        app_title2: '指挥大厅轻应用',
         phone: '',
         vCode: '',
         sendText: '发送验证码',

+ 3 - 1
light-application-wx/utils/msgpackager.js

@@ -6,7 +6,9 @@ module.exports = function (sendableMsg, type, myName) {
     var renderableMsg = {
         info: {
             from: sendableMsg.body.from,
-            to: sendableMsg.body.to
+            fromInfo: sendableMsg.body.fromInfo,
+            to: sendableMsg.body.to,
+            toInfo: sendableMsg.body.toInfo
         },
         username: sendableMsg.body.from == myName ? sendableMsg.body.to : sendableMsg.body.from,
         yourname: sendableMsg.body.from,

+ 130 - 105
light-application-wx/utils/msgstorage.js

@@ -3,133 +3,158 @@ let msgPackager = require("./msgpackager.js");
 let msgType = require("./msgtype.js");
 let msgStorage = new Disp();
 let disp = require("./broadcast.js");
+let WebIM = wx.WebIM = require("./WebIM")["default"];
+
 msgStorage.saveReceiveMsg = function (receiveMsg, type) {
+    console.info('receiveMsg', receiveMsg);
+    let users = [receiveMsg.from, receiveMsg.to];
     let sendableMsg;
-    if (type == msgType.IMAGE) {
-        sendableMsg = {
-            id: receiveMsg.id,
-            type: type,
-            body: {
+
+    WebIM.conn.fetchUserInfoById(users).then((res) => {
+        console.log(res);
+        let fromInfo = {};
+        let toInfo = {};
+        for (let k in res.data) {
+            if (k == receiveMsg.from) {
+                fromInfo = res.data[k];
+            } else {
+                toInfo = res.data[k];
+            }
+        }
+        console.info('from and to ', fromInfo, toInfo);
+
+        if (type == msgType.IMAGE) {
+            sendableMsg = {
                 id: receiveMsg.id,
-                from: receiveMsg.from,
-                to: receiveMsg.to,
-                type: receiveMsg.type,
-                ext: receiveMsg.ext,
-                chatType: receiveMsg.type,
-                toJid: "",
+                type: type,
                 body: {
-                    type: type,
-                    url: receiveMsg.url,
-                    filename: receiveMsg.filename,
-                    filetype: receiveMsg.filetype,
-                    size: {
-                        width: receiveMsg.width,
-                        height: receiveMsg.height
+                    id: receiveMsg.id,
+                    from: receiveMsg.from,
+                    to: receiveMsg.to,
+                    type: receiveMsg.type,
+                    ext: receiveMsg.ext,
+                    chatType: receiveMsg.type,
+                    toJid: "",
+                    body: {
+                        type: type,
+                        url: receiveMsg.url,
+                        filename: receiveMsg.filename,
+                        filetype: receiveMsg.filetype,
+                        size: {
+                            width: receiveMsg.width,
+                            height: receiveMsg.height
+                        },
                     },
                 },
-            },
-        };
-    }
-    else if (type == msgType.TEXT || type == msgType.EMOJI) {
-        sendableMsg = {
-            id: receiveMsg.id,
-            type: type,
-            body: {
+            };
+        }
+        else if (type == msgType.TEXT || type == msgType.EMOJI) {
+            sendableMsg = {
                 id: receiveMsg.id,
-                from: receiveMsg.from,
-                to: receiveMsg.to,
-                type: receiveMsg.type,
-                ext: receiveMsg.ext,
-                chatType: receiveMsg.type,
-                toJid: "",
+                type: type,
                 body: {
-                    type: type,
-                    msg: receiveMsg.data,
+                    id: receiveMsg.id,
+                    from: receiveMsg.from,
+                    fromInfo: fromInfo,
+                    to: receiveMsg.to,
+                    toInfo: toInfo,
+                    type: receiveMsg.type,
+                    ext: receiveMsg.ext,
+                    chatType: receiveMsg.type,
+                    toJid: "",
+                    body: {
+                        type: type,
+                        msg: receiveMsg.data,
+                    },
                 },
-            },
-            value: receiveMsg.data
-        };
-    }
-    else if (type == msgType.FILE) {
-        sendableMsg = {
-            id: receiveMsg.id,
-            type: type,
-            body: {
+                value: receiveMsg.data
+            };
+        }
+        else if (type == msgType.FILE) {
+            sendableMsg = {
                 id: receiveMsg.id,
-                length: receiveMsg.file_length,
-                from: receiveMsg.from,
-                to: receiveMsg.to,
-                type: receiveMsg.type,
-                ext: receiveMsg.ext,
-                chatType: receiveMsg.type,
-                toJid: "",
+                type: type,
                 body: {
-                    type: type,
-                    url: receiveMsg.url,
-                    filename: receiveMsg.filename,
-                    msg: "当前不支持此格式消息展示",
+                    id: receiveMsg.id,
+                    length: receiveMsg.file_length,
+                    from: receiveMsg.from,
+                    to: receiveMsg.to,
+                    type: receiveMsg.type,
+                    ext: receiveMsg.ext,
+                    chatType: receiveMsg.type,
+                    toJid: "",
+                    body: {
+                        type: type,
+                        url: receiveMsg.url,
+                        filename: receiveMsg.filename,
+                        msg: "当前不支持此格式消息展示",
+                    },
                 },
-            },
-            value: receiveMsg.data
-        };
-    }
-    else if (type == msgType.AUDIO) {
-        sendableMsg = {
-            id: receiveMsg.id,
-            type: type,
-            accessToken: receiveMsg.token || receiveMsg.accessToken,
-            body: {
+                value: receiveMsg.data
+            };
+        }
+        else if (type == msgType.AUDIO) {
+            sendableMsg = {
                 id: receiveMsg.id,
-                length: receiveMsg.length,
-                from: receiveMsg.from,
-                to: receiveMsg.to,
-                type: receiveMsg.type,
-                ext: receiveMsg.ext,
-                chatType: receiveMsg.type,
-                toJid: "",
+                type: type,
+                accessToken: receiveMsg.token || receiveMsg.accessToken,
                 body: {
-                    type: type,
-                    url: receiveMsg.url,
-                    filename: receiveMsg.filename,
-                    filetype: receiveMsg.filetype,
+                    id: receiveMsg.id,
+                    length: receiveMsg.length,
                     from: receiveMsg.from,
-                    to: receiveMsg.to
+                    to: receiveMsg.to,
+                    type: receiveMsg.type,
+                    ext: receiveMsg.ext,
+                    chatType: receiveMsg.type,
+                    toJid: "",
+                    body: {
+                        type: type,
+                        url: receiveMsg.url,
+                        filename: receiveMsg.filename,
+                        filetype: receiveMsg.filetype,
+                        from: receiveMsg.from,
+                        to: receiveMsg.to
+                    },
                 },
-            },
-        };
-    }
-    else if (type == msgType.VIDEO) {
-        sendableMsg = {
-            id: receiveMsg.id,
-            type: type,
-            accessToken: receiveMsg.token || receiveMsg.accessToken,
-            body: {
+            };
+        }
+        else if (type == msgType.VIDEO) {
+            sendableMsg = {
                 id: receiveMsg.id,
-                length: receiveMsg.length,
-                from: receiveMsg.from,
-                to: receiveMsg.to,
-                type: receiveMsg.type,
-                ext: receiveMsg.ext,
-                chatType: receiveMsg.type,
-                toJid: "",
+                type: type,
+                accessToken: receiveMsg.token || receiveMsg.accessToken,
                 body: {
-                    type: type,
-                    url: receiveMsg.url,
-                    filename: receiveMsg.filename,
-                    filetype: receiveMsg.filetype,
+                    id: receiveMsg.id,
+                    length: receiveMsg.length,
                     from: receiveMsg.from,
-                    to: receiveMsg.to
+                    to: receiveMsg.to,
+                    type: receiveMsg.type,
+                    ext: receiveMsg.ext,
+                    chatType: receiveMsg.type,
+                    toJid: "",
+                    body: {
+                        type: type,
+                        url: receiveMsg.url,
+                        filename: receiveMsg.filename,
+                        filetype: receiveMsg.filetype,
+                        from: receiveMsg.from,
+                        to: receiveMsg.to
+                    },
                 },
-            },
-        };
-    }
-    else {
-        return;
-    }
-    this.saveMsg(sendableMsg, type, receiveMsg);
+            };
+        }
+        else {
+            return;
+        }
+        this.saveMsg(sendableMsg, type, receiveMsg);
+
+    });
+
+
 };
+
 msgStorage.saveMsg = function (sendableMsg, type, receiveMsg) {
-    //console.log('sendableMsgsendableMsg', sendableMsg)
+    console.log('sendableMsgsendableMsg', sendableMsg)
     let me = this;
     let myName = wx.getStorageSync("myUsername");
     let sessionKey;