Browse Source

1、解决@PreAuthenticated没有拦截问题

rayson 6 months ago
parent
commit
1fa0e4f34a

+ 10 - 0
citu-framework/citu-spring-boot-starter-security/src/main/java/com/citu/framework/security/config/CituSecurityAutoConfiguration.java

@@ -1,5 +1,6 @@
 package com.citu.framework.security.config;
 
+import com.citu.framework.security.core.aop.PreAuthenticatedAspect;
 import com.citu.framework.security.core.context.TransmittableThreadLocalSecurityContextHolderStrategy;
 import com.citu.framework.security.core.filter.TokenAuthenticationFilter;
 import com.citu.framework.security.core.handler.AccessDeniedHandlerImpl;
@@ -38,6 +39,15 @@ public class CituSecurityAutoConfiguration {
     @Resource
     private SecurityProperties securityProperties;
 
+    /**
+     * 处理用户未登录拦截的切面的 Bean
+     */
+    @Bean
+    public PreAuthenticatedAspect preAuthenticatedAspect() {
+        return new PreAuthenticatedAspect();
+    }
+
+
     /**
      * 认证失败处理类 Bean
      */

+ 19 - 1
citu-module-mall/citu-module-promotion-api/src/main/java/com/citu/module/promotion/api/luck/LuckLotteryApi.java

@@ -19,9 +19,27 @@ public interface LuckLotteryApi {
     @PutMapping(PREFIX + "/update-user-lottery-num-incr")
     @Operation(summary = "修改用户抽奖次数")
     @Parameters({
-            @Parameter(name = "skuId", description = "SKU 编号", required = true, example = "2"),
+            @Parameter(name = "spuId", description = "spuId 编号", required = true, example = "2"),
             @Parameter(name = "userId", description = "用户iD", required = true, example = "3"),
     })
     CommonResult<Boolean> updateUserNumIncr(@RequestParam("spuId") Long spuId,
                                                @RequestParam("userId") Long userId);
+
+
+    /**
+     * spuId 用于判断商品是否符合下单抽奖条件
+     * userId 谁抽奖
+     * cityName 用户喜好城市(抽某个城市的房劵)
+     **/
+    @PutMapping(PREFIX + "/raffle")
+    @Operation(summary = "抽奖")
+    @Parameters({
+            @Parameter(name = "spuId", description = "SPU 编号", required = true, example = "2"),
+            @Parameter(name = "userId", description = "用户iD", required = true, example = "3"),
+            @Parameter(name = "cityName", description = "城市名称(广州市)", required = true, example = "广州市"),
+    })
+    CommonResult<Boolean> raffle(
+            @RequestParam("spuId") Long spuId,
+                                 @RequestParam("userId") Long userId,
+                                 @RequestParam("cityName") String cityName);
 }

+ 9 - 0
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/api/luck/LuckLotteryApiImpl.java

@@ -1,6 +1,7 @@
 package com.citu.module.promotion.api.luck;
 
 import com.citu.framework.common.pojo.CommonResult;
+import com.citu.module.promotion.service.luck.LuckLotteryRecordService;
 import com.citu.module.promotion.service.luck.LuckLotteryService;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RestController;
@@ -19,9 +20,17 @@ public class LuckLotteryApiImpl implements LuckLotteryApi{
     @Resource
     private LuckLotteryService luckLotteryService;
 
+    @Resource
+    private LuckLotteryRecordService luckLotteryRecordService;
+
     @Override
     public CommonResult<Boolean> updateUserNumIncr(Long spuId, Long userId) {
         luckLotteryService.updateUserNumIncr(spuId, userId);
         return CommonResult.success(true);
     }
+
+    @Override
+    public CommonResult<Boolean> raffle(Long spuId, Long userId, String cityName) {
+        return null;
+    }
 }

+ 10 - 0
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/dal/mysql/luck/LuckLotteryRecordMapper.java

@@ -38,4 +38,14 @@ public interface LuckLotteryRecordMapper extends BaseMapperX<LuckLotteryRecordDO
         );
     }
 
