From 6fbb7d6d6dca4003c8fb9cfeab2c2cfa88b736db Mon Sep 17 00:00:00 2001 From: owen Date: Mon, 16 Oct 2023 21:01:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=88=E7=AB=AF=E7=B1=BB=E5=9E=8B=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=94=B9=E4=B8=BA=E4=BB=8EController=E4=BC=A0?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/servlet/ServletUtils.java | 20 ------------------- .../app/order/AppTradeOrderController.java | 5 +++-- .../order/TradeOrderUpdateService.java | 4 +++- .../order/TradeOrderUpdateServiceImpl.java | 10 ++++------ .../order/TradeOrderUpdateServiceTest.java | 2 +- .../app/auth/AppAuthController.java | 5 +++-- .../service/auth/MemberAuthService.java | 6 ++++-- .../service/auth/MemberAuthServiceImpl.java | 7 ++++--- .../service/user/MemberUserService.java | 4 +++- .../service/user/MemberUserServiceImpl.java | 10 ++++------ 10 files changed, 29 insertions(+), 44 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java index 368ac738f..446079828 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.framework.common.util.servlet; -import cn.hutool.core.convert.Convert; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.servlet.ServletUtil; @@ -89,18 +88,6 @@ public class ServletUtils { return ServletUtil.getClientIP(request); } - public static Integer getTerminal() { - return getHeaderInt("terminal"); - } - - public static String getHeader(String header) { - return getHeader(getRequest(), header); - } - - public static Integer getHeaderInt(String header) { - return Convert.toInt(getHeader(header)); - } - public static boolean isJsonRequest(ServletRequest request) { return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE); } @@ -121,11 +108,4 @@ public class ServletUtils { return ServletUtil.getParamMap(request); } - public static String getHeader(HttpServletRequest request, String header) { - if (request == null) { - return null; - } - return request.getHeader(header); - } - } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java index 3ff9ceec4..905cb6dd0 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java @@ -61,8 +61,9 @@ public class AppTradeOrderController { @PostMapping("/create") @Operation(summary = "创建订单") @PreAuthenticated - public CommonResult createOrder(@Valid @RequestBody AppTradeOrderCreateReqVO createReqVO) { - TradeOrderDO order = tradeOrderUpdateService.createOrder(getLoginUserId(), getClientIP(), createReqVO); + public CommonResult createOrder(@Valid @RequestBody AppTradeOrderCreateReqVO createReqVO, + @RequestHeader Integer terminal) { + TradeOrderDO order = tradeOrderUpdateService.createOrder(getLoginUserId(), getClientIP(), createReqVO, terminal); return success(new AppTradeOrderCreateRespVO().setId(order.getId()).setPayOrderId(order.getPayOrderId())); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java index 42d3e6b65..488e24413 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.trade.service.order; +import cn.iocoder.yudao.framework.common.enums.TerminalEnum; import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderDeliveryReqVO; import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderRemarkReqVO; import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderUpdateAddressReqVO; @@ -37,9 +38,10 @@ public interface TradeOrderUpdateService { * @param userId 登录用户 * @param userIp 用户 IP 地址 * @param createReqVO 创建交易订单请求模型 + * @param terminal 终端 {@link TerminalEnum} * @return 交易订单的 */ - TradeOrderDO createOrder(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO); + TradeOrderDO createOrder(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO, Integer terminal); /** * 更新交易订单已支付 diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index 9951916e4..fcd52050d 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java @@ -11,7 +11,6 @@ import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.number.MoneyUtils; -import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.module.member.api.address.AddressApi; import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO; import cn.iocoder.yudao.module.pay.api.order.PayOrderApi; @@ -159,11 +158,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) { + public TradeOrderDO createOrder(Long userId, String userIp, AppTradeOrderCreateReqVO createReqVO, Integer terminal) { // 1.1 价格计算 TradePriceCalculateRespBO calculateRespBO = calculatePrice(userId, createReqVO); // 1.2 构建订单 - TradeOrderDO order = buildTradeOrder(userId, userIp, createReqVO, calculateRespBO); + TradeOrderDO order = buildTradeOrder(userId, userIp, createReqVO, calculateRespBO, terminal); List orderItems = buildTradeOrderItems(order, calculateRespBO); // 2. 订单创建前的逻辑 @@ -180,15 +179,14 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { } private TradeOrderDO buildTradeOrder(Long userId, String clientIp, AppTradeOrderCreateReqVO createReqVO, - TradePriceCalculateRespBO calculateRespBO) { + TradePriceCalculateRespBO calculateRespBO, Integer terminal) { TradeOrderDO order = TradeOrderConvert.INSTANCE.convert(userId, clientIp, 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)); - // TODO @疯狂:无状态,terminal 不从 servletuTILS 拿,而是通过 controller 传递给 service; - order.setTerminal(ServletUtils.getTerminal()); + order.setTerminal(terminal); // 支付 + 退款信息 order.setAdjustPrice(0).setPayStatus(false); order.setRefundStatus(TradeOrderRefundStatusEnum.NONE.getStatus()).setRefundPrice(0); diff --git a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceTest.java b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceTest.java index 6b58fd9b6..b91e5fcb6 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceTest.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceTest.java @@ -152,7 +152,7 @@ public class TradeOrderUpdateServiceTest extends BaseDbUnitTest { }))).thenReturn(1000L); // 调用方法 - TradeOrderDO order = tradeOrderUpdateService.createOrder(userId, userIp, reqVO); + TradeOrderDO order = tradeOrderUpdateService.createOrder(userId, userIp, reqVO, null); // 断言 TradeOrderDO 订单 List tradeOrderDOs = tradeOrderMapper.selectList(); assertEquals(tradeOrderDOs.size(), 1); diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java index e55cbc0f0..0765df143 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.java @@ -65,8 +65,9 @@ public class AppAuthController { @PostMapping("/sms-login") @Operation(summary = "使用手机 + 验证码登录") - public CommonResult smsLogin(@RequestBody @Valid AppAuthSmsLoginReqVO reqVO) { - return success(authService.smsLogin(reqVO)); + public CommonResult smsLogin(@RequestBody @Valid AppAuthSmsLoginReqVO reqVO, + @RequestHeader Integer terminal) { + return success(authService.smsLogin(reqVO, terminal)); } @PostMapping("/send-sms-code") diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java index c35754e7d..9ab878817 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthService.java @@ -1,5 +1,6 @@ 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 javax.validation.Valid; @@ -31,10 +32,11 @@ public interface MemberAuthService { /** * 手机 + 验证码登陆 * - * @param reqVO 登陆信息 + * @param reqVO 登陆信息 + * @param terminal 终端 {@link TerminalEnum} * @return 登录结果 */ - AppAuthLoginRespVO smsLogin(@Valid AppAuthSmsLoginReqVO reqVO); + AppAuthLoginRespVO smsLogin(@Valid AppAuthSmsLoginReqVO reqVO, Integer terminal); /** * 社交登录,使用 code 授权码 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java index e8f816ea6..0b8557e50 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java @@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.common.enums.TerminalEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; @@ -78,13 +79,13 @@ public class MemberAuthServiceImpl implements MemberAuthService { @Override @Transactional - public AppAuthLoginRespVO smsLogin(AppAuthSmsLoginReqVO reqVO) { + public AppAuthLoginRespVO smsLogin(AppAuthSmsLoginReqVO reqVO, Integer terminal) { // 校验验证码 String userIp = getClientIP(); smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MEMBER_LOGIN.getScene(), userIp)); // 获得获得注册用户 - MemberUserDO user = userService.createUserIfAbsent(reqVO.getMobile(), userIp); + MemberUserDO user = userService.createUserIfAbsent(reqVO.getMobile(), userIp, terminal); Assert.notNull(user, "获取用户失败,结果为空"); // 如果 socialType 非空,说明需要绑定社交用户 @@ -128,7 +129,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { throw exception(AUTH_WEIXIN_MINI_APP_PHONE_CODE_ERROR); } // 获得获得注册用户 - MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(), getClientIP()); + MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(), getClientIP(), TerminalEnum.WECHAT_MINI_PROGRAM.getTerminal()); Assert.notNull(user, "获取用户失败,结果为空"); // 绑定社交用户 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index 50433a559..8b640c850 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.member.service.user; +import cn.iocoder.yudao.framework.common.enums.TerminalEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.validation.Mobile; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; @@ -43,9 +44,10 @@ public interface MemberUserService { * * @param mobile 手机号 * @param registerIp 注册 IP + * @param terminal 终端 {@link TerminalEnum} * @return 用户对象 */ - MemberUserDO createUserIfAbsent(@Mobile String mobile, String registerIp); + MemberUserDO createUserIfAbsent(@Mobile String mobile, String registerIp, Integer terminal); /** * 更新用户的最后登陆信息 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index 504778dde..0d53c388d 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java @@ -7,7 +7,6 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.module.infra.api.file.FileApi; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; @@ -81,17 +80,17 @@ public class MemberUserServiceImpl implements MemberUserService { @Override @Transactional(rollbackFor = Exception.class) - public MemberUserDO createUserIfAbsent(String mobile, String registerIp) { + public MemberUserDO createUserIfAbsent(String mobile, String registerIp, Integer terminal) { // 用户已经存在 MemberUserDO user = memberUserMapper.selectByMobile(mobile); if (user != null) { return user; } // 用户不存在,则进行创建 - return createUser(mobile, registerIp); + return createUser(mobile, registerIp, terminal); } - private MemberUserDO createUser(String mobile, String registerIp) { + private MemberUserDO createUser(String mobile, String registerIp, Integer terminal) { // 生成密码 String password = IdUtil.fastSimpleUUID(); // 插入用户 @@ -100,8 +99,7 @@ public class MemberUserServiceImpl implements MemberUserService { user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 user.setPassword(encodePassword(password)); // 加密密码 user.setRegisterIp(registerIp); - // TODO @疯狂:无状态,terminal 不从 servletuTILS 拿,而是通过 controller 传递给 service; - user.setRegisterTerminal(ServletUtils.getTerminal()); + user.setRegisterTerminal(terminal); memberUserMapper.insert(user); // 发送 MQ 消息:用户创建