From 9e76f75f69415280973cf25424f8b0cb8f2985e1 Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Wed, 9 Oct 2024 15:07:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A4=B4=E5=83=8F=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E3=80=81=E7=94=A8=E6=88=B7=E5=90=8D=E7=A7=B0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E3=80=81=E7=94=A8=E6=88=B7=E8=B4=A6=E6=88=B7=E6=B3=A8?= =?UTF-8?q?=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hfkj/controller/FileUploadController.java | 88 ++++++++++++ .../com/hfkj/controller/UserController.java | 126 +++++++++++++++++- .../hfkj/dao/BsUserParentRelMapperExt.java | 2 +- .../service/user/BsUserParentRelService.java | 7 + .../com/hfkj/service/user/BsUserService.java | 11 ++ .../user/impl/BsUserParentRelServiceImpl.java | 7 + .../service/user/impl/BsUserServiceImpl.java | 44 +++++- .../com/hfkj/sysenum/user/UserStatusEnum.java | 9 +- 8 files changed, 276 insertions(+), 18 deletions(-) create mode 100644 cweb/src/main/java/com/hfkj/controller/FileUploadController.java diff --git a/cweb/src/main/java/com/hfkj/controller/FileUploadController.java b/cweb/src/main/java/com/hfkj/controller/FileUploadController.java new file mode 100644 index 0000000..9e639c1 --- /dev/null +++ b/cweb/src/main/java/com/hfkj/controller/FileUploadController.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 fileNames = new ArrayList(); + if (multipartResolver.isMultipart(request)) { + // 转换成多部分request + MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; + Iterator 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); + } + + } + + +} diff --git a/cweb/src/main/java/com/hfkj/controller/UserController.java b/cweb/src/main/java/com/hfkj/controller/UserController.java index 7bd7ed5..5719446 100644 --- a/cweb/src/main/java/com/hfkj/controller/UserController.java +++ b/cweb/src/main/java/com/hfkj/controller/UserController.java @@ -5,9 +5,12 @@ import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; import com.hfkj.common.security.UserCenter; +import com.hfkj.common.utils.DateUtil; import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.config.CommonSysConst; +import com.hfkj.config.SysConfig; import com.hfkj.entity.BsUser; import com.hfkj.model.ResponseData; import com.hfkj.model.SecUserSessionObject; @@ -17,17 +20,21 @@ import com.hfkj.sysenum.user.UserLoginType; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; +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 java.util.HashMap; -import java.util.Map; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileOutputStream; +import java.util.*; /** * @className: ClientController @@ -38,12 +45,15 @@ import java.util.Map; @RequestMapping(value="/user") @Api(value="客户端业务") public class UserController { + private static Logger log = LoggerFactory.getLogger(UserController.class); @Resource private BsUserService userService; @Resource private RedisUtil redisUtil; @Autowired private UserCenter userCenter; + @Autowired + private SysConfig sysConfig; @RequestMapping(value = "/getUser", method = RequestMethod.GET) @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 fileNames = new ArrayList(); + if (multipartResolver.isMultipart(request)) { + // 转换成多部分request + MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; + Iterator 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) @ResponseBody @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); + } + } + } diff --git a/service/src/main/java/com/hfkj/dao/BsUserParentRelMapperExt.java b/service/src/main/java/com/hfkj/dao/BsUserParentRelMapperExt.java index d9c4bc3..913d0a9 100644 --- a/service/src/main/java/com/hfkj/dao/BsUserParentRelMapperExt.java +++ b/service/src/main/java/com/hfkj/dao/BsUserParentRelMapperExt.java @@ -23,7 +23,7 @@ public interface BsUserParentRelMapperExt { " au.invite_user_id inviteUserId," + " 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` ]]> 0) au," + " (SELECT @pid := #{param.parentUserId}) pd" + " WHERE FIND_IN_SET(invite_user_id, @pid) > 0 and @pid := concat(@pid, ',', id)" + " ORDER BY id" + diff --git a/service/src/main/java/com/hfkj/service/user/BsUserParentRelService.java b/service/src/main/java/com/hfkj/service/user/BsUserParentRelService.java index e1d5ee5..11c89c9 100644 --- a/service/src/main/java/com/hfkj/service/user/BsUserParentRelService.java +++ b/service/src/main/java/com/hfkj/service/user/BsUserParentRelService.java @@ -43,6 +43,13 @@ public interface BsUserParentRelService { */ BsUserParentRel getParent(List grade, Long userId); + /** + * 查询用户直属下级用户关系列表 + * @param userId + * @return + */ + List getSonRelList(Long userId); + /** * 查询下级用户列表 * @param param diff --git a/service/src/main/java/com/hfkj/service/user/BsUserService.java b/service/src/main/java/com/hfkj/service/user/BsUserService.java index 8b701d5..0072900 100644 --- a/service/src/main/java/com/hfkj/service/user/BsUserService.java +++ b/service/src/main/java/com/hfkj/service/user/BsUserService.java @@ -27,6 +27,11 @@ public interface BsUserService { */ void updatePhone(Long userId, String newPhone) throws Exception; + /** + * 修改信息 + */ + void updateInfo(BsUser user) throws Exception; + /** * 修改微信号 * @param userId @@ -41,6 +46,12 @@ public interface BsUserService { */ void bindInviteUser(Long userId, Long inviteUseId) throws Exception; + /** + * 注销账户 + * @param userId + */ + void delete(Long userId); + /** * 查询详情 * @param userId diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserParentRelServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserParentRelServiceImpl.java index a9c1589..7e2af05 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserParentRelServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserParentRelServiceImpl.java @@ -69,6 +69,13 @@ public class BsUserParentRelServiceImpl implements BsUserParentRelService { return recursionParent(grade, userParentRel.getParentUserId(), new ArrayList<>(Arrays.asList(userParentRel))); } + @Override + public List getSonRelList(Long userId) { + BsUserParentRelExample example = new BsUserParentRelExample(); + example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(0); + return userParentRelMapper.selectByExample(example); + } + @Override public List getTeamSubList(Map param) { return userParentRelMapper.queryTeamSubList(param); diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java index 39846ae..fe885e9 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java @@ -8,12 +8,10 @@ import com.hfkj.common.security.SessionObject; import com.hfkj.common.security.UserCenter; import com.hfkj.common.utils.RedisUtil; import com.hfkj.dao.BsUserMapper; -import com.hfkj.entity.BsUser; -import com.hfkj.entity.BsUserExample; -import com.hfkj.entity.BsUserInviteCode; -import com.hfkj.entity.BsUserParentRel; +import com.hfkj.entity.*; import com.hfkj.model.UserSessionObject; import com.hfkj.service.user.*; +import com.hfkj.sysenum.user.UserAccountStatusEnum; import com.hfkj.sysenum.user.UserGradeEnum; import com.hfkj.sysenum.user.UserLoginType; import com.hfkj.sysenum.user.UserStatusEnum; @@ -104,6 +102,18 @@ public class BsUserServiceImpl implements BsUserService { 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 public void updateWechatNum(Long userId, String wechatNum) throws Exception { // 查询用户 @@ -151,6 +161,32 @@ public class BsUserServiceImpl implements BsUserService { 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 sonRelList = userParentRelService.getSonRelList(userId); + for (BsUserParentRel sonRel : sonRelList) { + // 删除关系 + sonRel.setStatus(0); + userParentRelService.editData(sonRel); + } + } + @Override public BsUser getUser(Long userId) { return userMapper.selectByPrimaryKey(userId); diff --git a/service/src/main/java/com/hfkj/sysenum/user/UserStatusEnum.java b/service/src/main/java/com/hfkj/sysenum/user/UserStatusEnum.java index a3e8303..913a771 100644 --- a/service/src/main/java/com/hfkj/sysenum/user/UserStatusEnum.java +++ b/service/src/main/java/com/hfkj/sysenum/user/UserStatusEnum.java @@ -10,9 +10,9 @@ import lombok.Getter; @Getter public enum UserStatusEnum { /** - * 删除 + * 已注销 */ - status0(0, "删除"), + status0(0, "已注销"), /** * 正常 @@ -23,11 +23,6 @@ public enum UserStatusEnum { * 禁用 */ status2(2, "禁用"), - - /** - * 已注销 - */ - status3(3, "已注销"), ; private int code;