| 
					
				 | 
			
			
				@ -0,0 +1,123 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.iot.service.common; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.iot.dao.common.BaseLoginLogDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.base.login.BaseLoginLogDO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.jdbc.core.BeanPropertyRowMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.jdbc.core.JdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.web.context.request.RequestContextHolder; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.web.context.request.ServletRequestAttributes; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import javax.servlet.http.HttpServletRequest; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * Created by yeshijie on 2022/3/14. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class PermissionService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final Logger logger = LoggerFactory.getLogger(PermissionService.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private JdbcTemplate jdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private BaseLoginLogDao baseLoginLogDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Value("${spring.profiles}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String profiles; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 判断用户是否有权限 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public boolean isPermission(String url){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String uid = getUID(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //            String uid = "402803f9658455110165845b84850000"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            logger.info("uid:" + uid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(StringUtils.isBlank(uid)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //判断是否开启权限校验 未设置或者为0则不开启权限校验 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String sql = " select code from iot_system_dict where dict_name ='isPermission'  "; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<String> isPermissions = jdbcTemplate.queryForList(sql,String.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(isPermissions.size()==0||"0".equals(isPermissions.get(0))){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if ("2c9a80ed72068fa20172164d756c000c".equals(uid)||"402803f9658455110165845b84850000".equals(uid)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //管理员 admxin 和 测试管理员 18800000001 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String usrSql = "SELECT role_id from "+getDbName()+".base_user where id=?"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<String> roleIds = jdbcTemplate.queryForList(usrSql,new Object[]{uid},String.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(roleIds.size()==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String roleId = roleIds.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if("company".equals(roleId)||"platform".equals(roleId)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //新申请的厂商和平台商才做权限校验 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String db = getDbName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String sqlCount = "SELECT count(DISTINCT mu.url) from "+db+".base_menu m,"+db+".base_role_menu rm,"+db+".base_menu_url mu " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "WHERE rm.role_id = '"+roleId+"'  and m.id = rm.menu_id and m.status=1 " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "and m.id = mu.menu_id and mu.url='"+url+"'"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                Integer num = jdbcTemplate.queryForObject(sqlCount,Integer.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(num==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public String getUID(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        BaseLoginLogDO loginLogDO = getLoginLog(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(loginLogDO != null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return loginLogDO.getUserId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private BaseLoginLogDO getLoginLog(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String accessToken = this.extractToken(getRequest()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql = "select * from "+getDbName()+".base_login_log a WHERE a.token=? ORDER BY a.create_time desc LIMIT 1"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        logger.info("sql+"+sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<BaseLoginLogDO> list = jdbcTemplate.query(sql,new Object[]{accessToken},new BeanPropertyRowMapper<>(BaseLoginLogDO.class)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(list!=null&&list.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return list.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public String getDbName(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String db = "base"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if("iotprod".equals(profiles)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            db = "`iot-base`"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return db; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String extractToken(HttpServletRequest request) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String accessToken = request.getHeader("token"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (null == accessToken) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            accessToken = request.getParameter("token"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return accessToken; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public HttpServletRequest getRequest(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |