用户资料修改功能

This commit is contained in:
ronger 2020-01-09 13:20:08 +08:00
parent cac7566c09
commit 07509807b9
7 changed files with 269 additions and 30 deletions

View File

@ -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;

View File

@ -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<User> {
/**
* 根据账号获取获取用户信息
* @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);
}

View File

@ -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<User> {
* @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);
}

View File

@ -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<User> 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<User> 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<User> 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<User> 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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -21,6 +21,7 @@
<result column="updated_time" property="updatedTime"/>
</resultMap>
<resultMap id="UserInfoResultMapper" type="com.rymcu.vertical.dto.UserInfoDTO">
<result column="id" property="idUser"/>
<result column="account" property="account"/>
<result column="nickname" property="nickname"/>
<result column="sex" property="sex"/>
@ -30,6 +31,7 @@
<result column="phone" property="phone"/>
<result column="status" property="status"/>
<result column="last_login_time" property="lastLoginTime"/>
<result column="signature" property="signature"/>
</resultMap>
<resultMap id="DTOResultMapper" type="com.rymcu.vertical.dto.UserDTO">
<result column="id" property="idUser"/>
@ -50,12 +52,19 @@
<update id="updateStatus">
update vertical_user set status = #{status} where id = #{idUser}
</update>
<update id="updateUserInfo">
update vertical_user set nickname = #{nickname},email = #{email},signature = #{signature},avatar_type = #{avatarType},avatar_url = #{avatarUrl},sex = #{sex}
<if test="phone != null and phone != ''">
,phone = #{phone}
</if>
where id = #{idUser}
</update>
<select id="findByAccount" resultMap="BaseResultMap">
select id, nickname, account, password, status from vertical_user where account = #{account} AND status = 0
select id, nickname, account, password, status from vertical_user where account = #{account} and status = 0
</select>
<select id="findUserInfoByAccount" resultType="com.rymcu.vertical.dto.UserInfoDTO">
select id, nickname, sex, avatar_type, avatar_url, email, phone, account, password, status, last_login_time from vertical_user when account = #{account}
<select id="findUserInfoByAccount" resultMap="UserInfoResultMapper">
select id, nickname, sex, avatar_type, avatar_url, email, phone, account, status, signature, last_login_time from vertical_user where account = #{account}
</select>
<select id="selectUserDTOByNickname" resultMap="DTOResultMapper">
select id, nickname, avatar_type, avatar_url, account from vertical_user where nickname = #{nickname} and status = 0
@ -66,5 +75,11 @@
<select id="selectCountByNickName" resultType="java.lang.Integer">
select count(*) from vertical_user where nickname = #{nickname}
</select>
<select id="selectUserInfo" resultMap="UserInfoResultMapper">
select id, nickname, sex, avatar_type, avatar_url, email, phone, account, status, signature, last_login_time from vertical_user where id = #{idUser}
</select>
<select id="checkNicknameByIdUser" resultType="java.lang.Integer">
select count(*) from vertical_user where nickname = #{nickname} and id != #{idUser}
</select>
</mapper>