TASK:绑定邮箱、绑定微信;

This commit is contained in:
shahaibo
2025-02-04 11:39:42 +08:00
parent ca320f4334
commit 87768c6b53
4 changed files with 60 additions and 6 deletions

View File

@@ -9,6 +9,7 @@ import com.ai.da.mapper.primary.entity.TrialOrder;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.AccountLoginVO;
import com.ai.da.model.vo.AccountPreLoginVO;
import com.ai.da.model.vo.BindEmailVO;
import com.ai.da.model.vo.PersonalHomepageVO;
import com.ai.da.service.AccountService;
import io.swagger.annotations.Api;
@@ -326,7 +327,7 @@ public class AccountController {
@GetMapping("/bindEmail")
@ApiOperation(value = "绑定邮箱")
public Response<Boolean> bindEmail(@RequestParam("email") String email) {
public Response<BindEmailVO> bindEmail(@RequestParam("email") String email) {
return Response.success(accountService.bindEmail(email));
}

View File

@@ -0,0 +1,9 @@
package com.ai.da.model.vo;
import lombok.Data;
@Data
public class BindEmailVO {
private String token;
}

View File

@@ -7,6 +7,7 @@ import com.ai.da.mapper.primary.entity.TrialOrder;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.AccountLoginVO;
import com.ai.da.model.vo.AccountPreLoginVO;
import com.ai.da.model.vo.BindEmailVO;
import com.ai.da.model.vo.PersonalHomepageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
@@ -49,7 +50,7 @@ public interface AccountService extends IService<Account> {
*/
Boolean bindEmail(AccountBindEmailDTO accountBindEmailDTO);
Boolean bindEmail(String email);
BindEmailVO bindEmail(String email);
/**
* 忘记密码

View File

@@ -413,7 +413,14 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
qw.lambda().eq(Account::getUserEmail, emailSendDTO.getEmail());
List<Account> accounts = accountMapper.selectList(qw);
if (CollectionUtil.isNotEmpty(accounts)) {
throw new BusinessException("This email has been bound");
Account account = accounts.get(0);
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
accountExtendQW.lambda().eq(AccountExtend::getAccountId, account.getId());
accountExtendQW.lambda().eq(AccountExtend::getAuth, "WeChat");
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
if (CollectionUtil.isNotEmpty(accountExtends)) {
throw new BusinessException("This email account has already been linked to another WeChat account.");
}
}
result = SendEmailUtil.send(emailSendDTO.getEmail(), null,
SendEmailUtil.BIND_MAILBOX_TEMPLATE_ID, randomVerifyCode);
@@ -2562,6 +2569,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
@Override
@Transactional
public AccountExtend bindWeChat(String code) {
// 1. 获取 access_token 和 openid
JSONObject accessTokenResponse = getAccessTokenFromWeChat(code);
@@ -2588,7 +2596,14 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
if (CollectionUtil.isNotEmpty(accountExtends)) {
throw new BusinessException("The WeChat has been bound.");
AccountExtend accountExtend = accountExtends.get(0);
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
accountMapper.deleteById(accountExtend.getAccountId());
accountExtend.setAccountId(authPrincipalVo.getId());
accountExtendMapper.updateById(accountExtend);
return accountExtend;
}
AccountExtend accountExtendInsert = new AccountExtend();
@@ -2625,12 +2640,40 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
@Override
public Boolean bindEmail(String email) {
@Transactional
public BindEmailVO bindEmail(String email) {
AuthPrincipalVo userHolder = UserContext.getUserHolder();
BindEmailVO result = new BindEmailVO();
QueryWrapper<Account> qw = new QueryWrapper<>();
qw.lambda().eq(Account::getUserEmail, email);
List<Account> accounts = accountMapper.selectList(qw);
if (CollectionUtil.isNotEmpty(accounts)) {
Account account = accounts.get(0);
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
accountExtendQW.lambda().eq(AccountExtend::getAccountId, userHolder.getId());
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Wechat");
AccountExtend accountExtend = accountExtendMapper.selectOne(accountExtendQW);
accountExtend.setAccountId(account.getId());
accountExtendMapper.updateById(accountExtend);
accountMapper.deleteById(userHolder.getId());
String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId()));
if (StringUtils.isNotBlank(token)) {
//用户已登入
result.setToken(token);
} else {
result.setToken(createAccountToken(account));
}
return result;
}
Account account = accountMapper.selectById(userHolder.getId());
account.setUserEmail(email);
accountMapper.updateById(account);
return Boolean.TRUE;
return result;
}
public void updateAccountValidity(Long accountId, Long currentPeriodEnd){