|
|
@@ -2,13 +2,15 @@ package com.cyksj.service.market.task.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.lang.Assert;
|
|
|
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;
|
|
|
@@ -18,11 +20,12 @@ 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.wechat.WeChatService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Date;
|
|
|
@@ -41,7 +44,7 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
|
|
|
private final TaskTypeMapper taskTypeMapper;
|
|
|
|
|
|
- private final UserBindDetailMapper UserBindDetailMapper;
|
|
|
+ private final UserBindDetailMapper userBindDetailMapper;
|
|
|
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
@@ -49,33 +52,44 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
+ private final WeChatService weChatService;
|
|
|
|
|
|
@Override
|
|
|
- public void bindPhone(Long userId, String phone, String code, Boolean isCheckCode) {
|
|
|
- UserBindDetail other = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
- .eq(UserBindDetail::getPhone, phone).ne(UserBindDetail::getUserId, userId).select(UserBindDetail::getId).last("limit 1"));
|
|
|
+ public void bindPhone(User user, String phone, String code) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, phone)
|
|
|
+ .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(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您通过手机号登录,无法绑定手机号");
|
|
|
+ }
|
|
|
+ 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 (isCheckCode) {
|
|
|
+ if (StrUtil.isNotBlank(code)) {
|
|
|
//校验
|
|
|
checkPhoneCode(phone, code);
|
|
|
}
|
|
|
- bindPhone.setUserId(userId);
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ 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"));
|
|
|
@@ -84,108 +98,132 @@ public class BindTaskServiceImpl implements BindTaskService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateBindPhone(Long userId, String phone, String code) {
|
|
|
- UserBindDetail bindPhone = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ public void updateBindPhone(User user, String phone, String code) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ UserBindDetail bindPhone = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
.eq(UserBindDetail::getUserId, userId).last("limit 1"));
|
|
|
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("该手机号已被绑定");
|
|
|
}
|
|
|
checkPhoneCode(phone, code);
|
|
|
bindPhone.setPhone(phone);
|
|
|
- UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateBindEmail(Long userId, String email, String inputCode) {
|
|
|
- UserBindDetail bindPhone = UserBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ public void updateBindEmail(User user, String email, String inputCode) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ 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)
|
|
|
+ if (bindPhone.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);
|
|
|
bindPhone.setEmail(email);
|
|
|
- UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void bindEmail(Long userId, String account, String inputCode) {
|
|
|
+ public void bindEmail(User user, String account, String inputCode) {
|
|
|
+ Long userId = user.getId();
|
|
|
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 (StrUtil.isNotBlank(user.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("该邮箱已被绑定");
|
|
|
}
|
|
|
- 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.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("您已绑定邮箱");
|
|
|
}
|
|
|
- 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("邮箱验证码错误,请重新输入");
|
|
|
+ if (StrUtil.isNotBlank(inputCode)) {
|
|
|
+ checkEmailCode(account, inputCode);
|
|
|
}
|
|
|
bindPhone.setEmail(account);
|
|
|
- bindPhone.setUserId(userId);
|
|
|
- try {
|
|
|
- if (bindPhone.getId() == null) {
|
|
|
- UserBindDetailMapper.insert(bindPhone);
|
|
|
- } else {
|
|
|
- UserBindDetailMapper.updateById(bindPhone);
|
|
|
+ if (StrUtil.isNotBlank(user.getOpenId())) {
|
|
|
+ bindPhone.setUserId(userId);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
}
|
|
|
- } catch (DuplicateKeyException e) {
|
|
|
-
|
|
|
+ } else {
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
}
|
|
|
redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(account));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void bindOtherAccount(UserBindInfoReq request) {
|
|
|
+ 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 (Validator.isMobile(account)) {
|
|
|
+ if (type == 1) {
|
|
|
if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
throw BusinessRuntimeException.getInstance("您通过手机号登录,无法绑定手机号");
|
|
|
}
|
|
|
- bindPhone(userId, account, code, true);
|
|
|
- return;
|
|
|
+ bindPhone(user, account, code);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
}
|
|
|
- if (Validator.isEmail(account)) {
|
|
|
+ if (type == 2) {
|
|
|
if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
throw BusinessRuntimeException.getInstance("您通过邮箱登录,无法绑定邮箱");
|
|
|
}
|
|
|
- bindEmail(userId, account, code);
|
|
|
+ bindEmail(user, account, code);
|
|
|
}
|
|
|
+ if (type == 3) {
|
|
|
+ //绑定微信 获取临时带参数二维码
|
|
|
+ //获取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(userId, account, code);
|
|
|
+ updateBindPhone(user, account, code);
|
|
|
return;
|
|
|
}
|
|
|
if (Validator.isEmail(account)) {
|
|
|
- updateBindEmail(userId, account, code);
|
|
|
+ updateBindEmail(user, account, code);
|
|
|
}
|
|
|
|
|
|
}
|