hyperf-chat/app/Service/EmoticonService.php

163 lines
4.7 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2021-09-04 20:18:20 +08:00
declare(strict_types=1);
2020-11-04 11:57:16 +08:00
2020-11-04 16:47:17 +08:00
namespace App\Service;
2020-11-04 11:57:16 +08:00
2021-07-08 19:30:03 +08:00
use App\Constants\TalkMessageType;
2021-07-20 23:12:18 +08:00
use App\Constants\TalkModeConstant;
2021-07-09 22:57:19 +08:00
use App\Model\Talk\TalkRecords;
use App\Model\Talk\TalkRecordsFile;
2022-01-20 21:20:58 +08:00
use App\Model\Emoticon\EmoticonItem;
use App\Model\Emoticon\UsersEmoticon;
2021-07-28 23:08:07 +08:00
use App\Service\Group\GroupMemberService;
2020-11-04 11:57:16 +08:00
2020-11-07 22:57:10 +08:00
/**
* 表情服务层
* Class EmoticonService
2021-04-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @package App\Services
*/
2020-11-04 11:57:16 +08:00
class EmoticonService extends BaseService
{
2020-11-07 22:57:10 +08:00
/**
* 安装系统表情包
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2020-11-07 22:57:10 +08:00
* @param int $emoticon_id 表情包ID
2021-04-22 16:14:34 +08:00
* @return bool
2020-11-07 22:57:10 +08:00
*/
2021-09-04 20:18:20 +08:00
public function installSysEmoticon(int $user_id, int $emoticon_id): bool
2020-11-07 22:57:10 +08:00
{
$info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first();
if (!$info) {
2021-04-22 16:14:34 +08:00
return (bool)UsersEmoticon::create(['user_id' => $user_id, 'emoticon_ids' => $emoticon_id]);
2020-11-07 22:57:10 +08:00
}
2020-11-04 11:57:16 +08:00
2020-11-07 22:57:10 +08:00
$emoticon_ids = $info->emoticon_ids;
if (in_array($emoticon_id, $emoticon_ids)) {
return true;
}
$emoticon_ids[] = $emoticon_id;
2021-04-22 16:14:34 +08:00
return (bool)UsersEmoticon::where('user_id', $user_id)->update([
2020-11-07 22:57:10 +08:00
'emoticon_ids' => implode(',', $emoticon_ids)
2021-04-22 16:14:34 +08:00
]);
2020-11-07 22:57:10 +08:00
}
/**
* 移除已安装的系统表情包
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2020-11-07 22:57:10 +08:00
* @param int $emoticon_id 表情包ID
* @return bool
*/
2021-09-04 20:18:20 +08:00
public function removeSysEmoticon(int $user_id, int $emoticon_id): bool
2020-11-07 22:57:10 +08:00
{
$info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first();
if (!$info || !in_array($emoticon_id, $info->emoticon_ids)) {
return false;
}
$emoticon_ids = $info->emoticon_ids;
foreach ($emoticon_ids as $k => $id) {
2021-04-22 16:14:34 +08:00
if ($id == $emoticon_id) unset($emoticon_ids[$k]);
2020-11-07 22:57:10 +08:00
}
if (count($info->emoticon_ids) == count($emoticon_ids)) {
return false;
}
2021-04-22 16:14:34 +08:00
return (bool)UsersEmoticon::where('user_id', $user_id)->update([
2020-11-07 22:57:10 +08:00
'emoticon_ids' => implode(',', $emoticon_ids)
2021-04-22 16:14:34 +08:00
]);
2020-11-07 22:57:10 +08:00
}
/**
* 获取用户安装的表情ID
*
* @param int $user_id 用户ID
* @return array
*/
2021-09-04 20:18:20 +08:00
public function getInstallIds(int $user_id): array
2020-11-07 22:57:10 +08:00
{
$result = UsersEmoticon::where('user_id', $user_id)->value('emoticon_ids');
2021-09-04 20:18:20 +08:00
2020-11-21 19:53:01 +08:00
return $result ? array_filter($result) : [];
2020-11-07 22:57:10 +08:00
}
/**
* 收藏聊天图片
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2020-11-07 22:57:10 +08:00
* @param int $record_id 聊天消息ID
* @return array
*/
2021-09-04 20:18:20 +08:00
public function collect(int $user_id, int $record_id): array
2020-11-07 22:57:10 +08:00
{
2021-07-05 21:52:44 +08:00
$result = TalkRecords::where([
2020-11-07 22:57:10 +08:00
['id', '=', $record_id],
2021-07-08 19:30:03 +08:00
['msg_type', '=', TalkMessageType::FILE_MESSAGE],
2020-11-07 22:57:10 +08:00
['is_revoke', '=', 0],
2021-07-05 21:52:44 +08:00
])->first(['id', 'talk_type', 'receiver_id', 'msg_type', 'user_id', 'is_revoke']);
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
if (!$result) return [false, []];
2020-11-07 22:57:10 +08:00
2021-07-20 23:12:18 +08:00
if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) {
2021-07-05 21:52:44 +08:00
if ($result->user_id != $user_id && $result->receiver_id != $user_id) {
2020-11-07 22:57:10 +08:00
return [false, []];
}
} else {
2021-07-28 23:08:07 +08:00
if (!di()->get(GroupMemberService::class)->isMember($result->receiver_id, $user_id)) {
2020-11-07 22:57:10 +08:00
return [false, []];
}
}
2021-07-05 21:52:44 +08:00
$fileInfo = TalkRecordsFile::where('record_id', $result->id)->where('file_type', 1)->first([
2020-11-07 22:57:10 +08:00
'file_suffix',
'file_size',
'save_dir'
]);
2021-07-05 21:52:44 +08:00
if (!$fileInfo) return [false, []];
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
$result = EmoticonItem::where('user_id', $user_id)->where('url', $fileInfo->save_dir)->first();
if ($result) return [false, []];
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
$res = EmoticonItem::create([
2021-04-20 16:30:57 +08:00
'user_id' => $user_id,
'url' => $fileInfo->save_dir,
2020-11-07 22:57:10 +08:00
'file_suffix' => $fileInfo->file_suffix,
2021-04-20 16:30:57 +08:00
'file_size' => $fileInfo->file_size,
2021-07-05 21:52:44 +08:00
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
2020-11-07 22:57:10 +08:00
]);
return $res ? [true, ['media_id' => $res->id, 'src' => get_media_url($res->url)]] : [false, []];
}
/**
* 移除收藏的表情包
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param array $ids 表情包详情ID
2021-04-22 16:14:34 +08:00
* @return bool
* @throws \Exception
2020-11-07 22:57:10 +08:00
*/
public function deleteCollect(int $user_id, array $ids)
{
2021-07-05 21:52:44 +08:00
return EmoticonItem::whereIn('id', $ids)->where('user_id', $user_id)->delete();
2020-11-07 22:57:10 +08:00
}
/**
* 获取表情包列表
*
* @param array $where
2021-04-22 16:14:34 +08:00
* @return array
2020-11-07 22:57:10 +08:00
*/
2021-09-04 20:18:20 +08:00
public function getDetailsAll(array $where = []): array
2020-11-07 22:57:10 +08:00
{
2022-01-22 12:48:28 +08:00
return EmoticonItem::where($where)->get(['id as media_id', 'url as src'])->toArray();
2020-11-07 22:57:10 +08:00
}
2020-11-04 11:57:16 +08:00
}