Commit 338a1f20 by guojuxing

门店选择器子管理员权限数据接口

parent 264f9436
package com.gic.store.constant;
import java.util.HashSet;
import java.util.Set;
/**
*
* @ClassName: StoreESFieldsEnum
......@@ -197,6 +200,25 @@ public enum StoreESFieldsEnum {
this.desc = desc;
}
/**
* 自定义字段的key
* @return
*/
public static Set<String> getStoreFieldKey() {
Set<String> storeField = new HashSet<>();
storeField.add(StoreESFieldsEnum.C1.getField());
storeField.add(StoreESFieldsEnum.C2.getField());
storeField.add(StoreESFieldsEnum.C3.getField());
storeField.add(StoreESFieldsEnum.C4.getField());
storeField.add(StoreESFieldsEnum.C5.getField());
storeField.add(StoreESFieldsEnum.C6.getField());
storeField.add(StoreESFieldsEnum.C7.getField());
storeField.add(StoreESFieldsEnum.C8.getField());
storeField.add(StoreESFieldsEnum.C9.getField());
storeField.add(StoreESFieldsEnum.C10.getField());
return storeField;
}
public String getField() {
return field;
}
......
package com.gic.store.dto;
import java.io.Serializable;
/**
* 门店选择器登录人权限
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 11:37 AM

*/
public class StoreResourceDTO implements Serializable{
private static final long serialVersionUID = -1670193003220258987L;
private String id;
private String name;
/**
* 1自有 2共享
*/
private Integer ownType;
/**
* 自有商户名称
*/
private String ownerEntName;
private Integer ownerEntId;
private Integer enterpriseId;
public String getId() {
return id;
}
public StoreResourceDTO setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public StoreResourceDTO setName(String name) {
this.name = name;
return this;
}
public Integer getOwnType() {
return ownType;
}
public StoreResourceDTO setOwnType(Integer ownType) {
this.ownType = ownType;
return this;
}
public String getOwnerEntName() {
return ownerEntName;
}
public StoreResourceDTO setOwnerEntName(String ownerEntName) {
this.ownerEntName = ownerEntName;
return this;
}
public Integer getOwnerEntId() {
return ownerEntId;
}
public StoreResourceDTO setOwnerEntId(Integer ownerEntId) {
this.ownerEntId = ownerEntId;
return this;
}
public Integer getEnterpriseId() {
return enterpriseId;
}
public StoreResourceDTO setEnterpriseId(Integer enterpriseId) {
this.enterpriseId = enterpriseId;
return this;
}
@Override
public String toString() {
return "StoreResourceDTO{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", ownType=" + ownType +
", ownerEntName='" + ownerEntName + '\'' +
", ownerEntId=" + ownerEntId +
", enterpriseId=" + enterpriseId +
'}';
}
}
package com.gic.store.service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.store.dto.StoreResourceDTO;
import java.util.List;
import java.util.Set;
/**
* 门店选择器资源过滤
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 10:49 AM

*/
public interface StoreResourceApiService {
/**
* 获取登录人的权限
* @param userId 登录人ID
* @param esField StoreESFieldsEnum 枚举
* @return
*/
ServiceResponse<List<StoreResourceDTO>> getAuthDataFromStoreWidget(Integer userId, String esField);
}
package com.gic.store.service.outer.impl;
import java.util.Collections;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.service.ResourceGroupApiService;
import com.gic.auth.service.UserApiService;
import com.gic.enterprise.exception.CommonException;
import com.gic.store.dto.StoreResourceDTO;
import com.gic.store.dto.StoreWidgetDTO;
import com.gic.store.service.StoreResourceApiService;
import com.gic.store.service.StoreWidgetApiService;
import com.gic.store.utils.resource.AuthDataHandleUtils;
/**
* 门店选择器资源过滤
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 10:54 AM

*/
@Service("storeResourceApiService")
public class StoreResourceApiServiceImpl implements StoreResourceApiService{
private static final Logger LOGGER = LogManager.getLogger(StoreResourceApiServiceImpl.class);
@Autowired
private StoreWidgetApiService storeWidgetApiService;
@Autowired
private UserApiService userApiService;
@Autowired
private ResourceGroupApiService resourceGroupApiService;
@Override
public ServiceResponse<List<StoreResourceDTO>> getAuthDataFromStoreWidget(Integer userId, String esField) {
UserDTO userDTO = getUser(userId);
List<StoreResourceDTO> list;
if (userDTO.getSuperAdmin() == 1) {
//超管
list = AuthDataHandleUtils.getAuthData(userDTO.getEnterpriseId(), esField);
return ServiceResponse.success(list);
}
ServiceResponse<com.gic.auth.dto.StoreResourceDTO> storeResource = resourceGroupApiService.getStoreResourceByUserId(userId);
boolean hasStoreWidgetId = storeResource.isSuccess() && storeResource.getResult() != null
&& storeResource.getResult().getStoreResourceId() != null;
if (!hasStoreWidgetId) {
LOGGER.info("该子管理员没有配置资源组门店选择器权限");
return ServiceResponse.success(getAuthDataWhenNo());
}
Integer storeWidgetId = storeResource.getResult().getStoreResourceId();
ServiceResponse<StoreWidgetDTO> storeWidget = storeWidgetApiService.getStoreWidget(storeWidgetId);
if (storeWidget.isSuccess()) {
//todo 目前做了品牌,待补充其他类型
return ServiceResponse.success(AuthDataHandleUtils.getUserData(storeWidget.getResult(), esField));
} else {
LOGGER.info("该子管理员没有配置资源组门店选择器权限");
return ServiceResponse.success(getAuthDataWhenNo());
}
}
private UserDTO getUser(Integer userId) {
ServiceResponse<UserDTO> response = userApiService.getUserInfoById(userId);
if (!response.isSuccess()) {
throw new CommonException(response.getCode(), response.getMessage());
}
return response.getResult();
}
/**
* 没有资源的时候
* @return
*/
private static List<StoreResourceDTO> getAuthDataWhenNo() {
return Collections.emptyList();
}
}
package com.gic.store.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring ApplicationContext 工具类
* @ClassName: ApplicationContextUtils

