聊天,基本
This commit is contained in:
@@ -317,38 +317,33 @@ class GroupController extends ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置群免打扰 (废弃)
|
* 设置群免打扰
|
||||||
*/
|
*/
|
||||||
public function setDoNotDisturb(Request $request): Response
|
public function setDoNotDisturb(Request $request): Response
|
||||||
{
|
{
|
||||||
$groupId = $request->post('group_id');
|
$groupId = $request->post('group_id');
|
||||||
$status = $request->post('status'); // 0=关闭,1=开启
|
$status = $request->post('status'); // 0=关闭,1=开启
|
||||||
|
|
||||||
if (!$groupId) {
|
if (!$groupId) {
|
||||||
return $this->error('缺少group_id');
|
return $this->error('缺少group_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证是否在群内
|
// 验证是否在群内
|
||||||
$isMember = Db::name('chat_group_member')->where([
|
$isMember = Db::name('chat_group_member')->where([
|
||||||
'group_id' => $groupId,
|
'group_id' => $groupId,
|
||||||
'user_id' => $this->uid,
|
'user_id' => $this->uid,
|
||||||
'quit_time' => null,
|
'quit_time' => null,
|
||||||
'is_kicked' => 0
|
'is_kicked' => 0
|
||||||
])->exists();
|
])->find();
|
||||||
if (!$isMember) {
|
if (!$isMember) {
|
||||||
return $this->error('不在群内');
|
return $this->error('不在群内');
|
||||||
}
|
}
|
||||||
|
$res = Db::name('chat_group_member')->where([
|
||||||
// 生成会话ID
|
'group_id' => $groupId,
|
||||||
$sessionId = Session::generateSessionId(2, $uid, $groupId);
|
'user_id' => $this->uid,
|
||||||
|
])->update(['is_dist' => $status]);
|
||||||
// 更新免打扰状态
|
if($res) {
|
||||||
ChatDoNotDisturb::updateOrCreate(
|
return $this->success();
|
||||||
['user_id' => $uid, 'session_id' => $sessionId],
|
}
|
||||||
['status' => $status, 'updated_at' => time()]
|
return $this->error();
|
||||||
);
|
|
||||||
|
|
||||||
return json(['code' => 200, 'msg' => $status ? '开启免打扰成功' : '关闭免打扰成功']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -129,11 +129,12 @@ namespace extend\chat\model;
|
|||||||
// 回馈
|
// 回馈
|
||||||
Gateway::sendToClient($client_id, self::sendGormat($message, $list_arr));
|
Gateway::sendToClient($client_id, self::sendGormat($message, $list_arr));
|
||||||
|
|
||||||
$str_uid = self::getUid(self::$user_info['uid'], self::$user_info['user_type']);
|
|
||||||
// 上线后立马发送没有发送的消息
|
// 上线后立马发送没有发送的消息
|
||||||
$send_data = Redis::get('nosuccessful:message:sent:' . $str_uid);
|
$send_data = Business::getRedisMessage(self::$user_info['uid'], self::$user_info['user_type']);
|
||||||
if($send_data) {
|
if($send_data) {
|
||||||
Gateway::sendToClient($client_id, $send_data);
|
foreach ($send_data as $k => $v) {
|
||||||
|
Gateway::sendToClient($client_id, $v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,8 +242,8 @@ namespace extend\chat\model;
|
|||||||
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
|
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
|
||||||
return;
|
return;
|
||||||
}else {
|
}else {
|
||||||
// 不在线的存下来等上线了发
|
// 存消息
|
||||||
Redis::set('nosuccessful:message:sent:' . $str_uid, json_encode($notice_data));
|
Business::setRedisMessage($message['to_id'], $message['to_user_type'], $notice_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
}elseif ($message['chat_type'] == 2) {
|
}elseif ($message['chat_type'] == 2) {
|
||||||
@@ -258,19 +259,21 @@ namespace extend\chat\model;
|
|||||||
if($grep['user_id'] != self::$user_info['uid']) {
|
if($grep['user_id'] != self::$user_info['uid']) {
|
||||||
$user_type = $grep['role']==1?2:1;
|
$user_type = $grep['role']==1?2:1;
|
||||||
$str_uid = self::getUid($grep['user_id'], $user_type);
|
$str_uid = self::getUid($grep['user_id'], $user_type);
|
||||||
// 群成员是否在线,在线发 不在线存起来等上线了发
|
// 群成员是否在线,在线发 不在线存起来等上线了发 免打扰也不发消息
|
||||||
if(Gateway::isUidOnline($str_uid)) {
|
if($grep['is_dist'] == 0) {
|
||||||
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
|
if(Gateway::isUidOnline($str_uid)) {
|
||||||
return;
|
Gateway::sendToUid($str_uid, self::sendReceive($notice_data));
|
||||||
}else {
|
return;
|
||||||
// 不在线的存下来等上线了发
|
}else {
|
||||||
Redis::set('nosuccessful:message:sent:' . $str_uid, json_encode($notice_data));
|
// 存消息
|
||||||
}
|
Business::setRedisMessage($grep['user_id'], $user_type, $notice_data);
|
||||||
// 加入未读计数
|
}
|
||||||
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id']])->find()) {
|
// 加入未读计数
|
||||||
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->update(['count' => 1, 'updated_time' => d()]);
|
if(!Db::name('chat_unread_count')->where(['user_id' => $grep['user_id']])->find()) {
|
||||||
}else {
|
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->update(['count' => 1, 'updated_time' => d()]);
|
||||||
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
|
}else {
|
||||||
|
Db::name('chat_unread_count')->where(['user_id' => $grep['user_id'], 'session_id' => $session_id])->inc('count')->update(['updated_time' => d()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
extend/chat/model/Business.php
Normal file
45
extend/chat/model/Business.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace extend\chat\model;
|
||||||
|
|
||||||
|
use ba\Random;
|
||||||
|
use support\Redis;
|
||||||
|
|
||||||
|
class Business
|
||||||
|
{
|
||||||
|
// 存消息
|
||||||
|
public static function setRedisMessage($uid, $user_type, $data)
|
||||||
|
{
|
||||||
|
$str_uid = Base::getUid($uid, $user_type);
|
||||||
|
$rand_msg_str = $str_uid . Random::build();
|
||||||
|
$set_str = 'nomsg:message:' . $str_uid;
|
||||||
|
$set_info_str = 'nomsg:message:' . $rand_msg_str;
|
||||||
|
$sent = Redis::get($set_str);
|
||||||
|
if($sent) {
|
||||||
|
$sent = json_decode($sent, true);
|
||||||
|
$sent[count($sent - 1)] = $set_info_str;
|
||||||
|
Redis::set($set_str, json_encode($sent));
|
||||||
|
}else {
|
||||||
|
Redis::set($set_str, json_encode([$set_info_str]));
|
||||||
|
}
|
||||||
|
Redis::set($set_info_str, json_encode($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 拉消息
|
||||||
|
public static function getRedisMessage($uid, $user_type)
|
||||||
|
{
|
||||||
|
$str_uid = Base::getUid($uid, $user_type);
|
||||||
|
$set_str = 'nomsg:message:' . $str_uid;
|
||||||
|
$sent = Redis::get($set_str);
|
||||||
|
$data = [];
|
||||||
|
if($sent) {
|
||||||
|
$sent = json_decode($sent, true);
|
||||||
|
foreach ($sent as $k => $v) {
|
||||||
|
$data[] = json_decode(Redis::get($v), true);
|
||||||
|
Redis::del($v);
|
||||||
|
}
|
||||||
|
Redis::del($set_str);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user