|
|
@@ -1,20 +1,34 @@
|
|
|
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.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.constant.TaskTypeEnum;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.dto.Result;
|
|
|
+import com.cyksj.enums.GatewayApiCode;
|
|
|
+import com.cyksj.enums.GatewayResponse;
|
|
|
+import com.cyksj.mapper.PhoneCodeMapper;
|
|
|
+import com.cyksj.mapper.UserMapper;
|
|
|
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.User;
|
|
|
import com.cyksj.model.entity.UserBindDetail;
|
|
|
+import com.cyksj.model.request.UserBindInfoReq;
|
|
|
+import com.cyksj.model.response.WxGzhQrCodeTicketRep;
|
|
|
+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 com.cyksj.service.wechat.WeChatService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/*
|
|
|
@@ -30,58 +44,312 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
|
|
|
private final TaskTypeMapper taskTypeMapper;
|
|
|
|
|
|
- private final PhoneCodeService phoneCodeService;
|
|
|
+ private final UserBindDetailMapper userBindDetailMapper;
|
|
|
|
|
|
- private final UserBindDetailMapper UserBindDetailMapper;
|
|
|
+ private final RedisService redisService;
|
|
|
|
|
|
+ private final PhoneCodeMapper phoneCodeMapper;
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ private final WeChatService weChatService;
|
|
|
|
|
|
@Override
|
|
|
- public void bindPhone(Long userId, String phone, String code, Boolean isCode) {
|
|
|
- UserBindDetail other = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
- .eq(UserBindDetail::getPhone, phone).ne(UserBindDetail::getUserId, userId).select(UserBindDetail::getId).last("limit 1"));
|
|
|
- if (other != null) {
|
|
|
- throw BusinessRuntimeException.getInstance("该手机号已被绑定");
|
|
|
+ public UserBindDetail bindPhone(User user, String phone, String code) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ if (!Validator.isMobile(phone)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的手机号");
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过手机号登录,无法绑定其他手机号");
|
|
|
}
|
|
|
- UserBindDetail bindPhone = Optional.ofNullable(UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
- .eq(UserBindDetail::getUserId, userId).last("limit 1"))).orElse(new UserBindDetail());
|
|
|
+ LambdaQueryWrapper<UserBindDetail> wrapper = Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(StrUtil.isNotBlank(user.getEmail()), UserBindDetail::getEmail, user.getEmail())
|
|
|
+ .eq(StrUtil.isNotBlank(user.getOpenId()), UserBindDetail::getUserId, user.getId())
|
|
|
+ .last("limit 1");
|
|
|
+
|
|
|
+ UserBindDetail bindPhone = Optional.ofNullable(userBindDetailMapper.selectOne(wrapper)).orElse(new UserBindDetail());
|
|
|
if (StrUtil.isNotBlank(bindPhone.getPhone())) {
|
|
|
throw BusinessRuntimeException.getInstance("您已绑定手机号");
|
|
|
}
|
|
|
- if (isCode) {
|
|
|
+
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, phone)
|
|
|
+ .select(UserBindDetail::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该手机号已被绑定");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(code)) {
|
|
|
//校验
|
|
|
- phoneCodeService.checkPhone(phone, code);
|
|
|
+ checkPhoneCode(phone, code);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ bindPhone.setUserId(userId);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ bindPhone.setEmailUserId(userId);
|
|
|
+ bindPhone.setEmail(user.getEmail());
|
|
|
+ }
|
|
|
+ if (bindPhone.getPhoneUserId() == null) {
|
|
|
+ Long phoneUserId = user.getPhoneUserId();
|
|
|
+ if (phoneUserId == null) {
|
|
|
+ User phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, phone).last("limit 1"));
|
|
|
+ if (phoneUser != null) {
|
|
|
+ phoneUserId = phoneUser.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bindPhone.setPhoneUserId(phoneUserId);
|
|
|
}
|
|
|
- bindPhone.setUserId(userId);
|
|
|
bindPhone.setPhone(phone);
|
|
|
- try {
|
|
|
- if (bindPhone.getId() == null) {
|
|
|
- UserBindDetailMapper.insert(bindPhone);
|
|
|
- } else {
|
|
|
- UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ if (bindPhone.getId() == null) {
|
|
|
+ if (redisService.setNx(RedisService.key.USER_BIND_ACCOUNT_INFO_KEY.getName() + phone, phone, RedisService.key.USER_BIND_ACCOUNT_INFO_KEY.getTimeout())) {
|
|
|
+ userBindDetailMapper.insert(bindPhone);
|
|
|
}
|
|
|
- } catch (DuplicateKeyException e) {
|
|
|
-
|
|
|
+ } else {
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
}
|
|
|
TaskType taskType = taskTypeMapper.selectOne(Wrappers.lambdaQuery(TaskType.class)
|
|
|
.eq(TaskType::getName, TaskTypeEnum.bind_phone.getDesc()).last("limit 1"));
|
|
|
//完成任务
|
|
|
taskService.completeTask(taskType, userId);
|
|
|
+ return bindPhone;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateBindPhone(Long userId, String phone, String code) {
|
|
|
- UserBindDetail bindPhone = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
- .eq(UserBindDetail::getUserId, userId).last("limit 1"));
|
|
|
+ public void updateBindPhone(User user, String phone, String code) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ LambdaQueryWrapper<UserBindDetail> wrapper = Wrappers.lambdaQuery(UserBindDetail.class).last("limit 1");
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无法更换自身登录手机号");
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ wrapper.eq(UserBindDetail::getUserId, userId);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ wrapper.eq(UserBindDetail::getEmail, user.getEmail());
|
|
|
+ }
|
|
|
+ UserBindDetail bindPhone = userBindDetailMapper.selectOne(wrapper);
|
|
|
if (bindPhone == null || StrUtil.isEmpty(bindPhone.getPhone())) {
|
|
|
throw BusinessRuntimeException.getInstance("您未绑定手机号");
|
|
|
}
|
|
|
- UserBindDetail other = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ if (bindPhone.getPhone().equals(phone)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("换绑手机号重复");
|
|
|
+ }
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
.eq(UserBindDetail::getPhone, phone).ne(UserBindDetail::getUserId, userId).select(UserBindDetail::getId).last("limit 1"));
|
|
|
if (other != null) {
|
|
|
throw BusinessRuntimeException.getInstance("该手机号已被绑定");
|
|
|
}
|
|
|
- phoneCodeService.checkPhone(phone, code);
|
|
|
+ User phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, phone).last("limit 1"));
|
|
|
+ if (phoneUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("换绑手机号重复未注册");
|
|
|
+ }
|
|
|
+ checkPhoneCode(phone, code);
|
|
|
+ bindPhone.setPhoneUserId(phoneUser.getId());
|
|
|
bindPhone.setPhone(phone);
|
|
|
- UserBindDetailMapper.updateById(bindPhone);
|
|
|
+
|
|
|
+ userBindDetailMapper.update(null, Wrappers.lambdaUpdate(UserBindDetail.class)
|
|
|
+ .set(UserBindDetail::getPhone, phone)
|
|
|
+ .set(UserBindDetail::getPhoneUserId, bindPhone.getPhoneUserId())
|
|
|
+ .eq(UserBindDetail::getId, bindPhone.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateBindEmail(User user, String email, String inputCode) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ LambdaQueryWrapper<UserBindDetail> wrapper = Wrappers.lambdaQuery(UserBindDetail.class).last("limit 1");
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无法更换自身登录邮箱");
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ wrapper.eq(UserBindDetail::getUserId, userId);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ wrapper.eq(UserBindDetail::getPhone, user.getLoginPhone());
|
|
|
+ }
|
|
|
+ UserBindDetail bindDetail = userBindDetailMapper.selectOne(wrapper);
|
|
|
+ if (bindDetail == null || StrUtil.isEmpty(bindDetail.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您未绑定邮箱");
|
|
|
+ }
|
|
|
+ if (bindDetail.getEmail().equals(email)) {
|
|
|
+ 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("该邮箱已被绑定");
|
|
|
+ }
|
|
|
+ checkEmailCode(email, inputCode);
|
|
|
+ User emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, email).last("limit 1"));
|
|
|
+ if (emailUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("绑定的邮箱未注册");
|
|
|
+ }
|
|
|
+ bindDetail.setEmailUserId(emailUser.getId());
|
|
|
+ bindDetail.setEmail(email);
|
|
|
+
|
|
|
+ userBindDetailMapper.update(null, Wrappers.lambdaUpdate(UserBindDetail.class)
|
|
|
+ .set(UserBindDetail::getEmail, email)
|
|
|
+ .set(UserBindDetail::getEmailUserId, bindDetail.getEmailUserId())
|
|
|
+ .eq(UserBindDetail::getId, bindDetail.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserBindDetail bindEmail(User user, String account, String inputCode) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ if (!Validator.isEmail(account)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过邮箱登录,无法绑定其他邮箱");
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<UserBindDetail> wrapper = Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(StrUtil.isNotBlank(user.getLoginPhone()), UserBindDetail::getPhone, user.getLoginPhone())
|
|
|
+ .eq(StrUtil.isNotBlank(user.getOpenId()), UserBindDetail::getUserId, user.getId())
|
|
|
+ .last("limit 1");
|
|
|
+ UserBindDetail bindPhone = Optional.ofNullable(userBindDetailMapper.selectOne(wrapper)).orElse(new UserBindDetail());
|
|
|
+ if (StrUtil.isNotBlank(bindPhone.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您已绑定邮箱");
|
|
|
+ }
|
|
|
+
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, account)
|
|
|
+ .select(UserBindDetail::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该邮箱已被绑定");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(inputCode)) {
|
|
|
+ checkEmailCode(account, inputCode);
|
|
|
+ }
|
|
|
+ bindPhone.setEmail(account);
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ bindPhone.setUserId(userId);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ bindPhone.setPhoneUserId(userId);
|
|
|
+ bindPhone.setPhone(user.getLoginPhone());
|
|
|
+ }
|
|
|
+ if (bindPhone.getEmailUserId() == null) {
|
|
|
+ Long emailUserId = user.getEmailUserId();
|
|
|
+ if (emailUserId == null) {
|
|
|
+ User emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, account).last("limit 1"));
|
|
|
+ if (emailUser != null) {
|
|
|
+ emailUserId = emailUser.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bindPhone.setEmailUserId(emailUserId);
|
|
|
+ }
|
|
|
+ if (bindPhone.getId() == null) {
|
|
|
+ if (redisService.setNx(RedisService.key.USER_BIND_ACCOUNT_INFO_KEY.getName() + account, account, RedisService.key.USER_BIND_ACCOUNT_INFO_KEY.getTimeout())) {
|
|
|
+ userBindDetailMapper.insert(bindPhone);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
+ }
|
|
|
+ redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(account));
|
|
|
+ return bindPhone;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Object> bindOtherAccount(UserBindInfoReq request) throws Exception {
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ Integer type = request.getType();
|
|
|
+ if (type == null) throw BusinessRuntimeException.getInstance("绑定类型为空");
|
|
|
+
|
|
|
+ if (user == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
|
|
|
+ String account = request.getAccount();
|
|
|
+ String code = request.getCode();
|
|
|
+ if (type != 3 && (StrUtil.isEmpty(account) || StrUtil.isEmpty(code))) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的绑定信息");
|
|
|
+ }
|
|
|
+ if (type == 1) {
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过手机号登录,无法绑定其他手机号");
|
|
|
+ }
|
|
|
+ User phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, account).last("limit 1"));
|
|
|
+ if (phoneUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("绑定的手机号未注册");
|
|
|
+ }
|
|
|
+ user.setPhoneUserId(phoneUser.getId());
|
|
|
+ bindPhone(user, account, code);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+ if (type == 2) {
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过邮箱登录,无法绑定其他邮箱");
|
|
|
+ }
|
|
|
+ User emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, account).last("limit 1"));
|
|
|
+ if (emailUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("绑定的邮箱未注册");
|
|
|
+ }
|
|
|
+ user.setEmailUserId(emailUser.getId());
|
|
|
+ bindEmail(user, account, code);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+ if (type == 3) {
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过微信登录,无法绑定其他微信");
|
|
|
+ }
|
|
|
+ //绑定微信 获取临时带参数二维码
|
|
|
+ //获取gzh带参数的二维码
|
|
|
+ WxGzhQrCodeTicketRep result = weChatService.getWxGzhQrCode(null, null, null, user.getLoginPhone(), user.getEmail(), null);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(result);
|
|
|
+ }
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateBindAccount(UserBindInfoReq request) {
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ String account = request.getAccount();
|
|
|
+ String code = request.getCode();
|
|
|
+ if (Validator.isMobile(account)) {
|
|
|
+ updateBindPhone(user, account, code);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (Validator.isEmail(account)) {
|
|
|
+ updateBindEmail(user, account, code);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @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("邮箱验证码错误,请重新输入");
|
|
|
+ }
|
|
|
}
|
|
|
}
|