* @Description: 

* @author guojuxing

* @date 2020/11/11 10:22 AM

*/
@Component
public class ApplicationContextUtils implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtils.applicationContext = applicationContext;
}
public static <T> T getBean(String beanName) {
if (applicationContext.containsBean(beanName)) {
return (T) applicationContext.getBean(beanName);
}
return null;
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
package com.gic.store.utils;
import javax.validation.ConstraintViolationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.exception.CommonException;
/**
* api层错误全部统一返回
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/2/9 9:46 AM

*/
@Aspect
@Configuration
public class ExceptionResultAop {
private Logger logger = LogManager.getLogger(ExceptionResultAop.class);
/**
* 环绕通知
*
* @param joinPoint
* @return
* @throws Throwable
*/
@Around(value = "execution(* com.gic.*.service.outer.impl..*.*(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
try {
// 执行当前方法
return joinPoint.proceed();
} catch (Throwable throwable) {
logger.warn("拦截器错误", throwable);
return handlerException(throwable);
}
}
private ServiceResponse handlerException(Throwable e) {
ServiceResponse response = new ServiceResponse();
if (e instanceof CommonException) {
response.setCode(((CommonException) e).getErrorCode());
} else if (e instanceof ConstraintViolationException) {
response.setCode(ErrorCode.SYSTEM_ERROR.getCode());
} else {
response.setCode(ErrorCode.SYSTEM_ERROR.getCode());
}
response.setMessage(e.getMessage());
return response;
}
}
\ No newline at end of file
package com.gic.store.utils.resource;
import com.gic.store.dto.StoreResourceDTO;
import java.util.List;
import java.util.Set;
/** 查询超管的权限
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 11:16 AM

*/
public abstract class AbstractAuthDataUtils {
/**
* 查询超管数据
* @param enterpriseId
* @return id集合
*/
abstract List<StoreResourceDTO> getAuthData(Integer enterpriseId);
/**
* 子管理员权限过滤
* @param authIdSet 门店选择器配置的对应数据ID集合
* @return
*/
abstract List<StoreResourceDTO> auth(Set<String> authIdSet);
}
package com.gic.store.utils.resource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gic.store.constant.StoreESFieldsEnum;
import com.gic.store.dto.StoreResourceDTO;
import com.gic.store.dto.StoreWidgetDTO;
import java.util.*;
/**
* 查询超管的权限
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 11:31 AM

*/
public class AuthDataHandleUtils {
private static Map<String, AbstractAuthDataUtils> map = new HashMap<>(16);
public final static String STORE_FIELD_KEY = "storeFieldKey";
static {
//门店分组不用过滤
map.put(StoreESFieldsEnum.STOREBRANDIDLIST.getField(), new BrandAbstractAuthData());
}
/**
* 查询超管数据
* @param enterpriseId
* @param esField
* @return
*/
public static List<StoreResourceDTO> getAuthData(Integer enterpriseId, String esField) {
return map.get(esField).getAuthData(enterpriseId);
}
/**
* 子管理员权限数据
* @param storeWidget 门店选择器记录
* @param esField
* @return
*/
public static List<StoreResourceDTO> getUserData(StoreWidgetDTO storeWidget, String esField) {
Set<String> authIdSet = getAuthData(storeWidget, esField);
return map.get(esField).auth(authIdSet);
}
protected static Set<String> getAuthData(StoreWidgetDTO storeWidget, String field) {
Set<String> authRegionSet = new HashSet<>();
String searchParam = storeWidget.getSearchParam();
JSONArray jsonArr = JSON.parseArray(searchParam);
if (jsonArr.isEmpty()) {
return authRegionSet;
}
JSONObject json = jsonArr.getJSONObject(0);
JSONArray list = json.getJSONArray("list");
Set<String> storeFieldKey = StoreESFieldsEnum.getStoreFieldKey();
//是否是自定义字段类型的数据
//因为这两者解析不一致
boolean isStoreField = isStoreField(field);
for (int i = 0, len = list.size(); i < len; i ++) {
JSONObject o = list.getJSONObject(i).getJSONObject("data");
String key = o.getString("key");
o.put("value", o.getString("value").replaceAll(",", " "));
if (isStoreField) {
//如果是自定义字段,应该存key,而不是value
if (storeFieldKey.contains(key)) {
authRegionSet.add(key);
}
continue;
}
if (key.equals(field)) {
String[] arr = o.getString("value").split(" ");
for(String s : arr){
authRegionSet.add(s);
}
}
}
return authRegionSet;
}
private static boolean isStoreField(String field) {
if (AuthDataHandleUtils.STORE_FIELD_KEY.equals(field)) {
return true;
}
return false;
}
}
package com.gic.store.utils.resource;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.store.dto.StoreBrandDTO;
import com.gic.store.dto.StoreResourceDTO;
import com.gic.store.dto.StoreWidgetDTO;
import com.gic.store.service.StoreBrandApiService;
import com.gic.store.utils.ApplicationContextUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 品牌数据
* @ClassName:
* @Description: 

* @author guojuxing

* @date 2021/5/26 11:18 AM

*/
public class BrandAbstractAuthData extends AbstractAuthDataUtils {
@Override
public List<StoreResourceDTO> getAuthData(Integer enterpriseId) {
StoreBrandApiService storeBrandApiService = ApplicationContextUtils.getBean("storeBrandApiService");
ServiceResponse<List<StoreBrandDTO>> response = storeBrandApiService.listAllStoreBrand(enterpriseId, null);
return getData(response);
}
@Override
public List<StoreResourceDTO> auth(Set<String> authIdSet) {
StoreBrandApiService storeBrandApiService = ApplicationContextUtils.getBean("storeBrandApiService");
ServiceResponse<List<StoreBrandDTO>> response = storeBrandApiService.listAllStoreBrandInfoByIds(authIdSet.stream()
.filter(e -> StringUtils.isNotBlank(e))
.mapToInt(e -> Integer.valueOf(e)).boxed()
.collect(Collectors.toList()));
return getData(response);
}
private List<StoreResourceDTO> getData(ServiceResponse<List<StoreBrandDTO>> response) {
if (response.isSuccess()) {
List<StoreBrandDTO> list = response.getResult();
if (CollectionUtils.isNotEmpty(list)) {
List<StoreResourceDTO> result = list.stream().map(e -> new StoreResourceDTO().setId(e.getStoreBrandId().toString())
.setName(e.getStoreBrandName()).setOwnType(e.getType()).setEnterpriseId(e.getEnterpriseId())).collect(Collectors.toList());
return result;
}
}
return Collections.EMPTY_LIST;
}
}
......@@ -66,6 +66,7 @@
<dubbo:service interface="com.gic.store.service.StoreTransferApiService" ref="storeTransferApiService" timeout="6000" />
<dubbo:service interface="com.gic.store.service.StoreWidgetLogApiService" ref="storeWidgetLogApiService" timeout="6000" />
<dubbo:service interface="com.gic.store.service.UnionStoreChangeApiService" ref="unionStoreChangeApiService" timeout="6000" />
<dubbo:service interface="com.gic.store.service.StoreResourceApiService" ref="storeResourceApiService" timeout="6000" />
<dubbo:reference interface="com.gic.weimob.api.service.WeimobStoreSiteApiService" id="weimobStoreSiteApiService" timeout="6000"/>
<dubbo:reference interface="com.gic.enterprise.service.WmStoreSyncLogApiService" id="wmStoreSyncLogApiService" timeout="6000"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment