Commit 5ae97a31 by chamberone

feat: 添加用户登录和获取用户接口

1 parent baf13f7e
package com.dituhui.pea.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@Data
public class UserLoginDTO {
private String account;
@JsonIgnore
private String password;
private String nickname;
private String email;
private String phone;
private String birthday;
private String region;
private Integer sex;
private String avatar;
private String wechat;
private String token;
}
package com.dituhui.pea.user;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.ThirdPartyEnum;
import com.dituhui.pea.pojo.ThirdUserInfo;
import com.dituhui.pea.pojo.UserInfo;
import com.dituhui.pea.pojo.UserLoginDTO;
import com.dituhui.pea.pojo.WebResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -16,6 +19,12 @@ import org.springframework.web.bind.annotation.RequestParam;
*/
@FeignClient(value = "user")
public interface IUser {
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public Result<?> userLogin(@RequestBody UserLoginDTO user);
@RequestMapping(value = "/user/userInfo", method = RequestMethod.GET)
public Result<?> getUserInfo(@RequestHeader(name="Authorization", required = true) String authorization);
/**
* 获取当前登陆用户信息
......
......@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController;
/**
* 角色控制层
* @author zl
*/
@RestController
@RefreshScope
......
......@@ -2,6 +2,8 @@ package com.dituhui.pea.user.controller;
import cn.hutool.core.util.ObjectUtil;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.RedisKeyGroup;
import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.enums.ThirdPartyEnum;
......@@ -13,13 +15,15 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户控制层
*
* @author zl
* @since 2020-12-09 15:42:00
*/
@RestController
public class UserController implements IUser {
......@@ -29,6 +33,14 @@ public class UserController implements IUser {
@Autowired
private RedisService redisService;
public Result<?> userLogin(UserLoginDTO user) {
return userService.userLogin(user.getAccount(), user.getAccount());
}
public Result<?> getUserInfo(String token) {
return userService.getUserInfo(token);
}
@Override
public WebResult<UserInfo> getCurrentUserInfo(String userToken, Boolean needTeamInfo) {
......
package com.dituhui.pea.user.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
......@@ -15,56 +14,72 @@ import com.dituhui.pea.user.entity.UserEntity;
* @author zl
* @since 2020-12-09 15:34:42
*/
public interface UserDao extends JpaRepository<UserEntity, String>,
JpaSpecificationExecutor<UserEntity>, CrudRepository<UserEntity, String> {
public interface UserDao extends JpaRepository<UserEntity, String>, JpaSpecificationExecutor<UserEntity>,
CrudRepository<UserEntity, String> {
/**
* 根据手机号查询
*
* @param phoneNumber 手机号码
* @return
*/
UserEntity findByPhone(String phone);
/**
* 根据手机号查询
* @param phoneNumber 手机号码
* @return
*/
UserEntity findByPhone(String phone);
/**
* 根据邮箱查询
*
* @param email 邮箱
* @return
*/
UserEntity findByEmail(String email);
/**
* 根据邮箱查询
* @param email 邮箱
* @return
*/
UserEntity findByEmail(String email);
/**
* 根据微信查询
*
* @param wechat 微信
* @return
*/
List<UserEntity> findByWechat(String wechat);
/**
* 根据微信查询
* @param wechat 微信
* @return
*/
List<UserEntity> findByWechat(String wechat);
/**
* 根据微信小程序查询
*
* @param wechatMiniProgram 微信小程序openId
* @return
*/
List<UserEntity> findByWechatMiniProgram(String wechatMiniProgram);
/**
* 根据微信小程序查询
* @param wechatMiniProgram 微信小程序openId
* @return
*/
List<UserEntity> findByWechatMiniProgram(String wechatMiniProgram);
/**
* 根据账号查询
*
* @param account 账号
* @return
*/
UserEntity findByAccount(String account);
/**
* 根据账号查询
* @param account 账号
* @return
*/
UserEntity findByAccount(String account);
/**
* 根据账号查询
*
* @param account 账号
* @param password 加密密码
* @return
*/
UserEntity findByAccountAndPassword(String account, String password);
/**
* 根据账号查询
* @param account 账号
* @return
*/
List<UserEntity> findAllByAccount(String account);
/**
* 根据账号查询
*
* @param account 账号
* @return
*/
List<UserEntity> findAllByAccount(String account);
/**
* 根据用户ID批量查询
* @param userIds 用户ID集合
* @return
*/
List<UserEntity> findByIdIn(List<String> userIds);
/**
* 根据用户ID批量查询
*
* @param userIds 用户ID集合
* @return
*/
List<UserEntity> findByIdIn(List<String> userIds);
}
......@@ -12,11 +12,15 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.enums.RedisKeyGroup;
import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.enums.ThirdPartyEnum;
import com.dituhui.pea.exception.BusinessException;
import com.dituhui.pea.pojo.ThirdUserInfo;
import com.dituhui.pea.pojo.UserInfo;
import com.dituhui.pea.pojo.UserLoginDTO;
import com.dituhui.pea.user.commom.RedisService;
import com.dituhui.pea.user.constant.TextConstant;
import com.dituhui.pea.user.dao.UserDao;
import com.dituhui.pea.user.entity.UserEntity;
......@@ -27,404 +31,440 @@ import com.dituhui.pea.user.utils.TextHelper;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
/**
* 用户业务层
* @author zl
*
*/
@Service
public class UserService {
@Autowired
UserDao userDao;
@Autowired
ThirdStrategyFactory thirdStrategyFactory;
public String getUserName(String userId) {
return "Jown Snow " + userId;
}
/**
* 查询用户信息
* @param id 第三方唯一id
* @param type 第三方类型
* @return
*/
public UserInfo queryUserByThirdParty(String id, ThirdPartyEnum type) {
ThirdStrategy strategy = thirdStrategyFactory.getStrategy(type);
UserEntity userEntity = strategy.getUser(id);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
*
* @param phone 电话
* @return
*/
public UserInfo queryUserByPhone(String phone) {
UserEntity userEntity = userDao.findByPhone(phone);
if (ObjectUtil.isEmpty(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
*
* @param account 账号
* @return
*/
public UserInfo queryTeamByAccount(String account) {
UserEntity userEntity = null;
if (TextHelper.check(account, TextConstant.CHINA_PHONE)) {
//手机号码
userEntity = userDao.findByPhone(account);
} else if (TextHelper.check(account, TextConstant.USERNAME)) {
//用户名
userEntity = userDao.findByAccount(account);
} else if (TextHelper.check(account, TextConstant.EMAIL)) {
//邮箱
userEntity = userDao.findByEmail(account);
}
if (userEntity == null) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
* @param id 用户ID
* @return
*/
public UserInfo queryUserById(String id) {
UserEntity userEntity = userDao.findById(id).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
return BeanUtil.copyProperties(userEntity, UserInfo.class);
}
/**
* 保存用户信息
* @param userInfo
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo saveUser(UserInfo userInfo) {
// 校验用户信息
checkAccount(userInfo);
UserEntity userEntity = BeanUtil.copyProperties(userInfo, UserEntity.class);
// 密码MD5加密
userEntity.setPassword(SecureUtil.md5(userInfo.getPassword()));
// 保存用户信息
userEntity = userDao.save(userEntity);
return BeanUtil.copyProperties(userEntity, UserInfo.class);
}
/**
* 注册用户
* @param userInfo 用户信息
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo register(UserInfo userInfo) {
// 校验 [账号,手机号] 是否有重复
if (checkAccountOrPhone(userInfo.getPhone())) {
throw new BusinessException(StatusCodeEnum.ACCOUNT_EXISTS);
}
// 校验邮箱
if (StringUtils.isNotBlank(userInfo.getEmail())) {
if (checkEmail(userInfo.getEmail(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.EMAIL_EXISTS);
}
}
UserEntity userEntity = BeanUtil.copyProperties(userInfo, UserEntity.class);
userEntity.setCreatedTime(new Date());
userEntity.setCreatedBy(userEntity.getNickname());
// 密码MD5加密
userEntity.setPassword(SecureUtil.md5(userInfo.getPassword()));
// 保存用户信息
userEntity = userDao.save(userEntity);
userInfo.setId(userEntity.getId());
return userInfo;
}
@Transactional(rollbackFor = Exception.class)
public UserInfo thirdRegister(ThirdUserInfo thirdUserInfo) {
// 组装用户信息
UserEntity userEntity = new UserEntity();
userEntity.setAccount(initUserAccount(thirdUserInfo.getThirdType()));
userEntity.setNickname(thirdUserInfo.getThirdName());
userEntity.setAvatar(thirdUserInfo.getThirdAvatar());
userEntity.setCreatedTime(new Date());
ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdUserInfo.getThirdType());
switch (thirdPartyEnum) {
case WECHAT:
userEntity.setWechat(thirdUserInfo.getThirdId());
break;
case WECHAT_MINI_PROGRAM:
userEntity.setWechatMiniProgram(thirdUserInfo.getThirdId());
userEntity.setPhone(thirdUserInfo.getPhone());
default:
}
if (StringUtils.isNotBlank(thirdUserInfo.getPhone())) {
// 查询手机号是否存在, 存在就和第三方账号绑定, 不存在就注册
String thirdId = thirdUserInfo.getThirdId();
String phone = thirdUserInfo.getPhone();
UserInfo user = bindThirdPartyUsers(thirdId, thirdPartyEnum, phone);
if (null != user) {
return user;
}
}
// 保存用户信息
userEntity = userDao.save(userEntity);
// 对象转换
UserInfo userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
userInfo.setId(userEntity.getId());
return userInfo;
}
/**
* 绑定三方用户
* @param thirdId 第三方ID
* @param thirdPartyEnum 第三方类型枚举
* @param phone 手机号
*/
public UserInfo bindThirdPartyUsers(String thirdId, ThirdPartyEnum thirdPartyEnum, String phone) {
UserEntity userEntity = userDao.findByPhone(phone);
if (null == userEntity) {
return null;
}
switch (thirdPartyEnum) {
case WECHAT:
userEntity.setWechat(thirdId);
break;
case WECHAT_MINI_PROGRAM:
userEntity.setWechatMiniProgram(thirdId);
default:
}
userEntity = userDao.save(userEntity);
// 对象转换
UserInfo userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
return userInfo;
}
private String initUserAccount(String thirdType) {
ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdType);
String prefix = "";
switch (thirdPartyEnum) {
case QQ:
prefix = "QQ用户";
break;
case WECHAT:
prefix = "微信用户";
break;
case WEIBO:
prefix = "微博用户";
break;
case WECHAT_MINI_PROGRAM:
prefix = "小程序用户";
default:
}
long count = 600 + userDao.count();
String nickName;
for (int index = 0; index < 6; index++) {
nickName = prefix + (count + index);
UserEntity userEntity = userDao.findByAccount(nickName);
if (ObjectUtil.isNotEmpty(userEntity)) {
continue;
}
return nickName;
}
throw new BusinessException(StatusCodeEnum.FAILED);
}
/**
* 更新用户信息
* @param userInfo
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo updateUser(UserInfo userInfo) {
// 校验用户信息
checkAccount(userInfo);
// 查询用户信息是否存在
UserEntity userEntity = userDao.findById(userInfo.getId()).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
assembleUpdateUserInfo(userInfo, userEntity);
userEntity = userDao.save(userEntity);
userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
return userInfo;
}
/**
* 删除用户信息
* @param userId 用户ID
*/
@Transactional(rollbackFor = Exception.class)
public void deleteUser(String userId){
// 查询用户信息是否存在
UserEntity userEntity = userDao.findById(userId).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
// 删除用户
userDao.deleteById(userId);
}
/**
* 校验 [账号 手机号] 是否存在
* @param phone 手机号
* @return
*/
private Boolean checkAccountOrPhone(String phone) {
List<UserEntity> userEntities = userDao.findAll((Specification<UserEntity>) (root, query, cb) -> {
Path<String> accountField = root.get("account");
Path<String> phoneField = root.get("phone");
Predicate accountCondition = cb.equal(accountField, phone);
Predicate phoneCondition = cb.equal(phoneField, phone);
query.where(cb.or(accountCondition, phoneCondition));
return null;
});
return CollUtil.isNotEmpty(userEntities);
}
/**
* 校验账号
* @param userInfo 用户信息
*/
private void checkAccount(UserInfo userInfo) {
// 校验账号
if (StringUtils.isNotBlank(userInfo.getAccount())) {
if (checkAccount(userInfo.getAccount(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.ACCOUNT_EXISTS);
}
}
// 校验手机号
if (StringUtils.isNotBlank(userInfo.getPhone())) {
// 校验手机格式是否正确
if (!Validator.isMobile(userInfo.getPhone())) {
throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkPhone(userInfo.getPhone(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.MOBILE_NUMBER_EXISTS);
}
}
// 校验邮箱
if (StringUtils.isNotBlank(userInfo.getEmail())) {
// 校验邮箱格式是否正确
if (!Validator.isEmail(userInfo.getEmail())) {
throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkEmail(userInfo.getEmail(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.EMAIL_EXISTS);
}
}
}
/**
* 校验邮箱是否存在
* @param email 邮箱
* @return
*/
private Boolean checkEmail(String email, String id) {
UserEntity userEntity = userDao.findByEmail(email);
if (ObjectUtil.isNotNull(userEntity)){
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 校验账号是否存在
* @param account 账号
* @return
*/
private Boolean checkAccount(String account, String id){
UserEntity userEntity = userDao.findByAccount(account);
if (ObjectUtil.isNotNull(userEntity)){
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 校验手机号是否存在
* @param phone 账号
* @param id 用户Id
* @return
*/
private Boolean checkPhone(String phone, String id){
UserEntity userEntity = userDao.findByPhone(phone);
if (ObjectUtil.isNotNull(userEntity)){
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 组装更新用户信息
* @param formUserInfo
* @param toUserEntity
*/
private void assembleUpdateUserInfo(UserInfo formUserInfo, UserEntity toUserEntity) {
// 更新用户信息
if (StringUtils.isNotBlank(formUserInfo.getAccount())) {
toUserEntity.setAccount(formUserInfo.getAccount());
}
if (StringUtils.isNotBlank(formUserInfo.getPassword())) {
toUserEntity.setPassword(SecureUtil.md5(formUserInfo.getPassword()));
}
if (StringUtils.isNotBlank(formUserInfo.getNickname())) {
toUserEntity.setNickname(formUserInfo.getNickname());
}
if (StringUtils.isNotBlank(formUserInfo.getEmail())) {
toUserEntity.setEmail(formUserInfo.getEmail());
}
if (StringUtils.isNotBlank(formUserInfo.getPhone())) {
toUserEntity.setPhone(formUserInfo.getPhone());
}
if (StringUtils.isNotBlank(formUserInfo.getAvatar())) {
toUserEntity.setAvatar(formUserInfo.getAvatar());
}
if (StringUtils.isNotBlank(formUserInfo.getExtra())) {
toUserEntity.setExtra(formUserInfo.getExtra());
}
if (StringUtils.isNotBlank(formUserInfo.getBirthday())) {
toUserEntity.setBirthday(formUserInfo.getBirthday());
}
if (StringUtils.isNotBlank(formUserInfo.getRegion())) {
toUserEntity.setRegion(formUserInfo.getRegion());
}
toUserEntity.setSex(formUserInfo.getSex());
}
/**
* 失效时间ms
*/
private static final int aliveTime = 7200000;
@Autowired
UserDao userDao;
@Autowired
ThirdStrategyFactory thirdStrategyFactory;
@Autowired
private RedisService redisService;
public Result<?> userLogin(String account, String password) {
UserEntity user = userDao.findByAccountAndPassword(account, SecureUtil.md5(password));
if (null == user) {
// 生成token
UserLoginDTO userDTO = BeanUtil.copyProperties(user, UserLoginDTO.class);
// 生成token
String uuid = IdUtil.simpleUUID();
// 2小时
long timestamp = System.currentTimeMillis() + aliveTime;
redisService.set(RedisKeyGroup.authToken + ":" + uuid, user, timestamp);
userDTO.setToken(uuid);
return Result.success(userDTO);
} else {
return Result.failed("鉴权失败");
}
}
public Result<?> getUserInfo(String token) {
UserEntity user = (UserEntity) redisService.get(RedisKeyGroup.authToken + ":" + token);
return Result.success(user);
}
public String getUserName(String userId) {
return "Jown Snow " + userId;
}
/**
* 查询用户信息
*
* @param id 第三方唯一id
* @param type 第三方类型
* @return
*/
public UserInfo queryUserByThirdParty(String id, ThirdPartyEnum type) {
ThirdStrategy strategy = thirdStrategyFactory.getStrategy(type);
UserEntity userEntity = strategy.getUser(id);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
*
* @param phone 电话
* @return
*/
public UserInfo queryUserByPhone(String phone) {
UserEntity userEntity = userDao.findByPhone(phone);
if (ObjectUtil.isEmpty(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
*
* @param account 账号
* @return
*/
public UserInfo queryTeamByAccount(String account) {
UserEntity userEntity = null;
if (TextHelper.check(account, TextConstant.CHINA_PHONE)) {
// 手机号码
userEntity = userDao.findByPhone(account);
} else if (TextHelper.check(account, TextConstant.USERNAME)) {
// 用户名
userEntity = userDao.findByAccount(account);
} else if (TextHelper.check(account, TextConstant.EMAIL)) {
// 邮箱
userEntity = userDao.findByEmail(account);
}
if (userEntity == null) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
UserInfo user = BeanUtil.copyProperties(userEntity, UserInfo.class);
return user;
}
/**
* 查询用户信息
*
* @param id 用户ID
* @return
*/
public UserInfo queryUserById(String id) {
UserEntity userEntity = userDao.findById(id).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
return BeanUtil.copyProperties(userEntity, UserInfo.class);
}
/**
* 保存用户信息
*
* @param userInfo
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo saveUser(UserInfo userInfo) {
// 校验用户信息
checkAccount(userInfo);
UserEntity userEntity = BeanUtil.copyProperties(userInfo, UserEntity.class);
// 密码MD5加密
userEntity.setPassword(SecureUtil.md5(userInfo.getPassword()));
// 保存用户信息
userEntity = userDao.save(userEntity);
return BeanUtil.copyProperties(userEntity, UserInfo.class);
}
/**
* 注册用户
*
* @param userInfo 用户信息
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo register(UserInfo userInfo) {
// 校验 [账号,手机号] 是否有重复
if (checkAccountOrPhone(userInfo.getPhone())) {
throw new BusinessException(StatusCodeEnum.ACCOUNT_EXISTS);
}
// 校验邮箱
if (StringUtils.isNotBlank(userInfo.getEmail())) {
if (checkEmail(userInfo.getEmail(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.EMAIL_EXISTS);
}
}
UserEntity userEntity = BeanUtil.copyProperties(userInfo, UserEntity.class);
userEntity.setCreatedTime(new Date());
userEntity.setCreatedBy(userEntity.getNickname());
// 密码MD5加密
userEntity.setPassword(SecureUtil.md5(userInfo.getPassword()));
// 保存用户信息
userEntity = userDao.save(userEntity);
userInfo.setId(userEntity.getId());
return userInfo;
}
@Transactional(rollbackFor = Exception.class)
public UserInfo thirdRegister(ThirdUserInfo thirdUserInfo) {
// 组装用户信息
UserEntity userEntity = new UserEntity();
userEntity.setAccount(initUserAccount(thirdUserInfo.getThirdType()));
userEntity.setNickname(thirdUserInfo.getThirdName());
userEntity.setAvatar(thirdUserInfo.getThirdAvatar());
userEntity.setCreatedTime(new Date());
ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdUserInfo.getThirdType());
switch (thirdPartyEnum) {
case WECHAT:
userEntity.setWechat(thirdUserInfo.getThirdId());
break;
case WECHAT_MINI_PROGRAM:
userEntity.setWechatMiniProgram(thirdUserInfo.getThirdId());
userEntity.setPhone(thirdUserInfo.getPhone());
default:
}
if (StringUtils.isNotBlank(thirdUserInfo.getPhone())) {
// 查询手机号是否存在, 存在就和第三方账号绑定, 不存在就注册
String thirdId = thirdUserInfo.getThirdId();
String phone = thirdUserInfo.getPhone();
UserInfo user = bindThirdPartyUsers(thirdId, thirdPartyEnum, phone);
if (null != user) {
return user;
}
}
// 保存用户信息
userEntity = userDao.save(userEntity);
// 对象转换
UserInfo userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
userInfo.setId(userEntity.getId());
return userInfo;
}
/**
* 绑定三方用户
*
* @param thirdId 第三方ID
* @param thirdPartyEnum 第三方类型枚举
* @param phone 手机号
*/
public UserInfo bindThirdPartyUsers(String thirdId, ThirdPartyEnum thirdPartyEnum, String phone) {
UserEntity userEntity = userDao.findByPhone(phone);
if (null == userEntity) {
return null;
}
switch (thirdPartyEnum) {
case WECHAT:
userEntity.setWechat(thirdId);
break;
case WECHAT_MINI_PROGRAM:
userEntity.setWechatMiniProgram(thirdId);
default:
}
userEntity = userDao.save(userEntity);
// 对象转换
UserInfo userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
return userInfo;
}
private String initUserAccount(String thirdType) {
ThirdPartyEnum thirdPartyEnum = ThirdPartyEnum.valueOf(thirdType);
String prefix = "";
switch (thirdPartyEnum) {
case QQ:
prefix = "QQ用户";
break;
case WECHAT:
prefix = "微信用户";
break;
case WEIBO:
prefix = "微博用户";
break;
case WECHAT_MINI_PROGRAM:
prefix = "小程序用户";
default:
}
long count = 600 + userDao.count();
String nickName;
for (int index = 0; index < 6; index++) {
nickName = prefix + (count + index);
UserEntity userEntity = userDao.findByAccount(nickName);
if (ObjectUtil.isNotEmpty(userEntity)) {
continue;
}
return nickName;
}
throw new BusinessException(StatusCodeEnum.FAILED);
}
/**
* 更新用户信息
*
* @param userInfo
* @return
*/
@Transactional(rollbackFor = Exception.class)
public UserInfo updateUser(UserInfo userInfo) {
// 校验用户信息
checkAccount(userInfo);
// 查询用户信息是否存在
UserEntity userEntity = userDao.findById(userInfo.getId()).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
assembleUpdateUserInfo(userInfo, userEntity);
userEntity = userDao.save(userEntity);
userInfo = BeanUtil.copyProperties(userEntity, UserInfo.class);
return userInfo;
}
/**
* 删除用户信息
*
* @param userId 用户ID
*/
@Transactional(rollbackFor = Exception.class)
public void deleteUser(String userId) {
// 查询用户信息是否存在
UserEntity userEntity = userDao.findById(userId).orElse(null);
if (ObjectUtil.isNull(userEntity)) {
throw new BusinessException(StatusCodeEnum.USER_DOES_NOT_EXIST);
}
// 删除用户
userDao.deleteById(userId);
}
/**
* 校验 [账号 手机号] 是否存在
*
* @param phone 手机号
* @return
*/
private Boolean checkAccountOrPhone(String phone) {
List<UserEntity> userEntities = userDao.findAll((Specification<UserEntity>) (root, query, cb) -> {
Path<String> accountField = root.get("account");
Path<String> phoneField = root.get("phone");
Predicate accountCondition = cb.equal(accountField, phone);
Predicate phoneCondition = cb.equal(phoneField, phone);
query.where(cb.or(accountCondition, phoneCondition));
return null;
});
return CollUtil.isNotEmpty(userEntities);
}
/**
* 校验账号
*
* @param userInfo 用户信息
*/
private void checkAccount(UserInfo userInfo) {
// 校验账号
if (StringUtils.isNotBlank(userInfo.getAccount())) {
if (checkAccount(userInfo.getAccount(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.ACCOUNT_EXISTS);
}
}
// 校验手机号
if (StringUtils.isNotBlank(userInfo.getPhone())) {
// 校验手机格式是否正确
if (!Validator.isMobile(userInfo.getPhone())) {
throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkPhone(userInfo.getPhone(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.MOBILE_NUMBER_EXISTS);
}
}
// 校验邮箱
if (StringUtils.isNotBlank(userInfo.getEmail())) {
// 校验邮箱格式是否正确
if (!Validator.isEmail(userInfo.getEmail())) {
throw new BusinessException(StatusCodeEnum.COMMON_PARAM_ERROR);
}
if (checkEmail(userInfo.getEmail(), userInfo.getId())) {
throw new BusinessException(StatusCodeEnum.EMAIL_EXISTS);
}
}
}
/**
* 校验邮箱是否存在
*
* @param email 邮箱
* @return
*/
private Boolean checkEmail(String email, String id) {
UserEntity userEntity = userDao.findByEmail(email);
if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 校验账号是否存在
*
* @param account 账号
* @return
*/
private Boolean checkAccount(String account, String id) {
UserEntity userEntity = userDao.findByAccount(account);
if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 校验手机号是否存在
*
* @param phone 账号
* @param id 用户Id
* @return
*/
private Boolean checkPhone(String phone, String id) {
UserEntity userEntity = userDao.findByPhone(phone);
if (ObjectUtil.isNotNull(userEntity)) {
// 判断用户ID是否一致
return !StringUtils.equals(userEntity.getId(), id);
} else {
return false;
}
}
/**
* 组装更新用户信息
*
* @param formUserInfo
* @param toUserEntity
*/
private void assembleUpdateUserInfo(UserInfo formUserInfo, UserEntity toUserEntity) {
// 更新用户信息
if (StringUtils.isNotBlank(formUserInfo.getAccount())) {
toUserEntity.setAccount(formUserInfo.getAccount());
}
if (StringUtils.isNotBlank(formUserInfo.getPassword())) {
toUserEntity.setPassword(SecureUtil.md5(formUserInfo.getPassword()));
}
if (StringUtils.isNotBlank(formUserInfo.getNickname())) {
toUserEntity.setNickname(formUserInfo.getNickname());
}
if (StringUtils.isNotBlank(formUserInfo.getEmail())) {
toUserEntity.setEmail(formUserInfo.getEmail());
}
if (StringUtils.isNotBlank(formUserInfo.getPhone())) {
toUserEntity.setPhone(formUserInfo.getPhone());
}
if (StringUtils.isNotBlank(formUserInfo.getAvatar())) {
toUserEntity.setAvatar(formUserInfo.getAvatar());
}
if (StringUtils.isNotBlank(formUserInfo.getExtra())) {
toUserEntity.setExtra(formUserInfo.getExtra());
}
if (StringUtils.isNotBlank(formUserInfo.getBirthday())) {
toUserEntity.setBirthday(formUserInfo.getBirthday());
}
if (StringUtils.isNotBlank(formUserInfo.getRegion())) {
toUserEntity.setRegion(formUserInfo.getRegion());
}
toUserEntity.setSex(formUserInfo.getSex());
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!