Merge remote-tracking branch 'yudao/develop' into develop

This commit is contained in:
puhui999 2023-12-24 00:32:47 +08:00
commit 7286f33a7f
37 changed files with 217 additions and 105 deletions

View File

@ -31,7 +31,7 @@
![架构图](/.image/common/ruoyi-vue-pro-architecture.png)
* Java 后端:`master` 分支为 JDK 21 + Spring Boot 3.2.0`master-jdk8` 分支为 JDK8 + Spring Boot 2.7.18
* Java 后端:`master` 分支为 JDK 8 + Spring Boot 2.7.18`master-jdk21` 分支为 JDK21 + Spring Boot 3.2.0
* 管理后台的电脑端Vue3 提供 `element-plus`、`vben(ant-design-vue)` 两个版本Vue2 提供 `element-ui` 版本
* 管理后台的移动端:采用 `uni-app` 方案,一份代码多终端适配,同时支持 APP、小程序、H5
* 后端采用 Spring Boot 多模块架构、MySQL + MyBatis Plus、Redis + Redisson
@ -78,15 +78,19 @@
【完整版】包括系统功能、基础设施、会员中心、数据报表、工作流程、商城系统、微信公众号、CRM 等功能
* JDK 21 + Spring Boot 3.2.0 版本:<https://gitee.com/zhijiantianya/ruoyi-vue-pro>`master` 分支
* JDK 8 + Spring Boot 2.7.18 版本:<https://gitee.com/zhijiantianya/ruoyi-vue-pro>`master-jdk8` 分支
* JDK 8 + Spring Boot 2.7.18 版本:<https://gitee.com/zhijiantianya/ruoyi-vue-pro>`master` 分支
* JDK 21 + Spring Boot 3.2.0 版本:<https://gitee.com/zhijiantianya/ruoyi-vue-pro>`master-jdk21` 分支
两个分支的功能是一致的,可以放心使用!
### ➡️️ 精简版
【精简版】只包括系统功能、基础设施功能不包括会员中心、数据报表、工作流程、商城系统、微信公众号、CRM 等功能
* JDK 21 + Spring Boot 3.2.0 版本:<https://gitee.com/yudaocode/yudao-boot-mini>`master` 分支
* JDK 8 + Spring Boot 2.7.18 版本:<https://gitee.com/yudaocode/yudao-boot-mini>`master-jdk8` 分支
* JDK 8 + Spring Boot 2.7.18 版本:<https://gitee.com/yudaocode/yudao-boot-mini>`master` 分支
* JDK 21 + Spring Boot 3.2.0 版本:<https://gitee.com/yudaocode/yudao-boot-mini>`master-jdk21` 分支
如果你想把【完整版】的功能,迁移到【精简版】,可以参考 [《迁移功能到精简版》](https://doc.iocoder.cn/migrate-module/) 文档。
## 😎 开源协议

View File

@ -15,6 +15,7 @@ import java.util.Arrays;
@Getter
public enum TerminalEnum implements IntArrayValuable {
UNKNOWN(0, "未知"), // 目的在无法解析到 terminal 使用它
WECHAT_MINI_PROGRAM(10, "微信小程序"),
WECHAT_WAP(11, "微信公众号"),
H5(20, "H5 网页"),

View File

@ -88,8 +88,6 @@ public class ServletUtils {
return JakartaServletUtil.getClientIP(request);
}
// TODO @疯狂terminal 还是从 ServletUtils 里拿更容易全局治理
public static boolean isJsonRequest(ServletRequest request) {
return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE);
}

View File

@ -1,8 +1,11 @@
package cn.iocoder.yudao.framework.web.core.util;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
@ -25,6 +28,13 @@ public class WebFrameworkUtils {
public static final String HEADER_TENANT_ID = "tenant-id";
/**
* 终端的 Header
*
* @see cn.iocoder.yudao.framework.common.enums.TerminalEnum
*/
public static final String HEADER_TERMINAL = "terminal";
private static WebProperties properties;
public WebFrameworkUtils(WebProperties webProperties) {
@ -107,6 +117,15 @@ public class WebFrameworkUtils {
return getLoginUserId(request);
}
public static Integer getTerminal() {
HttpServletRequest request = getRequest();
if (request == null) {
return TerminalEnum.UNKNOWN.getTerminal();
}
String terminalValue = request.getHeader(HEADER_TERMINAL);
return NumberUtil.parseInt(terminalValue, TerminalEnum.UNKNOWN.getTerminal());
}
public static void setCommonResult(ServletRequest request, CommonResult<?> result) {
request.setAttribute(REQUEST_ATTRIBUTE_COMMON_RESULT, result);
}

View File

@ -38,17 +38,6 @@
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- Web 相关 -->
<dependency>
<!-- 为什么是 websocket 依赖 security 呢?而不是 security 拓展 websocket 呢?
因为 websocket 和 LoginUser 当前登录的用户有一定的相关性,具体可见 WebSocketSessionManagerImpl 逻辑。
如果让 security 拓展 websocket 的话,会导致 websocket 组件的封装很散,进而增大理解成本。
-->
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-security</artifactId>
<scope>provided</scope>
</dependency>
<!-- 消息队列相关 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>

View File

@ -82,6 +82,7 @@ public class DiyTemplateController {
return success(DiyTemplateConvert.INSTANCE.convertPage(pageResult));
}
// TODO @疯狂这个要不和 getDiyTemplate 合并然后 DiyTemplateRespVO 里面直接把 DiyPagePropertyRespVO 也加上减少 VO 好了管理后台 get 多返回点数据也问题不大的目的还是想尽可能降低大家的理解成本哈
@GetMapping("/get-property")
@Operation(summary = "获得装修模板属性")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@ -92,6 +93,7 @@ public class DiyTemplateController {
return success(DiyTemplateConvert.INSTANCE.convertPropertyVo(diyTemplate, pages));
}
// TODO @疯狂这个接口要不和 useDiyTemplate 合并成一个然后 VO 改成我们新的 VO 规范不改的字段就不传递
@PutMapping("/update-property")
@Operation(summary = "更新装修模板属性")
@PreAuthorize("@ss.hasPermission('promotion:diy-template:update')")

View File

@ -25,6 +25,7 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@RestController
@RequestMapping("/promotion/decorate")
@Validated
@Deprecated // 废弃
public class AppDecorateController {
@Resource

View File

@ -33,6 +33,7 @@ public class AppDiyTemplateController {
@Resource
private DiyPageService diyPageService;
// TODO @疯狂要不要把 used get 接口合并哈不传递 id直接拿默认
@GetMapping("/used")
@Operation(summary = "使用中的装修模板")
public CommonResult<AppDiyTemplatePropertyRespVO> getUsedDiyTemplate() {
@ -54,6 +55,7 @@ public class AppDiyTemplateController {
}
// 查询模板下的页面
List<DiyPageDO> pages = diyPageService.getDiyPageByTemplateId(diyTemplate.getId());
// TODO @疯狂首页我的要不枚举到 DiyPageDO 例如说 NAME_USERNAME_HOME 类似这种哈
String home = findFirst(pages, page -> "首页".equals(page.getName()), DiyPageDO::getProperty);
String user = findFirst(pages, page -> "我的".equals(page.getName()), DiyPageDO::getProperty);
// 拼接返回

View File

@ -44,6 +44,7 @@ public class DiyPageDO extends BaseDO {
* 备注
*/
private String remark;
// TODO @疯狂这个字段要不改成 previewPicUrls和别的模块一样用 pic 作为图片哇
/**
* 预览图多个逗号分隔
*/

View File

@ -50,13 +50,14 @@ public class DiyTemplateDO extends BaseDO {
* 备注
*/
private String remark;
// TODO @疯狂这个字段要不改成 previewPicUrls和别的模块一样用 pic 作为图片哇
/**
* 预览图
*/
@TableField(typeHandler = StringListTypeHandler.class)
private List<String> previewImageUrls;
/**
* 底部导航属性JSON 格式
* uni-app 底部导航属性JSON 格式
*/
private String property;

View File

@ -61,9 +61,8 @@ public class AppTradeOrderController {
@PostMapping("/create")
@Operation(summary = "创建订单")
@PreAuthenticated
public CommonResult<AppTradeOrderCreateRespVO> createOrder(@Valid @RequestBody AppTradeOrderCreateReqVO createReqVO,
@RequestHeader Integer terminal) {
TradeOrderDO order = tradeOrderUpdateService.createOrder(getLoginUserId(), getClientIP(), createReqVO, terminal);
public CommonResult<AppTradeOrderCreateRespVO> createOrder(@Valid @RequestBody AppTradeOrderCreateReqVO createReqVO) {
TradeOrderDO order = tradeOrderUpdateService.createOrder(getLoginUserId(), createReqVO);
return success(new AppTradeOrderCreateRespVO().setId(order.getId()).setPayOrderId(order.getPayOrderId()));
}

View File

@ -69,8 +69,7 @@ public interface TradeOrderConvert {
@Mapping(source = "calculateRespBO.price.vipPrice", target = "vipPrice"),
@Mapping(source = "calculateRespBO.price.payPrice", target = "payPrice")
})
TradeOrderDO convert(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO,
TradePriceCalculateRespBO calculateRespBO);
TradeOrderDO convert(Long userId, AppTradeOrderCreateReqVO createReqVO, TradePriceCalculateRespBO calculateRespBO);
TradeOrderRespDTO convert(TradeOrderDO orderDO);

View File

@ -36,12 +36,10 @@ public interface TradeOrderUpdateService {
* 会员创建交易订单
*
* @param userId 登录用户
* @param userIp 用户 IP 地址
* @param createReqVO 创建交易订单请求模型
* @param terminal 终端 {@link TerminalEnum}
* @return 交易订单的
*/
TradeOrderDO createOrder(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO, Integer terminal);
TradeOrderDO createOrder(Long userId, AppTradeOrderCreateReqVO createReqVO);
/**
* 更新交易订单已支付

View File

@ -64,6 +64,8 @@ import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.minusTime;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getTerminal;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
/**
@ -158,11 +160,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Override
@Transactional(rollbackFor = Exception.class)
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.MEMBER_CREATE)
public TradeOrderDO createOrder(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO, Integer terminal) {
public TradeOrderDO createOrder(Long userId, AppTradeOrderCreateReqVO createReqVO) {
// 1.1 价格计算
TradePriceCalculateRespBO calculateRespBO = calculatePrice(userId, createReqVO);
// 1.2 构建订单
TradeOrderDO order = buildTradeOrder(userId, userIp, createReqVO, calculateRespBO, terminal);
TradeOrderDO order = buildTradeOrder(userId, createReqVO, calculateRespBO);
List<TradeOrderItemDO> orderItems = buildTradeOrderItems(order, calculateRespBO);
// 2. 订单创建前的逻辑
@ -178,15 +180,15 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
return order;
}
private TradeOrderDO buildTradeOrder(Long userId, String clientIp, AppTradeOrderCreateReqVO createReqVO,
TradePriceCalculateRespBO calculateRespBO, Integer terminal) {
TradeOrderDO order = TradeOrderConvert.INSTANCE.convert(userId, clientIp, createReqVO, calculateRespBO);
private TradeOrderDO buildTradeOrder(Long userId, AppTradeOrderCreateReqVO createReqVO,
TradePriceCalculateRespBO calculateRespBO) {
TradeOrderDO order = TradeOrderConvert.INSTANCE.convert(userId, createReqVO, calculateRespBO);
order.setType(calculateRespBO.getType());
order.setNo(tradeNoRedisDAO.generate(TradeNoRedisDAO.TRADE_ORDER_NO_PREFIX));
order.setStatus(TradeOrderStatusEnum.UNPAID.getStatus());
order.setRefundStatus(TradeOrderRefundStatusEnum.NONE.getStatus());
order.setProductCount(getSumValue(calculateRespBO.getItems(), TradePriceCalculateRespBO.OrderItem::getCount, Integer::sum));
order.setTerminal(terminal);
order.setUserIp(getClientIP()).setTerminal(getTerminal());
// 支付 + 退款信息
order.setAdjustPrice(0).setPayStatus(false);
order.setRefundStatus(TradeOrderRefundStatusEnum.NONE.getStatus()).setRefundPrice(0);

View File

@ -18,7 +18,7 @@ public interface ErrorCodeConstants {
// ========== AUTH 模块 1-004-003-000 ==========
ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确");
ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用");
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_004_003_005, "未绑定账号,需要进行绑定");
ErrorCode AUTH_SOCIAL_USER_NOT_FOUND = new ErrorCode(1_004_003_005, "登录失败,解析不到三方登录信息");
ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用");
// ========== 用户收件地址 1-004-004-000 ==========

View File

@ -73,9 +73,8 @@ public class AppAuthController {
@PostMapping("/sms-login")
@Operation(summary = "使用手机 + 验证码登录")
public CommonResult<AppAuthLoginRespVO> smsLogin(@RequestBody @Valid AppAuthSmsLoginReqVO reqVO,
@RequestHeader Integer terminal) {
return success(authService.smsLogin(reqVO, terminal));
public CommonResult<AppAuthLoginRespVO> smsLogin(@RequestBody @Valid AppAuthSmsLoginReqVO reqVO) {
return success(authService.smsLogin(reqVO));
}
@PostMapping("/send-sms-code")

View File

@ -2,11 +2,16 @@ package cn.iocoder.yudao.module.member.controller.app.social;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBindReqVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserRespVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO;
import cn.iocoder.yudao.module.member.convert.social.SocialUserConvert;
import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
@ -15,11 +20,12 @@ import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "用户 App - 社交用户")
@RestController
@RequestMapping("/system/social-user")
@RequestMapping("/member/social-user")
@Validated
public class AppSocialUserController {
@ -28,17 +34,30 @@ public class AppSocialUserController {
@PostMapping("/bind")
@Operation(summary = "社交绑定,使用 code 授权码")
public CommonResult<Boolean> socialBind(@RequestBody @Valid AppSocialUserBindReqVO reqVO) {
socialUserApi.bindSocialUser(SocialUserConvert.INSTANCE.convert(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), reqVO));
return CommonResult.success(true);
public CommonResult<String> socialBind(@RequestBody @Valid AppSocialUserBindReqVO reqVO) {
SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO(getLoginUserId(), UserTypeEnum.MEMBER.getValue(),
reqVO.getType(), reqVO.getCode(), reqVO.getState());
String openid = socialUserApi.bindSocialUser(reqDTO);
return success(openid);
}
@DeleteMapping("/unbind")
@Operation(summary = "取消社交绑定")
@PreAuthenticated
public CommonResult<Boolean> socialUnbind(@RequestBody AppSocialUserUnbindReqVO reqVO) {
socialUserApi.unbindSocialUser(SocialUserConvert.INSTANCE.convert(getLoginUserId(), UserTypeEnum.MEMBER.getValue(), reqVO));
return CommonResult.success(true);
SocialUserUnbindReqDTO reqDTO = new SocialUserUnbindReqDTO(getLoginUserId(), UserTypeEnum.MEMBER.getValue(),
reqVO.getType(), reqVO.getOpenid());
socialUserApi.unbindSocialUser(reqDTO);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得社交用户")
@Parameter(name = "type", description = "社交平台的类型,参见 SocialTypeEnum 枚举值", required = true, example = "10")
@PreAuthenticated
public CommonResult<AppSocialUserRespVO> getSocialUser(@RequestParam("type") Integer type) {
SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(UserTypeEnum.MEMBER.getValue(), getLoginUserId(), type);
return success(BeanUtils.toBean(socialUser, AppSocialUserRespVO.class));
}
}

View File

@ -13,9 +13,6 @@ import jakarta.validation.constraints.NotNull;
@Schema(description = "用户 APP - 社交绑定 Request VO使用 code 授权码")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AppSocialUserBindReqVO {
@Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.member.controller.app.social.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "用户 APP - 社交用户 Response VO")
@Data
public class AppSocialUserRespVO {
@Schema(description = "社交用户的 openid", requiredMode = Schema.RequiredMode.REQUIRED, example = "IPRmJ0wvBptiPIlGEZiPewGwiEiE")
private String openid;
@Schema(description = "社交用户的昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道源码")
private String nickname;
@Schema(description = "社交用户的头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/1.png")
private String avatar;
}

View File

@ -13,9 +13,6 @@ import jakarta.validation.constraints.NotNull;
@Schema(description = "用户 APP - 取消社交绑定 Request VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AppSocialUserUnbindReqVO {
@Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")

View File

@ -1,19 +0,0 @@
package cn.iocoder.yudao.module.member.convert.social;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBindReqVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface SocialUserConvert {
SocialUserConvert INSTANCE = Mappers.getMapper(SocialUserConvert.class);
SocialUserBindReqDTO convert(Long userId, Integer userType, AppSocialUserBindReqVO reqVO);
SocialUserUnbindReqDTO convert(Long userId, Integer userType, AppSocialUserUnbindReqVO reqVO);
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.member.service.auth;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.*;
import jakarta.validation.Valid;
@ -33,10 +32,9 @@ public interface MemberAuthService {
* 手机 + 验证码登陆
*
* @param reqVO 登陆信息
* @param terminal 终端 {@link TerminalEnum}
* @return 登录结果
*/
AppAuthLoginRespVO smsLogin(@Valid AppAuthSmsLoginReqVO reqVO, Integer terminal);
AppAuthLoginRespVO smsLogin(@Valid AppAuthSmsLoginReqVO reqVO);
/**
* 社交登录使用 code 授权码

View File

@ -36,6 +36,7 @@ import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getTerminal;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;
/**
@ -78,13 +79,13 @@ public class MemberAuthServiceImpl implements MemberAuthService {
@Override
@Transactional
public AppAuthLoginRespVO smsLogin(AppAuthSmsLoginReqVO reqVO, Integer terminal) {
public AppAuthLoginRespVO smsLogin(AppAuthSmsLoginReqVO reqVO) {
// 校验验证码
String userIp = getClientIP();
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_LOGIN.getScene(), userIp));
// 获得获得注册用户
MemberUserDO user = userService.createUserIfAbsent(reqVO.getMobile(), userIp, terminal);
MemberUserDO user = userService.createUserIfAbsent(reqVO.getMobile(), userIp, getTerminal());
Assert.notNull(user, "获取用户失败,结果为空");
// 如果 socialType 非空说明需要绑定社交用户
@ -99,16 +100,25 @@ public class MemberAuthServiceImpl implements MemberAuthService {
}
@Override
@Transactional
public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) {
// 使用 code 授权码进行登录然后获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserApi.getSocialUser(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
SocialUserRespDTO socialUser = socialUserApi.getSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState());
if (socialUser == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
}
// 自动登录
MemberUserDO user = userService.getUser(socialUser.getUserId());
// 情况一已绑定直接读取用户信息
MemberUserDO user;
if (socialUser.getUserId() != null) {
user = userService.getUser(socialUser.getUserId());
// 情况二未绑定注册用户 + 绑定用户
} else {
user = userService.createUser(socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal());
socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
reqVO.getType(), reqVO.getCode(), reqVO.getState()));
}
if (user == null) {
throw exception(USER_NOT_EXISTS);
}

View File

@ -49,6 +49,18 @@ public interface MemberUserService {
*/
MemberUserDO createUserIfAbsent(@Mobile String mobile, String registerIp, Integer terminal);
/**
* 创建用户
* 目的三方登录时如果未绑定用户时自动创建对应用户
*
* @param nickname 昵称
* @param avtar 头像
* @param registerIp 注册 IP
* @param terminal 终端 {@link TerminalEnum}
* @return 用户对象
*/
MemberUserDO createUser(String nickname, String avtar, String registerIp, Integer terminal);
/**
* 更新用户的最后登陆信息
*

View File

@ -2,9 +2,7 @@ package cn.iocoder.yudao.module.member.service.user;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.*;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -81,10 +79,17 @@ public class MemberUserServiceImpl implements MemberUserService {
return user;
}
// 用户不存在则进行创建
return createUser(mobile, registerIp, terminal);
return createUser(mobile, null, null, registerIp, terminal);
}
private MemberUserDO createUser(String mobile, String registerIp, Integer terminal) {
@Override
@Transactional(rollbackFor = Exception.class)
public MemberUserDO createUser(String nickname, String avtar, String registerIp, Integer terminal) {
return createUser(null, nickname, avtar, registerIp, terminal);
}
private MemberUserDO createUser(String mobile, String nickname, String avtar,
String registerIp, Integer terminal) {
// 生成密码
String password = IdUtil.fastSimpleUUID();
// 插入用户
@ -92,8 +97,12 @@ public class MemberUserServiceImpl implements MemberUserService {
user.setMobile(mobile);
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
user.setPassword(encodePassword(password)); // 加密密码
user.setRegisterIp(registerIp);
user.setRegisterTerminal(terminal);
user.setRegisterIp(registerIp).setRegisterTerminal(terminal);
user.setNickname(nickname).setAvatar(avtar); // 基础信息
if (StrUtil.isEmpty(nickname)) {
// 昵称为空时随机一个名字避免一些依赖 nickname 的逻辑报错或者有点丑例如说短信发送有昵称时~
user.setNickname("用户" + RandomUtil.randomNumbers(6));
}
memberUserMapper.insert(user);
// 发送 MQ 消息用户创建

View File

@ -24,7 +24,8 @@ public interface PayWalletRechargeMapper extends BaseMapperX<PayWalletRechargeDO
default PageResult<PayWalletRechargeDO> selectPage(PageParam pageReqVO, Long walletId, Boolean payStatus) {
return selectPage(pageReqVO, new LambdaQueryWrapperX<PayWalletRechargeDO>()
.eq(PayWalletRechargeDO::getWalletId, walletId)
.eq(PayWalletRechargeDO::getPayStatus, payStatus));
.eq(PayWalletRechargeDO::getPayStatus, payStatus)
.orderByDesc(PayWalletRechargeDO::getId));
}
}

View File

@ -36,9 +36,9 @@ public class LoginLogCreateReqDTO {
private Integer userType;
/**
* 用户账号
*
* 不再强制校验 username 非空因为 Member 社交登录时此时暂时没有 username(mobile
*/
@NotBlank(message = "用户账号不能为空")
@Size(max = 30, message = "用户账号长度不能超过30个字符")
private String username;
/**

View File

@ -29,6 +29,16 @@ public interface SocialUserApi {
*/
void unbindSocialUser(@Valid SocialUserUnbindReqDTO reqDTO);
/**
* 获得社交用户基于 userId
*
* @param userType 用户类型
* @param userId 用户编号
* @param socialType 社交平台的类型
* @return 社交用户
*/
SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType);
/**
* 获得社交用户
*
@ -40,7 +50,6 @@ public interface SocialUserApi {
* @param state state
* @return 社交用户
*/
SocialUserRespDTO getSocialUser(Integer userType, Integer socialType,
String code, String state);
SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state);
}

View File

@ -15,9 +15,17 @@ import lombok.NoArgsConstructor;
public class SocialUserRespDTO {
/**
* 社交用户 openid
* 社交用户 openid
*/
private String openid;
/**
* 社交用户的昵称
*/
private String nickname;
/**
* 社交用户的头像
*/
private String avatar;
/**
* 关联的用户编号

View File

@ -3,10 +3,12 @@ package cn.iocoder.yudao.module.system.api.social.dto;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.NoArgsConstructor;
/**
* 社交绑定 Request DTO使用 code 授权码
@ -14,6 +16,8 @@ import jakarta.validation.constraints.NotNull;
* @author 芋道源码
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SocialUserUnbindReqDTO {
/**

View File

@ -33,8 +33,13 @@ public class SocialUserApiImpl implements SocialUserApi {
}
@Override
public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) {
return socialUserService.getSocialUser(userType, socialType, code, state);
public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) {
return socialUserService.getSocialUserByUserId(userType, userId, socialType);
}
@Override
public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
return socialUserService.getSocialUserByCode(userType, socialType, code, state);
}
}

View File

@ -34,4 +34,11 @@ public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
.eq(SocialUserBindDO::getUserType, userType));
}
default SocialUserBindDO selectByUserIdAndUserTypeAndSocialType(Long userId, Integer userType, Integer socialType) {
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserId, userId)
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getSocialType, socialType));
}
}

View File

@ -156,9 +156,9 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Override
public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) {
// 使用 code 授权码进行登录然后获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserService.getSocialUser(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState());
if (socialUser == null) {
if (socialUser == null || socialUser.getUserId() == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}

View File

@ -45,6 +45,16 @@ public interface SocialUserService {
*/
void unbindSocialUser(Long userId, Integer userType, Integer socialType, String openid);
/**
* 获得社交用户基于 userId
*
* @param userType 用户类型
* @param userId 用户编号
* @param socialType 社交平台的类型
* @return 社交用户
*/
SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType);
/**
* 获得社交用户
*
@ -56,7 +66,7 @@ public interface SocialUserService {
* @param state state
* @return 社交用户
*/
SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state);
SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state);
// ==================== 社交用户 CRUD ====================

View File

@ -26,7 +26,6 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND;
/**
@ -94,18 +93,30 @@ public class SocialUserServiceImpl implements SocialUserService {
}
@Override
public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) {
public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) {
// 获得绑定用户
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserIdAndUserTypeAndSocialType(userId, userType, socialType);
if (socialUserBind == null) {
return null;
}
// 获得社交用户
SocialUserDO socialUser = socialUserMapper.selectById(socialUserBind.getSocialUserId());
Assert.notNull(socialUser, "社交用户不能为空");
return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
socialUserBind.getUserId());
}
@Override
public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
// 获得社交用户
SocialUserDO socialUser = authSocialUser(socialType, userType, code, state);
Assert.notNull(socialUser, "社交用户不能为空");
// 如果未绑定的社交用户则无法自动登录进行报错
// 获得绑定用户
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType,
socialUser.getId());
if (socialUserBind == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}
return new SocialUserRespDTO(socialUser.getOpenid(), socialUserBind.getUserId());
return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
socialUserBind != null ? socialUserBind.getUserId() : null);
}
/**

View File

@ -236,7 +236,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
AuthSocialLoginReqVO reqVO = randomPojo(AuthSocialLoginReqVO.class);
// mock 方法绑定的用户编号
Long userId = 1L;
when(socialUserService.getSocialUser(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()),
when(socialUserService.getSocialUserByCode(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()),
eq(reqVO.getCode()), eq(reqVO.getState()))).thenReturn(new SocialUserRespDTO(randomString(), userId));
// mock用户
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(userId));

View File

@ -147,7 +147,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest {
socialUserBindMapper.insert(socialUserBind);
// 调用
SocialUserRespDTO socialUser = socialUserService.getSocialUser(userType, type, code, state);
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(userType, type, code, state);
// 断言
assertEquals(userId, socialUser.getUserId());
assertEquals(socialUserDO.getOpenid(), socialUser.getOpenid());