diff --git a/src/main/java/com/rymcu/vertical/dto/UserInfoDTO.java b/src/main/java/com/rymcu/vertical/dto/UserInfoDTO.java index 614afe8..f500e86 100644 --- a/src/main/java/com/rymcu/vertical/dto/UserInfoDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/UserInfoDTO.java @@ -6,10 +6,13 @@ import lombok.Data; import java.io.Serializable; import java.util.Date; +/** + * @author ronger + */ @Data public class UserInfoDTO implements Serializable { - private String idUser; + private Integer idUser; private String account; @@ -29,6 +32,8 @@ public class UserInfoDTO implements Serializable { private String sex; + private String signature; + @JSONField(format = "yyyy-MM-dd HH:mm") private Date lastLoginTime; diff --git a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java index 88b22c2..6096eab 100644 --- a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java @@ -6,23 +6,104 @@ import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.User; import org.apache.ibatis.annotations.Param; +/** + * @author ronger + */ public interface UserMapper extends Mapper { + /** + * 根据账号获取获取用户信息 + * @param account + * @return + */ User findByAccount(@Param("account") String account); + /** + * 添加用户权限 + * @param idUser + * @param idRole + * @return + */ Integer insertUserRole(@Param("idUser") Integer idUser, @Param("idRole") Integer idRole); + /** + * 根据账号获取获取用户信息 + * @param account + * @return + */ UserInfoDTO findUserInfoByAccount(@Param("account") String account); + /** + * 根据用户昵称获取用户信息 + * @param nickname + * @return + */ UserDTO selectUserDTOByNickname(@Param("nickname") String nickname); + /** + * 修改用户密码 + * @param account + * @param password + * @return + */ Integer updatePasswordByAccount(@Param("account") String account, @Param("password") String password); + /** + * 获取用户权限权重 + * @param idUser + * @return + */ Integer selectRoleWeightsByUser(@Param("idUser") Integer idUser); + /** + * 更新用户权限 + * @param idUser + * @param idRole + * @return + */ Integer updateUserRole(@Param("idUser") Integer idUser, @Param("idRole") Integer idRole); + /** + * 更新用户状态 + * @param idUser + * @param status + * @return + */ Integer updateStatus(@Param("idUser") Integer idUser, @Param("status") String status); + /** + * 根据昵称获取重名用户数量 + * @param nickname + * @return + */ Integer selectCountByNickName(@Param("nickname") String nickname); + + /** + * 获取用户信息 + * @param idUser + * @return + */ + UserInfoDTO selectUserInfo(@Param("idUser") Integer idUser); + + /** + * 更新用户信息 + * @param idUser + * @param nickname + * @param avatarType + * @param avatarUrl + * @param email + * @param phone + * @param signature + * @param sex + * @return + */ + Integer updateUserInfo(@Param("idUser") Integer idUser, @Param("nickname") String nickname, @Param("avatarType") String avatarType, @Param("avatarUrl") String avatarUrl, @Param("email") String email, @Param("phone") String phone, @Param("signature") String signature, @Param("sex") String sex); + + /** + * 验证昵称是否重复 + * @param idUser + * @param nickname + * @return + */ + Integer checkNicknameByIdUser(@Param("idUser") Integer idUser, @Param("nickname") String nickname); } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/service/UserService.java b/src/main/java/com/rymcu/vertical/service/UserService.java index 96d7afa..f3504c5 100644 --- a/src/main/java/com/rymcu/vertical/service/UserService.java +++ b/src/main/java/com/rymcu/vertical/service/UserService.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; import com.rymcu.vertical.dto.UserDTO; +import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.User; import org.apache.ibatis.exceptions.TooManyResultsException; @@ -70,4 +71,26 @@ public interface UserService extends Service { * @return Map * */ Map updateStatus(Integer idUser, String status); + + /** + * 获取用户信息 + * @param idUser + * @return + */ + Map findUserInfo(Integer idUser); + + /** + * 更新用户信息 + * @param user + * @return + */ + Map updateUserInfo(UserInfoDTO user); + + /** + * 验证昵称是否重复 + * @param idUser + * @param nickname + * @return + */ + Map checkNickname(Integer idUser, String nickname); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java index 13439cf..93b9e98 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java @@ -4,6 +4,7 @@ import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.core.service.redis.RedisService; import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.dto.UserDTO; +import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.Role; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.jwt.service.TokenManager; @@ -12,6 +13,7 @@ import com.rymcu.vertical.mapper.UserMapper; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.BeanCopierUtil; import com.rymcu.vertical.util.Utils; +import com.rymcu.vertical.web.api.common.UploadController; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.exceptions.TooManyResultsException; import org.springframework.stereotype.Service; @@ -39,6 +41,8 @@ public class UserServiceImpl extends AbstractService implements UserServic @Resource private TokenManager tokenManager; + private final static String avatarSvgType = "1"; + @Override public User findByAccount(String account) throws TooManyResultsException{ return userMapper.findByAccount(account); @@ -133,7 +137,7 @@ public class UserServiceImpl extends AbstractService implements UserServic } @Override - @Transactional + @Transactional(rollbackFor = Exception.class) public Map updateUserRole(Integer idUser, Integer idRole) { Map map = new HashMap(1); Integer result = userMapper.updateUserRole(idUser,idRole); @@ -144,7 +148,7 @@ public class UserServiceImpl extends AbstractService implements UserServic } @Override - @Transactional + @Transactional(rollbackFor = Exception.class) public Map updateStatus(Integer idUser, String status) { Map map = new HashMap(1); Integer result = userMapper.updateStatus(idUser,status); @@ -153,4 +157,49 @@ public class UserServiceImpl extends AbstractService implements UserServic } return map; } + + @Override + public Map findUserInfo(Integer idUser) { + Map map = new HashMap(1); + UserInfoDTO user = userMapper.selectUserInfo(idUser); + if (user == null) { + map.put("message", "用户不存在!"); + } else { + map.put("user", user); + } + return map; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Map updateUserInfo(UserInfoDTO user) { + Map map = new HashMap(1); + Integer number = userMapper.checkNicknameByIdUser(user.getIdUser(), user.getNickname()); + if (number > 0) { + map.put("message", "该昵称已使用!"); + return map; + } + if (StringUtils.isNotBlank(user.getAvatarType()) && avatarSvgType.equals(user.getAvatarType())) { + String avatarUrl = UploadController.uploadBase64File(user.getAvatarUrl(), 0); + user.setAvatarUrl(avatarUrl); + } + Integer result = userMapper.updateUserInfo(user.getIdUser(), user.getNickname(), user.getAvatarType(),user.getAvatarUrl(), + user.getEmail(),user.getPhone(),user.getSignature(), user.getSex()); + if (result == 0) { + map.put("message", "操作失败!"); + return map; + } + map.put("user",user); + return map; + } + + @Override + public Map checkNickname(Integer idUser, String nickname) { + Map map = new HashMap(1); + Integer number = userMapper.checkNicknameByIdUser(idUser, nickname); + if (number > 0) { + map.put("message", "该昵称已使用!"); + } + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java index 83f3c99..e2ae5c9 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/UploadController.java @@ -9,6 +9,7 @@ import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.Utils; import com.rymcu.vertical.web.api.exception.ErrorCode; import com.rymcu.vertical.web.api.exception.BaseApiException; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; @@ -22,6 +23,10 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +/** + * 文件上传控制器 + * @author ronger + */ @RestController @RequestMapping("/api/v1/upload") public class UploadController { @@ -35,18 +40,7 @@ public class UploadController { if (multipartFile == null) { return GlobalResultGenerator.genErrorResult("请选择要上传的文件"); } - String typePath = ""; - switch (type){ - case 0: - typePath = "avatar"; - break; - case 1: - typePath = "article"; - break; - case 2: - typePath = "tags"; - break; - } + String typePath = getTypePath(type); //图片存储路径 String dir = ctxHeadPicPath+"/"+typePath; File file = new File(dir); @@ -75,18 +69,7 @@ public class UploadController { @PostMapping("/file/batch") public GlobalResult batchFileUpload(@RequestParam(value = "file[]", required = false)MultipartFile[] multipartFiles,@RequestParam(defaultValue = "1")Integer type,HttpServletRequest request){ - String typePath = ""; - switch (type){ - case 0: - typePath = "avatar"; - break; - case 1: - typePath = "article"; - break; - case 2: - typePath = "tags"; - break; - } + String typePath = getTypePath(type); //图片存储路径 String dir = ctxHeadPicPath+"/"+typePath; File file = new File(dir); @@ -119,6 +102,24 @@ public class UploadController { return GlobalResultGenerator.genSuccessResult(data); } + private static String getTypePath(Integer type) { + String typePath; + switch (type){ + case 0: + typePath = "avatar"; + break; + case 1: + typePath = "article"; + break; + case 2: + typePath = "tags"; + break; + default: + typePath = "images"; + } + return typePath; + } + @GetMapping("/simple/token") public GlobalResult uploadSimpleToken(HttpServletRequest request) throws BaseApiException { String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); @@ -144,4 +145,29 @@ public class UploadController { map.put("uploadURL", UPLOAD_URL); return GlobalResultGenerator.genSuccessResult(map); } + + public static String uploadBase64File(String fileStr, Integer type) { + if (StringUtils.isBlank(fileStr)) { + return ""; + } + String typePath = getTypePath(type); + //图片存储路径 + String dir = ctxHeadPicPath+"/"+typePath; + File file = new File(dir); + if (!file.exists()) { + file.mkdirs();// 创建文件根目录 + } + + String localPath = Utils.getProperty("resource.file-path")+"/"+typePath+"/"; + String fileName = System.currentTimeMillis()+".png"; + String savePath = file.getPath() + File.separator + fileName; + File saveFile = new File(savePath); + try { + FileCopyUtils.copy(Base64.decodeBase64(fileStr.substring(fileStr.indexOf(",") + 1)), saveFile); + fileStr = localPath+fileName; + } catch (IOException e) { + fileStr = "上传失败!"; + } + return fileStr; + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java b/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java new file mode 100644 index 0000000..559b512 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java @@ -0,0 +1,40 @@ +package com.rymcu.vertical.web.api.user; + +import com.rymcu.vertical.core.result.GlobalResult; +import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.dto.UserInfoDTO; +import com.rymcu.vertical.service.UserService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/user-info") +public class UserInfoController { + + @Resource + private UserService userService; + + @GetMapping("/detail/{idUser}") + public GlobalResult detail(@PathVariable Integer idUser) { + Map map = userService.findUserInfo(idUser); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/check-nickname") + public GlobalResult checkNickname(@RequestParam Integer idUser, @RequestParam String nickname) { + Map map = userService.checkNickname(idUser,nickname); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PatchMapping("/update") + public GlobalResult updateUserInfo(@RequestBody UserInfoDTO user) { + Map map = userService.updateUserInfo(user); + return GlobalResultGenerator.genSuccessResult(map); + } + +} diff --git a/src/main/java/mapper/UserMapper.xml b/src/main/java/mapper/UserMapper.xml index 5e84986..ea2ab1a 100644 --- a/src/main/java/mapper/UserMapper.xml +++ b/src/main/java/mapper/UserMapper.xml @@ -21,6 +21,7 @@ + @@ -30,6 +31,7 @@ + @@ -50,12 +52,19 @@ update vertical_user set status = #{status} where id = #{idUser} + + update vertical_user set nickname = #{nickname},email = #{email},signature = #{signature},avatar_type = #{avatarType},avatar_url = #{avatarUrl},sex = #{sex} + + ,phone = #{phone} + + where id = #{idUser} + - + select id, nickname, sex, avatar_type, avatar_url, email, phone, account, status, signature, last_login_time from vertical_user where account = #{account} select count(*) from vertical_user where nickname = #{nickname} + + \ No newline at end of file