2021-07-12 22:00:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use App\Cache\FriendRemark;
|
2022-01-17 21:27:27 +08:00
|
|
|
use App\Model\Contact\Contact;
|
2021-07-12 22:00:48 +08:00
|
|
|
|
|
|
|
class UserFriendService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 获取好友备注
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $friend_id 好友ID
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
public function getFriendRemark(int $user_id, int $friend_id): string
|
2021-07-12 22:00:48 +08:00
|
|
|
{
|
|
|
|
$remark = FriendRemark::getInstance()->read($user_id, $friend_id);
|
|
|
|
if ($remark) return $remark;
|
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
$remark = Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->value('remark');
|
2021-07-12 22:00:48 +08:00
|
|
|
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_mutual 相互互为好友
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-11 12:41:28 +08:00
|
|
|
public function isFriend(int $user_id, int $friend_id, $is_mutual = false): bool
|
2021-07-12 22:00:48 +08:00
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$isTrue1 = Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
2021-07-23 22:48:25 +08:00
|
|
|
|
2021-07-13 23:34:43 +08:00
|
|
|
if ($is_mutual === false) {
|
|
|
|
return $isTrue1;
|
2021-07-12 22:00:48 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
$isTrue2 = Contact::where('user_id', $friend_id)->where('friend_id', $user_id)->where('status', 1)->exists();
|
2021-07-12 22:00:48 +08:00
|
|
|
|
2021-07-13 23:34:43 +08:00
|
|
|
return $isTrue1 && $isTrue2;
|
2021-07-12 22:00:48 +08:00
|
|
|
}
|
|
|
|
}
|