表一
表二
表三
/**
* 用户一对一发送消息
* @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(&