完成用户信息修改,以及用户信息审核
This commit is contained in:
@@ -71,10 +71,9 @@ public class UserController {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
@PostMapping("/info/update")
|
@PostMapping("/info/update")
|
||||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
public Result userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||||
Long userId = UserContext.getUserId();
|
Long userId = UserContext.getUserId();
|
||||||
userInfoUpdateDTO.setUserId(userId);
|
userInfoUpdateDTO.setUserId(userId);
|
||||||
UserInfoVO userInfoVO = userService.updateUserInfo(userInfoUpdateDTO);
|
return Result.success(ResultCode.SUCCESS_REVIEW, "用户信息更新成功", null);
|
||||||
return Result.success(ResultCode.SUCCESS_REVIEW, "用户信息更新成功", userInfoVO);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package com.bao.dating.handler;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@MappedJdbcTypes(JdbcType.VARCHAR) // 也可以使用JdbcType.JSON,如果数据库支持的话
|
||||||
|
@MappedTypes(List.class)
|
||||||
|
public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
if (parameter == null || parameter.isEmpty()) {
|
||||||
|
ps.setNull(i, java.sql.Types.VARCHAR); // 或者 Types.JSON 如果数据库支持
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String json = objectMapper.writeValueAsString(parameter);
|
||||||
|
ps.setString(i, json);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new SQLException("Error converting list to JSON", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
String json = rs.getString(columnName);
|
||||||
|
return convertJsonToList(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
String json = rs.getString(columnIndex);
|
||||||
|
return convertJsonToList(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
String json = cs.getString(columnIndex);
|
||||||
|
return convertJsonToList(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> convertJsonToList(String json) throws SQLException {
|
||||||
|
if (json == null || json.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(json, new TypeReference<List<String>>() {});
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new SQLException("Error converting JSON to list", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,5 +41,5 @@ public interface UserService {
|
|||||||
* 更新用户信息
|
* 更新用户信息
|
||||||
* @param userInfoUpdateDTO 用户信息
|
* @param userInfoUpdateDTO 用户信息
|
||||||
*/
|
*/
|
||||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
void updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -45,6 +47,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
|
*
|
||||||
* @param userLoginDTO
|
* @param userLoginDTO
|
||||||
* @return 登录信息
|
* @return 登录信息
|
||||||
*/
|
*/
|
||||||
@@ -56,7 +59,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
// 查询用户
|
// 查询用户
|
||||||
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
||||||
if (user == null){
|
if (user == null) {
|
||||||
throw new RuntimeException("用户不存在");
|
throw new RuntimeException("用户不存在");
|
||||||
}
|
}
|
||||||
// 密码校验
|
// 密码校验
|
||||||
@@ -65,7 +68,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
user.getSalt(),
|
user.getSalt(),
|
||||||
user.getPasswordHash()
|
user.getPasswordHash()
|
||||||
);
|
);
|
||||||
if (!match){
|
if (!match) {
|
||||||
throw new RuntimeException("密码错误");
|
throw new RuntimeException("密码错误");
|
||||||
}
|
}
|
||||||
// 生成token
|
// 生成token
|
||||||
@@ -80,13 +83,14 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息
|
* 获取用户信息
|
||||||
|
*
|
||||||
* @param userId
|
* @param userId
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserInfoVO getUserInfo(Long userId) {
|
public UserInfoVO getUserInfo(Long userId) {
|
||||||
User user = userMapper.selectByUserId(userId);
|
User user = userMapper.selectByUserId(userId);
|
||||||
if (user == null){
|
if (user == null) {
|
||||||
throw new RuntimeException("用户不存在");
|
throw new RuntimeException("用户不存在");
|
||||||
}
|
}
|
||||||
UserInfoVO userInfoVO = new UserInfoVO();
|
UserInfoVO userInfoVO = new UserInfoVO();
|
||||||
@@ -171,22 +175,92 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新用户信息
|
||||||
|
*
|
||||||
|
* @param userInfoUpdateDTO
|
||||||
|
* @return 用户信息
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
public void updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||||
Long userId = userInfoUpdateDTO.getUserId();
|
Long userId = userInfoUpdateDTO.getUserId();
|
||||||
User user = userMapper.selectByUserId(userId);
|
User user = userMapper.selectByUserId(userId);
|
||||||
if (user == null){
|
if (user == null) {
|
||||||
throw new RuntimeException("用户不存在");
|
throw new RuntimeException("用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 将需要审核的内容合并成一个文本,用于减少调用次数
|
||||||
|
StringBuilder textBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
if (userInfoUpdateDTO.getNickname() != null && !userInfoUpdateDTO.getNickname().isEmpty()) {
|
||||||
|
textBuilder.append(userInfoUpdateDTO.getNickname()).append(" ");
|
||||||
|
}
|
||||||
|
if (userInfoUpdateDTO.getHobbies() != null && !userInfoUpdateDTO.getHobbies().isEmpty()) {
|
||||||
|
// 将爱好列表转换为字符串,用空格分隔
|
||||||
|
String hobbiesStr = String.join(" ", userInfoUpdateDTO.getHobbies());
|
||||||
|
textBuilder.append(hobbiesStr).append(" ");
|
||||||
|
}
|
||||||
|
if (userInfoUpdateDTO.getSignature() != null && !userInfoUpdateDTO.getSignature().isEmpty()) {
|
||||||
|
textBuilder.append(userInfoUpdateDTO.getSignature()).append(" ");
|
||||||
|
}
|
||||||
|
// 文本审核
|
||||||
|
if (textBuilder.length() > 0) {
|
||||||
|
Map textResult;
|
||||||
|
try {
|
||||||
|
textResult = greenTextScan.greeTextScan(textBuilder.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("用户信息文本审核失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
String suggestion = (String) textResult.get("suggestion");
|
||||||
|
|
||||||
|
if ("block".equals(suggestion)) {
|
||||||
|
throw new RuntimeException("用户信息包含违规内容,修改失败");
|
||||||
|
}
|
||||||
|
if ("review".equals(suggestion)) {
|
||||||
|
throw new RuntimeException("用户信息需要人工审核,暂无法修改");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 图片审核
|
||||||
|
List<String> imageKeys = new ArrayList<>();
|
||||||
|
if (userInfoUpdateDTO.getAvatarUrl() != null && !userInfoUpdateDTO.getAvatarUrl().isEmpty()) {
|
||||||
|
imageKeys.add(userInfoUpdateDTO.getAvatarUrl());
|
||||||
|
}
|
||||||
|
if (userInfoUpdateDTO.getBackgroundUrl() != null && !userInfoUpdateDTO.getBackgroundUrl().isEmpty()) {
|
||||||
|
imageKeys.add(userInfoUpdateDTO.getBackgroundUrl());
|
||||||
|
}
|
||||||
|
if (!imageKeys.isEmpty()) {
|
||||||
|
Map imageResult;
|
||||||
|
try {
|
||||||
|
imageResult = greenImageScan.imageScan(imageKeys);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("用户图片审核失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
String suggestion = (String) imageResult.get("suggestion");
|
||||||
|
|
||||||
|
if ("block".equals(suggestion)) {
|
||||||
|
throw new RuntimeException("头像或背景图不合规,修改失败");
|
||||||
|
}
|
||||||
|
if ("review".equals(suggestion)) {
|
||||||
|
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String nickname = userInfoUpdateDTO.getNickname();
|
||||||
|
// 用户没传昵称 or 传空串
|
||||||
|
if (!StringUtils.hasText(nickname)) {
|
||||||
|
// 如果数据库里原本也没有昵称
|
||||||
|
if (!StringUtils.hasText(user.getNickname())) {
|
||||||
|
userInfoUpdateDTO.setNickname(user.getUserName());
|
||||||
|
} else {
|
||||||
|
userInfoUpdateDTO.setNickname(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
userInfoUpdateDTO.setUpdatedAt(LocalDateTime.now());
|
userInfoUpdateDTO.setUpdatedAt(LocalDateTime.now());
|
||||||
|
|
||||||
// 更新数据库
|
// 更新数据库
|
||||||
userMapper.updateUserInfoByUserId(userInfoUpdateDTO);
|
userMapper.updateUserInfoByUserId(userInfoUpdateDTO);
|
||||||
|
|
||||||
// 返回动态详情
|
|
||||||
UserInfoVO userInfoVO = new UserInfoVO();
|
|
||||||
BeanUtils.copyProperties(user, userInfoVO);
|
|
||||||
return userInfoVO;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
<result property="backgroundUrl" column="background_url"/>
|
<result property="backgroundUrl" column="background_url"/>
|
||||||
<result property="gender" column="gender"/>
|
<result property="gender" column="gender"/>
|
||||||
<result property="birthday" column="birthday"/>
|
<result property="birthday" column="birthday"/>
|
||||||
<result property="hobbies" column="hobbies" typeHandler="com.bao.dating.handler.ListToVarcharTypeHandler"/>
|
<result property="hobbies" column="hobbies" typeHandler="com.bao.dating.handler.ListToJsonTypeHandler"/>
|
||||||
<result property="signature" column="signature"/>
|
<result property="signature" column="signature"/>
|
||||||
<result property="createdAt" column="created_at"/>
|
<result property="createdAt" column="created_at"/>
|
||||||
<result property="updatedAt" column="updated_at"/>
|
<result property="updatedAt" column="updated_at"/>
|
||||||
@@ -63,7 +63,7 @@
|
|||||||
birthday = #{birthday},
|
birthday = #{birthday},
|
||||||
</if>
|
</if>
|
||||||
<if test="hobbies != null">
|
<if test="hobbies != null">
|
||||||
hobbies = #{hobbies, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
hobbies = #{hobbies, typeHandler=com.bao.dating.handler.ListToJsonTypeHandler},
|
||||||
</if>
|
</if>
|
||||||
<if test="signature != null">
|
<if test="signature != null">
|
||||||
signature = #{signature},
|
signature = #{signature},
|
||||||
|
|||||||
Reference in New Issue
Block a user