fire_point/src/main/java/com/xkrs/controller/SysUserController.java

444 lines
19 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xkrs.controller;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.common.tool.TokenUtil;
import com.xkrs.dao.AgentOrgDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.AgentOrgEntity;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.model.qo.SysUserQo;
import com.xkrs.model.qo.SysUserReceiveSmsQo;
import com.xkrs.model.qo.SysUserRemarkQo;
import com.xkrs.model.qo.SysUserVipLevelQo;
import com.xkrs.model.validation.SysUserQoInsert;
import com.xkrs.model.validation.SysUserQoUpdate;
import com.xkrs.model.vo.SysUserVo;
import com.xkrs.service.RedisService;
import com.xkrs.service.SysUserService;
import com.xkrs.sms.SMSHelper;
import com.xkrs.utils.FirePointCodeUtils;
import com.xkrs.utils.ListUtils;
import com.xkrs.utils.RandomUtil;
import org.apache.hc.core5.util.TextUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.utils.EncryptDecryptUtil.encry256;
/**
* 系统用户Controller
*/
@RestController
@RequestMapping(value = "/api/user")
public class SysUserController {
private final Locale locale = LocaleContextHolder.getLocale();
@Resource
private SysUserService sysUserService;
@Resource
private SysUserDao sysUserDao;
@Resource
private RedisService redisService;
@Resource
private AgentOrgDao agentOrgDao;
@Resource
private SMSHelper smsHelper;
/**
* 查询Vip用户
*/
@GetMapping("/selectVipUser")
public String selectVipUser() {
return sysUserService.selectVipUser();
}
/**
* 更新(批量)系统用户信息
*
* @param paramMap
* @return
*/
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updateSysUser", method = RequestMethod.POST)
public String updateSysUser(@RequestBody Map<String, String> paramMap) {
String userIdArray = paramMap.get("userIdArray");
String overTime = paramMap.get("overTime");
String remark = paramMap.get("remark");
List<String> userIdList = ListUtils.toStringList(userIdArray, ",");//解析的ID集合
if (userIdList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "修改失败请输入正确的ID", locale);
}
List<String> successIdList = new ArrayList<>();//操作成功的ID集合
List<String> failureIdList = new ArrayList<>();//操作失败的ID集合
for (String userId : userIdList) {
try {
Optional<SysUserEntity> targetEntityOptional = sysUserDao.findById(Integer.parseInt(userId));
if (targetEntityOptional.isPresent()) {
SysUserEntity targetEntity = targetEntityOptional.get();
if (!TextUtils.isEmpty(overTime)) {
targetEntity.setOverTime(overTime);
}
if (!TextUtils.isEmpty(remark)) {
targetEntity.setRemark(remark);
}
sysUserDao.save(targetEntity);
successIdList.add(userId);
} else {
failureIdList.add(userId);
}
} catch (Exception e) {
e.printStackTrace();
failureIdList.add(userId);
}
}
if (userIdList.size() == successIdList.size()) {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
}
StringBuilder builder = new StringBuilder();
if (successIdList.size() > 0) {
builder.append("ID").append(ListUtils.fromStringList(successIdList, "")).append("修改成功,");
}
builder.append("ID").append(ListUtils.fromStringList(failureIdList, "")).append("修改失败");
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "修改失败," + builder, locale);
}
/**
* 修改vip等级
*/
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updateviplevel", method = RequestMethod.POST)
public String updateVipLevel(@RequestBody SysUserVipLevelQo sysUserVipLevelQo) {
String ids = sysUserVipLevelQo.getIds();
List<Integer> idList = new ArrayList<>();
if (ids.contains(",")) {
String[] splits = ids.split(",");
if (splits != null && splits.length > 0) {
for (String split : splits) {
idList.add(Integer.parseInt(split));
}
}
} else {
idList.add(Integer.parseInt(ids));
}
for (Integer id : idList) {
sysUserDao.updateVipLevel(id, sysUserVipLevelQo.getVipLevel());
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
}
/**
* 修改是否接收短信
*/
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updatereceivesms", method = RequestMethod.POST)
public String updateReceiveSms(@RequestBody SysUserReceiveSmsQo sysUserReceiveSmsQo) {
String ids = sysUserReceiveSmsQo.getIds();
List<Integer> idList = new ArrayList<>();
if (ids.contains(",")) {
String[] splits = ids.split(",");
if (splits != null && splits.length > 0) {
for (String split : splits) {
idList.add(Integer.parseInt(split));
}
}
} else {
idList.add(Integer.parseInt(ids));
}
for (Integer id : idList) {
sysUserDao.updateReceiveSms(id, sysUserReceiveSmsQo.getReceiveSms());
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
}
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updateremark", method = RequestMethod.POST)
public String updateRemark(@RequestBody SysUserRemarkQo sysUserRemarkQo) {
Integer id = sysUserRemarkQo.getId();
Optional<SysUserEntity> optionalById = sysUserDao.findById(id);
if (optionalById.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "修改失败,用户找不到", locale);
}
sysUserDao.updateRemark(id, sysUserRemarkQo.getRemark());
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
}
/**
* 登录用户Token验证
*/
@RequestMapping(value = "/logged/check", method = RequestMethod.POST)
public String loginUserTokenCheck() {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
@RequestMapping(value = "/check/duplicate", method = RequestMethod.POST)
public String checkDuplicate(@RequestParam(value = "userName", required = false) String userName) {
// 验证用户名是否重复
if (!sysUserService.checkUserName(userName)) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "OK", locale);
}
@GetMapping("/selectAgentOrgList")
public String selectAgentOrgList() {
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll(Sort.by(Sort.Direction.ASC, "id"));
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, agentOrgList, locale);
}
/**
* 注册系统用户
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(@Validated({SysUserQoInsert.class}) @RequestBody SysUserQo userQo, BindingResult bindingResult, HttpServletRequest servletRequest) {
// 验证数据合法性
if (bindingResult.hasErrors()) {
return outputEncapsulationErrorList(bindingResult.getFieldErrors(), locale);
}
// 验证用户名是否重复
if (!sysUserService.checkUserName(userQo.getUserName())) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "该账号已经注册,请勿重复注册", locale);
}
// 添加新用户
return sysUserService.addUser(userQo, servletRequest);
}
/**
* 删除系统用户
*/
@RequestMapping(value = "/general/deletesysuser", method = RequestMethod.DELETE)
public String deleteSysUser(@RequestParam(value = "userName") String userName) {
return sysUserService.deleteSysUser(userName);
}
/**
* 软删除指定id的普通用户
*/
@RequestMapping(value = "/general/delete", method = RequestMethod.DELETE)
public String deleteCustomUser(@RequestParam(value = "userId", required = false) int id) {
// 验证数据合法性
int res = sysUserService.softDeleteGeneralUser(id);
if (res == 1) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "", locale);
} else {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateUser(@Validated({SysUserQoUpdate.class}) @RequestBody SysUserQo userQo, BindingResult bindingResult) {
// 验证数据合法性
if (bindingResult.hasErrors()) {
return outputEncapsulationErrorList(bindingResult.getFieldErrors(), locale);
}
// 修改用户
sysUserService.updateSysUser(userQo);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "OK", locale);
}
/**
* 判断用户名是否存在
*/
@GetMapping("/booleanUserName")
public String booleanUserName(@RequestParam("userName") String userName) {
Map map = new HashMap(3);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(userName);
if (sysUserEntity == null) {
map.put("status", 0);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale);
} else {
map.put("status", 1);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale);
}
}
/**
* 查看用户信息
*/
@GetMapping("/selectAllUser")
public String selectAllUser(@RequestHeader(value = "Authorization") String token) {
// 验证token
String tokenUserName = TokenUtil.getTokenUserName(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
List<SysUserVo> sysUserVos = sysUserService.selectAllUser();
if (sysUserVos == null || sysUserVos.size() == 0) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有任何用户数据", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, sysUserVos, locale);
}
/**
* 管理员进行启用禁用的操作
*/
@PostMapping("/operateActiveFlag")
public String operateActiveFlag(@RequestBody Map map, @RequestHeader(value = "Authorization") String token) {
Integer userId = (Integer) map.get("userId");
String keepType = (String) map.get("keepType");
// 验证token
String tokenUserName = TokenUtil.getTokenUserName(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
// 如果keepType等于1进行启用操作
if ("1".equals(keepType)) {
sysUserService.updateEnable(userId);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "启用成功", locale);
} else {
sysUserService.updateDisable(userId);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "禁用成功", locale);
}
}
/**
* 用户修改密码
*/
@PostMapping("/updatePassword")
public String updatePassword(@RequestBody Map map, @RequestHeader(value = "Authorization") String token) {
String oldPassword = (String) map.get("oldPassword");
String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword");
// 验证token
String tokenUserName = TokenUtil.getTokenUserName(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
return sysUserService.updatePassword(oldPassword, newPassword, confirmPassword, sysUserEntity);
}
/**
* 管理员修改用户的密码
*/
@PostMapping("/adminUpdatePassword")
public String adminUpdatePassword(@RequestBody Map map, @RequestHeader(value = "Authorization") String token) {
// 要修改的用户的id
Integer userId = (Integer) map.get("userId");
String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword");
// 验证token
String tokenUserName = TokenUtil.getTokenUserName(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
return sysUserService.adminUpdatePassword(userId, newPassword, confirmPassword);
}
/**
* 用户忘记密码
*/
@Transactional(rollbackFor = Exception.class)
@PostMapping("/userUnRememberPassword")
public String userUnRememberPassword(@RequestBody Map map) {
String phone = (String) map.get("phone");
String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword");
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(phone);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
if (!newPassword.equals(confirmPassword)) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "两次密码输入不一致", locale);
}
String newAfterPassword = encry256(newPassword + sysUserEntity.getSalt());
sysUserDao.updatePassword(sysUserEntity.getId(), newAfterPassword);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "密码修改成功", locale);
}
/**
* 发送验证码
*/
@GetMapping("/verificationCode")
public String verificationCode(@RequestParam("phone") String phone, @RequestParam(value = "agentOrgId", required = false) String agentOrgId) throws Exception {
Long finalAgentOrgId = Long.parseLong(TextUtils.isEmpty(agentOrgId) ? "1" : agentOrgId);
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll();
String targetAgentOrgName = FirePointCodeUtils.getTargetAgentOrgName(finalAgentOrgId, agentOrgList);
String optCode = String.valueOf(RandomUtil.returnCode());
redisService.set(phone, optCode, 10, TimeUnit.MINUTES);
smsHelper.dispatchSMSCode(targetAgentOrgName, phone, optCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
/**
* 填写手机号发送验证码,用于用户忘记密码
*/
@GetMapping("/verificationCodeUpdate")
public String verificationCodeUpdate(@RequestParam("phone") String phone) throws Exception {
SysUserVo sysUserVo = sysUserDao.selectUserByUserName(phone);
if (sysUserVo == null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "手机号错误,请使用您注册的手机号", locale);
}
List<AgentOrgEntity> agentOrgList = agentOrgDao.findAll();
String targetAgentOrgName = FirePointCodeUtils.getTargetAgentOrgName(sysUserVo.getAgentOrgId(), agentOrgList);
String optCode = String.valueOf(RandomUtil.returnCode());
redisService.set(phone, optCode, 10, TimeUnit.MINUTES);
smsHelper.dispatchSMSCode(targetAgentOrgName, phone, optCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
/**
* 输入验证码进行判断
*/
@GetMapping("/getVerificationCode")
public String getVerificationCode(@RequestParam("phone") String phone, @RequestParam("verificationCode") String verificationCode) {
String o = (String) redisService.get(phone);
if ("".equals(o) || o == null) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "请先发送验证码!", locale);
}
if (!redisService.get(phone).equals(verificationCode)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "验证码错误,请重新输入!", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "true", locale);
}
/**
* 更改账号过期时间
*/
@PostMapping("/updateExpireDate")
public String updateExpireDate(@RequestBody Map map, @RequestHeader(value = "Authorization") String token) {
Integer userId = (Integer) map.get("userId");
Integer timeNum = (Integer) map.get("timeNum");
String keepType = (String) map.get("keepType");
if ("1".equals(keepType)) {
return sysUserService.updateExpireDateDay(userId, timeNum);
} else if ("2".equals(keepType)) {
return sysUserService.updateExpireDateMonth(userId, timeNum);
} else if ("3".equals(keepType)) {
return sysUserService.updateExpireDateYear(userId, timeNum);
} else {
return sysUserService.updatePermanent(userId);
}
}
/**
* 修改账号到期时间
*/
@PostMapping("/updateOverTime")
public String updateOverTime(@RequestBody Map map, @RequestHeader(value = "Authorization") String token) {
Integer userId = (Integer) map.get("userId");
String overTime = (String) map.get("overTime");
return sysUserService.updateOverTime(userId, overTime);
}
}