PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > CMS教程 > Fastadmin

企业IM客服系统插件之消息撤回功能开发

管理员 2024-12-14
Fastadmin
8

最近根据客户需求,二开了IM的消息撤回、发送视频获取封面、以及群备注等功能,改动太多,就只放上需求最多的消息撤回代码。

代码:
一、后端代码:
1、 addons/fastim/library/controller/Message.php 文件大概317处新增判断

        } elseif ($data['action'] == 'message-delete') {            // ...代码忽略        } elseif ($data['action'] == 'message-withdraw') {            // 撤回消息            $sessionInfo = Common::sessionInfo($messageInfo['session_id'], $this->userInfo['id']);            $oldMessageData = $messageInfo;            // 单聊            if ($sessionInfo['type'] == 'single') {                Db::name('fastim_message')->where('id', $data['id'])->update([                    'type'      => 'system',                    'sender_id' => 0,                    'status'    => 2,                    'message'   => json_encode([                        'message'       => '对方撤回了一条消息~',                        'display_user'  => $oldMessageData['recipient_id']                    ])                ]);                $oldMessageData['type'] = 'system';                $oldMessageData['message'] = [                    'message'       => '对方撤回了一条消息~',                    'display_user'  => $oldMessageData['recipient_id']                ];                $this->swooleCommon->withdrawMessagePush($oldMessageData, $sessionInfo, $sessionInfo['sessionUser']['id']);            } elseif ($sessionInfo['type'] == 'group') {                // 撤回用户昵称-群昵称                $withdrawUserNickname = Db::name('fastim_group_user')                    ->where('group_id', $sessionInfo['chat_id'])                    ->where('user_id', $this->userInfo['id'])                    ->value('nickname', Db::name('fastim_user')->where('user_id', $this->userInfo['id'])->value('nickname', '用户'));                Db::name('fastim_message')->where('id', $data['id'])->update([                    'type'      => 'system',                    'sender_id' => 0,                    'status'    => 2,                    'message'   => json_encode([                        'message'       => $withdrawUserNickname . '撤回了一条消息~',                        'display_user'  => 'all'                    ])                ]);                $oldMessageData['type'] = 'system';                $oldMessageData['message'] = [                    'message'       => $withdrawUserNickname . '撤回了一条消息~',                    'display_user'  => 'all'                ];                $this->swooleCommon->pushWithdrawMessageToGroup('system', $oldMessageData, $this->userInfo['id']);            }            $this->server->push($this->frame->fd, json_encode([                'event' => 'send_message',                'data'  => [                    'message_id'     => $data['id'],                    'new_message_id' => $data['id'],                    'sendToYourself' => true                ]            ]));            $resMessage = [                'type' => 'success',                'msg'  => '消息已撤回~'            ];        } elseif ($data['action'] == 'message-new-window') {           // ...代码忽略        }

2、 addons/fastim/library/swoole/Common.php 文件新增2个撤回消息函数

   /**     * 撤回消息推送     *     * Name: withdrawMessagePush     * User: 神威无敌大将军     * Date: 2022/10/25 16:42     * @param $messageData     * @param $sessionInfo     * @param $recipientId     */    public function withdrawMessagePush($messageData, $sessionInfo, $recipientId)    {        // 会话置顶状态        $sessionInfo['top'] = Db::name('fastim_session_top')            ->where('user_id', $recipientId)            ->where('session_id', $sessionInfo['id'])            ->value('top');        // 接受人和发送人        if (!isset($sessionInfo['pushUser'])) {            if ($recipientId == $sessionInfo['sessionUser']['id']) {                $sessionInfo['pushUser']      = $sessionInfo['user'];                $sessionInfo['recipientUser'] = $sessionInfo['sessionUser'];            } else {                $sessionInfo['pushUser']      = $sessionInfo['sessionUser'];                $sessionInfo['recipientUser'] = $sessionInfo['user'];            }        }        unset($sessionInfo['sessionUser'], $sessionInfo['user']);        // 最后消息和推送消息资料        $lastMessage = extCommon::getLastMessage($recipientId, $sessionInfo);        $this->pushMessageToUser($recipientId, [            'event' => 'withdraw_message',            'data'  => [                'messageData'          => $messageData,                'sessionInfo'          => $sessionInfo,                'lastMessage'          => $lastMessage,                'unreadMessagesNumber' => extCommon::getUnreadMessagesNumber($recipientId, $sessionInfo),                'shield'               => false            ]        ]);    }    /**     * 发送撤回消息到群聊     * @param  [type]  $type         消息类型     * @param  [type]  $message      要入库的消息数据     * @param boolean $noPushUser 不推送给此用户     * @param integer $oldMessageId 前台生成的临时消息ID     * @param array $atUser at的群成员数据     */    public function pushWithdrawMessageToGroup($type, $messageData, $noPushUser = false, $oldMessageId = 0, $atUser = [])    {        if (!isset($messageData['group_id']) || !$messageData['group_id']) {            return false;        }        $messageData['type'] = $type; // 消息 type 通过外部传递,方便查找、识别代码        // 取得群聊中所有的用户        $groupUser = Db::name('fastim_group_user')->where('group_id', $messageData['group_id'])->select();        $senderUserInfo  = extCommon::getImUserInfo($messageData['sender_id']);        $imGroupChatInfo = extCommon::getImGroupChat($messageData['group_id']);        $speak = ($imGroupChatInfo['speak'] == 1 && $messageData['sender_id'] != $imGroupChatInfo['leader']) ? true : false;        $messageData['status']     = $speak ? 4 : 0;        $messageData['createtime'] = time();        if ($messageData) {            // 此时推送发送成功的消息(以免成功通知在群消息阅读通知之后)            if ($oldMessageId) {                $this->server->push($this->frame->fd, json_encode([                    'event' => 'send_message',                    'data'  => [                        'message_id'     => $oldMessageId,                        'new_message_id' => $messageData['id'],                        'status'         => $messageData['status'],                        'sendToYourself' => true                    ]                ]));            }            $pushMessage['messageData'] = $messageData;            $pushMessage['sessionInfo'] = [                'type'        => 'group',                'chat_id'     => $messageData['group_id'],                'sessionUser' => $imGroupChatInfo,                'pushUser'    => $senderUserInfo            ];            // 发信人群聊昵称            $groupUserNickname = Db::name('fastim_group_user')                ->where('group_id', $messageData['group_id'])                ->where('user_id', $messageData['sender_id'])                ->value('nickname');            foreach ($groupUser as $key => $value) {                if ($noPushUser && $value['user_id'] == $noPushUser) {                    continue;                }                $pushMessage['lastMessage'] = extCommon::getLastMessage($value['user_id'], $pushMessage['sessionInfo']);                // 获取发信人给群聊中的收信人设置的备注                // 不在大循环中使用 extCommon::getImUserInfo                $friend                                             = Db::name('fastim_friendship')                    ->field('user_id,remark')                    ->where('user_id', $value['user_id'])                    ->where('friend_id', $messageData['sender_id'])                    ->find();                $pushMessage['sessionInfo']['pushUser']['friend']   = $friend ? true : false;                $pushMessage['sessionInfo']['pushUser']['avatar']   = $senderUserInfo['avatar'];                $pushMessage['sessionInfo']['pushUser']['nickname'] = $senderUserInfo['nickname'];                $newNickname                                        = extCommon::trueAttr(extCommon::nicknameSort([                    $groupUserNickname,                    $friend['remark'] ?? '',// 加入用户群昵称                    $pushMessage['sessionInfo']['pushUser']['nickname']                ], $pushMessage['sessionInfo']['pushUser']['id']));                // 必要时重新生成收信人头像                if ($pushMessage['sessionInfo']['pushUser']['nickname'] != $newNickname) {                    $pushMessage['sessionInfo']['pushUser']['nickname'] = $newNickname;                    $defaultAvatar                                      = preg_match('/^data:image/', $pushMessage['sessionInfo']['pushUser']['avatar']);                    $pushMessage['sessionInfo']['pushUser']['avatar']   = extCommon::avatarSrc([                        $defaultAvatar ? '' : $pushMessage['sessionInfo']['pushUser']['avatar']                    ], $newNickname);                }                $pushMessage['sessionInfo']['id']    = $value['session_id'];                $pushMessage['shield']               = $value['block_messages'] ? true : false;                $pushMessage['unreadMessagesNumber'] = extCommon::getUnreadMessagesNumber($value['user_id'], $pushMessage['sessionInfo']);                $pushMessage['sessionInfo']['top'] = Db::name('fastim_session_top')                    ->where('user_id', $value['user_id'])                    ->where('session_id', $pushMessage['sessionInfo']['id'])                    ->value('top');                self::pushWithdrawMessageToUser($value['user_id'], [                    'event' => 'withdraw_message',                    'data'  => $pushMessage                ]);            }            return $messageData;        } else {            return false;        }    }

3、 public/assets/addons/fastim/js/fastim.js 文件大概1094处新增判断

            [{                // 代码略            }],            ['withdraw_message', () => {                var sessionType = msg.data.sessionInfo.type                if (sessionType == 'group') {                    // 在会话列表渲染群聊资料,在会话面板渲染发信用户资料                    let pushUser = msg.data.sessionInfo.pushUser,                        sessionUser = msg.data.sessionInfo.sessionUser;                    msg.data.sessionInfo.pushUser = sessionUser;                    msg.data.sessionInfo.sessionUser = pushUser;                    FastIm.imSession(msg.data); // 会话列表会话保障                    msg.data.sessionInfo.pushUser = pushUser;                    msg.data.sessionInfo.sessionUser = sessionUser;                } else {                    FastIm.imSession(msg.data); // 会话列表会话保障                }                var recordsEL = $('.fastim-window-right .chat-records')                var newHTML = '<li class="chat-record-item system"><span>' + msg.data.messageData.message.message + '</span></li>';                recordsEL.find('.chat-record-item[data-id="' + msg.data.messageData.id + '"]').before(newHTML);                recordsEL.find('.chat-record-item[data-id="' + msg.data.messageData.id + '"]').remove();            }],            ['new_message', () => {                // 代码略            }]

4、 public/assets/addons/fastim/js/fastim.js 文件大概3142处新增判断

                    // 前面代码忽略                    // 本人消息添加撤回 start                    if (e.prev('.record-avatar-img').data('session-user') == FastIm.userinfo.id) {                        menu.unshift({'action': 'message-withdraw', 'name': '撤回'});                    }                    // 本人消息添加撤回 end                    let sessionType = $('.fastim-session-li[data-data="' + FastIm.sessionId + '"]').data('type')                    if (sessionType == 'single') {                        menu.push({'action': 'message-delete', 'name': '删除消息'});                    }                    FastIm.common.contextMenu({top: event.pageY, left: (event.pageX + 14)}, menu, id);                } else if (e.hasClass('fastim-contextmenu-item')) {                    // 代码忽略                }

5、 public/assets/addons/fastim/js/fastim.js 文件大概3290处新增撤回事件

                    ['message-collection', messageOperation], // 原来的                    ['message-withdraw', messageOperation], // 消息撤回                    ['message-to-do', messageOperation], // 原来的

二、 uniapp代码:

1、 commom/websocket.js 文件大概1719处新增

            ['group_chat_at', () => {                // 代码忽略            }],            ['withdraw_message', () => {                let pages = getCurrentPages(), page = pages[pages.length - 1]                var sessionType = msg.data.sessionInfo.type, pushNickname = '', pushAvatar = ''                if (sessionType == 'group') {                    let messageShow = (pageThat = that.pageThat) => {                        that.imSession({                            lastMessage: msg.data.lastMessage,                            sessionInfo: {                                id: msg.data.sessionInfo.id,                                top: msg.data.sessionInfo.top,                                type: msg.data.sessionInfo.type,                                chat_id: msg.data.sessionInfo.chat_id,                                pushUser: msg.data.sessionInfo.sessionUser                            },                            unreadMessagesNumber: msg.data.unreadMessagesNumber,                            shield: msg.data.shield                        }, pageThat);// 会话列表会话                    }                    if (page.route == 'pages/message/message') {                        messageShow()                        messageShow = null                    } else if (page.route == 'pages/session-info/session-info') {                        that.messageShow.push(messageShow)                        if (msg.data.messageData.type == 'group_chat_notice' && parseInt(msg.data.messageData.message.popup) == 1) {                            // 弹窗公告                            that.pageThat.showNotice = true                            that.pageThat.noticeInfo = msg.data.messageData.message                        }                    } else {                        that.messageShow.push(messageShow)                    }                                        pushAvatar = that.imgUrl(msg.data.sessionInfo.sessionUser.avatar)                    pushNickname = msg.data.sessionInfo.sessionUser.nickname;                } else {                    pushAvatar = that.imgUrl(msg.data.sessionInfo.pushUser.avatar)                    pushNickname = msg.data.sessionInfo.pushUser.nickname;                    if (page.route == 'pages/message/message') {                        that.imSession(msg.data, that.pageThat);                    } else {                        that.messageShow.push((pageThat = that.pageThat) => {                            that.imSession(msg.data, pageThat);                        })                    }                }                                let messageListIndex = that.pageThat.messageList.length - 1;                if (that.pageThat.messageList[messageListIndex]) {                    that.pageThat.messageList[messageListIndex].data.map((item, index)=> {                        if (item.id == msg.data.messageData.id) {                            item.type = msg.data.messageData.type;                            item.message = msg.data.messageData.message;                        }                    });                }            }],            ['new_message', () => {                    // 代码忽略            }]

2、 pages/session-info/session-info.vue 文件大概739处新增

                    if (e.sender == 'me') {                            // 消息撤回 start                        action.unshift({id: e.id, name: '撤回', action: 'withdraw'});                        // 消息撤回 end                                                let actionWidth = action.length * 40, missWidth = (actionWidth - data.width) + 20, left = (missWidth > 0) ? (data.left - missWidth):data.left                        that.messageLongpressActionStyle = 'top: ' + (data.top - 46) + 'px;left: ' + left + 'px';                    } 

重启fastim命令,消息撤回大概就搞完了,希望能帮到你谢谢~


希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部