'integer', 'user1' => 'integer', 'user2' => 'integer', 'active' => 'integer', 'status' => 'integer', 'created_at' => 'datetime' ]; /** * 获取用户所有好友 * * @param int $uid 用户ID * @return mixed */ public static function getUserFriends(int $uid) { $prefix = config('databases.default.prefix'); $sql = << $user_id2) { [$user_id1, $user_id2] = [$user_id2, $user_id1]; } $cacheKey = "good_friends:{$user_id1}_$user_id2"; if ($cache && redis()->get($cacheKey)) { return true; } $isTrue = self::query()->where('user1', $user_id1)->where('user2', $user_id2)->where('status', 1)->exists(); if ($isTrue) { redis()->setex($cacheKey, 60 * 5, 1); } return $isTrue; } /** * 获取指定用户的所有朋友的用户ID * * @param int $user_id 指定用户ID * @return array */ public static function getFriendIds(int $user_id) { $prefix = config('databases.default.prefix'); $sql = "SELECT user2 as uid from {$prefix}users_friends where user1 = {$user_id} and `status` = 1 UNION all SELECT user1 as uid from {$prefix}users_friends where user2 = {$user_id} and `status` = 1"; return array_map(function ($item) { return $item->uid; }, Db::select($sql)); } }