|
@@ -1,33 +1,59 @@
|
|
|
package com.backendsys.modules.material.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import com.backendsys.exception.CustException;
|
|
|
+import com.backendsys.modules.common.config.redis.utils.RedisUtil;
|
|
|
+import com.backendsys.modules.common.config.security.utils.CaptchaUtil;
|
|
|
+import com.backendsys.modules.common.config.security.utils.LockStatusUtil;
|
|
|
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
import com.backendsys.modules.material.entity.MaterialAudit;
|
|
|
+import com.backendsys.modules.material.entity.MaterialUser;
|
|
|
import com.backendsys.modules.material.service.MaterialUserService;
|
|
|
import com.backendsys.modules.system.dao.SysUserDao;
|
|
|
import com.backendsys.modules.system.dao.SysUserInfoDao;
|
|
|
import com.backendsys.modules.system.dao.SysUserRoleDao;
|
|
|
import com.backendsys.modules.system.dao.SysUserRoleRelationDao;
|
|
|
-import com.backendsys.modules.system.entity.SysUserDTO;
|
|
|
-import com.backendsys.modules.system.entity.SysUserInfo;
|
|
|
-import com.backendsys.modules.system.entity.SysUserRole;
|
|
|
-import com.backendsys.modules.system.entity.SysUserRoleRelation;
|
|
|
+import com.backendsys.modules.system.entity.*;
|
|
|
+import com.backendsys.modules.system.service.SysAuthService;
|
|
|
+import com.backendsys.modules.system.service.SysCommonService;
|
|
|
+import com.backendsys.modules.system.service.SysUserIntegralService;
|
|
|
import com.backendsys.modules.system.service.SysUserService;
|
|
|
import com.backendsys.utils.response.PageEntity;
|
|
|
import com.backendsys.utils.response.PageInfoResult;
|
|
|
import com.backendsys.utils.v2.PageUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class MaterialUserServiceImpl implements MaterialUserService {
|
|
|
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ private String APPLICATION_NAME;
|
|
|
+ @Value("${tencent.sms.debug}")
|
|
|
+ private String SMS_DEBUG;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Environment env;
|
|
|
+ @Autowired
|
|
|
+ private LockStatusUtil lockStatusUtil;
|
|
|
+ @Autowired
|
|
|
+ private CaptchaUtil captchaUtil;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
@Autowired
|
|
|
private SysUserDao sysUserDao;
|
|
|
@Autowired
|
|
@@ -37,8 +63,14 @@ public class MaterialUserServiceImpl implements MaterialUserService {
|
|
|
@Autowired
|
|
|
private SysUserRoleRelationDao sysUserRoleRelationDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysAuthService sysAuthService;
|
|
|
@Autowired
|
|
|
private SysUserService sysUserService;
|
|
|
+ @Autowired
|
|
|
+ private SysCommonService sysCommonService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserIntegralService sysUserIntegralService;
|
|
|
|
|
|
/**
|
|
|
* 获取素材用户列表 ({ invite_code = Material })
|
|
@@ -110,4 +142,82 @@ public class MaterialUserServiceImpl implements MaterialUserService {
|
|
|
return Map.of("user_id", materialAudit.getUser_id());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // 注册素材用户 (手机号注册)
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Map<String, Object> registerMaterialUser(MaterialUser materialUser) {
|
|
|
+
|
|
|
+ // 判断是否允许注册
|
|
|
+ // [系统配置] 是否允许系统用户注册
|
|
|
+ Boolean SYSTEM_USER_ALLOW_REGISTER = Convert.toBool(sysCommonService.getCommonByTag("SYSTEM_USER_ALLOW_REGISTER"));
|
|
|
+ if (!SYSTEM_USER_ALLOW_REGISTER) throw new CustException("系统已禁止注册");
|
|
|
+
|
|
|
+ String phone = materialUser.getPhone();
|
|
|
+ Integer phoneAreaCode = materialUser.getPhone_area_code();
|
|
|
+ Integer phoneValidCode = materialUser.getPhone_valid_code();
|
|
|
+
|
|
|
+ String activeProfile = env.getActiveProfiles()[0];
|
|
|
+
|
|
|
+ //if (!"local".equals(activeProfile)) {
|
|
|
+ // // 判断是否处于登录错误的冻结状态 (2分钟内错误5次,则出现冻结提示)
|
|
|
+ // lockStatusUtil.checkLockStatus(APPLICATION_NAME + "-register-error", phone);
|
|
|
+ // // 判断图形验证码是否正确
|
|
|
+ // if (!captchaUtil.isCaptchaValid(captcha, httpRequestUtil.getKaptchaKey())) {
|
|
|
+ // loginFail("验证码错误", username, false);
|
|
|
+ // return null;
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
+ // 判断短信验证码是否正确
|
|
|
+ if (!"local".equals(activeProfile)) {
|
|
|
+ String redisKey = APPLICATION_NAME + "-sms-register" + "-" + phone;
|
|
|
+ Integer smsCode = redisUtil.getCacheObject(redisKey);
|
|
|
+ // 判断是否发送验证码
|
|
|
+ if ("false".equals(SMS_DEBUG) && smsCode == null) throw new CustException("请先发送短信验证码");
|
|
|
+ // 判断短信验证码是否错误
|
|
|
+ if ("false".equals(SMS_DEBUG) && !smsCode.equals(phoneValidCode)) sysAuthService.loginFail("短信验证码错误", phone, true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // [查询] 判断手机号是否存在 (如果存在就直接登录,返回Token)
|
|
|
+ LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(SysUser::getPhone, phone).eq(SysUser::getPhone_area_code, phoneAreaCode);
|
|
|
+ SysUser sysUserDetail = sysUserDao.selectOne(queryWrapper);
|
|
|
+ if (sysUserDetail != null) {
|
|
|
+ // [已注册] 返回登录信息 (并且7天免登录)
|
|
|
+ SysUserInfo sysUserInfo = sysAuthService.loginSuccess(sysUserDetail.getId(), 1);
|
|
|
+ return BeanUtil.beanToMap(sysUserInfo);
|
|
|
+ } else {
|
|
|
+ // [未注册] 通过校验,并注册
|
|
|
+
|
|
|
+ // 注册
|
|
|
+ SysUserDTO registerEntity = new SysUserDTO();
|
|
|
+ registerEntity.setUsername(phone);
|
|
|
+ registerEntity.setPhone(phone);
|
|
|
+ registerEntity.setPhone_area_code(phoneAreaCode);
|
|
|
+ registerEntity.setInvite_code("Material");
|
|
|
+
|
|
|
+ // 注册时,使用 [素材游客] 权限 (MATERIAL_GUEST)
|
|
|
+ String role_sign = "MATERIAL_GUEST";
|
|
|
+ LambdaQueryWrapper<SysUserRole> wrapperRole = new LambdaQueryWrapper<>();
|
|
|
+ wrapperRole.eq(SysUserRole::getRole_sign, role_sign);
|
|
|
+ SysUserRole roleDetail = sysUserRoleDao.selectOne(wrapperRole);
|
|
|
+ registerEntity.setRole_id(Arrays.asList(roleDetail.getRole_id()));
|
|
|
+
|
|
|
+ // 注册时,审核状态为 待审核 (-1拒绝, 1待审核, 2审核通过)
|
|
|
+ registerEntity.setAudit_status(2);
|
|
|
+ // 随机昵称 (6位)
|
|
|
+ registerEntity.setNickname("用户" + RandomUtil.randomStringUpper(6));
|
|
|
+
|
|
|
+ // 创建用户
|
|
|
+ sysUserDao.insertUser(registerEntity);
|
|
|
+
|
|
|
+ // 初始化用户积分
|
|
|
+ sysUserIntegralService.init(registerEntity.getId());
|
|
|
+
|
|
|
+ return Map.of("user_id", registerEntity.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|