|
|
|
@ -1,21 +1,34 @@
@@ -1,21 +1,34 @@
|
|
|
|
|
package com.cloud.kicc.system.service.impl; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
|
import cn.hutool.core.collection.ListUtil; |
|
|
|
|
import cn.hutool.core.lang.Validator; |
|
|
|
|
import cn.hutool.core.text.CharSequenceUtil; |
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
|
import cn.hutool.core.util.RandomUtil; |
|
|
|
|
import cn.javaer.aliyun.sms.SmsClient; |
|
|
|
|
import cn.javaer.aliyun.sms.SmsTemplate; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.cloud.kicc.common.core.api.R; |
|
|
|
|
import com.cloud.kicc.common.core.constant.CacheConstants; |
|
|
|
|
import com.cloud.kicc.common.core.constant.SecurityConstants; |
|
|
|
|
import com.cloud.kicc.common.data.entity.KiccUser; |
|
|
|
|
import com.cloud.kicc.system.service.AppService; |
|
|
|
|
import com.cloud.kicc.system.service.UserService; |
|
|
|
|
import com.cloud.kicc.common.core.exception.CheckedException; |
|
|
|
|
import com.cloud.kicc.common.core.exception.ValidateCodeException; |
|
|
|
|
import com.cloud.kicc.common.data.entity.SsoUser; |
|
|
|
|
import com.cloud.kicc.system.api.entity.Dept; |
|
|
|
|
import com.cloud.kicc.system.api.entity.User; |
|
|
|
|
import com.cloud.kicc.system.api.entity.UserRole; |
|
|
|
|
import com.cloud.kicc.system.service.*; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
*<p> |
|
|
|
@ -31,10 +44,12 @@ import java.util.concurrent.TimeUnit;
@@ -31,10 +44,12 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
public class AppServiceImpl implements AppService { |
|
|
|
|
|
|
|
|
|
private final RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
|
|
private final UserService userService; |
|
|
|
|
|
|
|
|
|
private final SmsClient smsClient; |
|
|
|
|
private final UserService userService; |
|
|
|
|
private final ConfigService configService; |
|
|
|
|
private final DeptService deptService; |
|
|
|
|
private final UserRoleService userRoleService; |
|
|
|
|
private final ISsoUserService iSsoUserService; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 发送手机验证码 |
|
|
|
@ -44,28 +59,79 @@ public class AppServiceImpl implements AppService {
@@ -44,28 +59,79 @@ public class AppServiceImpl implements AppService {
|
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public R<Boolean> sendSmsCode(String phone) { |
|
|
|
|
List<KiccUser> user = userService.getUserByPhone(phone); |
|
|
|
|
|
|
|
|
|
if (CollUtil.isEmpty(user)) { |
|
|
|
|
log.info("手机号未注册:{}", phone); |
|
|
|
|
return R.ok(Boolean.FALSE, "手机号未注册"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Object codeObj = redisTemplate.opsForValue().get(CacheConstants.VERIFICATION_CODE + phone); |
|
|
|
|
|
|
|
|
|
if (codeObj != null) { |
|
|
|
|
log.info("手机号验证码未过期:{},{}", phone, codeObj); |
|
|
|
|
return R.ok(Boolean.FALSE, "验证码发送过频繁"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.PHONE_CODE_SIZE)); |
|
|
|
|
log.info("手机号生成验证码成功:{},{}", phone, code); |
|
|
|
|
redisTemplate.opsForValue().set(CacheConstants.VERIFICATION_CODE + phone, code, SecurityConstants.CODE_TIME, |
|
|
|
|
TimeUnit.SECONDS); |
|
|
|
|
|
|
|
|
|
// 调用短信通道发送
|
|
|
|
|
this.smsClient.sendVerificationCode("ali-code",phone); |
|
|
|
|
redisTemplate.opsForValue().set(CacheConstants.VERIFICATION_CODE + phone, code, SecurityConstants.CODE_TIME, TimeUnit.SECONDS); |
|
|
|
|
smsClient.send(SmsTemplate.builder() |
|
|
|
|
.templateCode(code) |
|
|
|
|
.phoneNumbers(ListUtil.of(phone)) |
|
|
|
|
.signName("长沙康来") |
|
|
|
|
.build()); |
|
|
|
|
return R.ok(Boolean.TRUE, code); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public SsoUser phoneRegister(String phone, String captcha, String identityProvider) { |
|
|
|
|
// 设置不区分大小写,全部以小写验证
|
|
|
|
|
Validator.validateMobile(phone, "手机号码不合法"); |
|
|
|
|
String code = captcha.toLowerCase(); |
|
|
|
|
if (CharSequenceUtil.isBlank(code)) { |
|
|
|
|
throw new ValidateCodeException("验证码不能为空"); |
|
|
|
|
} |
|
|
|
|
String key = CacheConstants.VERIFICATION_CODE + phone; |
|
|
|
|
Object codeObj = redisTemplate.opsForValue().get(key); |
|
|
|
|
redisTemplate.delete(key); |
|
|
|
|
if (ObjectUtil.isEmpty(codeObj) || !code.equals(codeObj)) { |
|
|
|
|
throw new ValidateCodeException("验证码不合法"); |
|
|
|
|
} |
|
|
|
|
if (iSsoUserService.count(Wrappers.<SsoUser>lambdaQuery().eq(SsoUser::getPhone, phone)) > 0) { |
|
|
|
|
throw new ValidateCodeException("当前用户已经注册,不能重复注册"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 进行注册用户
|
|
|
|
|
String initDeptId = configService.getValueByKey("app.init.deptId"); |
|
|
|
|
Dept dept = deptService.getById(initDeptId); |
|
|
|
|
Optional.ofNullable(dept).orElseThrow(() -> new CheckedException("当前部门无效请重新在参数管理中配置!")); |
|
|
|
|
String initUserType = configService.getValueByKey("app.init.deptId"); |
|
|
|
|
String initTenantId = configService.getValueByKey("app.init.tenantId"); |
|
|
|
|
String initRoleIds = configService.getValueByKey("app.init.roleIds"); |
|
|
|
|
String initPassword = configService.getValueByKey("appid.password"); |
|
|
|
|
|
|
|
|
|
SsoUser ssoUser = new SsoUser(); |
|
|
|
|
ssoUser.setUserName(phone); |
|
|
|
|
ssoUser.setNickName(phone); |
|
|
|
|
ssoUser.setPassword(new BCryptPasswordEncoder().encode(initPassword)); |
|
|
|
|
ssoUser.setPhone(phone); |
|
|
|
|
ssoUser.setIdentityProvider(identityProvider); |
|
|
|
|
iSsoUserService.save(ssoUser); |
|
|
|
|
|
|
|
|
|
User user = new User(); |
|
|
|
|
user.setCasUserId(ssoUser.getId()); |
|
|
|
|
user.setDeptId(dept.getDeptId()); |
|
|
|
|
user.setDeptName(dept.getName()); |
|
|
|
|
user.setUserType(initUserType); |
|
|
|
|
user.setTenantId(initTenantId); |
|
|
|
|
userService.save(user); |
|
|
|
|
|
|
|
|
|
// 处理角色授权
|
|
|
|
|
String[] roles = initRoleIds.split(","); |
|
|
|
|
userRoleService.saveBatch(Arrays.stream(roles).map(roleId -> { |
|
|
|
|
UserRole ur = new UserRole(); |
|
|
|
|
ur.setUserId(user.getId()); |
|
|
|
|
ur.setRoleId(roleId); |
|
|
|
|
ur.setTenantId(initTenantId); |
|
|
|
|
return ur; |
|
|
|
|
}).collect(Collectors.toList())); |
|
|
|
|
// 处理原密码给到APP自动一键登陆
|
|
|
|
|
ssoUser.setPassword(initPassword); |
|
|
|
|
return ssoUser; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|