更新用户进度

This commit is contained in:
石皮幼鸟 2024-04-14 14:52:34 +08:00
parent 568a2f7d36
commit 432d0581b7
1 changed files with 18 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.service.LevelService;
import com.example.catchTheLetters.utils.JwtUtil;
import com.example.catchTheLetters.utils.R;
@ -27,6 +28,9 @@ public class LevelServiceImpl implements LevelService {
@Resource
private MongoTemplate mongoTemplate;
@Resource
private AuthService authService;
@Override
public R<Level> levelDetail(String id) {
Level level = mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), Level.class);
@ -46,16 +50,23 @@ public class LevelServiceImpl implements LevelService {
@Override
public R<ScoreInfo> settle(ScoreInfo scoreInfo, String token) {
var map = JwtUtil.getPayload(token);
String id = null;
if (map != null) {
id = map.get("id").asString().replaceAll("\"", "");
}
if (id == null) {
var user = authService.verify(token);
if (user == null) {
return R.fail("token解析失败");
}
if (id.equals(scoreInfo.getUserId())) {
if (user.getId().equals(scoreInfo.getUserId())) {
// 比对这一关是否是用户的最新进度如果是就更新用户的进度
var levelRes = levelDetail(scoreInfo.getLevelId());
if (levelRes.getCode() == R.FAIL) {
return R.fail("查询关卡失败,请重试");
}
if (levelRes.getData().getOrder() > user.getProgress()) {
user.setProgress(levelRes.getData().getOrder());
authService.update(user, token);
}
ScoreInfo insert = mongoTemplate.insert(scoreInfo);
return R.ok(insert);
}
return R.fail("添加失败,请重试");