+    default LuckLotteryRecordDO getLast(Long lotteryId, Long userId) {
+        return selectOne(new LambdaQueryWrapperX<LuckLotteryRecordDO>()
+                .eq(LuckLotteryRecordDO::getLotteryId, lotteryId)
+                .eq(LuckLotteryRecordDO::getUserId, userId)
+                .orderByDesc(LuckLotteryRecordDO::getCreateTime)
+                .last("limit 1")
+        );
+    }
+
+
 }

+ 16 - 0
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/service/luck/LuckLotteryRecordService.java

@@ -65,6 +65,15 @@ public interface LuckLotteryRecordService {
     LuckLotteryRecordRespVO raffle(Long lotteryId,Long userId);
 
 
+    /**
+     * 抽奖
+     * @param spuId 商品id
+     * @param userId 抽奖用户
+     * @param cityName 城市名称
+     */
+    void raffle(Long spuId, Long userId, String cityName);
+
+
     /**
      * 获取用户抽奖记录
      * @param lotteryId 抽奖活动id
@@ -85,4 +94,11 @@ public interface LuckLotteryRecordService {
      * @param reqVO 领用信息
      */
     void receive(AppLuckLotteryReceiveReqVO reqVO, Long userId);
+
+    /**
+     * 获取最新的一条抽奖记录
+     * @param lotteryId 抽奖活动id
+     * @param userId 抽奖用户
+     */
+    LuckLotteryRecordDetailRespVO getLast(Long lotteryId,Long userId);
 }

+ 73 - 2
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/service/luck/LuckLotteryRecordServiceImpl.java

@@ -17,11 +17,12 @@ import com.citu.module.promotion.dal.dataobject.luck.LuckLotteryDO;
 import com.citu.module.promotion.dal.dataobject.luck.LuckLotteryRecordDO;
 import com.citu.module.promotion.dal.dataobject.luck.LuckPrizeDO;
 import com.citu.module.promotion.dal.mysql.luck.LuckLotteryRecordMapper;
-import com.citu.module.promotion.dal.mysql.luck.LuckUserMapper;
+import com.citu.module.promotion.enums.luck.LuckPrizeTypeEnum;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
@@ -29,7 +30,7 @@ import java.util.concurrent.ThreadLocalRandom;
 import java.util.stream.Collectors;
 
 import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
