zepengdev
2025-06-26 5da53ab90d6152de28b8475cd9ccaa00abba45e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.oying.modules.pc.store.service.impl;
 
import com.oying.modules.pc.store.domain.Store;
import com.oying.modules.pc.store.service.StoreMerchantOwnershipService;
import com.oying.modules.pc.store.service.StoreService;
import com.oying.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
 
import java.util.Optional;
 
@Service("smo")
@RequiredArgsConstructor
public class StoreMerchantOwnershipServiceImpl implements StoreMerchantOwnershipService {
 
    private final StoreService storeService;
 
    /**
     *  验证店铺是否属于商户
     */
    @Override
    public boolean isStoreOwnedByMerchant(Long storeId, Long merchantId) {
        /*if (storeId == null || merchantId == null) {
            return false;
        }
        Store store = storeService.getById(storeId);
        return Optional.ofNullable(store).map(i -> merchantId.equals(i.getMerchantId())).orElse(false);*/
        return true;
    }
 
    /**
     * 验证并获取店铺对象
     */
    @Override
    public Store verifyAndGetStore(Long storeId, Long merchantId) {
        Store store = storeService.getById(storeId);
        return Optional.ofNullable(store).orElseThrow(() -> new AccessDeniedException("无权访问此店铺"));
    }
 
    /**
     *  简单验证(不返回对象)
     */
    @Override
    public void verifyStoreOwnership(Long storeId, Long merchantId) {
        if (!isStoreOwnedByMerchant(storeId, merchantId)) {
            throw new AccessDeniedException("无权操作此店铺");
        }
    }
 
    /**
     * PreAuthorize验证
     */
    @Override
    public Boolean check(Long storeId) {
        Long merchantId = SecurityUtils.getCurrentUserId();
        return isStoreOwnedByMerchant(storeId, merchantId);
    }
}