|
|
@@ -0,0 +1,455 @@
|
|
|
+package com.cyksj.service.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.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.i18n.I18nMessageUtil;
|
|
|
+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.UserBindDetailMapper;
|
|
|
+import com.cyksj.model.entity.PhoneCode;
|
|
|
+import com.cyksj.model.entity.User;
|
|
|
+import com.cyksj.model.entity.UserBindDetail;
|
|
|
+import com.cyksj.model.request.UserBindInfoReq;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.service.task.BindTaskService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/*
|
|
|
+ *项目名: netflix
|
|
|
+ *文件名: BindTaskServiceImpl
|
|
|
+ *创建者: JavaZou
|
|
|
+ *创建时间:2023/4/13 9:53
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class BindTaskServiceImpl implements BindTaskService {
|
|
|
+
|
|
|
+
|
|
|
+ private final UserBindDetailMapper userBindDetailMapper;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ private final PhoneCodeMapper phoneCodeMapper;
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<String> bindOtherAccount(UserBindInfoReq request) throws Exception {
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ Integer type = request.getType();
|
|
|
+ if (type == null) throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ if (type == 1) {
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ User phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, account).last("limit 1"));
|
|
|
+ if (phoneUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_not_register"));
|
|
|
+ }
|
|
|
+ user.setPhoneUserId(phoneUser.getId());
|
|
|
+ bindPhone(user, account, code);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+ if (type == 2) {
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ User emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, account).last("limit 1"));
|
|
|
+ if (emailUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_not_register"));
|
|
|
+ }
|
|
|
+ user.setEmailUserId(emailUser.getId());
|
|
|
+ bindEmail(user, account, code);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+ 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 changeBindAccount(UserBindInfoReq request) {
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
|
|
|
+ String account = request.getAccount();
|
|
|
+ String code = request.getCode();
|
|
|
+ Integer type = request.getType();
|
|
|
+ if (type == 1) {
|
|
|
+ //是否已注册
|
|
|
+ User changeUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, account).last("limit 1"));
|
|
|
+ if (changeUser != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_change_has_registered"));
|
|
|
+ }
|
|
|
+ //是否已绑定
|
|
|
+ UserBindDetail changeUserBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, account).last("limit 1"));
|
|
|
+ if (changeUserBindDetail != null) {I18nMessageUtil.getI18nMsg("bind_account_phone_other_bind_error");
|
|
|
+ }
|
|
|
+ //校验手机号
|
|
|
+ checkPhoneCode(account, code);
|
|
|
+ String loginPhone = user.getLoginPhone();
|
|
|
+ User phoneUser = user;
|
|
|
+ Long phoneUserId = phoneUser.getId();
|
|
|
+ //手机用户
|
|
|
+ if (StrUtil.isNotBlank(loginPhone)) {
|
|
|
+ UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, loginPhone).last("limit 1"));
|
|
|
+ if (userBindDetail != null) {
|
|
|
+ userBindDetail.setPhone(account);
|
|
|
+ userBindDetail.setPhoneUserId(phoneUserId);
|
|
|
+ userBindDetailMapper.updateById(userBindDetail);
|
|
|
+ }
|
|
|
+ user.setLoginPhone(account);
|
|
|
+ userMapper.updateById(user);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //非手机用户
|
|
|
+ UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .and(qr -> qr.eq(UserBindDetail::getUserId, userId)
|
|
|
+ .or().eq(UserBindDetail::getEmailUserId, userId))
|
|
|
+ .last("limit 1"));
|
|
|
+ if (userBindDetail == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(userBindDetail.getPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ String phone = userBindDetail.getPhone();
|
|
|
+ phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, phone).last("limit 1"));
|
|
|
+ if (phoneUser == null) throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_not_register"));
|
|
|
+ phoneUserId = phoneUser.getId();
|
|
|
+ userBindDetail.setPhone(account);
|
|
|
+ userBindDetail.setPhoneUserId(phoneUserId);
|
|
|
+ userBindDetailMapper.updateById(userBindDetail);
|
|
|
+
|
|
|
+ phoneUser.setLoginPhone(account);
|
|
|
+ userMapper.updateById(phoneUser);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type == 2) {
|
|
|
+ //是否已注册
|
|
|
+ User changeUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, account).last("limit 1"));
|
|
|
+ if (changeUser != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_change_has_registered"));
|
|
|
+ }
|
|
|
+ //是否已绑定
|
|
|
+ UserBindDetail changeUserBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, account).last("limit 1"));
|
|
|
+ if (changeUserBindDetail != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_other_bind_error"));
|
|
|
+ }
|
|
|
+ //校验邮箱
|
|
|
+ checkEmailCode(account, code);
|
|
|
+ String email = user.getEmail();
|
|
|
+ User emailUser = user;
|
|
|
+ Long emailUserId = emailUser.getId();
|
|
|
+ //邮箱用户
|
|
|
+ if (StrUtil.isNotBlank(email)) {
|
|
|
+ UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, email).last("limit 1"));
|
|
|
+ if (userBindDetail != null) {
|
|
|
+ userBindDetail.setEmail(account);
|
|
|
+ userBindDetail.setEmailUserId(emailUserId);
|
|
|
+ userBindDetailMapper.updateById(userBindDetail);
|
|
|
+ }
|
|
|
+ user.setEmail(account);
|
|
|
+ userMapper.updateById(user);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //非邮箱用户
|
|
|
+ UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .and(qr -> qr.eq(UserBindDetail::getUserId, userId)
|
|
|
+ .or().eq(UserBindDetail::getPhoneUserId, userId))
|
|
|
+ .last("limit 1"));
|
|
|
+ if (userBindDetail == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(userBindDetail.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ email = userBindDetail.getEmail();
|
|
|
+ emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, email).last("limit 1"));
|
|
|
+ if (emailUser == null) throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_not_register"));
|
|
|
+ emailUserId = emailUser.getId();
|
|
|
+ userBindDetail.setEmail(account);
|
|
|
+ userBindDetail.setEmailUserId(emailUserId);
|
|
|
+ userBindDetailMapper.updateById(userBindDetail);
|
|
|
+
|
|
|
+ emailUser.setEmail(account);
|
|
|
+ userMapper.updateById(emailUser);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserBindDetail bindPhone(User user, String phone, String code) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ if (StrUtil.isNotBlank(user.getLoginPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ 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 = userBindDetailMapper.selectOne(wrapper);
|
|
|
+ if (bindPhone != null && StrUtil.isNotBlank(bindPhone.getPhone())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, phone)
|
|
|
+ .ne(StrUtil.isNotBlank(user.getEmail()), UserBindDetail::getEmail, StrUtil.EMPTY)
|
|
|
+ .isNotNull(StrUtil.isNotEmpty(user.getOpenId()), UserBindDetail::getUserId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_other_bind_error"));
|
|
|
+ }
|
|
|
+ if (bindPhone == null) {
|
|
|
+ bindPhone = Optional.ofNullable(userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, phone).last("limit 1"))).orElse(new UserBindDetail());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(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.setPhone(phone);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ userBindDetailMapper.updateById(bindPhone);
|
|
|
+ }
|
|
|
+ return bindPhone;
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserBindDetail bindEmail(User user, String account, String inputCode) {
|
|
|
+ Long userId = user.getId();
|
|
|
+ if (!Validator.isEmail(account)) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("email_format_error"));
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(user.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ 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 = userBindDetailMapper.selectOne(wrapper);
|
|
|
+ if (bindPhone != null && StrUtil.isNotBlank(bindPhone.getEmail())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, account)
|
|
|
+ .ne(StrUtil.isNotBlank(user.getPhone()), UserBindDetail::getPhone, StrUtil.EMPTY)
|
|
|
+ .isNotNull(StrUtil.isNotBlank(user.getOpenId()), UserBindDetail::getUserId)
|
|
|
+ .select(UserBindDetail::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_other_bind_error"));
|
|
|
+ }
|
|
|
+ if (bindPhone == null) {
|
|
|
+ bindPhone = Optional.ofNullable(userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, account).last("limit 1"))).orElse(new UserBindDetail());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("bind_account_phone_change_self_error"));
|
|
|
+ }
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ if (bindPhone.getPhone().equals(phone)) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_repeat"));
|
|
|
+ }
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getPhone, phone).select(UserBindDetail::getId).last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_other_bind_error"));
|
|
|
+ }
|
|
|
+ User phoneUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getLoginPhone, phone).last("limit 1"));
|
|
|
+ if (phoneUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_phone_not_register"));
|
|
|
+ }
|
|
|
+ checkPhoneCode(phone, code);
|
|
|
+ bindPhone.setPhoneUserId(phoneUser.getId());
|
|
|
+ bindPhone.setPhone(phone);
|
|
|
+
|
|
|
+ userBindDetailMapper.update(null, Wrappers.lambdaUpdate(UserBindDetail.class)
|
|
|
+ .set(UserBindDetail::getPhone, phone)
|
|
|
+ .set(UserBindDetail::getPhoneUserId, bindPhone.getPhoneUserId())
|
|
|
+ .eq(UserBindDetail::getId, bindPhone.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("bind_account_email_change_self_error"));
|
|
|
+ }
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("sys_error"));
|
|
|
+ }
|
|
|
+ if (bindDetail.getEmail().equals(email)) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_repeat"));
|
|
|
+ }
|
|
|
+ UserBindDetail other = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
|
|
|
+ .eq(UserBindDetail::getEmail, email).select(UserBindDetail::getId).last("limit 1"));
|
|
|
+ if (other != null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_other_bind_error"));
|
|
|
+ }
|
|
|
+ checkEmailCode(email, inputCode);
|
|
|
+ User emailUser = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getEmail, email).last("limit 1"));
|
|
|
+ if (emailUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("bind_account_email_not_register"));
|
|
|
+ }
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("email_code_valid_error"));
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("verify_code_error"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(I18nMessageUtil.getI18nMsg("phone_code_valid_error"));
|
|
|
+ }
|
|
|
+ //3分钟失效
|
|
|
+ Date updateTime = phoneCode.getUpdateTime();
|
|
|
+ DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
+ if (now.isAfter(afterFiveMinutes)) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("phone_code_valid_error"));
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(inputCode, phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("verify_code_error"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|