hyperf-chat/app/Service/EmoticonService.php

177 lines
4.7 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2020-11-04 16:47:17 +08:00
namespace App\Service;
2020-11-04 11:57:16 +08:00
2020-11-07 22:57:10 +08:00
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsFile;
use App\Model\EmoticonDetail;
use App\Model\Group\Group;
2020-11-07 22:57:10 +08:00
use App\Model\UsersEmoticon;
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-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @return mixed
*/
public function installSysEmoticon(int $user_id, int $emoticon_id)
{
$info = UsersEmoticon::select(['id', 'user_id', 'emoticon_ids'])->where('user_id', $user_id)->first();
if (!$info) {
return UsersEmoticon::create(['user_id' => $user_id, 'emoticon_ids' => $emoticon_id]) ? true : false;
}
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;
return UsersEmoticon::where('user_id', $user_id)->update([
'emoticon_ids' => implode(',', $emoticon_ids)
]) ? true : false;
}
/**
* 移除已安装的系统表情包
*
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-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @return bool
*/
public function removeSysEmoticon(int $user_id, int $emoticon_id)
{
$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) {
if ($id == $emoticon_id) {
unset($emoticon_ids[$k]);
}
}
if (count($info->emoticon_ids) == count($emoticon_ids)) {
return false;
}
return UsersEmoticon::where('user_id', $user_id)->update([
'emoticon_ids' => implode(',', $emoticon_ids)
]) ? true : false;
}
/**
* 获取用户安装的表情ID
*
* @param int $user_id 用户ID
2021-04-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @return array
*/
public function getInstallIds(int $user_id)
{
$result = UsersEmoticon::where('user_id', $user_id)->value('emoticon_ids');
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
2021-04-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @return array
*/
public function collect(int $user_id, int $record_id)
{
$result = ChatRecord::where([
['id', '=', $record_id],
['msg_type', '=', 2],
['is_revoke', '=', 0],
])->first(['id', 'source', 'msg_type', 'user_id', 'receive_id', 'is_revoke']);
if (!$result) {
return [false, []];
}
if ($result->source == 1) {
if ($result->user_id != $user_id && $result->receive_id != $user_id) {
return [false, []];
}
} else {
if (!Group::isMember($result->receive_id, $user_id)) {
2020-11-07 22:57:10 +08:00
return [false, []];
}
}
$fileInfo = ChatRecordsFile::where('record_id', $result->id)->where('file_type', 1)->first([
'file_suffix',
'file_size',
'save_dir'
]);
if (!$fileInfo) {
return [false, []];
}
$result = EmoticonDetail::where('user_id', $user_id)->where('url', $fileInfo->save_dir)->first();
if ($result) {
return [false, []];
}
$res = EmoticonDetail::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,
'created_at' => time()
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
*
2020-11-07 22:57:10 +08:00
* @return
*/
public function deleteCollect(int $user_id, array $ids)
{
return EmoticonDetail::whereIn('id', $ids)->where('user_id', $user_id)->delete();
}
/**
* 获取表情包列表
*
* @param array $where
2021-04-20 16:30:57 +08:00
*
2020-11-07 22:57:10 +08:00
* @return mixed
*/
public function getDetailsAll(array $where = [])
{
$list = EmoticonDetail::where($where)->get(['id as media_id', 'url as src'])->toArray();
foreach ($list as $k => $value) {
$list[$k]['src'] = get_media_url($value['src']);
}
return $list;
}
2020-11-04 11:57:16 +08:00
}