You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bjhedasx/app/chat/model/ChatFriendsRoomUser.php

162 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\chat\model;
use app\base\model\user\UserInfo;
use think\model\concern\SoftDelete;
class ChatFriendsRoomUser extends Base
{
use SoftDelete;
/**
* 查询房间的好友user_id
* @param int $room_id 聊天室ID
* @date 2022-08-08
*/
public function getFriendUserId($room_id)
{
$where = [
['uid', '=', $this->mid],
['room_id', '=', $room_id],
['user_id', '=', $this->userId]
];
return $this->getOneData($where, 'to_user_id');
}
/**
* 该聊天室的未读消息归0
* @param int $room_id 聊天室ID
* @date 2022-08-16
*/
public function emptyRoomUnreadMessage($room_id)
{
$where = [
['room_id', '=', $room_id],
['user_id', '=', $this->userId]
];
return $this->where($where)->save([
'unread_number' => 0
]);
}
/**
* 获取房间双方头像以及好友昵称
* @param int $room_id 聊天室ID
* @date 2022-08-03
*/
public function getUsersInfo($room_id)
{
$where = [
['uid', '=', $this->mid],
['user_id', '=', $this->userId],
// 用户未删除
['is_user_delete', '=', 0],
// 获取特定聊天室
['room_id', '=', $room_id]
];
$data = $this
->with(['userInfo', 'selfUserInfo'])
->where($where)
->field('user_id,to_user_id')
->find()->toArray();
// 释放关联数据 和 无关数据
unset($data['userInfo']);
unset($data['selfUserInfo']);
return $data;
}
/**
* 更改某聊天室用户在线状态
* @param int $room_id 聊天室ID
* @param int $is_online 是否在线 1--在线 0--不在线
* @date 2022-08-15
*/
public function updateUserOnlineStatus($room_id, $is_online)
{
$where = [
'uid' => $this->mid,
'user_id' => $this->userId,
'room_id' => $room_id
];
// 更改状态不自动更新update_time否则进入聊天详情聊天列表时间会更新
$res = $this->isAutoWriteTimestamp(false)->where($where)->update([
'is_online' => $is_online
]);
return $res;
}
/**
* 查询聊天室对方是否在线
* @param int $room_id 聊天室ID
* @param int $user_id 好友ID
* @date 2022-08-16
*/
public function isFriendOnline($room_id, $user_id)
{
$where = [
['uid', '=', $this->mid],
['room_id', '=', $room_id],
['user_id', '=', $user_id]
];
return $this->getOneData($where, 'is_online');
}
/**
* 查询我的个人聊天室
* @date 2022-08-01
*/
public function listChatRoom()
{
$where = [
['uid', '=', $this->mid],
['user_id', '=', $this->userId],
// 用户未删除
['is_user_delete', '=', 0]
];
$data_list = $this
->with(['userInfo'])
->field('room_id,to_user_id,unread_number,last_message_type,last_message_content,update_time')
->where($where)
->order('update_time desc')
->select()
->toArray();
return $data_list;
}
//--------------------------------------------------关联模型---------------------------------------------------------
/**
* 一对一关联 -- 关联好友用户信息
* @date 2022-08-01
*/
public function userInfo()
{
return $this->hasOne(UserInfo::class, 'user_id', 'to_user_id')->bind([
'to_user_nick_name' => 'nick_name',
'to_user_head_img' => 'head_img'
]);
}
/**
* 一对一关联 -- 关联个人用户信息
* @date 2022-08-03
*/
public function selfUserInfo()
{
return $this->hasOne(UserInfo::class, 'user_id', 'user_id')->bind([
'user_head_img' => 'head_img'
]);
}
}