完成头像上传、用户名称修改、用户账户注销

master
胡锐 2 months ago
parent ee0468d95b
commit 9e76f75f69
  1. 88
      cweb/src/main/java/com/hfkj/controller/FileUploadController.java
  2. 126
      cweb/src/main/java/com/hfkj/controller/UserController.java
  3. 2
      service/src/main/java/com/hfkj/dao/BsUserParentRelMapperExt.java
  4. 7
      service/src/main/java/com/hfkj/service/user/BsUserParentRelService.java
  5. 11
      service/src/main/java/com/hfkj/service/user/BsUserService.java
  6. 7
      service/src/main/java/com/hfkj/service/user/impl/BsUserParentRelServiceImpl.java
  7. 44
      service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java
  8. 9
      service/src/main/java/com/hfkj/sysenum/user/UserStatusEnum.java

@ -0,0 +1,88 @@
package com.hfkj.controller;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.SysConfig;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
@RestController
@RequestMapping(value="/fileUpload")
@Api(value="文件上传")
public class FileUploadController {
private static Logger log = LoggerFactory.getLogger(FileUploadController.class);
@Resource
private SysConfig sysConfig;
@RequestMapping(value="/uploadfile",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "文件上传")
public ResponseData uploadFile(@RequestParam(value = "files" , required = false) MultipartFile files,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
response.setHeader("Access-Control-Allow-Origin", "*");
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 判断 request 是否有文件上传,即多部分请求
List<String> fileNames = new ArrayList<String>();
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile file = multiRequest.getFile(iterator.next());
if (file != null) {
FileOutputStream out = null;
try {
String fileType = file.getOriginalFilename()
.substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + "." + fileType;
String childPath = DateUtil.date2String(new Date(), "yyyyMM");
String destDirName = sysConfig.getFileUrl() + File.separator + childPath;
File dir = new File(destDirName);
if (!dir.exists()) {
dir.mkdirs();
}
out = new FileOutputStream(destDirName + File.separator + fileName);
out.write(file.getBytes());
out.flush();
fileNames.add(childPath + "/" + fileName);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (out != null) {
out.close();
}
}
}
}
}
return ResponseMsgUtil.success(fileNames);
} catch (Exception e) {
log.error(e.getMessage(), e);
return ResponseMsgUtil.exception(e);
}
}
}

