From 6dc7f20da7388fc44c45c02ca861db79f373287c Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Mon, 12 Jul 2021 22:00:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Command/TestCommand.php | 164 +----------------- app/Controller/Api/V1/TalkController.php | 19 +- app/Controller/WebSocketController.php | 12 +- app/Model/Article/Article.php | 1 - app/Model/Article/ArticleAnnex.php | 1 - app/Model/Article/ArticleClass.php | 1 - app/Model/Article/ArticleDetail.php | 1 - app/Model/Article/ArticleTag.php | 1 - app/Model/BaseModel.php | 9 + app/Model/Emoticon.php | 9 +- app/Model/EmoticonItem.php | 1 - app/Model/FileSplitUpload.php | 1 - app/Model/Talk/TalkList.php | 25 +++ app/Model/Talk/TalkRecords.php | 1 - app/Model/Talk/TalkRecordsCode.php | 1 - app/Model/Talk/TalkRecordsDelete.php | 1 - app/Model/Talk/TalkRecordsFile.php | 1 - app/Model/Talk/TalkRecordsForward.php | 1 - app/Model/Talk/TalkRecordsInvite.php | 1 - app/Model/User.php | 5 +- app/Model/UsersEmoticon.php | 1 - app/Model/UsersFriend.php | 43 ----- ...sFriendsApply.php => UsersFriendApply.php} | 3 +- app/Service/ArticleService.php | 8 +- app/Service/ContactApplyService.php | 12 +- app/Service/Message/ReceiveHandleService.php | 6 +- .../Message/SubscribeHandleService.php | 4 +- app/Service/TalkListService.php | 33 ++-- app/Service/TalkService.php | 2 +- app/Service/UserFriendService.php | 51 ++++++ app/Service/UserService.php | 8 +- app/Support/UserRelation.php | 4 +- app/helper.php | 2 +- 33 files changed, 154 insertions(+), 279 deletions(-) rename app/Model/{UsersFriendsApply.php => UsersFriendApply.php} (89%) create mode 100644 app/Service/UserFriendService.php diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 0e5c8d2..a3df730 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -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'), + ]); } } diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 5b984ee..d2da1b0 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -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; diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index dd4dddc..7d5daea 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -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, + ])); } } diff --git a/app/Model/Article/Article.php b/app/Model/Article/Article.php index 6007d0c..0bfd364 100644 --- a/app/Model/Article/Article.php +++ b/app/Model/Article/Article.php @@ -40,7 +40,6 @@ class Article extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'user_id' => 'integer', 'class_id' => 'integer', 'is_asterisk' => 'integer', diff --git a/app/Model/Article/ArticleAnnex.php b/app/Model/Article/ArticleAnnex.php index b0ffa41..6699977 100644 --- a/app/Model/Article/ArticleAnnex.php +++ b/app/Model/Article/ArticleAnnex.php @@ -38,7 +38,6 @@ class ArticleAnnex extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'user_id' => 'integer', 'article_id' => 'integer', 'file_size' => 'integer', diff --git a/app/Model/Article/ArticleClass.php b/app/Model/Article/ArticleClass.php index 9ea0e88..6842b7f 100644 --- a/app/Model/Article/ArticleClass.php +++ b/app/Model/Article/ArticleClass.php @@ -30,7 +30,6 @@ class ArticleClass extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'user_id' => 'integer', 'sort' => 'integer', 'is_default' => 'integer', diff --git a/app/Model/Article/ArticleDetail.php b/app/Model/Article/ArticleDetail.php index 30bab71..d34714e 100644 --- a/app/Model/Article/ArticleDetail.php +++ b/app/Model/Article/ArticleDetail.php @@ -26,7 +26,6 @@ class ArticleDetail extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'article_id' => 'integer' ]; } diff --git a/app/Model/Article/ArticleTag.php b/app/Model/Article/ArticleTag.php index 0b56ad7..feafb53 100644 --- a/app/Model/Article/ArticleTag.php +++ b/app/Model/Article/ArticleTag.php @@ -27,7 +27,6 @@ class ArticleTag extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'user_id' => 'integer', 'sort' => 'integer', 'created_at' => 'integer' diff --git a/app/Model/BaseModel.php b/app/Model/BaseModel.php index 1280e18..d3d854c 100644 --- a/app/Model/BaseModel.php +++ b/app/Model/BaseModel.php @@ -13,6 +13,15 @@ use Hyperf\DbConnection\Model\Model as CModel; */ abstract class BaseModel extends CModel { + /** + * 不可批量赋值的属性 + * + * @var array + */ + protected $guarded = [ + 'id' + ]; + /** * 关闭自动维护时间字段 * diff --git a/app/Model/Emoticon.php b/app/Model/Emoticon.php index 9bdacd2..6458ad0 100644 --- a/app/Model/Emoticon.php +++ b/app/Model/Emoticon.php @@ -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', diff --git a/app/Model/EmoticonItem.php b/app/Model/EmoticonItem.php index c0035d8..8d4accf 100644 --- a/app/Model/EmoticonItem.php +++ b/app/Model/EmoticonItem.php @@ -34,7 +34,6 @@ class EmoticonItem extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'emoticon_id' => 'integer', 'user_id' => 'integer', 'file_size' => 'integer', diff --git a/app/Model/FileSplitUpload.php b/app/Model/FileSplitUpload.php index 66bd92f..16f2838 100644 --- a/app/Model/FileSplitUpload.php +++ b/app/Model/FileSplitUpload.php @@ -40,7 +40,6 @@ class FileSplitUpload extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'file_type' => 'integer', 'user_id' => 'integer', 'split_index' => 'integer', diff --git a/app/Model/Talk/TalkList.php b/app/Model/Talk/TalkList.php index 942f9d8..50e2426 100644 --- a/app/Model/Talk/TalkList.php +++ b/app/Model/Talk/TalkList.php @@ -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)); + } } diff --git a/app/Model/Talk/TalkRecords.php b/app/Model/Talk/TalkRecords.php index 96da3c0..befcce7 100644 --- a/app/Model/Talk/TalkRecords.php +++ b/app/Model/Talk/TalkRecords.php @@ -44,7 +44,6 @@ class TalkRecords extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'talk_type' => 'integer', 'msg_type' => 'integer', 'user_id' => 'integer', diff --git a/app/Model/Talk/TalkRecordsCode.php b/app/Model/Talk/TalkRecordsCode.php index e0b482c..d3fe8b0 100644 --- a/app/Model/Talk/TalkRecordsCode.php +++ b/app/Model/Talk/TalkRecordsCode.php @@ -30,7 +30,6 @@ class TalkRecordsCode extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime' diff --git a/app/Model/Talk/TalkRecordsDelete.php b/app/Model/Talk/TalkRecordsDelete.php index 9fd76d7..92d0cbb 100644 --- a/app/Model/Talk/TalkRecordsDelete.php +++ b/app/Model/Talk/TalkRecordsDelete.php @@ -22,7 +22,6 @@ class TalkRecordsDelete extends BaseModel protected $fillable = []; protected $casts = [ - 'id' => 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime' diff --git a/app/Model/Talk/TalkRecordsFile.php b/app/Model/Talk/TalkRecordsFile.php index c21f7ce..d6ce715 100644 --- a/app/Model/Talk/TalkRecordsFile.php +++ b/app/Model/Talk/TalkRecordsFile.php @@ -42,7 +42,6 @@ class TalkRecordsFile extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'record_id' => 'integer', 'user_id' => 'integer', 'file_source' => 'integer', diff --git a/app/Model/Talk/TalkRecordsForward.php b/app/Model/Talk/TalkRecordsForward.php index 794e991..abbf5ab 100644 --- a/app/Model/Talk/TalkRecordsForward.php +++ b/app/Model/Talk/TalkRecordsForward.php @@ -30,7 +30,6 @@ class TalkRecordsForward extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'record_id' => 'integer', 'user_id' => 'integer', ]; diff --git a/app/Model/Talk/TalkRecordsInvite.php b/app/Model/Talk/TalkRecordsInvite.php index e3293d8..1eca1b3 100644 --- a/app/Model/Talk/TalkRecordsInvite.php +++ b/app/Model/Talk/TalkRecordsInvite.php @@ -28,7 +28,6 @@ class TalkRecordsInvite extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'record_id' => 'integer', 'type' => 'integer', 'operate_user_id' => 'integer' diff --git a/app/Model/User.php b/app/Model/User.php index 75b6c13..9a1f4d6 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -42,7 +42,10 @@ class User extends BaseModel implements Authenticatable 'updated_at', ]; - protected $casts = []; + protected $casts = [ + 'gender' => 'integer', + 'is_robot' => 'integer', + ]; protected $hidden = [ 'password' diff --git a/app/Model/UsersEmoticon.php b/app/Model/UsersEmoticon.php index e1ab64e..b12559b 100644 --- a/app/Model/UsersEmoticon.php +++ b/app/Model/UsersEmoticon.php @@ -22,7 +22,6 @@ class UsersEmoticon extends BaseModel ]; protected $casts = [ - 'id' => 'integer', 'user_id' => 'integer' ]; diff --git a/app/Model/UsersFriend.php b/app/Model/UsersFriend.php index 8921fe9..8ce67e0 100644 --- a/app/Model/UsersFriend.php +++ b/app/Model/UsersFriend.php @@ -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; - } } diff --git a/app/Model/UsersFriendsApply.php b/app/Model/UsersFriendApply.php similarity index 89% rename from app/Model/UsersFriendsApply.php rename to app/Model/UsersFriendApply.php index 2c85b3d..866409e 100644 --- a/app/Model/UsersFriendsApply.php +++ b/app/Model/UsersFriendApply.php @@ -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' diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index 1259d72..9d1c33f 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -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(); } diff --git a/app/Service/ContactApplyService.php b/app/Service/ContactApplyService.php index cd96a82..56fe7ce 100644 --- a/app/Service/ContactApplyService.php +++ b/app/Service/ContactApplyService.php @@ -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', diff --git a/app/Service/Message/ReceiveHandleService.php b/app/Service/Message/ReceiveHandleService.php index 8f0018a..9fd78b8 100644 --- a/app/Service/Message/ReceiveHandleService.php +++ b/app/Service/Message/ReceiveHandleService.php @@ -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; diff --git a/app/Service/Message/SubscribeHandleService.php b/app/Service/Message/SubscribeHandleService.php index e33957a..2e4cd24 100644 --- a/app/Service/Message/SubscribeHandleService.php +++ b/app/Service/Message/SubscribeHandleService.php @@ -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); diff --git a/app/Service/TalkListService.php b/app/Service/TalkListService.php index 4b1b20d..d761224 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkListService.php @@ -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']; } diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index cb23160..54c1b3a 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -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([ diff --git a/app/Service/UserFriendService.php b/app/Service/UserFriendService.php new file mode 100644 index 0000000..7b42278 --- /dev/null +++ b/app/Service/UserFriendService.php @@ -0,0 +1,51 @@ +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; + } +} diff --git a/app/Service/UserService.php b/app/Service/UserService.php index d450f38..7b070ea 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -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(); diff --git a/app/Support/UserRelation.php b/app/Support/UserRelation.php index 799a594..be5f2b0 100644 --- a/app/Support/UserRelation.php +++ b/app/Support/UserRelation.php @@ -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); } diff --git a/app/helper.php b/app/helper.php index 605dc6b..edbc5d5 100644 --- a/app/helper.php +++ b/app/helper.php @@ -21,7 +21,7 @@ use Hyperf\Redis\Redis; * * @return \Psr\Container\ContainerInterface */ -function container() +function container(string $service = '') { return ApplicationContext::getContainer(); }