122 lines
3.7 KiB
Java
122 lines
3.7 KiB
Java
|
|
package com.newyetai.usercenter.user.util;
|
||
|
|
|
||
|
|
import org.apache.commons.lang3.StringUtils;
|
||
|
|
|
||
|
|
import com.newyetai.common.constant.CommonConstants;
|
||
|
|
import com.newyetai.common.exception.util.Assert;
|
||
|
|
import com.newyetai.common.util.EnvironmentUtils;
|
||
|
|
import com.newyetai.common.util.RandomUtils;
|
||
|
|
import com.newyetai.common.util.UserContextHolder;
|
||
|
|
import com.newyetai.security.util.PasswordUtils;
|
||
|
|
import com.newyetai.usercenter.user.constant.UserStatusEnum;
|
||
|
|
import com.newyetai.usercenter.user.constant.UserTypeEnum;
|
||
|
|
import com.newyetai.usercenter.user.dto.IUserPhoneCodeSms;
|
||
|
|
import com.newyetai.usercenter.user.entity.User;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author sr on 2021-05-24
|
||
|
|
*/
|
||
|
|
public class UserUtils {
|
||
|
|
|
||
|
|
public static final int NICKNAME_LENGTH = 12;
|
||
|
|
|
||
|
|
public static final String DEFAULT_AVATAR_KEY = "user.default.avatars";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置创建用户初始状态数据 (账号随机生成/非超管/非实名/待激活/已启用-登录相关)
|
||
|
|
*
|
||
|
|
* @param user
|
||
|
|
* @author sr on 2021-06-10
|
||
|
|
*/
|
||
|
|
public static void setInitToCreateUser(User user) {
|
||
|
|
user.setUsername(RandomUtils.generateUUID());
|
||
|
|
String nickname = user.getNickname();
|
||
|
|
user.setNickname(dealNickname(nickname, user.getPhone()));
|
||
|
|
user.setAvatar(dealAvatar(user.getAvatar()));
|
||
|
|
user.setSupeiror(Boolean.FALSE);
|
||
|
|
user.setAuthenticated(Boolean.FALSE);
|
||
|
|
user.setStatus(UserStatusEnum.INIT.getStatus());
|
||
|
|
user.setEnabled(Boolean.TRUE);
|
||
|
|
user.setAccountNonExpired(Boolean.TRUE);
|
||
|
|
user.setAccountNonLocked(Boolean.TRUE);
|
||
|
|
user.setCredentialsNonExpired(Boolean.TRUE);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @description: 初始化账号密码
|
||
|
|
* @return:
|
||
|
|
* @author: guixinman
|
||
|
|
* @param:
|
||
|
|
*/
|
||
|
|
public static void setInitUserNameAndPasswordToCreateUser(User user, Boolean administrator) {
|
||
|
|
//创建用户的时候就给手机号绑定状态改为已绑定
|
||
|
|
user.setPhoneBinding(true);
|
||
|
|
if (Boolean.TRUE.equals(administrator)) {
|
||
|
|
user.setUsername("lgsc"+user.getPhone());
|
||
|
|
user.setPassword(PasswordUtils.encode("lgsc123567"));
|
||
|
|
}else {
|
||
|
|
user.setUsername("ou"+user.getPhone());
|
||
|
|
user.setPassword(PasswordUtils.encode("ou123567"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String dealNickname(String nickname, String phone) {
|
||
|
|
if (StringUtils.isNotBlank(nickname)) {
|
||
|
|
return nickname;
|
||
|
|
}
|
||
|
|
if (StringUtils.isNotBlank(phone)) {
|
||
|
|
return "用户" + StringUtils.right(phone, 4);
|
||
|
|
}
|
||
|
|
return "用户" + RandomUtils.nextSixInt();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String dealAvatar(String avatar) {
|
||
|
|
if (StringUtils.isNotBlank(avatar)) {
|
||
|
|
return avatar;
|
||
|
|
}
|
||
|
|
String avatarStr = EnvironmentUtils.getProperty(DEFAULT_AVATAR_KEY);
|
||
|
|
if (StringUtils.isBlank(avatarStr)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
String[] avatars = avatarStr.split(CommonConstants.COMMA);
|
||
|
|
return avatars[org.apache.commons.lang3.RandomUtils.nextInt(0, avatars.length)];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 校验是否api用户
|
||
|
|
*
|
||
|
|
* @author sr on 2021-08-16
|
||
|
|
*/
|
||
|
|
public static void checkApiUser() {
|
||
|
|
checkApiUser(UserContextHolder.getUserType());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 校验是否api用户
|
||
|
|
*
|
||
|
|
* @param userType
|
||
|
|
* @author sr on 2021-08-16
|
||
|
|
*/
|
||
|
|
public static void checkApiUser(Integer userType) {
|
||
|
|
Assert.isTrue(!UserTypeEnum.API_ORG.getType().equals(userType),
|
||
|
|
"用户类型异常");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 带有用户类型对象获取用户类型(优先取入参)
|
||
|
|
*
|
||
|
|
* @param iUserType
|
||
|
|
* @return
|
||
|
|
* @author sr on 2021-09-02
|
||
|
|
*/
|
||
|
|
public static UserTypeEnum getUserType(IUserPhoneCodeSms iUserType) {
|
||
|
|
Integer userType = iUserType.getUserType();
|
||
|
|
if (userType == null) {
|
||
|
|
userType = UserContextHolder.getUserType();
|
||
|
|
}
|
||
|
|
Assert.notNull(userType, "无法获取验证码,用户类型不能为空");
|
||
|
|
return UserTypeEnum.one(userType);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|