-import static com.citu.module.promotion.enums.ErrorCodeConstants.LUCK_LOTTERY_RECORD_NOT_EXISTS;
+import static com.citu.module.promotion.enums.ErrorCodeConstants.*;
 
 /**
  * 幸运抽奖-抽奖记录 Service 实现类
@@ -148,6 +149,62 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
         return LuckLotteryRecordConvert.INSTANCE.convert(luckLotteryRecord);
     }
 
+    @Override
+    @DSTransactional
+    @Lock4j(keys = {"#spuId", "#userId", "#cityName"}, acquireTimeout = 6000)
+    public void raffle(Long spuId, Long userId, String cityName) {
+        LuckLotteryDO luckLottery = luckLotteryService.getLuckLotteryBySpuId(String.valueOf(spuId));
+        // 效验活动是否过期
+        LocalDateTime now = LocalDateTime.now();
+        // 判断活动有没有开始,判断活动有没有结束
+        if (luckLottery.getStartTime().isAfter(now)||luckLottery.getEndTime().isBefore(now)) {
+           return;
+        }
+
+        // 获取奖品
+        List<LuckPrizeDO> luckPrizeList = luckPrizeService.getListByLotteryId(luckLottery.getId());
+        if (CollUtil.isEmpty(luckPrizeList)) {
+            return;
+        }
+        // 筛选出 喜爱城市
+        luckPrizeList = luckPrizeList.stream().filter(prize->
+                prize.getType().equals(LuckPrizeTypeEnum.CUSTOM.getType())
+                && prize.getExtend().getCityName().equals(cityName)).collect(Collectors.toList());
+
+        // 计算总权重并随机选择奖品
+        int totalWeight = luckPrizeList.stream().mapToInt(prize -> prize.getChance() * prize.getTotal()).sum();
+        Random random = ThreadLocalRandom.current();
+        int randomNumber = random.nextInt(totalWeight);
+
+        LuckPrizeDO selectedPrize = luckPrizeService.selectPrizeByWeight(randomNumber, luckPrizeList);
+
+        // 扣除奖品剩余数量
+        if (null == selectedPrize) {
+            // 炸了
+            return ;
+        }
+
+        if (selectedPrize.getTotal() <= 0) {
+            // 没数了
+            return ;
+        }
+
+        selectedPrize.setTotal(selectedPrize.getTotal() - 1);
+        luckPrizeService.updateById(selectedPrize);
+
+        // 插入抽奖记录
+        LuckLotteryRecordDO luckLotteryRecord = LuckLotteryRecordDO.builder()
+                .userId(userId)
+                .lotteryId(luckLottery.getId())
+                .prizeId(selectedPrize.getId())
+                .type(selectedPrize.getType())
+                .extend(selectedPrize.getExtend())
+                .isReceive(false)
+                .isDeliver(false)
+                .build();
+        luckLotteryRecordMapper.insert(luckLotteryRecord);
+
+    }
 
     @Override
     public List<LuckLotteryRecordDetailRespVO> getLuckLotteryRecordList(Long lotteryId, Long userId) {
@@ -214,6 +271,20 @@ public class LuckLotteryRecordServiceImpl implements LuckLotteryRecordService {
             luckLotteryRecord.setReceiveInfo(reqVO.getReceiveInfo());
             luckLotteryRecordMapper.updateById(luckLotteryRecord);
         }
+    }
 
+    @Override
+    public LuckLotteryRecordDetailRespVO getLast(Long lotteryId, Long userId) {
+        LuckLotteryRecordDO record = luckLotteryRecordMapper.getLast(lotteryId, userId);
+        if (null == record) {
+            return null;
+        }
+        UserInfoRespDTO user =
+                mendunerUserApi.getUser(record.getUserId()).getCheckedData();
+        return LuckLotteryRecordConvert.INSTANCE.convertDetail(
+                LuckLotteryRecordConvert.INSTANCE.convert(record),
+                luckLotteryService.detail(record.getLotteryId()),
+                luckPrizeService.detail(record.getPrizeId()),
+                user);
     }
 }

+ 8 - 0
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/service/luck/LuckPrizeService.java

@@ -121,5 +121,13 @@ public interface LuckPrizeService {
     Map<String, List<LuckPrizeDetailRespVO>> getLuckPrizeExtendAreaMap(Long lotteryId, String type, Long areaId);
 
 
+    /**
+     * 根据抽奖活动id和自定义奖品(所在城市名称)获取相关城市的奖品数据
+     *
+     * @param lotteryId 抽奖活动id
+     * @param cityName 城市名称
+     * @return 分页结果
+     */
+    List<LuckPrizeDetailRespVO> getByLotteryIdAndCityName(Long lotteryId,String cityName);
 
 }

+ 13 - 0
citu-module-mall/citu-module-promotion-biz/src/main/java/com/citu/module/promotion/service/luck/LuckPrizeServiceImpl.java

@@ -375,4 +375,17 @@ public class LuckPrizeServiceImpl implements LuckPrizeService {
 
     }
 
+    @Override
+    public List<LuckPrizeDetailRespVO> getByLotteryIdAndCityName(Long lotteryId, String cityName) {
+        List<LuckPrizeDetailRespVO> list = getByLotteryId(lotteryId);
+        if (CollUtil.isEmpty(list)) {
+            return list;
+        }
+        return list.stream().filter(item -> {
+            if (LuckPrizeTypeEnum.CUSTOM.getType().equals(item.getType())) {
+                return cityName.equals(item.getExtend().getCityName());
+            }
+            return false;
+        }).collect(Collectors.toList());
+    }
 }

