|
@@ -0,0 +1,204 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.dto.UserDTO;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysRole;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.vo.UserVO;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.SysUserMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.MobileService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.util.CnbgSendSmsUtils;
|
|
|
+import com.qunzhixinxi.hnqz.admin.util.SendSms;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.constant.CacheConstants;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.constant.SecurityConstants;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.constant.enums.LoginTypeEnum;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.util.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 中生短信服务
|
|
|
+ *
|
|
|
+ * @author jimmy
|
|
|
+ * @version 1.0.0
|
|
|
+ * @date 2024/05/24 12:02
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service("cnbgMobileService")
|
|
|
+@AllArgsConstructor
|
|
|
+public class CnbgMobileServiceImpl implements MobileService {
|
|
|
+
|
|
|
+ private final RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ private final SysUserMapper userMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送手机验证码
|
|
|
+ *
|
|
|
+ * @param mobile mobile
|
|
|
+ * @return code
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<Boolean> sendSmsCode(String mobile) {
|
|
|
+ UserDTO userDTO = new UserDTO();
|
|
|
+ userDTO.setPlatId(mobile);
|
|
|
+ List<UserVO> userListNew = userMapper.getUserVoByPlatId(userDTO);
|
|
|
+ boolean sendFlag = false;
|
|
|
+ String tempCode = null;
|
|
|
+ List<UserVO> userList = new ArrayList<>();
|
|
|
+ if (CollUtil.isNotEmpty(userListNew)) {
|
|
|
+ for (UserVO userVO : userListNew) {
|
|
|
+ Set<Integer> collect = userVO.getRoleList().stream().map(SysRole::getRoleId).collect(Collectors.toSet());
|
|
|
+ if (CollUtil.isNotEmpty(userVO.getRoleList())) {
|
|
|
+ boolean isLevel2 = false;
|
|
|
+ boolean isLevel3 = false;
|
|
|
+ boolean isLevel4 = false;
|
|
|
+ boolean isLevel5 = false;
|
|
|
+ boolean isLevel6 = false;
|
|
|
+ boolean isLevel10 = false;
|
|
|
+ boolean isLevel13 = false;
|
|
|
+ boolean isLevel31 = false;
|
|
|
+
|
|
|
+ for (SysRole sysRole : userVO.getRoleList()) {
|
|
|
+ if (sysRole.getRoleId() == 2) {
|
|
|
+ isLevel2 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 3) {
|
|
|
+ isLevel3 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 4) {
|
|
|
+ isLevel4 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 5) {
|
|
|
+ isLevel5 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 6) {
|
|
|
+ isLevel6 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 10) {
|
|
|
+ isLevel10 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 13) {
|
|
|
+ // 患者教育-HCP角色
|
|
|
+ isLevel13 = true;
|
|
|
+ } else if (sysRole.getRoleId() == 31) {
|
|
|
+ // 招商经理角色
|
|
|
+ isLevel31 = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isLevel5 || isLevel6 || isLevel10 || isLevel13 || isLevel31) {
|
|
|
+ userList.add(userVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!sendFlag && StringUtils.isNotEmpty(userVO.getTemp4())) {
|
|
|
+ if (StrUtil.isNotBlank(userVO.getTemp4())) {
|
|
|
+ sendFlag = true;
|
|
|
+ tempCode = userVO.getTemp4();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ log.info("请联系管理员为您开通账号:{}", mobile);
|
|
|
+ return R.ok(Boolean.FALSE, "请联系管理员为您开通账号");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(userList)) {
|
|
|
+ log.info("请联系管理员为您开通账号:{}", mobile);
|
|
|
+ return R.ok(Boolean.FALSE, "请联系管理员为您开通账号");
|
|
|
+ }
|
|
|
+
|
|
|
+ Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile);
|
|
|
+
|
|
|
+ if (codeObj != null) {
|
|
|
+ log.info("手机号验证码未过期:{},{}", mobile, codeObj);
|
|
|
+ return R.ok(Boolean.FALSE, "验证码发送过频繁");
|
|
|
+ }
|
|
|
+
|
|
|
+ String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
|
|
|
+
|
|
|
+ if (sendFlag) {
|
|
|
+ code = "1".equals(tempCode) ? "5657" : tempCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!sendFlag) {
|
|
|
+ CnbgSendSmsUtils.sendSms(userListNew.get(0).getRealname(), mobile, code, "5");
|
|
|
+ }
|
|
|
+ log.debug("手机号生成验证码成功:{},{}", mobile, code);
|
|
|
+ redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile, code, SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
|
|
|
+ return R.ok(Boolean.TRUE, "验证码发送成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送手机验证码 TODO: 调用短信网关发送验证码,测试返回前端
|
|
|
+ *
|
|
|
+ * @param mobile mobile
|
|
|
+ * @return code
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<Boolean> sendSmsCodePc(String mobile) {
|
|
|
+ UserDTO userDTO = new UserDTO();
|
|
|
+ userDTO.setPlatId(mobile);
|
|
|
+ List<UserVO> userListNew = userMapper.getUserVoByPlatId(userDTO);
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(userListNew)) {
|
|
|
+ for (UserVO userVO : userListNew) {
|
|
|
+ Set<Integer> roleList = userVO.getRoleList().stream().map(SysRole::getRoleId).collect(Collectors.toSet());
|
|
|
+ if (roleList.contains(5) || roleList.contains(6)) {
|
|
|
+ log.info("服务商业务员不能登录管理端:{}", mobile);
|
|
|
+ return R.ok(Boolean.FALSE, "服务商业务员不能登录管理端");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("请联系管理员为您开通账号:{}", mobile);
|
|
|
+ return R.ok(Boolean.FALSE, "请联系管理员为您开通账号");
|
|
|
+ }
|
|
|
+
|
|
|
+ Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile);
|
|
|
+
|
|
|
+ if (codeObj != null) {
|
|
|
+ log.info("手机号验证码未过期:{},{}", mobile, codeObj);
|
|
|
+ return R.ok(Boolean.FALSE, "验证码发送过频繁");
|
|
|
+ }
|
|
|
+
|
|
|
+ String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
|
|
|
+ CnbgSendSmsUtils.sendSms(userListNew.get(0).getRealname(), mobile, code, "5");
|
|
|
+ log.debug("手机号生成验证码成功:{},{}", mobile, code);
|
|
|
+ redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile, code, SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
|
|
|
+ return R.ok(Boolean.TRUE, "验证码发送成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Boolean sendSms(String mobile, Boolean need) {
|
|
|
+
|
|
|
+
|
|
|
+ final String key = CacheConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + StringPool.AT + mobile + "@fina";
|
|
|
+ Object code = redisTemplate.opsForValue().get(key);
|
|
|
+
|
|
|
+ final String codeStr = Objects.isNull(code) ? (need ? RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE)) : "5657") : code.toString();
|
|
|
+
|
|
|
+ boolean res = false;
|
|
|
+ try {
|
|
|
+ if (need) {
|
|
|
+ res = SendSms.sendSmsCode1(mobile, codeStr);
|
|
|
+ }
|
|
|
+ log.debug("手机号生成验证码成功:{},{}", mobile, codeStr);
|
|
|
+
|
|
|
+ redisTemplate.opsForValue().set(key, codeStr, SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
|
|
|
+ } catch (ClientException | InterruptedException e) {
|
|
|
+ log.error("手机号生成验证码失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+}
|