From 31f6ae9734e4e43bfd6b90c73d2e1b4ce1b322ee Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Mon, 11 Aug 2025 10:09:02 +0800 Subject: [PATCH] 1 --- .../jeecg/common/system/util/AppletUserUtil.java | 14 +++++++++++--- .../controller/AppletApiAddressController.java | 5 ----- .../service/impl/AppletApiAddressServiceImpl.java | 21 ++++++++++++--------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/AppletUserUtil.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/AppletUserUtil.java index e1d1d2f..69d5064 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/AppletUserUtil.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/system/util/AppletUserUtil.java @@ -6,6 +6,8 @@ import org.apache.shiro.subject.Subject; import org.jeecg.common.api.IAppletUserService; import org.jeecg.common.system.vo.AppletUser; import org.jeecg.common.util.SpringContextUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; @@ -58,14 +60,20 @@ public class AppletUserUtil { return user != null ? user.getOpenid() : null; } + public static HttpServletRequest getCurrentRequest() { + ServletRequestAttributes attributes = + (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + return attributes.getRequest(); + } + /** - * 从请求中获取token并解析用户信息 + * 从请求中获取token并解析用户信息,在用户未登录的情况下可能返回null * - * @param request HTTP请求 * @return 小程序用户信息 */ - public static AppletUser getAppletUserFromRequest(HttpServletRequest request) { + public static AppletUser getAppletUserFromRequest() { try { + HttpServletRequest request = getCurrentRequest(); String token = request.getHeader("X-Access-Token"); if (token == null) { token = request.getParameter("token"); diff --git a/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/controller/AppletApiAddressController.java b/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/controller/AppletApiAddressController.java index cc1475a..531740f 100644 --- a/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/controller/AppletApiAddressController.java +++ b/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/controller/AppletApiAddressController.java @@ -45,7 +45,6 @@ public class AppletApiAddressController { // @AutoLog(value = "地址管理") @Operation(summary="小程序收货地址-地址列表查询") @GetMapping(value = "/list") - @IgnoreAuth public Result> queryPageList(AppletShippingAddress appletShippingAddress, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, @@ -63,7 +62,6 @@ public class AppletApiAddressController { */ @Operation(summary="小程序收货地址-添加地址") @PostMapping(value = "/add") - @IgnoreAuth public Result add(AppletShippingAddress appletShippingAddress) { // 去掉@RequestBody appletAddressService.save(appletShippingAddress); return Result.OK("添加成功!"); @@ -78,7 +76,6 @@ public class AppletApiAddressController { //@AutoLog(value = "小程序收货地址-通过id查询") @Operation(summary="小程序收货地址-通过id查询") @GetMapping(value = "/getById") - @IgnoreAuth public Result queryById(@RequestParam(name="id",required=true) String id) { AppletShippingAddress appletShippingAddress = appletAddressService.getById(id); if(appletShippingAddress==null) { @@ -95,7 +92,6 @@ public class AppletApiAddressController { */ @Operation(summary="小程序收货地址-修改地址") @PutMapping(value = "/update") - @IgnoreAuth public Result edit(AppletShippingAddress appletShippingAddress) { appletAddressService.updateById(appletShippingAddress); return Result.OK("编辑成功!"); @@ -109,7 +105,6 @@ public class AppletApiAddressController { */ @Operation(summary="小程序收货地址-设置默认地址") @PutMapping(value = "/setDefault") - @IgnoreAuth public Result setDefaultAddress(@RequestParam String addressId) { appletAddressService.setDefaultAddress(addressId); return Result.OK("设置默认地址成功!"); diff --git a/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/service/impl/AppletApiAddressServiceImpl.java b/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/service/impl/AppletApiAddressServiceImpl.java index e4cd962..0b31bde 100644 --- a/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/service/impl/AppletApiAddressServiceImpl.java +++ b/jeecg-boot/jeecg-boot-module/jeecgboot-boot-applet/src/main/java/org/jeecg/modules/applet/service/impl/AppletApiAddressServiceImpl.java @@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.system.util.AppletUserUtil; +import org.jeecg.common.system.vo.AppletUser; import org.jeecg.modules.applet.service.AppletApiAddressService; import org.jeecg.modules.demo.appletShippingAddress.entity.AppletShippingAddress; import org.jeecg.modules.demo.appletShippingAddress.service.IAppletShippingAddressService; @@ -31,6 +33,7 @@ public class AppletApiAddressServiceImpl implements AppletApiAddressService { String userId = AppletUserUtil.getCurrentAppletUserId(); + Page page1 = appletShippingAddressService .lambdaQuery() .eq(AppletShippingAddress::getUserId, userId) @@ -50,16 +53,16 @@ public class AppletApiAddressServiceImpl implements AppletApiAddressService { @Override public void save(AppletShippingAddress appletShippingAddress) { if (appletShippingAddress == null) { - throw new IllegalArgumentException("地址信息不能为空"); + throw new JeecgBootException("地址信息不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getName())) { - throw new IllegalArgumentException("收货人不能为空"); + throw new JeecgBootException("收货人不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getPhone())) { - throw new IllegalArgumentException("联系电话不能为空"); + throw new JeecgBootException("联系电话不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getDetail())) { - throw new IllegalArgumentException("详细地址不能为空"); + throw new JeecgBootException("详细地址不能为空"); } String userId = AppletUserUtil.getCurrentAppletUserId(); @@ -90,16 +93,16 @@ public class AppletApiAddressServiceImpl implements AppletApiAddressService { @Override public void updateById(AppletShippingAddress appletShippingAddress) { if (appletShippingAddress == null || StrUtil.isBlank(appletShippingAddress.getId())) { - throw new IllegalArgumentException("地址信息或ID不能为空"); + throw new JeecgBootException("地址信息或ID不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getName())) { - throw new IllegalArgumentException("收货人不能为空"); + throw new JeecgBootException("收货人不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getPhone())) { - throw new IllegalArgumentException("联系电话不能为空"); + throw new JeecgBootException("联系电话不能为空"); } if (StrUtil.isBlank(appletShippingAddress.getDetail())) { - throw new IllegalArgumentException("详细地址不能为空"); + throw new JeecgBootException("详细地址不能为空"); } appletShippingAddressService.updateById(appletShippingAddress); @@ -113,7 +116,7 @@ public class AppletApiAddressServiceImpl implements AppletApiAddressService { @Override public void setDefaultAddress(String addressId) { if (StrUtil.isBlank(addressId)) { - throw new IllegalArgumentException("地址ID不能为空"); + throw new JeecgBootException("地址ID不能为空"); } String userId = AppletUserUtil.getCurrentAppletUserId();