162 lines
4.5 KiB
PHP
162 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ImCallback\Message;
|
|
|
|
use App\Common\cache\redis\ApiRedisKey;
|
|
use App\Jobs\LoveGPTChat;
|
|
use App\Models\App\MsgHistory;
|
|
use App\Models\App\UserTeam;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
class Team
|
|
{
|
|
public function handle()
|
|
{
|
|
$msg_type = request()->input('msgType');
|
|
if (!in_array($msg_type, ['TEXT', 'PICTURE', 'AUDIO', 'VIDEO',"NOTIFICATION"])) {
|
|
return;
|
|
}
|
|
$this->saveMessage();
|
|
$this->cacheGroupChatUnread();
|
|
$this->loveGPT();
|
|
}
|
|
|
|
public function recall() {
|
|
$msgId = request()->input("msgId");
|
|
if ($msgId) {
|
|
MsgHistory::where('msgid', $msgId)->update(['is_recall'=> 1]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存消息记录
|
|
* @return void
|
|
*/
|
|
private function saveMessage()
|
|
{
|
|
$msg_type = request()->input('msgType');
|
|
$from_client_type = request()->input('fromClientType');
|
|
$body = request()->input('body');
|
|
$attach = request()->input('attach');
|
|
$msgid_server = request()->input('msgidServer');
|
|
switch ($msg_type) {
|
|
case 'TEXT':
|
|
$type = 0;
|
|
break;
|
|
case 'PICTURE':
|
|
$type = 1;
|
|
break;
|
|
case 'AUDIO':
|
|
$type = 2;
|
|
break;
|
|
case 'VIDEO':
|
|
$type = 3;
|
|
break;
|
|
case "NOTIFICATION":
|
|
$type = 7;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
switch ($from_client_type) {
|
|
case 'AOS':
|
|
$fromclient_type = 1;
|
|
break;
|
|
case 'IOS':
|
|
$fromclient_type = 2;
|
|
break;
|
|
case 'PC':
|
|
$fromclient_type = 4;
|
|
break;
|
|
case 'WEB':
|
|
$fromclient_type = 16;
|
|
break;
|
|
case 'REST':
|
|
$fromclient_type = 32;
|
|
break;
|
|
default:
|
|
$fromclient_type = 0;
|
|
break;
|
|
}
|
|
if ($msg_type == 'TEXT') {
|
|
$body = json_encode(['msg' => $body], JSON_UNESCAPED_UNICODE);
|
|
} else {
|
|
$body = $attach;
|
|
}
|
|
$check = MsgHistory::where('msgid', $msgid_server)->count();
|
|
if ($check) {
|
|
return;
|
|
}
|
|
$msg_history = new MsgHistory();
|
|
$msg_history->user_id = request()->input('fromAccount');
|
|
$msg_history->team_id = request()->input('to');
|
|
$msg_history->from = request()->input('fromAccount');
|
|
$msg_history->send_time = request()->input('msgTimestamp');
|
|
$msg_history->msgid = $msgid_server;
|
|
$msg_history->type = $type;
|
|
$msg_history->fromclient_type = $fromclient_type;
|
|
$msg_history->msgidclient = request()->input('msgidClient');
|
|
$msg_history->body = $body;
|
|
$msg_history->save();
|
|
}
|
|
|
|
/**
|
|
* 群聊未读用户集合缓存
|
|
* @return void
|
|
*/
|
|
private function cacheGroupChatUnread()
|
|
{
|
|
$from_account = request()->input('fromAccount');
|
|
$team_id = request()->input('to');
|
|
$where = [
|
|
['team_id', '=', $team_id],
|
|
['user_id', '!=', $from_account]
|
|
];
|
|
$user_ids = UserTeam::where($where)->pluck('user_id');
|
|
$cache_key = ApiRedisKey::getGroupChatUnreadKey($team_id);
|
|
foreach ($user_ids as $user_id) {
|
|
Redis::sAdd($cache_key, $user_id);
|
|
}
|
|
Redis::sRem($cache_key, $from_account);
|
|
}
|
|
|
|
/**
|
|
* 小恋自动回复
|
|
* @return void
|
|
*/
|
|
private function loveGPT()
|
|
{
|
|
$from_account = request()->input('fromAccount');
|
|
$msg_type = request()->input('msgType');
|
|
$to = request()->input('to');
|
|
$body = request()->input('body');
|
|
|
|
//不是文本
|
|
if ($msg_type != 'TEXT') {
|
|
return;
|
|
}
|
|
|
|
//过滤是小恋发的 会触发循环
|
|
if ($from_account == User::LOVE_GPT_USER_ID) {
|
|
return;
|
|
}
|
|
|
|
//没有指定字符串
|
|
if (!preg_match("/小恋/i", $body)){
|
|
return;
|
|
}
|
|
|
|
//小恋是否在群
|
|
$where = [
|
|
['user_id', '=', User::LOVE_GPT_USER_ID],
|
|
['team_id', '=', $to],
|
|
];
|
|
$check = UserTeam::where($where)->first();
|
|
if (!$check) {
|
|
return;
|
|
}
|
|
|
|
LoveGPTChat::dispatch('group_chat', $from_account, $to, User::LOVE_GPT_USER_ID, $body)->onQueue('love_gpt');
|
|
}
|
|
} |