where('uid', $uid)->get(); $users = UserModel::whereIn('uid', $voinces->pluck('get_uid')->toArray())->get([ 'uid', 'headimgurl', 'nickname', 'sex', 'like', 'voice', 'voice_state', 'contact_type', 'contact' ]); foreach ($users as &$user) { $voince = $voinces->where('get_uid', $user->uid)->pop()->toArray(); $user->get_voice_id = $voince['id']; $user->lock = $voince['lock'] ?? 0; if ($user->lock < 3) { unset($user->contact); } } return $users; } /** * 给某人推荐一条语音 * @param int $uid * @return int 语音用户id * @throws \ApiException */ public function pop(int $uid) { $getVoices = GetVoiceModel::where('uid', $uid)->get(); $voices = $getVoices->pluck('get_uid')->toArray(); array_push($voices, $uid); $user = UserModel::findOrFail($uid); if ($user->like == 0) { $likes = [0, 1, 2]; } else { $likes = [$user->like]; } $count = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn( 'sex', $likes )->where('voice_state', 1)->count(); $rand = rand(0, $count - 1); $voice = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn( 'sex', $likes )->where('voice_state', 1)->skip($rand)->first(); if (collect($voice)->isEmpty()) { throw new \ApiException("语音库空空如也", 401); } return $voice->uid; } }