|
@@ -0,0 +1,219 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.rongwei.bscommon.sys.feign.RwCommonServerFeignService;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxRegisterService;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxSupervisionCustomUserManageService;
|
|
|
+import com.rongwei.bscommon.sys.utils.CxConstants;
|
|
|
+import com.rongwei.bscommon.sys.utils.HwSmsUtil;
|
|
|
+import com.rongwei.bscommon.sys.utils.PasswordGenerator;
|
|
|
+import com.rongwei.bscommon.sys.utils.SmsCodeGenerateUtils;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxSupervisionCustomUserManageDo;
|
|
|
+import com.rongwei.commonservice.service.RedisService;
|
|
|
+import com.rongwei.rwadmincommon.system.domain.SysUserDo;
|
|
|
+import com.rongwei.rwadmincommon.system.service.SysUserService;
|
|
|
+import com.rongwei.rwcommon.base.R;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommon.vo.MailDo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ZhcxRegisterServiceImpl implements ZhcxRegisterService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserService sysUserService;
|
|
|
+ @Autowired
|
|
|
+ private ZhcxSupervisionCustomUserManageService manageService;
|
|
|
+ @Autowired
|
|
|
+ private RwCommonServerFeignService rwCommonServerFeignService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R supervisionRegister(JSONObject jsonObject) {
|
|
|
+ try {
|
|
|
+ String jygsid = jsonObject.getString("jygsid");
|
|
|
+ String name = jsonObject.getString("name");
|
|
|
+ String account = jsonObject.getString("account");
|
|
|
+ String email = jsonObject.getString("email");
|
|
|
+ String mobile = jsonObject.getString("mobile");
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
+ Object yzmObj = redisService.getRedisCatchObj("register_"+mobile);
|
|
|
+ if(ObjectUtil.isEmpty(yzmObj)){
|
|
|
+ return R.error("未获取到验证码或验证码已过期,请获取验证码");
|
|
|
+ }
|
|
|
+ String yzm = (String) yzmObj;
|
|
|
+ if(code.equals(yzm)){
|
|
|
+ LambdaQueryWrapper<SysUserDo> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.and(i -> i.eq(SysUserDo::getAccount, account).
|
|
|
+ or().eq(SysUserDo::getMobile, mobile).
|
|
|
+ or().eq(SysUserDo::getEmail, email));
|
|
|
+ List<SysUserDo> list = sysUserService.list(queryWrapper);
|
|
|
+ if(list.size() > 0){
|
|
|
+ return R.error("账号,手机号,邮箱已存在,请联系管理员");
|
|
|
+ }else{
|
|
|
+ SysUserDo sysUserDo = new SysUserDo();
|
|
|
+ ZhcxSupervisionCustomUserManageDo ManageDo = new ZhcxSupervisionCustomUserManageDo();
|
|
|
+ String uuid = SecurityUtil.getUUID();
|
|
|
+ sysUserDo.setId(uuid);
|
|
|
+ sysUserDo.setAccount(account);
|
|
|
+ sysUserDo.setMobile(mobile);
|
|
|
+ sysUserDo.setName(name);
|
|
|
+ sysUserDo.setEmail(email);
|
|
|
+ sysUserDo.setEnabled("1");
|
|
|
+ sysUserService.save(sysUserDo);
|
|
|
+ ManageDo.setId(SecurityUtil.getUUID());
|
|
|
+ ManageDo.setUserid(uuid);
|
|
|
+ ManageDo.setAccount(account);
|
|
|
+ ManageDo.setPhone(mobile);
|
|
|
+ ManageDo.setUserName(name);
|
|
|
+ ManageDo.setEmail(email);
|
|
|
+ ManageDo.setPid(jygsid);
|
|
|
+ ManageDo.setStatus("待审核");
|
|
|
+ manageService.save(ManageDo);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return R.error("验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ redisService.getRedisTemplate().delete(CxConstants.REGISTER_PREFIX+mobile);
|
|
|
+ sendMail();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("监理注册审核邮件发送失败");
|
|
|
+ }
|
|
|
+ return R.ok("请等待后台管理员审核");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+
|
|
|
+ private R sendMail() {
|
|
|
+ List<SysUserDo> receiveUserList = manageService.getUserByRoleCode("role088");
|
|
|
+ List<String> userEmail = receiveUserList.stream().map(SysUserDo::getEmail).filter(StringUtils::isNotBlank).collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (userEmail.isEmpty()) {
|
|
|
+ log.error("收件人邮箱为空:{}", userEmail);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ Set<String> set = new HashSet<>(userEmail);
|
|
|
+ userEmail.clear();
|
|
|
+ userEmail.addAll(set);
|
|
|
+ log.debug("开始发送监理账号审核接收人邮件", userEmail);
|
|
|
+ MailDo mailDo = new MailDo();
|
|
|
+ mailDo.setReceiveEmail(userEmail.toArray(new String[userEmail.size()]));
|
|
|
+ mailDo.setSubject("监理账号审核");
|
|
|
+ mailDo.setCcEmail(new String[]{});
|
|
|
+ mailDo.setNeedTransReceive(false);
|
|
|
+ mailDo.setContent(CxConstants.CHECK_MAIL_CONTENT);
|
|
|
+ rwCommonServerFeignService.sendTextMail(mailDo);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R sendCode(Map<String, Object> map) {
|
|
|
+ Object mobileObj = map.get("mobile");
|
|
|
+ if(ObjectUtil.isEmpty(mobileObj)){
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+ String mobile = (String) mobileObj;
|
|
|
+ boolean exists = redisService.hasKey(CxConstants.REGISTER_PREFIX+mobile);
|
|
|
+ String sendCodeTimeObj = (String) redisService.getRedisCatchObj("registerCount_"+mobile);
|
|
|
+ int sendCodeTime = StringUtils.isBlank(sendCodeTimeObj) ? 0 : Integer.parseInt(sendCodeTimeObj);
|
|
|
+ if (exists && sendCodeTime >= CxConstants.MAX_SEND_CODE_TIME) {
|
|
|
+ return R.error("501", "验证码发送频繁,请过5分钟后再尝试");
|
|
|
+ }
|
|
|
+ String code = SmsCodeGenerateUtils.getInstance().generateVerificationCode();
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = HwSmsUtil.sendSms(null,null, code.toString(), mobile);
|
|
|
+ String returnCode = jsonObject.getString("code");
|
|
|
+ if(CxConstants.SUCCESS_CODE.equals(returnCode)){
|
|
|
+ if(!exists){
|
|
|
+ redisService.redisCatchInit(CxConstants.REGISTER_COUNT_PREFIX+mobile,"0",CxConstants.SEND_CODE_INTERVAL);
|
|
|
+ }
|
|
|
+ sendCodeTime += 1;
|
|
|
+ redisService.redisCatchInit(CxConstants.REGISTER_COUNT_PREFIX+mobile,String.valueOf(sendCodeTime),CxConstants.SEND_CODE_INTERVAL);
|
|
|
+ redisService.redisCatchInit(CxConstants.REGISTER_PREFIX+mobile,code,CxConstants.VERIFICATION_CODE_EXPIRATION);
|
|
|
+ return R.ok("发送成功");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("发送验证码失败", e);
|
|
|
+ }
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
|
|
|
+ public R checkSupervisionUser(List<ZhcxSupervisionCustomUserManageDo> list) {
|
|
|
+ try {
|
|
|
+ updateStatus(list);
|
|
|
+ List<String> userIdList = list.stream().map(ZhcxSupervisionCustomUserManageDo::getUserid).collect(Collectors.toList());
|
|
|
+ updateUserPasswordAndSendSms(userIdList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理监理注册用户更新发送信息异常", e);
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 更新用户密码并发送短信通知
|
|
|
+ *
|
|
|
+ * @param userIdList 需要更新密码和发送短信的用户ID列表
|
|
|
+ * 该方法首先根据提供的用户ID列表查询用户信息,然后为每个用户生成新密码,更新密码及启用状态,并尝试发送包含新密码的短信通知。
|
|
|
+ */
|
|
|
+ private void updateUserPasswordAndSendSms(List<String> userIdList) {
|
|
|
+ LambdaQueryWrapper<SysUserDo> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.and(i -> i.in(SysUserDo::getId, userIdList));
|
|
|
+ List<SysUserDo> userList = sysUserService.list(queryWrapper);
|
|
|
+ userList.forEach((item)->{
|
|
|
+ String pwd = PasswordGenerator.generatePassword(12);
|
|
|
+ String saltPwd = SecurityUtil.getSaltMd5AndSha(pwd, item.getId());
|
|
|
+ item.setPassword(saltPwd);
|
|
|
+ item.setEnabled("0");
|
|
|
+ sysUserService.updateById(item);
|
|
|
+ sendSmsAsync(item);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendSmsAsync(SysUserDo item) {
|
|
|
+ new Thread(() -> {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = HwSmsUtil.sendSms("7133b7ce87a84896a143b02edf5a4757", "8823102504763", item.getAccount() + "," + "123", item.getMobile());
|
|
|
+ String returnCode = jsonObject.getString("code");
|
|
|
+ if(CxConstants.SUCCESS_CODE.equals(returnCode)){
|
|
|
+ log.info("发送用户注册信息成功");
|
|
|
+ }else {
|
|
|
+ log.info("发送用户注册信息失败: 返回码 {}", returnCode);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("发送短信异常", e);
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户状态
|
|
|
+ *
|
|
|
+ * @param list 包含待更新状态的用户信息列表
|
|
|
+ * 此方法遍历列表中的每个用户,将状态为"待审核"的用户状态更新为"正常",然后批量通过manageService更新到数据库中。
|
|
|
+ */
|
|
|
+ private void updateStatus(List<ZhcxSupervisionCustomUserManageDo> list) {
|
|
|
+ list.forEach((item)->{
|
|
|
+ if ("待审核".equals(item.getStatus())) {
|
|
|
+ item.setStatus("正常");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ manageService.updateBatchById(list);
|
|
|
+ }
|
|
|
+}
|