diff --git a/backendcloud/backend/src/main/java/com/kob/backend/controller/ranklist/GetRankListController.java b/backendcloud/backend/src/main/java/com/kob/backend/controller/ranklist/GetRankListController.java new file mode 100644 index 0000000..e9f63df --- /dev/null +++ b/backendcloud/backend/src/main/java/com/kob/backend/controller/ranklist/GetRankListController.java @@ -0,0 +1,23 @@ +package com.kob.backend.controller.ranklist; + +import com.alibaba.fastjson2.JSONObject; +import com.kob.backend.service.ranklist.GetRankListService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +@RestController +public class GetRankListController { + @Autowired + private GetRankListService getRankListService; + + @GetMapping("/ranklist/getranklist/") + JSONObject getRankList(@RequestParam Map data) { + Integer page = Integer.parseInt(data.get("page_index")); + Integer.parseInt(data.get("page_index")); + return getRankListService.getRankList(page); + } +} diff --git a/backendcloud/backend/src/main/java/com/kob/backend/service/impl/ranklist/GetRankListServiceImpl.java b/backendcloud/backend/src/main/java/com/kob/backend/service/impl/ranklist/GetRankListServiceImpl.java new file mode 100644 index 0000000..b09c2b4 --- /dev/null +++ b/backendcloud/backend/src/main/java/com/kob/backend/service/impl/ranklist/GetRankListServiceImpl.java @@ -0,0 +1,47 @@ +package com.kob.backend.service.impl.ranklist; + +import com.alibaba.fastjson2.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.kob.backend.mapper.UserMapper; +import com.kob.backend.pojo.Record; +import com.kob.backend.pojo.User; +import com.kob.backend.service.ranklist.GetRankListService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.LinkedList; +import java.util.List; + +@Service +public class GetRankListServiceImpl implements GetRankListService { + @Autowired + private UserMapper userMapper; + + @Override + public JSONObject getRankList(Integer page) { + // 使用 Mybatis API : IPage 实现 + IPage rankListIPage = new Page<>(page, 10); // 参数列表:Page<>(传第几页, 每一页有多少项目) + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.orderByDesc("rating"); // 通过 User 的 rating (积分从高到低)降序排序 + List users = userMapper.selectPage(rankListIPage, queryWrapper).getRecords(); + + JSONObject resp = new JSONObject(); + // 对数据进行预处理,发送数据包之前,先过滤掉用户的密码信息 + List processed_users = new LinkedList<>(); + for (User user : users) { // 枚举用户排行 + // 将枚举到的玩家的头像,用户名和积分进行绑定 + JSONObject processed_user = new JSONObject(); + processed_user.put("id", user.getId()); + processed_user.put("photo", user.getPhoto()); + processed_user.put("username", user.getUsername()); + processed_user.put("rating", user.getRating()); + // 将 jsonObject 对象 processed_user,添加到 processed_users 列表中 + processed_users.add(processed_user); + } + resp.put("users", processed_users); // 将处理后的用户信息重新打包起来 + resp.put("user_count", userMapper.selectCount(null)); // 存入当前记录页面的总数, null 表示全部存入 + return resp; + } +} diff --git a/backendcloud/backend/src/main/java/com/kob/backend/service/ranklist/GetRankListService.java b/backendcloud/backend/src/main/java/com/kob/backend/service/ranklist/GetRankListService.java new file mode 100644 index 0000000..e71e1d7 --- /dev/null +++ b/backendcloud/backend/src/main/java/com/kob/backend/service/ranklist/GetRankListService.java @@ -0,0 +1,8 @@ +// 排行榜接口 +package com.kob.backend.service.ranklist; + +import com.alibaba.fastjson2.JSONObject; + +public interface GetRankListService { + JSONObject getRankList(Integer page); +} diff --git a/web/src/views/ranklist/RanklistIndexView.vue b/web/src/views/ranklist/RanklistIndexView.vue index 47a3f93..b3ad365 100644 --- a/web/src/views/ranklist/RanklistIndexView.vue +++ b/web/src/views/ranklist/RanklistIndexView.vue @@ -1,15 +1,133 @@ - +