聊天列表

16 篇文章 0 订阅
//聊天记录列表
    function chat_list()
    {
//        $user_id = $_POST['send_man'];
        //聊天记录
        $chat_log = M('chat_log');
        //请求传入的参数(当前用户)
        $id = $chat_log->create();
        $user_id = $id['send_man'];
        //用户信息
        $user_info = M('user_info');
        //取出当前用户的联系人及与每个联系人的最后聊天时间
        $sqlString = "select contact, max(time) as max_time from " .
            "((select content,send_man as contact,time from lswoa_chat_log where receive_man=$user_id and (group_chat is null or group_chat='')) " .
            "UNION " .
            "(select content,receive_man as contact,time from lswoa_chat_log where send_man=$user_id and (group_chat is null or group_chat=''))) as chat_contact " .
            "GROUP BY contact " .
            "ORDER BY max_time DESC";
        $chat_list = $chat_log->query($sqlString);
        $chat_staff = M('chat_group_staff');
        $chatmap['staff'] = $user_id;
        $chatmap['status'] = 1;
        //取出用户所在群组ID
        $res_chat_staff = $chat_staff->where($chatmap)->field("chat_group_id")->select();
        //群聊
        if ($res_chat_staff) {
            foreach ($res_chat_staff as $chatk => $chatv) {
                $group_id[] = $chatv['chat_group_id'];
            }
            //用户所在群ID
            $group_id_str = implode(',', $group_id);
            //群聊天记录
            $logres = "select *,max(time) as max_time from lswoa_chat_log where group_chat in(" . $group_id_str . ") GROUP BY group_chat order by max_time desc";
            $lres = M()->query($logres);
            foreach ($lres as $lok => $lov) {
                $mapsta['chat_group_id'] = $lov['group_chat'];
                $chat_li['cy'] = $chat_staff->where($mapsta)->field('staff')->select();
                $group_name = M('chat_group')->where('id=' . $lov['group_chat'])->find();
                $chat_li['group_chat'] = $lov['group_chat'];
                //群名称
                $chat_li['group_name'] = $group_name['name'];
                //群好友id
                $mapstaff['id'] = $lov['send_man'];
                //群聊天内容
                $chat_li['content'] = $lov['content'];
                //查找该联系人(好友)信息
                $user = $user_info->where($mapstaff)->find();
                //取出姓名
                $chat_li['name'] = $user['name'];
                //最后聊天时间
                $chat_li['max_time'] = $lov['max_time'];
                //是否群聊
                $chat_li['is_group'] = '1';
                //头像
                $chat_li['head_protrait'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $user['head_protrait'];
                //未读消息条数
                $not_read_count = "select COUNT(*) as nr_count from lswoa_chat_log WHERE lswoa_chat_log.group_chat = " . $lov['group_chat'] . " and lswoa_chat_log.send_man != $user_id and lswoa_chat_log.id not in (select lswoa_chat_log_read.chat_log_id from lswoa_chat_log_read WHERE receive_man = " . $user_id . ")";
                $not_read_count_res = M()->query($not_read_count);
                $chat_li['not_read_count'] = $not_read_count_res[0]['nr_count'];

                $list['list']['chat'][] = $chat_li;
            }
        }
        //私聊
        if ($chat_list) {
            //取出该条聊天记录对应的聊天内容和用户信息
            foreach ($chat_list as $key => $value) {
                //联系人(好友)
                $contact = $value['contact'];
                //最后聊天时间
                $max_time = $value['max_time'];
                //聊天记录的查找条件
                $map_chat_log["send_man"] = $contact;
                $map_chat_log["receive_man"] = $user_id;
                $map_chat_log["time"] = $max_time;
                //用户信息的查找条件
                $map_user_info["id"] = $contact;
                //查找该条聊天信息
                $chat_info = $chat_log->where($map_chat_log)->find();
                if (!$chat_info) {
                    //置换聊天记录的查找条件中的发送与接受方
                    $map_chat_log["receive_man"] = $contact;
                    $map_chat_log["send_man"] = $user_id;
                    //查找该条聊天信息
                    $chat_info = $chat_log->where($map_chat_log)->find();
                }
                //取出其聊天内容
                $chat_list[$key]['content'] = $chat_info['content'];
                //查找该联系人(好友)信息
                $user = $user_info->where($map_user_info)->find();
                //取出姓名
                $chat_list[$key]['name'] = $user['name'];
                //最后聊天时间
                $chat_list[$key]['max_time'] = $max_time;
                //是否群聊
                $chat_list[$key]['is_group'] = '0';
                //头像
                $chat_list[$key]['head_protrait'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $user['head_protrait'];
                //未读消息条数
                $not_read_count = "select COUNT(*) as nr_count from lswoa_chat_log WHERE (lswoa_chat_log.send_man = " . $contact . " and lswoa_chat_log.receive_man = " . $user_id . ") and (lswoa_chat_log.group_chat is null or lswoa_chat_log.group_chat='') and lswoa_chat_log.id not in (select lswoa_chat_log_read.chat_log_id from lswoa_chat_log_read WHERE receive_man = " . $user_id . ")";
                $not_read_count_res = M()->query($not_read_count);
                $chat_list[$key]['not_read_count'] = $not_read_count_res[0]['nr_count'];

            }
            foreach ($chat_list as $k => $v) {
                //在聊天列表中按照max_time进行倒序排序
                $size = count($list['list']['chat']);
                for ($i=0; $i<=$size; $i++) {
                    if ($list['list']['chat'][$i]['max_time'] < $v['max_time']) {
                        array_splice($list['list']['chat'], $i, 0, array($v));
                        break;
                    }
                }
            }
            $code = array('code' => 1, 'message' => '请求成功');
            $this->ajaxReturn(array_merge($code, $list));
        } else {
            $code = array('code' => 2, 'message' => '请求失败');
            $this->ajaxReturn($code);
        }
    }


    //聊天记录详情
    function chat_content()
    {
        $group_chat = $_POST['group_chat'];
        $send_man = $_POST['send_man'];
        $receive_man = $_POST['contact'];
        if (!empty($group_chat)) {
            //群组
            $chat_log = M('chat_log');
//                    $map['send_man'] = $send_man;
            $map['group_chat'] = $group_chat;
            $res = $chat_log->where($map)->order('time')->select();
            $group_staff = M('chat_group_staff');
            $comap['lswoa_chat_group_staff.chat_group_id'] = $group_chat;
            $comap['lswoa_chat_group_staff.status'] = 1;
            $head = $group_staff->where($comap)->join('LEFT JOIN lswoa_user_info ON lswoa_user_info.id=lswoa_chat_group_staff.staff')->field('lswoa_user_info.id,lswoa_user_info.name,lswoa_user_info.head_protrait')->select();
            if ($res) {
                //读出的信息如果没有写入已读数据表则写入
                $sql_insert = "insert into lswoa_chat_log_read(chat_log_id,receive_man,read_time) select id,'".$send_man."' as receive_man,now() from lswoa_chat_log where group_chat=$group_chat and id not in (select chat_log_id from lswoa_chat_log_read)";
                $Model = new \Think\Model();
                $Model->execute($sql_insert);
                foreach ($head as $k => $v) {
                    $v['head_protrait'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $v['head_protrait'];
                    $list['list']['man'][] = $v;
                }
                foreach ($res as $ke => $ve) {
                    $list['list']['chat_content'][] = $ve;
                }
                $code = array('code' => 1, 'message' => '请求成功');
                $this->ajaxReturn(array_merge($code, $list));
            } else {
                $code = array('code' => 2, 'message' => '请求失败');
                $this->ajaxReturn($code);
            }
        } else {
            //私聊
            $sql = "select id,content,send_man,receive_man,time from lswoa_chat_log where (send_man=$send_man and receive_man=$receive_man) or (send_man=$receive_man and receive_man=$send_man) ORDER BY time";
            $res = M()->query($sql);
            $user_info = M('user_info');
            $condition['id'] = $receive_man;
            $head = $user_info->where($condition)->field('id,name,head_protrait')->select();
            //读出的信息如果没有写入已读数据表则写入
            $sql_insert = "insert into lswoa_chat_log_read(chat_log_id,receive_man,read_time) select id,receive_man,now() from lswoa_chat_log where receive_man=$send_man and send_man=$receive_man and id not in (select chat_log_id from lswoa_chat_log_read)";
            $Model = new \Think\Model();
            $Model->execute($sql_insert);
            if ($res) {
                foreach ($head as $key => $val) {
                    $val['head_protrait'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['head_protrait'];
                    $list['list']['man'][] = $val;
                }
                foreach ($res as $k => $v) {
                    $list['list']['chat_content'][] = $v;
                }
                $code = array('code' => 1, 'message' => '请求成功');
                $this->ajaxReturn(array_merge($code, $list));
            } else {
                $code = array('code' => 2, 'message' => '请求失败');
                $this->ajaxReturn($code);
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值