商品添加店铺权限数据控制

This commit is contained in:
liyj 2025-06-16 16:24:55 +08:00
parent ad7d2a8bde
commit 76e14d4bd0
9 changed files with 158 additions and 44 deletions

View File

@ -50,7 +50,7 @@ public class UserDto {
* @return
*/
public boolean isStore() {
return StrUtil.isNotBlank(this.store_id);
return StrUtil.isNotBlank(this.store_id)&&!"0".equals(this.store_id);
}
/**

View File

@ -8,6 +8,8 @@ import com.suisung.mall.common.modules.base.ShopBaseProductBrand;
import com.suisung.mall.common.modules.base.ShopBaseProductType;
import com.suisung.mall.common.service.impl.BaseControllerImpl;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.ContextUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.shop.base.service.ShopBaseProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -18,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.suisung.mall.common.utils.ContextUtil.getCurrentUser;
@ -58,8 +62,7 @@ public class ShopBaseProductBrandController extends BaseControllerImpl {
@RequestMapping(value = "/brands", method = RequestMethod.GET)
public CommonResult getList(@RequestParam(name = "brand_name", required = false) String brand_name) {
QueryWrapper<ShopBaseProductBrand> queryWrapper = new QueryWrapper<>();
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
queryWrapper.eq("store_id", store_id);
new FilterUtils<ShopBaseProductBrand>().applyStoreFilter(queryWrapper);
queryWrapper.eq("brand_enable", 1);
queryWrapper.orderByAsc("brand_id");
if (StrUtil.isNotEmpty(brand_name)) {
@ -71,8 +74,23 @@ public class ShopBaseProductBrandController extends BaseControllerImpl {
@ApiOperation(value = "品牌表 - 540-编辑", notes = "品牌表 - 540-编辑")
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public CommonResult edit(ShopBaseProductBrand shopBaseProductBrand) {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
shopBaseProductBrand.setStore_id(store_id);
if(shopBaseProductBrand.getBrand_id()!=null) {
ShopBaseProductBrand oldShopBaseProductBrand= shopBaseProductBrandService.get(shopBaseProductBrand.getBrand_id());
if (oldShopBaseProductBrand == null) {
return CommonResult.failed("记录不存在");
}
// 店员只能操作自己店铺的数据
if (getCurrentUser().isStore() &&
!oldShopBaseProductBrand.getStore_id().equals(new FilterUtils<ShopBaseProductBrand>().
getCurrentUserStoreId())) {
return CommonResult.failed("无权限修改该记录");
}
shopBaseProductBrand.setStore_id(oldShopBaseProductBrand.getStore_id());
}else{
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
shopBaseProductBrand.setStore_id(store_id);
}
return CommonResult.success(shopBaseProductBrandService.saveOrUpdateBrand(shopBaseProductBrand));
}
@ -88,12 +106,45 @@ public class ShopBaseProductBrandController extends BaseControllerImpl {
@ApiOperation(value = "品牌表 -通过brand_id删除", notes = "品牌表 -通过brand_id删除")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public CommonResult delete(@RequestParam(name = "brand_ids") String brand_ids) {
List<Integer> ids = Arrays.stream(brand_ids.split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
// =============== 批量权限校验 ===============
if (getCurrentUser().isStore()) {
Integer currentStoreId = new FilterUtils<ShopBaseProductBrand>().getCurrentUserStoreId();
List<ShopBaseProductBrand> records = shopBaseProductBrandService.listByIds(ids);
// 验证所有记录是否属于当前店铺
boolean anyUnauthorized = records.stream()
.anyMatch(record -> !currentStoreId.equals(record.getStore_id()));
if (anyUnauthorized) {
return CommonResult.failed("包含无权限删除的记录");
}
}
return CommonResult.success(shopBaseProductBrandService.remove(Arrays.asList(brand_ids.split(","))));
}
@ApiOperation(value = "品牌表 -批量删除", notes = "品牌表 -批量删除")
@RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
public CommonResult deleteBatch(@RequestParam(name = "brand_ids") String brand_ids) {
List<Integer> ids = Arrays.stream(brand_ids.split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
// =============== 批量权限校验 ===============
if (getCurrentUser().isStore()) {
Integer currentStoreId = new FilterUtils<ShopBaseProductBrand>().getCurrentUserStoreId();
List<ShopBaseProductBrand> records = shopBaseProductBrandService.listByIds(ids);
// 验证所有记录是否属于当前店铺
boolean anyUnauthorized = records.stream()
.anyMatch(record -> !currentStoreId.equals(record.getStore_id()));
if (anyUnauthorized) {
return CommonResult.failed("包含无权限删除的记录");
}
}
return CommonResult.success(shopBaseProductBrandService.remove(Arrays.asList(brand_ids.split(","))));
}
@ -108,8 +159,7 @@ public class ShopBaseProductBrandController extends BaseControllerImpl {
public CommonResult getBrandS() {
String brand_name = getParameter("brand_name");
QueryWrapper<ShopBaseProductBrand> queryWrapper = new QueryWrapper<>();
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
queryWrapper.eq("store_id", store_id);
new FilterUtils<ShopBaseProductBrand>().applyStoreFilter(queryWrapper);
queryWrapper.like("brand_name", brand_name);
return CommonResult.success(shopBaseProductBrandService.find(queryWrapper));
}

View File

@ -10,6 +10,7 @@ import com.suisung.mall.common.modules.base.ShopBaseProductSpec;
import com.suisung.mall.common.modules.product.ShopProductSpecItem;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.ContextUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.common.utils.I18nUtil;
import com.suisung.mall.shop.base.service.ShopBaseProductSpecService;
import com.suisung.mall.shop.product.service.ShopProductSpecItemService;
@ -24,6 +25,8 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;
import static com.suisung.mall.common.utils.ContextUtil.getCurrentUser;
/**
* <p>
* 商品规格表 前端控制器
@ -56,9 +59,7 @@ public class ShopBaseProductSpecController {
@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
QueryWrapper<ShopBaseProductSpec> queryWrapper = new QueryWrapper<>();
UserDto userDto= ContextUtil.getCurrentUser();
Integer storeId = Integer.valueOf(userDto.getStore_id());
queryWrapper.eq("store_id",storeId);
new FilterUtils<ShopBaseProductSpec>().applyStoreFilter(queryWrapper);
queryWrapper.orderByAsc("spec_id");
if (CheckUtil.isNotEmpty(shopBaseProductSpec.getSpec_name())) {
queryWrapper.like("spec_name", shopBaseProductSpec.getSpec_name());
@ -79,15 +80,31 @@ public class ShopBaseProductSpecController {
@RequestParam(value = "spec_category_id", required = false) Integer spec_category_id,
@RequestParam(value = "spec_order") Integer spec_order,
@RequestParam(value = "spec_id", required = false) Integer spec_id) {
UserDto userDto= ContextUtil.getCurrentUser();
Integer storeId = Integer.valueOf(userDto.getStore_id());
UserDto userDto= getCurrentUser();
ShopBaseProductSpec shopBaseProductSpec = new ShopBaseProductSpec();
shopBaseProductSpec.setStore_id(storeId);
if(spec_id!=null){
ShopBaseProductSpec oldSpec= shopBaseProductSpecService.get(spec_id);
if (oldSpec == null) {
return CommonResult.failed("记录不存在");
}
// 店员只能操作自己店铺的数据
if (getCurrentUser().isStore() &&
!oldSpec.getStore_id().equals(new FilterUtils<ShopBaseProductSpec>().getCurrentUserStoreId())) {
return CommonResult.failed("无权限修改该记录");
}
// 保留原店铺ID防止篡改
shopBaseProductSpec.setStore_id(oldSpec.getStore_id());
}else {
Integer storeId = Integer.valueOf(userDto.getStore_id());
shopBaseProductSpec.setStore_id(storeId);
}
shopBaseProductSpec.setSpec_id(spec_id);
shopBaseProductSpec.setSpec_name(spec_name);
shopBaseProductSpec.setSpec_format(spec_format);
shopBaseProductSpec.setSpec_category_id(spec_category_id);
shopBaseProductSpec.setSpec_order(spec_order);
return CommonResult.success(shopBaseProductSpecService.saveOrUpdate(shopBaseProductSpec));
}
@ -104,14 +121,24 @@ public class ShopBaseProductSpecController {
if (CollUtil.isEmpty(spes)) {
throw new ApiException(I18nUtil._("商品规格编号异常spec_id: ") + spec_ids);
}
QueryWrapper<ShopProductSpecItem> queryWrapper = new QueryWrapper<>();
queryWrapper.in("spec_id", spes);
List<Serializable> spec_item_ids = shopProductSpecItemService.findKey(queryWrapper);
if (CollUtil.isNotEmpty(spec_item_ids)) {
throw new ApiException(String.format(I18nUtil._("不能删除正在被商品规格值表使用的规格!商品规格值编号【%s】"), CollUtil.join(spec_item_ids, ",")));
}
if (getCurrentUser().isStore()) {
Integer currentStoreId = Integer.valueOf(getCurrentUser().getStore_id());
List<ShopProductSpecItem> records = shopProductSpecItemService.listByIds(spes);
// 验证所有记录是否属于当前店铺
boolean anyUnauthorized = records.stream()
.anyMatch(record -> !currentStoreId.equals(record.getStore_id()));
if (anyUnauthorized) {
return CommonResult.failed("包含无权限删除的记录");
}
}
return CommonResult.success(shopBaseProductSpecService.remove(spes));
}

View File

@ -2,11 +2,13 @@ package com.suisung.mall.shop.base.controller.admin;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.modules.base.ShopBaseProductType;
import com.suisung.mall.common.modules.product.ShopProductInfo;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.shop.base.service.ShopBaseProductTypeService;
import com.suisung.mall.shop.product.mapper.ShopProductInfoMapper;
import com.suisung.mall.shop.product.service.ShopProductItemService;
@ -20,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static com.suisung.mall.common.utils.ContextUtil.getCurrentUser;
@ -58,9 +61,12 @@ public class ShopBaseProductTypeController {
@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
QueryWrapper<ShopBaseProductType> queryWrapper = new QueryWrapper<>();
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
new FilterUtils<ShopBaseProductType>().applyStoreFilter(queryWrapper);
// if(getCurrentUser().isStore()){
// Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
// queryWrapper.eq("store_id", store_id);
// }
queryWrapper.orderByAsc("type_id");
queryWrapper.eq("store_id", store_id);
if (CheckUtil.isNotEmpty(shopBaseProductType.getType_name())) {
queryWrapper.like("type_name", shopBaseProductType.getType_name());
}
@ -79,9 +85,8 @@ public class ShopBaseProductTypeController {
@ApiOperation(value = "商品类型表-强调共性,类别cat是强调区别.-列表查询", notes = "商品类型表-强调共性,类别cat是强调区别.-列表查询")
@RequestMapping(value = "/lists", method = RequestMethod.GET)
public CommonResult lists() {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
QueryWrapper<ShopBaseProductType> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("store_id", store_id);
new FilterUtils<ShopBaseProductType>().applyStoreFilter(queryWrapper);
queryWrapper.orderByAsc("type_id");
return CommonResult.success(shopBaseProductTypeService.find(queryWrapper));
}
@ -89,9 +94,8 @@ public class ShopBaseProductTypeController {
@ApiOperation(value = "商品类型表-强调共性,类别cat是强调区别.-列表查询", notes = "商品类型表-强调共性,类别cat是强调区别.-列表查询")
@RequestMapping(value = "/getLists", method = RequestMethod.GET)
public CommonResult getLists() {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
QueryWrapper<ShopBaseProductType> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("store_id", store_id);
new FilterUtils<ShopBaseProductType>().applyStoreFilter(queryWrapper);
queryWrapper.orderByAsc("type_id");
queryWrapper.eq("type_is_draft", 0);
return CommonResult.success(shopBaseProductTypeService.find(queryWrapper));
@ -109,16 +113,21 @@ public class ShopBaseProductTypeController {
public CommonResult edit(ShopBaseProductType shopBaseProductType) {
String type_spec_ids = shopBaseProductType.getType_spec_ids();
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
shopBaseProductType.setStore_id(store_id);
//读取当前已经存在的type_id
Integer type_id = shopBaseProductType.getType_id();
//修改
if (CheckUtil.isNotEmpty(type_id)) {
ShopBaseProductType shopBaseProductTypeOld = shopBaseProductTypeService.get(type_id);
if (shopBaseProductTypeOld != null) {
// 店员只能操作自己店铺的数据
if (getCurrentUser().isStore() &&
!shopBaseProductTypeOld.getStore_id().equals(new FilterUtils<ShopBaseProductType>().getCurrentUserStoreId())) {
return CommonResult.failed("无权限修改该记录");
}
// 保留原店铺ID防止篡改
shopBaseProductType.setStore_id(shopBaseProductTypeOld.getStore_id());
String type_spec_ids_old = shopBaseProductTypeOld.getType_spec_ids();
List<Integer> type_spec_id_row_old = Convert.toList(Integer.class, type_spec_ids_old);
@ -137,7 +146,12 @@ public class ShopBaseProductTypeController {
}
}
}
}else {
return CommonResult.failed("记录不存在");
}
}else {//新增
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
shopBaseProductType.setStore_id(store_id);
}
if (StrUtil.isBlank(type_spec_ids)) {
@ -164,6 +178,22 @@ public class ShopBaseProductTypeController {
@ApiOperation(value = "商品类型表-强调共性,类别cat是强调区别.-通过type_id删除", notes = "商品类型表-强调共性,类别cat是强调区别.-通过type_id删除")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public CommonResult delete(@RequestParam(name = "type_ids") String type_ids) {
List<Integer> ids = Arrays.stream(type_ids.split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
// =============== 批量权限校验 ===============
if (getCurrentUser().isStore()) {
Integer currentStoreId = new FilterUtils<ShopBaseProductType>().getCurrentUserStoreId();
List<ShopBaseProductType> records = shopBaseProductTypeService.listByIds(ids);
// 验证所有记录是否属于当前店铺
boolean anyUnauthorized = records.stream()
.anyMatch(record -> !currentStoreId.equals(record.getStore_id()));
if (anyUnauthorized) {
return CommonResult.failed("包含无权限删除的记录");
}
}
return CommonResult.success(shopBaseProductTypeService.removeType(Arrays.asList(type_ids.split(","))));
}

View File

@ -13,6 +13,7 @@ import com.suisung.mall.common.exception.ApiException;
import com.suisung.mall.common.modules.base.ShopBaseProductBrand;
import com.suisung.mall.common.modules.base.ShopBaseProductCategory;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.common.utils.I18nUtil;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.base.mapper.ShopBaseProductBrandMapper;
@ -52,8 +53,10 @@ public class ShopBaseProductBrandServiceImpl extends BaseServiceImpl<ShopBasePro
@Override
public Map<String, List<ShopBaseProductBrand>> brandMap() {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
List<ShopBaseProductBrand> brands = find(new QueryWrapper<ShopBaseProductBrand>().eq("brand_enable", 1).eq("store_id", store_id));
QueryWrapper<ShopBaseProductBrand> queryWrapper= new QueryWrapper<>();
queryWrapper.eq("brand_enable", 1);
new FilterUtils<ShopBaseProductBrand>().applyStoreFilter(queryWrapper);
List<ShopBaseProductBrand> brands = find(queryWrapper);
if (CollectionUtil.isEmpty(brands)) {
throw new ApiException(I18nUtil._("启用品牌列表为空!"));
@ -94,11 +97,7 @@ public class ShopBaseProductBrandServiceImpl extends BaseServiceImpl<ShopBasePro
public Map getList(QueryWrapper<ShopBaseProductBrand> queryWrapper, Integer pageNum, Integer pageSize) {
queryWrapper.orderByDesc("brand_id");
if (getCurrentUser().isStore()) {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
queryWrapper.eq("store_id", store_id);
}
new FilterUtils<ShopBaseProductBrand>().applyStoreFilter(queryWrapper);
String brand_name = getParameter("brand_name");
if (StrUtil.isNotBlank(brand_name)) {

View File

@ -29,10 +29,7 @@ import com.suisung.mall.common.modules.store.ShopStoreActivityItem;
import com.suisung.mall.common.modules.store.ShopStoreProductCategory;
import com.suisung.mall.common.modules.user.ShopUserCart;
import com.suisung.mall.common.pojo.dto.ProductSearchDTO;
import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.common.utils.CommonUtil;
import com.suisung.mall.common.utils.I18nUtil;
import com.suisung.mall.common.utils.UserInfoService;
import com.suisung.mall.common.utils.*;
import com.suisung.mall.core.web.BaseQueryWrapper;
import com.suisung.mall.core.web.service.RedisService;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
@ -245,18 +242,22 @@ public class ShopBaseProductCategoryServiceImpl extends BaseServiceImpl<ShopBase
@Override
public List<Map> getCategoryTree() {
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
List<Map> cateTree = Convert.toList(Map.class, redisService.get(RedisConstant.Product_Cate_Key+":"+store_id));
QueryWrapper<ShopBaseProductCategory> queryWrapper = new QueryWrapper<>();
String redisKey=RedisConstant.Product_Cate_Key;
if(getCurrentUser().isStore()){
Integer store_id = Convert.toInt(getCurrentUser().getStore_id());
queryWrapper.eq("store_id", store_id);
redisKey=RedisConstant.Product_Cate_Key+":"+store_id;
}
List<Map> cateTree = Convert.toList(Map.class, redisService.get(redisKey));
if (CollUtil.isNotEmpty(cateTree)) {
return cateTree;
}
QueryWrapper<ShopBaseProductCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("store_id", store_id);
queryWrapper.orderByAsc("category_order");
List<Map> categories = Convert.toList(Map.class, find(queryWrapper));
categories = shopProductBaseService.fixProductTypeDate(categories);
List<Map> categoryTree = getCategoryTree(categories, 0);
redisService.set(RedisConstant.Product_Cate_Key, categoryTree);
redisService.set(redisKey, categoryTree);
return categoryTree;
}
@ -940,7 +941,11 @@ public class ShopBaseProductCategoryServiceImpl extends BaseServiceImpl<ShopBase
@Override
public Map lists() {
List<ShopBaseProductCategory> shopBaseProductCategories = shopBaseProductCategoryMapper.selectCategoryList(getCurrentUser().getStore_id());
String storeId=null;
if(getCurrentUser().isStore()){
storeId=getCurrentUser().getStore_id();
}
List<ShopBaseProductCategory> shopBaseProductCategories = shopBaseProductCategoryMapper.selectCategoryList(storeId);
Map map = new HashMap(1);
map.put("items", shopBaseProductCategories);
return map;
@ -1268,4 +1273,5 @@ public class ShopBaseProductCategoryServiceImpl extends BaseServiceImpl<ShopBase
// 设置cache
redisService.del(cache_key);
}
}

View File

@ -8,6 +8,7 @@ import com.suisung.mall.common.modules.base.ShopBaseProductCategory;
import com.suisung.mall.common.modules.base.ShopBaseProductSpec;
import com.suisung.mall.common.modules.product.ShopProductSpecItem;
import com.suisung.mall.common.utils.ContextUtil;
import com.suisung.mall.common.utils.FilterUtils;
import com.suisung.mall.common.utils.I18nUtil;
import com.suisung.mall.core.web.service.RedisService;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
@ -45,9 +46,7 @@ public class ShopBaseProductSpecServiceImpl extends BaseServiceImpl<ShopBaseProd
@Override
public Map<String, List<ShopBaseProductSpec>> specMap() {
QueryWrapper<ShopBaseProductSpec> queryWrapper= new QueryWrapper<>();
UserDto userDto= ContextUtil.getCurrentUser();
Integer store_id = Integer.valueOf(userDto.getStore_id());
queryWrapper.eq("store_id",store_id);
new FilterUtils<ShopBaseProductSpec>().applyStoreFilter(queryWrapper);
List<ShopBaseProductSpec> specs = find(queryWrapper);
Map<String, List<ShopBaseProductSpec>> map = new HashMap<>();
for (ShopBaseProductSpec spec : specs) {

View File

@ -109,6 +109,7 @@ public abstract class SyncBaseThirdSxAbstract{
JSONObject o = (JSONObject) categoryListJSON.get(i);
ShopBaseProductType productType=new ShopBaseProductType();
productType.setType_is_draft(1);//发布
if (o != null) {
// 重要分类类型处理强调共性
Integer typeId = 1001;

View File

@ -31,7 +31,9 @@
c.category_name AS `name`,
t.type_name AS `type_name`
FROM shop_base_product_category c JOIN shop_base_product_type t ON c.type_id = t.type_id
where c.store_id=#{storeId}
<if test="storeId!=null and storeId!=''">
where c.store_id=#{storeId}
</if>
</select>
</mapper>