|
|
@@ -16,11 +16,11 @@ import com.xjrsoft.common.constant.GlobalConstant;
|
|
|
import com.xjrsoft.common.enums.EnabledMark;
|
|
|
import com.xjrsoft.common.enums.RoleEnum;
|
|
|
import com.xjrsoft.common.exception.MyException;
|
|
|
-import com.xjrsoft.common.model.result.RT;
|
|
|
import com.xjrsoft.common.utils.FixedArithmeticCaptcha;
|
|
|
import com.xjrsoft.common.utils.RSAUtil;
|
|
|
import com.xjrsoft.common.utils.RedisUtil;
|
|
|
import com.xjrsoft.common.utils.WeChatUtil;
|
|
|
+import com.xjrsoft.config.CommonPropertiesConfig;
|
|
|
import com.xjrsoft.config.LicenseConfig;
|
|
|
import com.xjrsoft.module.organization.entity.Department;
|
|
|
import com.xjrsoft.module.organization.entity.Post;
|
|
|
@@ -52,6 +52,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -82,6 +83,8 @@ public class LoginServiceImpl implements ILoginService {
|
|
|
|
|
|
private final IFileService fileService;
|
|
|
|
|
|
+ private final CommonPropertiesConfig propertiesConfig;
|
|
|
+
|
|
|
@Override
|
|
|
public LoginVo login(LoginDto dto) throws Exception {
|
|
|
if (licenseConfig.getEnabled()) {
|
|
|
@@ -227,6 +230,83 @@ public class LoginServiceImpl implements ILoginService {
|
|
|
return Boolean.TRUE;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public LoginVo findUserByCode(LoginByCodeDto dto) throws Exception {
|
|
|
+ WeChatUserInfo weChatUserInfo;
|
|
|
+ if (dto.getType() == 0) {
|
|
|
+ weChatUserInfo = weChatUtil.getMpOpenid(dto.getCode());
|
|
|
+ }else{
|
|
|
+ weChatUserInfo = weChatUtil.getOpenid(dto.getCode());
|
|
|
+ }
|
|
|
+ LoginByCodeVo result = new LoginByCodeVo(){{
|
|
|
+ setOpenId(weChatUserInfo.getOpenid());
|
|
|
+ setUnionId(weChatUserInfo.getUnionid());
|
|
|
+ }};
|
|
|
+ if (weChatUserInfo == null) throw new MyException("code无效");
|
|
|
+
|
|
|
+ LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(User::getUnionId, weChatUserInfo.getUnionid());
|
|
|
+
|
|
|
+ User loginUser = userService.getOne(queryWrapper);
|
|
|
+ if (loginUser == null) {//如果未找到用户,需要创建一个游客账号
|
|
|
+ User user = new User();
|
|
|
+ user.setUnionId(weChatUserInfo.getUnionid());
|
|
|
+ user.setOpenId(weChatUserInfo.getOpenid());
|
|
|
+ user.setName("游客");
|
|
|
+ user.setUserName(getUserName());
|
|
|
+ user.setCode(weChatUserInfo.getOpenid());
|
|
|
+ //密码加密加盐存储到数据库
|
|
|
+ user.setPassword(BCrypt.hashpw(propertiesConfig.getDefaultPassword(), BCrypt.gensalt()));
|
|
|
+ userService.save(user);
|
|
|
+ //给用户增加游客角色
|
|
|
+ UserRoleRelation userRoleRelation = new UserRoleRelation();
|
|
|
+ userRoleRelation.setUserId(user.getId());
|
|
|
+ userRoleRelation.setRoleId(5L);
|
|
|
+ userRoleRelationMapper.insert(userRoleRelation);
|
|
|
+
|
|
|
+ //给用户增加部门
|
|
|
+ UserDeptRelation userDeptRelation = new UserDeptRelation();
|
|
|
+ userDeptRelation.setUserId(user.getId());
|
|
|
+ userDeptRelation.setDeptId(1684107782861680000L);
|
|
|
+ userDeptRelationService.save(userDeptRelation);
|
|
|
+
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ List<User> list = userService.list();
|
|
|
+ redisUtil.set(GlobalConstant.USER_CACHE_KEY, list);
|
|
|
+
|
|
|
+ List<UserDeptRelation> deptRelationList = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class));
|
|
|
+ redisUtil.set(GlobalConstant.USER_DEPT_RELATION_CACHE_KEY, deptRelationList);
|
|
|
+
|
|
|
+ List<UserRoleRelation> roleRelationList = userRoleRelationMapper.selectList(Wrappers.lambdaQuery(UserRoleRelation.class));
|
|
|
+ redisUtil.set(GlobalConstant.USER_ROLE_RELATION_CACHE_KEY, roleRelationList);
|
|
|
+ });
|
|
|
+ loginUser = user;
|
|
|
+ }
|
|
|
+ LoginVo loginVo = getLoginInfo(loginUser, "WX-MP");
|
|
|
+ result.setToken(loginVo.getToken());
|
|
|
+ result.setUserType(loginVo.getUserType());
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getUserName(){
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String temp = "0123456789abcdefghjkmnpqrstuvwxyz";
|
|
|
+
|
|
|
+ for(int i=0; i < 6; i++){
|
|
|
+ int intVal = (int)(Math.random() * temp.length());
|
|
|
+ sb.append(temp.charAt(intVal));
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(User::getUserName,sb.toString());
|
|
|
+ long count = userService.count(queryWrapper);
|
|
|
+ if(count > 0){
|
|
|
+ return getUserName();
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
private LoginVo getLoginInfo(User loginUser, String loginType) throws Exception {
|
|
|
LoginVo result = new LoginVo();
|
|
|
if (loginUser.getEnabledMark() == EnabledMark.DISABLED.getCode()) {
|