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

362 lines
16 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.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.model.qo.SysUserQo;
import com.xkrs.model.qo.SysUserReceiveSmsQo;
import com.xkrs.model.va.SysUserQoInsert;
import com.xkrs.model.va.SysUserQoUpdate;
import com.xkrs.model.vo.SysUserVo;
import com.xkrs.service.RedisService;
import com.xkrs.service.SysUserService;
import com.xkrs.utilsnew.*;
import org.apache.hc.core5.util.TextUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
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.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
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.utilsnew.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;
/**
* 更新(批量)系统用户信息
*
* @param paramMap
* @return
*/
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updateSysUser", method = RequestMethod.POST)
public String updateSysUser(@RequestHeader(value = "Authorization") String token, @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(Long.parseLong(userId));
if (targetEntityOptional.isPresent()) {
SysUserEntity targetEntity = targetEntityOptional.get();
if (!TextUtils.isEmpty(overTime)) {
//获取LocalDateTime格式的过期时间
LocalDateTime newLocalDateTime = LocalDateTime.parse(overTime, DateTimeUtils.DATE_TIME_FORMATTER_1);
//生成新的VIP时间范围Json内容
String newVipTimeRangeJson = VipTimeRangeUtils.obtainNewVipTimeRangeJson(targetEntity.getVipTimeRangeJson(), newLocalDateTime);
//更新字段
targetEntity.setVipTimeRangeJson(newVipTimeRangeJson);
}
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);
}
/**
* 修改是否接收短信
*/
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/updatereceivesms", method = RequestMethod.POST)
public String updateReceiveSms(@RequestBody SysUserReceiveSmsQo sysUserReceiveSmsQo) {
String ids = sysUserReceiveSmsQo.getIds();
List<Long> idList = new ArrayList<>();
if (ids.contains(",")) {
String[] splits = ids.split(",");
for (String split : splits) {
idList.add(Long.parseLong(split));
}
} else {
idList.add(Long.parseLong(ids));
}
for (Long id : idList) {
sysUserDao.updateReceiveSms(id, sysUserReceiveSmsQo.getReceiveSms());
}
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);
}
/**
* 注册系统用户
*/
@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 = "/add2", method = RequestMethod.POST)
public String addUser2(@Validated({SysUserQoInsert.class}) @RequestBody SysUserQo userQo) {
// 验证用户名是否重复
if (!sysUserService.checkUserName(userQo.getUserName())) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "该账号已经注册,请勿重复注册", locale);
}
// 添加新用户
return sysUserService.addUser2(userQo);
}
/**
* 删除系统用户
*/
@RequestMapping(value = "/general/deletesysuser", method = RequestMethod.DELETE)
public String deleteSysUser(@RequestParam(value = "userName") String userName) {
return sysUserService.deleteSysUser(userName);
}
@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.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
List<SysUserEntity> sysUserEntityList = sysUserService.selectAllUser();
List<SysUserVo> sysUserVoList = new ArrayList<>();
for (SysUserEntity sysUser : sysUserEntityList) {
sysUserVoList.add(SysUserConvertUtils.convert(sysUser));
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, sysUserVoList, 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.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
}
// 如果keepType等于1进行启用操作
if ("1".equals(keepType)) {
sysUserService.updateEnable(Long.valueOf(userId));
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "启用成功", locale);
} else {
sysUserService.updateDisable(Long.valueOf(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.obtainUserNameByToken(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
Long userId = (Long) map.get("userId");
String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword");
// 验证token
String tokenUserName = TokenUtil.obtainUserNameByToken(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 {
String optCode = String.valueOf(RandomUtil.returnCode());
redisService.set(phone, optCode, 10, TimeUnit.MINUTES);
SendSmsResponse response = AliYunSmsUtils.sendCode(phone, optCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
/**
* 填写手机号发送验证码,用于用户忘记密码
*/
@GetMapping("/verificationCodeUpdate")
public String verificationCodeUpdate(@RequestParam("phone") String phone) throws Exception {
Specification<SysUserEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
//查询条件集合
List<Predicate> predicateList = new ArrayList<>();
predicateList.add(criteriaBuilder.equal(root.get("userName").as(String.class), phone));
Predicate[] predicateArray = new Predicate[predicateList.size()];
return criteriaBuilder.and(predicateList.toArray(predicateArray));
};
List<SysUserEntity> sysUserEntityList = sysUserDao.findAll(specification, Sort.by(Sort.Direction.DESC, "id"));
if (sysUserEntityList.size() == 0) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "手机号错误,请使用您注册的手机号", locale);
}
SysUserEntity sysUserEntity = sysUserEntityList.get(0);
String optCode = String.valueOf(RandomUtil.returnCode());
redisService.set(phone, optCode, 10, TimeUnit.MINUTES);
SendSmsResponse response = AliYunSmsUtils.sendCode(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);
}
}