feat: 游戏房间
This commit is contained in:
@@ -40,7 +40,7 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
|||||||
// 允许指定域访问跨域资源
|
// 允许指定域访问跨域资源
|
||||||
.setHeader("Access-Control-Allow-Origin", "*")// 允许指定域访问跨域资源
|
.setHeader("Access-Control-Allow-Origin", "*")// 允许指定域访问跨域资源
|
||||||
// 允许所有请求方式
|
// 允许所有请求方式
|
||||||
.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
|
.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, PATCH")
|
||||||
// 有效时间
|
// 有效时间
|
||||||
.setHeader("Access-Control-Max-Age", "3600")
|
.setHeader("Access-Control-Max-Age", "3600")
|
||||||
// 允许的header参数
|
// 允许的header参数
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.webgame.webgamebackend.common.dto.user;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新在线状态请求
|
||||||
|
*/
|
||||||
|
public record UpdateStatusRequest(
|
||||||
|
@NotBlank(message = "状态不能为空")
|
||||||
|
@Pattern(
|
||||||
|
regexp = "ONLINE|AWAY|DND|INVISIBLE",
|
||||||
|
message = "状态值无效,可选值:ONLINE、AWAY、DND、INVISIBLE"
|
||||||
|
)
|
||||||
|
String status
|
||||||
|
) {}
|
||||||
@@ -8,24 +8,28 @@ import java.time.LocalDate;
|
|||||||
* 用户信息响应
|
* 用户信息响应
|
||||||
*/
|
*/
|
||||||
public record UserInfoResponse(
|
public record UserInfoResponse(
|
||||||
|
String userId,
|
||||||
String nickName,
|
String nickName,
|
||||||
String avatar,
|
String avatar,
|
||||||
String signature,
|
String signature,
|
||||||
Integer age,
|
Integer age,
|
||||||
Integer sex,
|
Integer sex,
|
||||||
LocalDate birthday
|
LocalDate birthday,
|
||||||
|
String status
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* 从实体转换
|
* 从实体转换
|
||||||
*/
|
*/
|
||||||
public static UserInfoResponse from(UserEntity user) {
|
public static UserInfoResponse from(UserEntity user) {
|
||||||
return new UserInfoResponse(
|
return new UserInfoResponse(
|
||||||
|
user.getId(),
|
||||||
user.getNickName(),
|
user.getNickName(),
|
||||||
user.getAvatar(),
|
user.getAvatar(),
|
||||||
user.getSignature(),
|
user.getSignature(),
|
||||||
user.getAge(),
|
user.getAge(),
|
||||||
user.getSex(),
|
user.getSex(),
|
||||||
user.getBirthday()
|
user.getBirthday(),
|
||||||
|
user.getStatus()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.webgame.webgamebackend.controller;
|
|||||||
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.webgame.webgamebackend.common.dto.ApiResponse;
|
import com.webgame.webgamebackend.common.dto.ApiResponse;
|
||||||
|
import com.webgame.webgamebackend.common.dto.user.UpdateStatusRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
||||||
import com.webgame.webgamebackend.service.UserService;
|
import com.webgame.webgamebackend.service.UserService;
|
||||||
@@ -41,4 +42,16 @@ public class UserController {
|
|||||||
UserInfoResponse result = userService.updateUserInfo(accountId, request);
|
UserInfoResponse result = userService.updateUserInfo(accountId, request);
|
||||||
return ApiResponse.ok(result);
|
return ApiResponse.ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新当前登录用户在线状态
|
||||||
|
*
|
||||||
|
* 入参 status 可选值:ONLINE、AWAY、DND、INVISIBLE
|
||||||
|
*/
|
||||||
|
@PatchMapping("/status")
|
||||||
|
public ApiResponse<Void> updateStatus(@Valid @RequestBody UpdateStatusRequest request) {
|
||||||
|
String accountId = StpUtil.getLoginIdAsString();
|
||||||
|
userService.updateStatus(accountId, request);
|
||||||
|
return ApiResponse.ok(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,4 +74,10 @@ public class UserEntity extends UUIDBaseEntity {
|
|||||||
*/
|
*/
|
||||||
@Column(name = "balance", nullable = false)
|
@Column(name = "balance", nullable = false)
|
||||||
private Long balance = 1000L;
|
private Long balance = 1000L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在线状态:ONLINE / AWAY / DND / INVISIBLE,默认 ONLINE
|
||||||
|
*/
|
||||||
|
@Column(name = "status", length = 16, nullable = false)
|
||||||
|
private String status = "ONLINE";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.webgame.webgamebackend.service;
|
package com.webgame.webgamebackend.service;
|
||||||
|
|
||||||
|
import com.webgame.webgamebackend.common.dto.user.UpdateStatusRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
||||||
|
|
||||||
@@ -23,4 +24,12 @@ public interface UserService {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
UserInfoResponse updateUserInfo(String accountId, UpdateUserInfoRequest request);
|
UserInfoResponse updateUserInfo(String accountId, UpdateUserInfoRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新当前登录用户的在线状态
|
||||||
|
*
|
||||||
|
* @param accountId 账号 ID
|
||||||
|
* @param request 状态更新请求
|
||||||
|
*/
|
||||||
|
void updateStatus(String accountId, UpdateStatusRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.webgame.webgamebackend.service.impl;
|
package com.webgame.webgamebackend.service.impl;
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.webgame.webgamebackend.common.dto.user.UpdateStatusRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
import com.webgame.webgamebackend.common.dto.user.UpdateUserInfoRequest;
|
||||||
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
import com.webgame.webgamebackend.common.dto.user.UserInfoResponse;
|
||||||
import com.webgame.webgamebackend.common.exception.BusinessException;
|
import com.webgame.webgamebackend.common.exception.BusinessException;
|
||||||
@@ -62,4 +63,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
log.info("[用户服务] 更新用户信息, accountId={}", accountId);
|
log.info("[用户服务] 更新用户信息, accountId={}", accountId);
|
||||||
return UserInfoResponse.from(user);
|
return UserInfoResponse.from(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void updateStatus(String accountId, UpdateStatusRequest request) {
|
||||||
|
UserEntity user = userRepository.findByAccountId(accountId)
|
||||||
|
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
||||||
|
|
||||||
|
user.setStatus(request.status());
|
||||||
|
userRepository.save(user);
|
||||||
|
log.info("[用户服务] 更新在线状态, accountId={}, status={}", accountId, request.status());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user