|
|
@@ -1,20 +1,27 @@
|
|
|
package com.cyksj.service.market.task.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.constant.TaskTypeEnum;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.mapper.PhoneCodeMapper;
|
|
|
import com.cyksj.mapper.market.task.TaskTypeMapper;
|
|
|
import com.cyksj.mapper.market.task.UserBindDetailMapper;
|
|
|
+import com.cyksj.model.entity.PhoneCode;
|
|
|
import com.cyksj.model.entity.TaskType;
|
|
|
import com.cyksj.model.entity.UserBindDetail;
|
|
|
+import com.cyksj.model.request.UserBindInfoReq;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.market.task.BindTaskService;
|
|
|
import com.cyksj.service.market.task.TaskService;
|
|
|
-import com.cyksj.service.phone.PhoneCodeService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/*
|
|
|
@@ -30,10 +37,12 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
|
|
|
private final TaskTypeMapper taskTypeMapper;
|
|
|
|
|
|
- private final PhoneCodeService phoneCodeService;
|
|
|
-
|
|
|
private final UserBindDetailMapper UserBindDetailMapper;
|
|
|
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ private final PhoneCodeMapper phoneCodeMapper;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public void bindPhone(Long userId, String phone, String code, Boolean isCode) {
|
|
|
@@ -80,8 +89,108 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
if (other != null) {
|
|
|
throw BusinessRuntimeException.getInstance("该手机号已被绑定");
|
|
|
}
|
|
|
- phoneCodeService.checkPhone(phone, code);
|
|
|
+ checkPhoneCode(phone, code);
|
|
|
bindPhone.setPhone(phone);
|
|
|
UserBindDetailMapper.updateById(bindPhone);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateBindEmail(Long userId, String email, String inputCode) {
|
|
|
+ UserBindDetail bindPhone = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getUserId, userId).last("limit 1"));
|
|
|
+ if (bindPhone == null || StrUtil.isEmpty(bindPhone.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您未绑定邮箱");
|
|
|
+ }
|
|
|
+ UserBindDetail other = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, email).ne(UserBindDetail::getUserId, userId).select(UserBindDetail::getId).last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该邮箱已被绑定");
|
|
|
+ }
|
|
|
+ String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(email));
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ bindPhone.setEmail(email);
|
|
|
+ UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void bindEmail(Long userId, String account, String inputCode) {
|
|
|
+ if (!Validator.isEmail(account)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
|
|
|
+ }
|
|
|
+ UserBindDetail other = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, account).ne(UserBindDetail::getUserId, userId).select(UserBindDetail::getId).last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该邮箱已被绑定");
|
|
|
+ }
|
|
|
+ UserBindDetail bindPhone = Optional.ofNullable(UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getUserId, userId).last("limit 1"))).orElse(new UserBindDetail());
|
|
|
+ if (StrUtil.isNotBlank(bindPhone.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您已绑定邮箱");
|
|
|
+ }
|
|
|
+ String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(account));
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ bindPhone.setEmail(account);
|
|
|
+ bindPhone.setUserId(userId);
|
|
|
+ try {
|
|
|
+ if (bindPhone.getId() == null) {
|
|
|
+ UserBindDetailMapper.insert(bindPhone);
|
|
|
+ } else {
|
|
|
+ UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ }
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(account));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateBindAccount(UserBindInfoReq request) {
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ String account = request.getAccount();
|
|
|
+ String code = request.getCode();
|
|
|
+ if (Validator.isMobile(account)) {
|
|
|
+ updateBindPhone(userId, account, code);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkPhoneCode(String phone, String inputCode) {
|
|
|
+ DateTime now = DateTime.now();
|
|
|
+ PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, phone).last("limit 1"));
|
|
|
+ if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
|
|
|
+ }
|
|
|
+ //3分钟失效
|
|
|
+ Date updateTime = phoneCode.getUpdateTime();
|
|
|
+ DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
+ if (now.isAfter(afterFiveMinutes)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkEmailCode(String email, String inputCode) {
|
|
|
+ String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(email));
|
|
|
+ if (StrUtil.isEmpty(code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|