+ 2 - 2
citu-module-pay/citu-module-pay-biz/src/main/java/com/citu/module/pay/controller/app/currency/AppPayCurrencyController.java

@@ -2,7 +2,7 @@ package com.citu.module.pay.controller.app.currency;
 
 import com.citu.framework.common.enums.UserTypeEnum;
 import com.citu.framework.common.pojo.CommonResult;
-import javax.annotation.security.PermitAll;
+import com.citu.framework.security.core.annotations.PreAuthenticated;
 import com.citu.module.pay.controller.app.currency.vo.currency.AppPayCurrencyRespVO;
 import com.citu.module.pay.convert.currency.PayCurrencyConvert;
 import com.citu.module.pay.dal.dataobject.currency.PayCurrencyDO;
@@ -36,7 +36,7 @@ public class AppPayCurrencyController {
 
     @GetMapping("/get")
     @Operation(summary = "获取货币账户")
-    @PermitAll
+    @PreAuthenticated
     public CommonResult<AppPayCurrencyRespVO> getPayCurrency() {
         PayCurrencyDO currency = payCurrencyService.getOrCreateCurrency(
                 getLoginUserDataId(Long.class), getLoginUserId(), UserTypeEnum.MEMBER.getValue());

+ 2 - 2
citu-module-pay/citu-module-pay-biz/src/main/java/com/citu/module/pay/controller/app/wallet/AppPayWalletController.java

@@ -2,7 +2,7 @@ package com.citu.module.pay.controller.app.wallet;
 
 import com.citu.framework.common.enums.UserTypeEnum;
 import com.citu.framework.common.pojo.CommonResult;
-import javax.annotation.security.PermitAll;
+import com.citu.framework.security.core.annotations.PreAuthenticated;
 import com.citu.module.pay.controller.app.wallet.vo.wallet.AppPayWalletRespVO;
 import com.citu.module.pay.convert.wallet.PayWalletConvert;
 import com.citu.module.pay.dal.dataobject.wallet.PayWalletDO;
@@ -35,7 +35,7 @@ public class AppPayWalletController {
 
     @GetMapping("/get")
     @Operation(summary = "获取钱包")
-    @PermitAll
+    @PreAuthenticated
     public CommonResult<AppPayWalletRespVO> getPayWallet() {
         PayWalletDO wallet = payWalletService.getOrCreateWallet(getLoginUserId(), UserTypeEnum.MEMBER.getValue());
         return success(PayWalletConvert.INSTANCE.convert(wallet));

+ 3 - 1
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/api/error/ErrorRecordApiImpl.java

@@ -2,6 +2,7 @@ package com.citu.module.menduner.system.api.error;
 
 import com.citu.framework.common.pojo.CommonResult;
 import com.citu.framework.common.util.object.BeanUtils;
+import com.citu.framework.security.core.LoginUser;
 import com.citu.module.menduner.common.util.LoginUserContext;
 import com.citu.module.menduner.system.dal.dataobject.error.ErrorRecordDO;
 import com.citu.module.menduner.system.dal.mysql.error.ErrorRecordMapper;
@@ -22,8 +23,9 @@ public class ErrorRecordApiImpl implements ErrorRecordApi{
     @Override
     public CommonResult<Boolean> create(ErrorRecordReqDTO req) {
         ErrorRecordDO insert = BeanUtils.toBean(req, ErrorRecordDO.class);
+        LoginUser loginUser = LoginUserContext.get();
         insert.setUserId(LoginUserContext.getUserId2());
-        insert.setEnterpriseId(LoginUserContext.getEnterpriseId3());
+        insert.setEnterpriseId(LoginUserContext.getEnterpriseId2(loginUser));
         errorRecordMapper.insert(insert);
         return success(true);
     }