优化代码
parent
4ae4c19186
commit
6dc7f20da7
|
@ -4,31 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Cache\FriendRemark;
|
||||
use App\Cache\LastMessage;
|
||||
use App\Cache\Repository\HashRedis;
|
||||
use App\Cache\Repository\ListRedis;
|
||||
use App\Cache\Repository\LockRedis;
|
||||
use App\Cache\Repository\HashGroupRedis;
|
||||
use App\Cache\Repository\SetGroupRedis;
|
||||
use App\Cache\Repository\SetRedis;
|
||||
use App\Cache\Repository\StreamRedis;
|
||||
use App\Cache\Repository\StringRedis;
|
||||
use App\Cache\Repository\ZSetRedis;
|
||||
use App\Cache\SocketFdBindUser;
|
||||
use App\Cache\SocketRoom;
|
||||
use App\Cache\SocketUserBindFds;
|
||||
use App\Cache\UnreadTalk;
|
||||
use App\Constants\MediaFileType;
|
||||
use App\Model\Group\Group;
|
||||
use App\Model\Group\GroupMember;
|
||||
use App\Model\TalkList;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Service\TalkService;
|
||||
use App\Helper\Hash;
|
||||
use App\Model\User;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use League\Flysystem\Filesystem;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
|
@ -56,141 +36,9 @@ class TestCommand extends HyperfCommand
|
|||
|
||||
public function handle()
|
||||
{
|
||||
//$lock = LockRedis::getInstance();
|
||||
//var_dump($lock->delete('ttt'));
|
||||
//var_dump($lock->lock('ttt', 180, 5));
|
||||
|
||||
//$string = StringRedis::getInstance();
|
||||
//var_dump($string->set('yuandong', 'dong', 30));
|
||||
//var_dump($string->ttl('yuandong'));
|
||||
//var_dump($string->isExist('yuandong'));
|
||||
|
||||
//$hash = HashRedis::getInstance();
|
||||
//for ($i = 0; $i < 10; $i++) {
|
||||
// $hash->add('user:' . $i, (string)rand(0, 100));
|
||||
//}
|
||||
//var_dump($hash->count());
|
||||
//var_dump($hash->all());
|
||||
//var_dump($hash->isMember('user:1'));
|
||||
//var_dump($hash->rem('user:3'));
|
||||
//var_dump($hash->get('user:6','user:7'));
|
||||
//$hash->incr('user:6',11);
|
||||
//var_dump($hash->get('user:6'));
|
||||
|
||||
//$list = ListRedis::getInstance();
|
||||
//$list->push('1','2','3','4','5');
|
||||
//var_dump($list->all());
|
||||
//var_dump($list->clear());
|
||||
//var_dump($list->count());
|
||||
|
||||
//$set = SetRedis::getInstance();
|
||||
//$set->add('user1','user:2','user:3');
|
||||
//var_dump($set->all());
|
||||
//var_dump($set->count());
|
||||
//var_dump($set->isMember('user:3'));
|
||||
//var_dump($set->randMember(2));
|
||||
|
||||
//$zset = ZSetRedis::getInstance();
|
||||
//for ($i = 1; $i < 100; $i++) {
|
||||
// $zset->add('user:' . $i, $i);
|
||||
//}
|
||||
//$zset->delete();
|
||||
//var_dump($zset->count());
|
||||
//var_dump($zset->rank(2,10));
|
||||
//var_dump($zset->getMemberScore('user:2'));
|
||||
//var_dump($zset->getMemberRank('user:2'));
|
||||
//var_dump($zset->rank());
|
||||
//var_dump($zset->range('20','60'));
|
||||
|
||||
//$stream = StreamRedis::getInstance();
|
||||
//var_dump($stream->info());
|
||||
//for ($i = 0; $i < 10; $i++) {
|
||||
// $stream->add([
|
||||
// 'user_id' => $i,
|
||||
// 'time' => time()
|
||||
// ]);
|
||||
//}
|
||||
//
|
||||
//$stream->run(function (string $id, array $data): bool {
|
||||
// echo PHP_EOL . " 消息ID: {$id} , 任务数据: " . json_encode($data);
|
||||
//
|
||||
// return true;
|
||||
//}, 'default', 'default');
|
||||
|
||||
|
||||
//FriendRemark::getInstance()->reload();
|
||||
|
||||
//LastMessage::getInstance()->save(2, 1, 3, [
|
||||
// 'created_at' => date('Y-m-d H:i:s'),
|
||||
// 'content' => '那三级卡那那可是那那会计师哪安顺科技那发'
|
||||
//]);
|
||||
//var_dump(LastMessage::getInstance()->read(3, 6, 3));
|
||||
|
||||
//var_dump(UnreadTalk::getInstance()->read(1, 2));
|
||||
//UnreadTalk::getInstance()->save(1, 2);
|
||||
|
||||
//$talk = UnreadTalk::getInstance();
|
||||
//for ($i = 1; $i < 10; $i++) {
|
||||
// for ($j = 1; $j < 10; $j++) {
|
||||
// $talk->increment($i, $j);
|
||||
// }
|
||||
//}
|
||||
|
||||
//$model = new TalkService();
|
||||
//$model->talks(2054);
|
||||
|
||||
//var_dump(FriendRemark::getInstance()->read(2054,2055));
|
||||
|
||||
//$socketRoom = SocketRoom::getInstance();
|
||||
//$socketRoom->addRoomMember('');
|
||||
//$keys = redis()->keys('rds-set*');
|
||||
//foreach ($keys as $key) {
|
||||
// redis()->del($keys);
|
||||
//}
|
||||
|
||||
//SocketFdBindUser::getInstance()->bind(1, 2054);
|
||||
//SocketUserBindFds::getInstance()->bind(1, 2054);
|
||||
|
||||
//$model1 = SocketUserBindFds::getInstance();
|
||||
//$model2 = FriendRemark::getInstance();
|
||||
//
|
||||
//var_dump($model1 === SocketUserBindFds::getInstance());
|
||||
//var_dump($model2 === FriendRemark::getInstance());
|
||||
//
|
||||
//var_dump(SocketUserBindFds::getInstance());
|
||||
//var_dump(FriendRemark::getInstance());
|
||||
|
||||
//SocketUserBindFds::getInstance();
|
||||
//SocketUserBindFds::getInstance();
|
||||
//SocketRoom::getInstance();
|
||||
//FriendRemark::getInstance();
|
||||
//SocketUserBindFds::getInstance();
|
||||
//SocketRoom::getInstance();
|
||||
//FriendRemark::getInstance();
|
||||
|
||||
//var_dump(SocketUserBindFds::getInstance());
|
||||
//var_dump(SocketRoom::getInstance());
|
||||
//var_dump(FriendRemark::getInstance());
|
||||
//
|
||||
//var_dump('------');
|
||||
//var_dump(SocketUserBindFds::getInstance());
|
||||
//var_dump(SocketRoom::getInstance());
|
||||
//var_dump(FriendRemark::getInstance());
|
||||
|
||||
//var_dump(Group::isManager(2054,116));
|
||||
|
||||
UsersFriend::where('id', '<=', UsersFriend::max('id'))->chunk(100, function ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
UsersFriend::create([
|
||||
'user_id' => $row->friend_id,
|
||||
'friend_id' => $row->user_id,
|
||||
'remark' => $row->user2_remark,
|
||||
'status' => 1,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
User::create([
|
||||
'mobile' => '18798271181',
|
||||
'password' => Hash::make('asdfbasjhdfbasj'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,9 @@ use App\Cache\Repository\LockRedis;
|
|||
use App\Cache\UnreadTalk;
|
||||
use App\Constants\TalkMessageType;
|
||||
use App\Constants\TalkMode;
|
||||
use App\Model\Talk\TalkList;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Service\UserFriendService;
|
||||
use App\Support\UserRelation;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
|
@ -75,7 +77,7 @@ class TalkController extends CController
|
|||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function create()
|
||||
public function create(UserFriendService $service)
|
||||
{
|
||||
$params = $this->request->inputs(['talk_type', 'receiver_id']);
|
||||
$this->validate($params, [
|
||||
|
@ -100,27 +102,18 @@ class TalkController extends CController
|
|||
return $this->response->fail('创建失败!');
|
||||
}
|
||||
|
||||
$data = [
|
||||
$data = TalkList::item([
|
||||
'id' => $result['id'],
|
||||
'talk_type' => $result['talk_type'],
|
||||
'receiver_id' => $result['receiver_id'],
|
||||
'is_top' => 0,
|
||||
'is_disturb' => 0,
|
||||
'is_online' => 1,
|
||||
'avatar' => '',
|
||||
'name' => '',
|
||||
'remark_name' => '',
|
||||
'unread_num' => 0,
|
||||
'msg_text' => '',
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
]);
|
||||
|
||||
if ($result['talk_type'] == TalkMode::PRIVATE_CHAT) {
|
||||
$userInfo = User::where('id', $data['receiver_id'])->first(['nickname', 'avatar']);
|
||||
$data['avatar'] = $userInfo->avatar;
|
||||
$data['name'] = $userInfo->nickname;
|
||||
$data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id);
|
||||
$data['remark_name'] = UsersFriend::getFriendRemark($user_id, (int)$data['receiver_id']);
|
||||
$data['remark_name'] = $service->getFriendRemark($user_id, (int)$data['receiver_id']);
|
||||
} else if ($result['talk_type'] == TalkMode::GROUP_CHAT) {
|
||||
$groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']);
|
||||
$data['name'] = $groupInfo->group_name;
|
||||
|
|
|
@ -121,11 +121,11 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|||
|
||||
// 判断是否存在异地登录
|
||||
$isOnline = $this->client->isOnlineAll($user_id);
|
||||
if (!$isOnline) {
|
||||
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
|
||||
'user_id' => $user_id,
|
||||
'status' => 0,
|
||||
]));
|
||||
}
|
||||
if ($isOnline) return;
|
||||
|
||||
MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
|
||||
'user_id' => $user_id,
|
||||
'status' => 0,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ class Article extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'class_id' => 'integer',
|
||||
'is_asterisk' => 'integer',
|
||||
|
|
|
@ -38,7 +38,6 @@ class ArticleAnnex extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'article_id' => 'integer',
|
||||
'file_size' => 'integer',
|
||||
|
|
|
@ -30,7 +30,6 @@ class ArticleClass extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'is_default' => 'integer',
|
||||
|
|
|
@ -26,7 +26,6 @@ class ArticleDetail extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'article_id' => 'integer'
|
||||
];
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ class ArticleTag extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'created_at' => 'integer'
|
||||
|
|
|
@ -13,6 +13,15 @@ use Hyperf\DbConnection\Model\Model as CModel;
|
|||
*/
|
||||
abstract class BaseModel extends CModel
|
||||
{
|
||||
/**
|
||||
* 不可批量赋值的属性
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [
|
||||
'id'
|
||||
];
|
||||
|
||||
/**
|
||||
* 关闭自动维护时间字段
|
||||
*
|
||||
|
|
|
@ -19,10 +19,15 @@ class Emoticon extends BaseModel
|
|||
{
|
||||
protected $table = 'emoticon';
|
||||
|
||||
protected $fillable = [];
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'icon',
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'status' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
|
|
|
@ -34,7 +34,6 @@ class EmoticonItem extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'emoticon_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'file_size' => 'integer',
|
||||
|
|
|
@ -40,7 +40,6 @@ class FileSplitUpload extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'file_type' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'split_index' => 'integer',
|
||||
|
|
|
@ -51,4 +51,29 @@ class TalkList extends BaseModel
|
|||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public static function item(array $params): array
|
||||
{
|
||||
$item = [
|
||||
'id' => 0,
|
||||
'talk_type' => 0,
|
||||
'receiver_id' => 0,
|
||||
'is_top' => 0,
|
||||
'is_disturb' => 0,
|
||||
'is_online' => false,
|
||||
'avatar' => '',
|
||||
'name' => '',
|
||||
'remark_name' => '',
|
||||
'unread_num' => 0,
|
||||
'msg_text' => '',
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
return array_merge($item, array_intersect_key($params, $item));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,6 @@ class TalkRecords extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'talk_type' => 'integer',
|
||||
'msg_type' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
|
|
|
@ -30,7 +30,6 @@ class TalkRecordsCode extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'record_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'created_at' => 'datetime'
|
||||
|
|
|
@ -22,7 +22,6 @@ class TalkRecordsDelete extends BaseModel
|
|||
protected $fillable = [];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'record_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'created_at' => 'datetime'
|
||||
|
|
|
@ -42,7 +42,6 @@ class TalkRecordsFile extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'record_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'file_source' => 'integer',
|
||||
|
|
|
@ -30,7 +30,6 @@ class TalkRecordsForward extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'record_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
];
|
||||
|
|
|
@ -28,7 +28,6 @@ class TalkRecordsInvite extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'record_id' => 'integer',
|
||||
'type' => 'integer',
|
||||
'operate_user_id' => 'integer'
|
||||
|
|
|
@ -42,7 +42,10 @@ class User extends BaseModel implements Authenticatable
|
|||
'updated_at',
|
||||
];
|
||||
|
||||
protected $casts = [];
|
||||
protected $casts = [
|
||||
'gender' => 'integer',
|
||||
'is_robot' => 'integer',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'password'
|
||||
|
|
|
@ -22,7 +22,6 @@ class UsersEmoticon extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer'
|
||||
];
|
||||
|
||||
|
|
|
@ -32,53 +32,10 @@ class UsersFriend extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'friend_id' => 'integer',
|
||||
'status' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* 判断用户之间是否存在好友关系
|
||||
*
|
||||
* @param int $user_id 用户ID
|
||||
* @param int $friend_id 好友ID
|
||||
* @param bool $is_cache 是否允许读取缓存
|
||||
* @param bool $is_mutual 相互互为好友
|
||||
* @return bool
|
||||
*/
|
||||
public static function isFriend(int $user_id, int $friend_id, bool $is_cache = false, $is_mutual = false)
|
||||
{
|
||||
$cacheKey = "good_friends:{$user_id}_{$friend_id}";
|
||||
if ($is_cache && redis()->get($cacheKey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$isTrue = self::query()->where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
||||
if ($isTrue) {
|
||||
redis()->setex($cacheKey, 60 * 5, 1);
|
||||
}
|
||||
|
||||
return $isTrue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取好友备注
|
||||
*
|
||||
* @param int $user_id 用户ID
|
||||
* @param int $friend_id 好友ID
|
||||
* @return string
|
||||
*/
|
||||
public static function getFriendRemark(int $user_id, int $friend_id)
|
||||
{
|
||||
$remark = FriendRemark::getInstance()->read($user_id, $friend_id);
|
||||
if ($remark) return $remark;
|
||||
|
||||
$remark = UsersFriend::where('user_id', $user_id)->where('friend_id', $friend_id)->value('remark');
|
||||
if ($remark) FriendRemark::getInstance()->save($user_id, $friend_id, $remark);
|
||||
|
||||
return (string)$remark;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace App\Model;
|
|||
* @property string $created_at 创建时间
|
||||
* @package App\ModelÒ
|
||||
*/
|
||||
class UsersFriendsApply extends BaseModel
|
||||
class UsersFriendApply extends BaseModel
|
||||
{
|
||||
protected $table = 'users_friends_apply';
|
||||
|
||||
|
@ -27,7 +27,6 @@ class UsersFriendsApply extends BaseModel
|
|||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'friend_id' => 'integer',
|
||||
'created_at' => 'datetime'
|
|
@ -30,11 +30,17 @@ class ArticleService extends BaseService
|
|||
{
|
||||
$subJoin = Article::select('class_id', Db::raw('count(class_id) as count'))->where('user_id', $user_id)->where('status', 1)->groupBy('class_id');
|
||||
|
||||
$fields = [
|
||||
'article_class.id', 'article_class.class_name',
|
||||
'article_class.is_default',
|
||||
Db::raw('ifnull(sub_join.count,0) as count')
|
||||
];
|
||||
|
||||
return ArticleClass::leftJoinSub($subJoin, 'sub_join', function ($join) {
|
||||
$join->on('article_class.id', '=', Db::raw('sub_join.class_id'));
|
||||
})->where('article_class.user_id', $user_id)
|
||||
->orderBy('article_class.sort', 'asc')
|
||||
->get(['article_class.id', 'article_class.class_name', 'article_class.is_default', Db::raw('ifnull(sub_join.count,0) as count')])
|
||||
->get($fields)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ use App\Cache\FriendApply;
|
|||
use App\Constants\TalkMessageEvent;
|
||||
use App\Model\User;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Model\UsersFriendsApply;
|
||||
use App\Model\UsersFriendApply;
|
||||
use App\Support\MessageProducer;
|
||||
use App\Traits\PagingTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
@ -25,13 +25,13 @@ class ContactApplyService
|
|||
*/
|
||||
public function create(int $user_id, int $friend_id, string $remark)
|
||||
{
|
||||
$result = UsersFriendsApply::where([
|
||||
$result = UsersFriendApply::where([
|
||||
['user_id', '=', $user_id],
|
||||
['friend_id', '=', $friend_id],
|
||||
])->orderByDesc('id')->first();
|
||||
|
||||
if (!$result) {
|
||||
$result = UsersFriendsApply::create([
|
||||
$result = UsersFriendApply::create([
|
||||
'user_id' => $user_id,
|
||||
'friend_id' => $friend_id,
|
||||
'remark' => $remark,
|
||||
|
@ -66,7 +66,7 @@ class ContactApplyService
|
|||
*/
|
||||
public function accept(int $user_id, int $apply_id, string $remarks = '')
|
||||
{
|
||||
$info = UsersFriendsApply::where('id', $apply_id)->first();
|
||||
$info = UsersFriendApply::where('id', $apply_id)->first();
|
||||
if (!$info || $info->friend_id != $user_id) {
|
||||
return false;
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ class ContactApplyService
|
|||
*/
|
||||
public function decline(int $user_id, int $apply_id, string $reason = '')
|
||||
{
|
||||
$result = UsersFriendsApply::where('id', $apply_id)->where('friend_id', $user_id)->delete();
|
||||
$result = UsersFriendApply::where('id', $apply_id)->where('friend_id', $user_id)->delete();
|
||||
|
||||
if (!$result) return false;
|
||||
|
||||
|
@ -136,7 +136,7 @@ class ContactApplyService
|
|||
*/
|
||||
public function getApplyRecords(int $user_id, $page = 1, $page_size = 30): array
|
||||
{
|
||||
$rowsSqlObj = UsersFriendsApply::select([
|
||||
$rowsSqlObj = UsersFriendApply::select([
|
||||
'users_friends_apply.id',
|
||||
'users_friends_apply.remark',
|
||||
'users.nickname',
|
||||
|
|
|
@ -11,6 +11,7 @@ use App\Model\Talk\TalkRecords;
|
|||
use App\Model\Group\Group;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Service\SocketClientService;
|
||||
use App\Service\UserFriendService;
|
||||
use App\Support\MessageProducer;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\WebSocket\Frame;
|
||||
|
@ -57,9 +58,8 @@ class ReceiveHandleService
|
|||
|
||||
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系
|
||||
if ($data['talk_type'] == TalkMode::PRIVATE_CHAT) {
|
||||
if (!UsersFriend::isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true)) {
|
||||
return;
|
||||
}
|
||||
$isTrue = container()->get(UserFriendService::class)->isFriend((int)$data['sender_id'], (int)$data['receiver_id'], true);
|
||||
if (!$isTrue) return;
|
||||
} else if ($data['talk_type'] == TalkMode::GROUP_CHAT) {
|
||||
if (!Group::isMember((int)$data['receiver_id'], (int)$data['sender_id'])) {
|
||||
return;
|
||||
|
|
|
@ -13,7 +13,7 @@ use App\Model\Talk\TalkRecordsForward;
|
|||
use App\Model\Talk\TalkRecordsInvite;
|
||||
use App\Model\Group\Group;
|
||||
use App\Model\User;
|
||||
use App\Model\UsersFriendsApply;
|
||||
use App\Model\UsersFriendApply;
|
||||
use App\Service\SocketClientService;
|
||||
use App\Service\UserService;
|
||||
|
||||
|
@ -282,7 +282,7 @@ class SubscribeHandleService
|
|||
{
|
||||
$data = $data['data'];
|
||||
|
||||
$applyInfo = UsersFriendsApply::where('id', $data['apply_id'])->first();
|
||||
$applyInfo = UsersFriendApply::where('id', $data['apply_id'])->first();
|
||||
if (!$applyInfo) return true;
|
||||
|
||||
$fds = $this->clientService->findUserFds($data['type'] == 1 ? $applyInfo->friend_id : $applyInfo->user_id);
|
||||
|
|
|
@ -7,7 +7,6 @@ use App\Cache\ServerRunID;
|
|||
use App\Cache\UnreadTalk;
|
||||
use App\Constants\TalkMode;
|
||||
use App\Model\Talk\TalkList;
|
||||
use App\Model\UsersFriend;
|
||||
|
||||
class TalkListService
|
||||
{
|
||||
|
@ -126,30 +125,24 @@ class TalkListService
|
|||
|
||||
if (!$rows) return [];
|
||||
|
||||
$socketFDService = make(SocketClientService::class);
|
||||
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
|
||||
|
||||
return array_map(function ($item) use ($user_id, $socketFDService, $runIdAll) {
|
||||
$data['id'] = $item['id'];
|
||||
$data['talk_type'] = $item['talk_type'];
|
||||
$data['receiver_id'] = $item['receiver_id'];
|
||||
$data['avatar'] = ''; // 默认头像
|
||||
$data['name'] = ''; // 对方昵称/群名称
|
||||
$data['remark_name'] = ''; // 好友备注
|
||||
$data['unread_num'] = 0; // 未读消息
|
||||
$data['is_online'] = false; // 是否在线
|
||||
$data['is_top'] = $item['is_top'];
|
||||
$data['is_disturb'] = $item['is_disturb'];
|
||||
$data['msg_text'] = '......';
|
||||
$data['updated_at'] = $item['updated_at'] ?: '2020-01-01 00:00:00';
|
||||
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
|
||||
return array_map(function ($item) use ($user_id, $runIdAll) {
|
||||
$data = TalkList::item([
|
||||
'id' => $item['id'],
|
||||
'talk_type' => $item['talk_type'],
|
||||
'receiver_id' => $item['receiver_id'],
|
||||
'is_top' => $item['is_top'],
|
||||
'is_disturb' => $item['is_disturb'],
|
||||
'updated_at' => $item['updated_at'] ?: '2020-01-01 00:00:00',
|
||||
]);
|
||||
|
||||
if ($item['talk_type'] == TalkMode::PRIVATE_CHAT) {
|
||||
$data['name'] = $item['nickname'];
|
||||
$data['avatar'] = $item['user_avatar'];
|
||||
$data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id);
|
||||
$data['is_online'] = $socketFDService->isOnlineAll($item['receiver_id'], $runIdAll);
|
||||
$data['remark_name'] = UsersFriend::getFriendRemark($user_id, (int)$item['receiver_id']);
|
||||
} else {
|
||||
$data['is_online'] = container()->get(SocketClientService::class)->isOnlineAll($item['receiver_id'], $runIdAll);
|
||||
$data['remark_name'] = container()->get(UserFriendService::class)->getFriendRemark($user_id, $item['receiver_id']);
|
||||
} else if (TalkMode::GROUP_CHAT) {
|
||||
$data['name'] = strval($item['group_name']);
|
||||
$data['avatar'] = $item['group_avatar'];
|
||||
}
|
||||
|
|
|
@ -425,7 +425,7 @@ class TalkService extends BaseService
|
|||
$sqlObj = TalkRecords::whereIn('id', $records_ids);
|
||||
|
||||
if ($talk_type == TalkMode::PRIVATE_CHAT) {
|
||||
if (!UsersFriend::isFriend($user_id, $receiver_id)) return [];
|
||||
if (!container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id)) return [];
|
||||
|
||||
$sqlObj = $sqlObj->where(function ($query) use ($user_id, $receiver_id) {
|
||||
$query->where([
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Cache\FriendRemark;
|
||||
use App\Model\UsersFriend;
|
||||
|
||||
class UserFriendService
|
||||
{
|
||||
/**
|
||||
* 获取好友备注
|
||||
*
|
||||
* @param int $user_id 用户ID
|
||||
* @param int $friend_id 好友ID
|
||||
* @return string
|
||||
*/
|
||||
public function getFriendRemark(int $user_id, int $friend_id)
|
||||
{
|
||||
$remark = FriendRemark::getInstance()->read($user_id, $friend_id);
|
||||
if ($remark) return $remark;
|
||||
|
||||
$remark = UsersFriend::where('user_id', $user_id)->where('friend_id', $friend_id)->value('remark');
|
||||
if ($remark) FriendRemark::getInstance()->save($user_id, $friend_id, $remark);
|
||||
|
||||
return (string)$remark;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户之间是否存在好友关系
|
||||
*
|
||||
* @param int $user_id 用户ID
|
||||
* @param int $friend_id 好友ID
|
||||
* @param bool $is_cache 是否允许读取缓存
|
||||
* @param bool $is_mutual 相互互为好友
|
||||
* @return bool
|
||||
*/
|
||||
public function isFriend(int $user_id, int $friend_id, bool $is_cache = false, $is_mutual = false)
|
||||
{
|
||||
$cacheKey = "good_friends:{$user_id}_{$friend_id}";
|
||||
if ($is_cache && redis()->get($cacheKey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$isTrue = UsersFriend::query()->where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
||||
if ($isTrue) {
|
||||
redis()->setex($cacheKey, 60 * 5, 1);
|
||||
}
|
||||
|
||||
return $isTrue;
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ use App\Helper\Hash;
|
|||
use App\Model\User;
|
||||
use App\Model\Article\ArticleClass;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Model\UsersFriendsApply;
|
||||
use App\Model\UsersFriendApply;
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
||||
class UserService extends BaseService
|
||||
|
@ -125,13 +125,13 @@ class UserService extends BaseService
|
|||
|
||||
// 判断查询信息是否是自己
|
||||
if ($friend_id != $me_user_id) {
|
||||
$is_friend = UsersFriend::isFriend($me_user_id, $friend_id, true, true);
|
||||
$is_friend = container()->get(UserFriendService::class)->isFriend($me_user_id, $friend_id, true, true);
|
||||
|
||||
$info['friend_status'] = $is_friend ? 2 : 1;
|
||||
if ($is_friend) {
|
||||
$info['nickname_remark'] = UsersFriend::getFriendRemark($me_user_id, $friend_id);
|
||||
$info['nickname_remark'] = container()->get(UserFriendService::class)->getFriendRemark($me_user_id, $friend_id);
|
||||
} else {
|
||||
$res = UsersFriendsApply::where('user_id', $me_user_id)
|
||||
$res = UsersFriendApply::where('user_id', $me_user_id)
|
||||
->where('friend_id', $friend_id)
|
||||
->orderBy('id', 'desc')
|
||||
->exists();
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace App\Support;
|
|||
|
||||
use App\Constants\TalkMode;
|
||||
use App\Model\Group\Group;
|
||||
use App\Model\UsersFriend;
|
||||
use App\Service\UserFriendService;
|
||||
|
||||
class UserRelation
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ class UserRelation
|
|||
public static function isFriendOrGroupMember(int $user_id, int $receiver_id, int $talk_type)
|
||||
{
|
||||
if ($talk_type == TalkMode::PRIVATE_CHAT) {
|
||||
return UsersFriend::isFriend($user_id, $receiver_id, true);
|
||||
return container()->get(UserFriendService::class)->isFriend($user_id, $receiver_id, true);
|
||||
} else if ($talk_type == TalkMode::GROUP_CHAT) {
|
||||
return Group::isMember($receiver_id, $user_id);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ use Hyperf\Redis\Redis;
|
|||
*
|
||||
* @return \Psr\Container\ContainerInterface
|
||||
*/
|
||||
function container()
|
||||
function container(string $service = '')
|
||||
{
|
||||
return ApplicationContext::getContainer();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue