//package com.crm.manager.interceptor;
//
//import com.crm.manager.service.UserRoleService;
//import com.crm.rely.backend.aspect.ControllerAop;
//import com.crm.rely.backend.core.constant.Constants;
//import com.crm.rely.backend.core.constant.PrefixEnum;
//import com.crm.rely.backend.core.constant.RoleConstants;
//import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
//import com.crm.rely.backend.core.exception.LoginException;
//import com.crm.rely.backend.core.exception.ServiceException;
//import com.crm.rely.backend.service.RedisService;
//import com.crm.rely.backend.util.HttpServletRequestUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.List;
//
//
//public class RoleInterceptor implements HandlerInterceptor {
//
//    @Autowired
//    private RedisService redisService;
//    @Autowired
//    private UserRoleService roleInfoService;
//
//    @Autowired
//    private ControllerAop controllerAop;
//
//    public RoleInterceptor() {
//
//    }
//
//    public RoleInterceptor(RedisService redisService, UserRoleService roleInfoService, ControllerAop controllerAop) {
//        this.redisService = redisService;
//
//        this.roleInfoService = roleInfoService;
//
//        this.controllerAop = controllerAop;
//    }
//
//    @Override
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//        //如果为OPTIONS直接返回成功
//        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
//            return true;
//        }
//        UserInfoEntity infoEntity = HttpServletRequestUtil.getLoginInfo(UserInfoEntity.class,httpServletRequest,
//                redisService, true);
//
//        if (infoEntity == null) {
//            throw new LoginException(Constants.INVALID);
//        }
//        if (PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return true;
//        }
//
//        String key = null;
//        if (infoEntity.getRoleId() == null || infoEntity.getRoleId() == 0) {
//            key = RoleConstants.ROLE_USER_ACTION + RoleConstants.ROLE_ADMIN;
//        } else {
//            key = RoleConstants.ROLE_USER_ACTION + infoEntity.getRoleId().toString();
//        }
//        String requestPath = httpServletRequest.getRequestURI();
//        /*
//         * 验证权限
//         */
//        if (!redisService.hasKey(key)) {
//            roleInfoService.getUserNodeDisplay(infoEntity.getRoleId());
//        }
//        List<String> actionList = redisService.getObject(key);
//        if (actionList == null || actionList.size() == 0) {
//            throw new ServiceException(Constants.NOT_PERMIT);
//        }
//        for (String action : actionList) {
//            if (requestPath.startsWith(action)) {
//                return true;
//            }
//        }
//        throw new ServiceException(Constants.NOT_PERMIT);
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//
//    }
//}