tp5中一对一聊天

该博客介绍了在TP5框架下如何实现一对一聊天的功能,包括发送消息、检查聊天关系、获取聊天列表、解除聊天关系等操作。通过一系列的数据库交互和条件判断,确保了聊天过程的正常进行。
摘要由CSDN通过智能技术生成

表一

   

表二

表三

/**
     * 用户一对一发送消息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
     public function put_chat_content(){
         if($this->checkToken()){
             $openid=input('openid');
             $chat_user_id=input('chat_id');
             $chat_res=$this->check_chat_id($chat_user_id);
             if (!$chat_res){
                 $res['status']=400;
                 $res['msg']='接收方用户不存在';
                 $res['data']=null;
                 echo json_encode($res);die;
             }
             $chat_content=input('chat_content');
             if ($chat_content == ''){
                 $res['status']=400;
                 $res['msg']='发送内容不能为空';
                 $res['data']=null;
                 echo json_encode($res);die;
             }
             $user_id=$this->getUserId($openid);
             if ($chat_user_id == $user_id){
                 $res['status']=400;
                 $res['msg']='接收方id不能是自己';
                 $res['data']=null;
                 echo json_encode($res);die;
             }
             $char_temp=db('chattemp')->where(array('user_id'=>$user_id,'chart_user'=>$chat_user_id))->find();
             $char_temp_two=db('chattemp')->where(array('user_id'=>$chat_user_id,'chart_user'=>$user_id))->find();
             $time=time();
             if (empty($char_temp) && empty($char_temp_two)){
                 $temp_arr=array();
                 $temp_arr['user_id']=$user_id;
                 $temp_arr['chart_user']=$chat_user_id;
                 $temp_arr['add_time']=$time;

                 $temp_arr_two['user_id']=$chat_user_id;
                 $temp_arr_two['chart_user']=$user_id;
                 $temp_arr_two['add_time']=$time;
                 db('chattemp')->insert($temp_arr);
                 db('chattemp')->insert($temp_arr_two);
             }
             $insert_arr=array();
             $insert_arr['user_id']=$user_id;
             $insert_arr['chat_id']=$chat_user_id;
             $insert_arr['text']=$chat_content;
             $insert_arr['add_time']=$time;
             $insert_arr['is_read']=0;
             $res_id=db('chattext')->insert($insert_arr);
             $res['status']=200;
             $res['msg']='消息发送成功';
             $res['data']=$res_id;
         }else{
             $res['status']=400;
             $res['msg']='用户信息不正确';
             $res['data']=null;
         }
         echo json_encode($res);
     }
     private function check_chat_id($chat_user_id=''){
         if ($chat_user_id=='')return false;
         $chart_row=db('user')->where("id=$chat_user_id")->field('id,openid,token')->find();
         if (empty($chart_row) || $chart_row['openid']==''){
             return false;
         }else{
             return true;
         }
     }
    /**
     * 获取用户消息关系列表
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get_chat_list(){
        if($this->checkToken()){
            $openid=input('openid');
            $user_id=$this->getUserId($openid);
            $tmp_arr=db('chattemp')->where("user_id=$user_id")->select();
            $chat_id = '';
            foreach ($tmp_arr as $k => $v){
                $tmp_arr[$k]['add_time']=date("Y-m-d h:i:s",$v['add_time']);
                $chat_id .=",".$v['chart_user'];
                $chat_id = ltrim($chat_id);
            }
            $where['id'] = array('in', $chat_id);
            $wheres['chat_id'] = array('=', $user_id);
            $wheres['is_read'] = array('=',0);
            $user_row=db('user')->where($where)->field('id,nickName,avatarUrl')->select();
            $moedel=db('chattext');
            foreach ($user_row as $ks => $vs){
                $wheres['user_id'] = array('=', $vs['id']);
                $user_row[$ks]['no_read_count']=$moedel->where($wheres)->count();
                $add_time=$moedel->where(array('chat_id'=>$user_id,'user_id'=>$vs['id']))->order('id desc')->value('add_time');
                $add_text=$moedel->where(array('chat_id'=>$user_id,'user_id'=>$vs['id']))->order('add_time desc')->value('text');
                if (strtotime(date(&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xingxingwuxin

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值