2020-11-04 17:36:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
2020-11-04 17:36:52 +08:00
|
|
|
namespace App\Model;
|
|
|
|
|
|
|
|
/**
|
2020-11-14 18:10:28 +08:00
|
|
|
* 表情包收藏数据表模型
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @property int $id
|
2021-07-06 23:32:14 +08:00
|
|
|
* @property int $user_id 用户ID
|
|
|
|
* @property int $friend_id 好友ID
|
|
|
|
* @property string $remark 好友备注
|
|
|
|
* @property int $status 好友状态
|
|
|
|
* @property string $created_at 创建时间
|
|
|
|
* @property string $updated_at 更新时间
|
2020-11-14 18:10:28 +08:00
|
|
|
* @package App\Model
|
2020-11-04 17:36:52 +08:00
|
|
|
*/
|
2020-11-05 17:40:51 +08:00
|
|
|
class UsersFriend extends BaseModel
|
2020-11-04 17:36:52 +08:00
|
|
|
{
|
|
|
|
protected $table = 'users_friends';
|
|
|
|
|
2020-11-27 19:48:41 +08:00
|
|
|
protected $fillable = [
|
2021-07-06 23:32:14 +08:00
|
|
|
'user_id',
|
|
|
|
'friend_id',
|
2020-11-27 19:48:41 +08:00
|
|
|
'status',
|
2021-07-06 23:32:14 +08:00
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
2020-11-27 19:48:41 +08:00
|
|
|
];
|
2020-11-04 17:36:52 +08:00
|
|
|
|
2020-11-14 18:10:28 +08:00
|
|
|
protected $casts = [
|
2021-04-20 16:30:57 +08:00
|
|
|
'id' => 'integer',
|
2021-07-06 23:32:14 +08:00
|
|
|
'user_id' => 'integer',
|
|
|
|
'friend_id' => 'integer',
|
2021-04-20 16:30:57 +08:00
|
|
|
'status' => 'integer',
|
2021-07-06 23:32:14 +08:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
'updated_at' => 'datetime',
|
2020-11-14 18:10:28 +08:00
|
|
|
];
|
2020-11-05 17:40:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户所有好友
|
|
|
|
*
|
2021-07-06 23:32:14 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @return array
|
2020-11-05 17:40:51 +08:00
|
|
|
*/
|
2021-07-06 23:32:14 +08:00
|
|
|
public static function getUserFriends(int $user_id)
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-07-06 23:32:14 +08:00
|
|
|
return UsersFriend::leftJoin('users', 'users.id', '=', 'users_friends.friend_id')
|
|
|
|
->where('user_id', $user_id)->where('users_friends.status', 1)
|
|
|
|
->get([
|
|
|
|
'users.id',
|
|
|
|
'users.nickname',
|
|
|
|
'users.avatar',
|
|
|
|
'users.motto',
|
|
|
|
'users.gender',
|
|
|
|
'users_friends.remark as friend_remark',
|
|
|
|
])->toArray();
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断用户之间是否存在好友关系
|
|
|
|
*
|
2021-07-06 23:32:14 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $friend_id 好友ID
|
|
|
|
* @param bool $is_cache 是否允许读取缓存
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-06 23:32:14 +08:00
|
|
|
public static function isFriend(int $user_id, int $friend_id, bool $is_cache = false)
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-07-06 23:32:14 +08:00
|
|
|
$cacheKey = "good_friends:{$user_id}_{$friend_id}";
|
|
|
|
if ($is_cache && redis()->get($cacheKey)) {
|
2020-12-01 22:50:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-06 23:32:14 +08:00
|
|
|
$isTrue = self::query()->where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->exists();
|
2020-12-01 22:50:43 +08:00
|
|
|
if ($isTrue) {
|
|
|
|
redis()->setex($cacheKey, 60 * 5, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $isTrue;
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取指定用户的所有朋友的用户ID
|
|
|
|
*
|
|
|
|
* @param int $user_id 指定用户ID
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getFriendIds(int $user_id)
|
|
|
|
{
|
2021-07-06 23:32:14 +08:00
|
|
|
return UsersFriend::where('user_id', $user_id)->where('status', 1)->pluck('friend_id')->toArray();
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
2020-11-04 17:36:52 +08:00
|
|
|
}
|