@ -5,9 +5,12 @@ import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode; import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter; import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil; import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.config.SysConfig;
import com.hfkj.entity.BsUser; import com.hfkj.entity.BsUser;
import com.hfkj.model.ResponseData; import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject; import com.hfkj.model.SecUserSessionObject;
@ -17,17 +20,21 @@ import com.hfkj.sysenum.user.UserLoginType;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.HashMap; import javax.servlet.http.HttpServletResponse;
import java.util.Map; import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
/** /**
* @className: ClientController * @className: ClientController
@ -38,12 +45,15 @@ import java.util.Map;
@RequestMapping(value="/user") @RequestMapping(value="/user")
@Api(value="客户端业务") @Api(value="客户端业务")
public class UserController { public class UserController {
private static Logger log = LoggerFactory.getLogger(UserController.class);
@Resource @Resource
private BsUserService userService; private BsUserService userService;
@Resource @Resource
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Autowired @Autowired
private UserCenter userCenter; private UserCenter userCenter;
@Autowired
private SysConfig sysConfig;
@RequestMapping(value = "/getUser", method = RequestMethod.GET) @RequestMapping(value = "/getUser", method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ -61,6 +71,94 @@ public class UserController {
} }
} }
@RequestMapping(value="uploadHead",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "头像上传")
public ResponseData uploadHead(@RequestParam(value = "files" , required = false) MultipartFile files,HttpServletRequest request,HttpServletResponse response) {
try {
UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
// 查询用户
BsUser user = userService.getUser(userSession.getUser().getId());
if (user == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的用户");
}
response.setHeader("Access-Control-Allow-Origin", "*");
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 判断 request 是否有文件上传,即多部分请求
List<String> fileNames = new ArrayList<String>();
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile file = multiRequest.getFile(iterator.next());
if (file != null) {
FileOutputStream out = null;
try {
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String fileName = System.currentTimeMillis() + "." + fileType;
String childPath = "user-head";
String destDirName = sysConfig.getFileUrl() + File.separator + childPath;
File dir = new File(destDirName);
if (!dir.exists()) {
dir.mkdirs();
}
out = new FileOutputStream(destDirName + File.separator + fileName);
out.write(file.getBytes());
out.flush();
fileNames.add(childPath + "/" + fileName);
// 修改用户头像
user.setHeadImg(CommonSysConst.getSysConfig().getDomain()+"/"+fileNames.get(0));
userService.updateInfo(user);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "上传失败");
} finally {
if (out != null) {
out.close();
}
}
}
}
}
return ResponseMsgUtil.success("上传成功");
} catch (Exception e) {
log.error(e.getMessage(), e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/updateName", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "修改用户名称")
public ResponseData updateName(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("name"))) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
// 查询用户
BsUser user = userService.getUser(userSession.getUser().getId());
if (user == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的用户");
}
user.setName(body.getString("name"));
userService.updateInfo(user);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/updateWechat", method = RequestMethod.POST) @RequestMapping(value = "/updateWechat", method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "修改用户微信号") @ApiOperation(value = "修改用户微信号")
@ -219,4 +317,20 @@ public class UserController {
} }
} }
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "注销账户")
public ResponseData delete() {
try {
UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
userService.delete(userSession.getUser().getId());
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
} }

@ -23,7 +23,7 @@ public interface BsUserParentRelMapperExt {
" au.invite_user_id inviteUserId," + " au.invite_user_id inviteUserId," +
" case au.invite_user_id when #{param.parentUserId} then 1 else 2 end relType" + " case au.invite_user_id when #{param.parentUserId} then 1 else 2 end relType" +
" " + " " +
" FROM (SELECT * FROM bs_user WHERE invite_user_id IS NOT NULL) au," + " FROM (SELECT * FROM bs_user WHERE invite_user_id IS NOT NULL and `status` <![CDATA[ <> ]]> 0) au," +
" (SELECT @pid := #{param.parentUserId}) pd" + " (SELECT @pid := #{param.parentUserId}) pd" +
" WHERE FIND_IN_SET(invite_user_id, @pid) > 0 and @pid := concat(@pid, ',', id)" + " WHERE FIND_IN_SET(invite_user_id, @pid) > 0 and @pid := concat(@pid, ',', id)" +
" ORDER BY id" + " ORDER BY id" +

@ -43,6 +43,13 @@ public interface BsUserParentRelService {
*/ */
BsUserParentRel getParent(List<UserGradeEnum> grade, Long userId); BsUserParentRel getParent(List<UserGradeEnum> grade, Long userId);
/**
* 查询用户直属下级用户关系列表
* @param userId
* @return
*/
List<BsUserParentRel> getSonRelList(Long userId);
/** /**
* 查询下级用户列表 * 查询下级用户列表
* @param param * @param param

@ -27,6 +27,11 @@ public interface BsUserService {
*/ */
void updatePhone(Long userId, String newPhone) throws Exception; void updatePhone(Long userId, String newPhone) throws Exception;
/**
* 修改信息
*/
void updateInfo(BsUser user) throws Exception;
/** /**
* 修改微信号 * 修改微信号
* @param userId * @param userId
@ -41,6 +46,12 @@ public interface BsUserService {
*/ */
void bindInviteUser(Long userId, Long inviteUseId) throws Exception; void bindInviteUser(Long userId, Long inviteUseId) throws Exception;
/**
* 注销账户
* @param userId
*/
void delete(Long userId);
/** /**
* 查询详情 * 查询详情
* @param userId * @param userId

@ -69,6 +69,13 @@ public class BsUserParentRelServiceImpl implements BsUserParentRelService {
return recursionParent(grade, userParentRel.getParentUserId(), new ArrayList<>(Arrays.asList(userParentRel))); return recursionParent(grade, userParentRel.getParentUserId(), new ArrayList<>(Arrays.asList(userParentRel)));
} }
@Override
public List<BsUserParentRel> getSonRelList(Long userId) {
BsUserParentRelExample example = new BsUserParentRelExample();
example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(0);
return userParentRelMapper.selectByExample(example);
}
@Override @Override
public List<UserTeamModel> getTeamSubList(Map<String, Object> param) { public List<UserTeamModel> getTeamSubList(Map<String, Object> param) {
return userParentRelMapper.queryTeamSubList(param); return userParentRelMapper.queryTeamSubList(param);

@ -8,12 +8,10 @@ import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter; import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsUserMapper; import com.hfkj.dao.BsUserMapper;
import com.hfkj.entity.BsUser; import com.hfkj.entity.*;
import com.hfkj.entity.BsUserExample;
import com.hfkj.entity.BsUserInviteCode;
import com.hfkj.entity.BsUserParentRel;
import com.hfkj.model.UserSessionObject; import com.hfkj.model.UserSessionObject;
import com.hfkj.service.user.*; import com.hfkj.service.user.*;
import com.hfkj.sysenum.user.UserAccountStatusEnum;
import com.hfkj.sysenum.user.UserGradeEnum; import com.hfkj.sysenum.user.UserGradeEnum;
import com.hfkj.sysenum.user.UserLoginType; import com.hfkj.sysenum.user.UserLoginType;
import com.hfkj.sysenum.user.UserStatusEnum; import com.hfkj.sysenum.user.UserStatusEnum;
@ -104,6 +102,18 @@ public class BsUserServiceImpl implements BsUserService {
redisUtil.hdel(CACHE_KEY, oldPhone); redisUtil.hdel(CACHE_KEY, oldPhone);
} }
@Override
public void updateInfo(BsUser user) throws Exception {
// 查询用户
editData(user);
// 更新session
UserSessionObject session = new UserSessionObject();
session.setUser(user);
SessionObject sessionObject = new SessionObject(userToken(user.getId()), session);
userCenter.save(sessionObject);
}
@Override @Override
public void updateWechatNum(Long userId, String wechatNum) throws Exception { public void updateWechatNum(Long userId, String wechatNum) throws Exception {
// 查询用户 // 查询用户
@ -151,6 +161,32 @@ public class BsUserServiceImpl implements BsUserService {
userCenter.save(sessionObject); userCenter.save(sessionObject);
} }
@Override
@Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
public void delete(Long userId) {
// 查询用户
BsUser user = getUser(userId);
if (user == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户");
}
user.setStatus(UserStatusEnum.status0.getCode());
editData(user);
// 删除上级关系
BsUserParentRel userParentRel = userParentRelService.getDetailByUserId(userId);
if (userParentRel != null) {
userParentRel.setStatus(0);
userParentRelService.editData(userParentRel);
}
// 查询下级关系
List<BsUserParentRel> sonRelList = userParentRelService.getSonRelList(userId);
for (BsUserParentRel sonRel : sonRelList) {
// 删除关系
sonRel.setStatus(0);
userParentRelService.editData(sonRel);
}
}
@Override @Override
public BsUser getUser(Long userId) { public BsUser getUser(Long userId) {
return userMapper.selectByPrimaryKey(userId); return userMapper.selectByPrimaryKey(userId);

@ -10,9 +10,9 @@ import lombok.Getter;
@Getter @Getter
public enum UserStatusEnum { public enum UserStatusEnum {
/** /**
* 删除 * 已注销
*/ */
status0(0, "删除"), status0(0, "已注销"),
/** /**
* 正常 * 正常
@ -23,11 +23,6 @@ public enum UserStatusEnum {
* 禁用 * 禁用
*/ */
status2(2, "禁用"), status2(2, "禁用"),
/**
* 已注销
*/
status3(3, "已注销"),
; ;
private int code; private int code;

Loading…
Cancel
Save