$areaType, 'scoreType' => $scoreType, 'page' => $page]; $queryTop = 20; // 每页查询数量 // 层数榜 if ($scoreType === 0) { if ($page > 0) { $where = ['weekScore' => ['$gt' => 0]]; // 大于0的数据 $options = [ 'projection' => ['_id' => 0, 'weekScore' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1], 'sort' => ['weekScore' => -1], // 倒序 'limit' => $queryTop, 'skip' => ($page - 1) * $queryTop, ]; $dbRet = $dbOpt->query($where, $options); $rankData["listData"] = $dbRet; } // 查自己 else if ($openid) { $where = ['weekScore' => ['$gt' => 0]]; // 大于0的数据 $options = [ 'projection' => ['_id' => 0, 'weekScore' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1], 'sort' => ['weekScore' => -1], // 倒序 ]; $dbRet = $dbOpt->query($where, $options); if ($dbRet) { $rankData["listData"] = getSelfArroudData($openid, $dbRet); } } } // 关卡榜 else if ($scoreType === 1) { if ($page > 0) { $where = ['missionNum' => ['$gt' => 0]]; // 大于0的数据 $options = [ 'projection' => ['_id' => 0, 'missionNum' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1], 'sort' => ['missionNum' => -1], // 倒序 'limit' => $queryTop, 'skip' => ($page - 1) * $queryTop, ]; $dbRet = $dbOpt->query($where, $options); $rankData["listData"] = $dbRet; } // 查自己 else if ($openid) { $where = ['missionNum' => ['$gt' => 0]]; // 大于0的数据 $options = [ 'projection' => ['_id' => 0, 'missionNum' => 1, 'openid' => 1, 'avatarUrl' => 1, 'nickName' => 1], 'sort' => ['missionNum' => -1], // 倒序 ]; $dbRet = $dbOpt->query($where, $options); if ($dbRet) { $rankData["listData"] = getSelfArroudData($openid, $dbRet); } } } echo json_encode($rankData); // 获取玩家排行上下x名玩家数据,这里暂用遍历的,实际不能用这样处理 function getSelfArroudData($openid, $dbRet, $getCount = 2) { $playerRank = 0; foreach ($dbRet as $i => $d) { $dbRet[$i]['rank'] = $i + 1; // 赋值排名 if ($d['openid'] == $openid) { $playerRank = $dbRet[$i]['rank']; } if ($playerRank > 0 and $d['rank'] >= ($playerRank + $getCount)) { break; } } if ($playerRank <= 0) { return []; } $start = max(0, $playerRank - $getCount - 1); return array_slice($dbRet, $start, $getCount * 2 + 1); }