|
@@ -1,11 +1,25 @@
|
|
|
package com.zanxiang.sdk.service.Impl;
|
|
|
|
|
|
+import com.zanxiang.common.domain.ResultVo;
|
|
|
+import com.zanxiang.common.enums.ExpireTimeEnum;
|
|
|
+import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
+import com.zanxiang.common.utils.StringUtils;
|
|
|
+import com.zanxiang.sdk.constant.RedisKeyConstant;
|
|
|
+import com.zanxiang.sdk.domain.dto.UserDTO;
|
|
|
import com.zanxiang.sdk.domain.enums.SmsTypeEnum;
|
|
|
import com.zanxiang.sdk.domain.params.SmsSendParam;
|
|
|
+import com.zanxiang.sdk.service.ISmsService;
|
|
|
+import com.zanxiang.sdk.service.IUserService;
|
|
|
+import com.zanxiang.sdk.utils.RedisUtils;
|
|
|
+import com.zanxiangnet.module.sms.pojo.SendResult;
|
|
|
+import com.zanxiangnet.module.sms.service.impl.AliSmsService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Objects;
|
|
|
+import java.util.Random;
|
|
|
|
|
|
/**
|
|
|
* @author : lingfeng
|
|
@@ -14,21 +28,120 @@ import java.util.Objects;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class SmsServiceImpl {
|
|
|
+public class SmsServiceImpl implements ISmsService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisUtils<String> redisUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AliSmsService aliSmsService;
|
|
|
|
|
|
/**
|
|
|
* 发送短信验证码
|
|
|
*
|
|
|
* @param smsSendParam : 获取短信验证码参数
|
|
|
*/
|
|
|
- public void smsSend(SmsSendParam smsSendParam) {
|
|
|
+ @Override
|
|
|
+ public ResultVo<Void> smsSend(SmsSendParam smsSendParam) {
|
|
|
//类型
|
|
|
Integer type = smsSendParam.getType();
|
|
|
//手机号码
|
|
|
String mobile = smsSendParam.getMobile();
|
|
|
+ //根据手机号获取用户信息
|
|
|
+ UserDTO userDTO = userService.getUserInfoByMobile(mobile);
|
|
|
//找回密码获取
|
|
|
- if (Objects.equals(type, SmsTypeEnum.SMS_FIND_PWD.getType())){
|
|
|
+ if (Objects.equals(type, SmsTypeEnum.SMS_FIND_PWD.getType())) {
|
|
|
+ //手机号对应用户信息不存在
|
|
|
+ if (userDTO == null) {
|
|
|
+ return new ResultVo<>(HttpStatusEnum.USERNAME_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //用户注册获取
|
|
|
+ if (Objects.equals(type, SmsTypeEnum.SMS_REG.getType())) {
|
|
|
+ //判断手机号是否已注册
|
|
|
+ if (userDTO != null) {
|
|
|
+ return new ResultVo<>(HttpStatusEnum.PHONE_IS_REG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //验证手机号
|
|
|
+ if (!StringUtils.checkPhone(mobile)) {
|
|
|
+ return new ResultVo<>(HttpStatusEnum.PHONE_ERROR);
|
|
|
+ }
|
|
|
+ //验证类型
|
|
|
+ SmsTypeEnum smsTypeEnum = SmsTypeEnum.getByType(type);
|
|
|
+ if (smsTypeEnum == null) {
|
|
|
+ return new ResultVo<>(HttpStatusEnum.CODE_TYPE_ERROR);
|
|
|
+ }
|
|
|
+ //判断是否已经发送
|
|
|
+ String cache = redisUtils.getCache(this.smsKey(mobile, type));
|
|
|
+ if (Strings.isNotBlank(cache)) {
|
|
|
+ return new ResultVo<>(HttpStatusEnum.PHONE_HAS_SEND);
|
|
|
+ }
|
|
|
+ //发送验证码
|
|
|
+ String randomCode = this.randomCode();
|
|
|
+ SendResult sendResult = aliSmsService.sendCode(randomCode, mobile);
|
|
|
+ //发送成功设置缓存, 时效5分钟
|
|
|
+ if (sendResult.isSuccess()) {
|
|
|
+ redisUtils.setCache(this.smsKey(mobile, type), randomCode, ExpireTimeEnum.FIVE_MIN.getTime());
|
|
|
+ }
|
|
|
+ return new ResultVo<>(HttpStatusEnum.SUCCESS);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验短信验证码
|
|
|
+ *
|
|
|
+ * @param type : 短信类型
|
|
|
+ * @param mobile : 手机号
|
|
|
+ * @param code : 验证码
|
|
|
+ * @return : 返回验证结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public HttpStatusEnum smsCheck(Integer type, String mobile, String code) {
|
|
|
+ //验证类型
|
|
|
+ SmsTypeEnum smsTypeEnum = SmsTypeEnum.getByType(type);
|
|
|
+ if (smsTypeEnum == null) {
|
|
|
+ return HttpStatusEnum.CODE_TYPE_ERROR;
|
|
|
}
|
|
|
+ //验证手机号
|
|
|
+ if (!StringUtils.checkPhone(mobile)) {
|
|
|
+ return HttpStatusEnum.PHONE_ERROR;
|
|
|
+ }
|
|
|
+ //获取缓存中的验证码
|
|
|
+ String smsKey = this.smsKey(mobile, type);
|
|
|
+ String cache = redisUtils.getCache(smsKey);
|
|
|
+ //验证码已过期
|
|
|
+ if (Strings.isBlank(cache)) {
|
|
|
+ return HttpStatusEnum.CODE_IS_EXPIRED;
|
|
|
+ }
|
|
|
+ //验证码错误
|
|
|
+ if (!Objects.equals(cache, code)) {
|
|
|
+ return HttpStatusEnum.CODE_ERROR;
|
|
|
+ }
|
|
|
+ //验证成功, 删除缓存
|
|
|
+ redisUtils.deleteCache(smsKey);
|
|
|
+ //返回成功
|
|
|
+ return HttpStatusEnum.SUCCESS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取手机验证码
|
|
|
+ */
|
|
|
+ private String randomCode() {
|
|
|
+ Random random = new Random();
|
|
|
+ return String.valueOf(random.nextInt(999999) + 100000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机验证码缓存key
|
|
|
+ *
|
|
|
+ * @param mobile : 手机号
|
|
|
+ * @param type : 短信类型
|
|
|
+ * @return : 返回redis缓存key
|
|
|
+ */
|
|
|
+ private String smsKey(String mobile, Integer type) {
|
|
|
+ return RedisKeyConstant.SMS_PHONE_KEY + "_" + mobile + "_" + type;
|
|
|
}
|
|
|
}
|