李进 2 years ago
parent
commit
59c6100d28
1 changed files with 146 additions and 7 deletions
  1. 146 7
      authorize-sdk/src/main/java/com/xy/utils/AuthorizeUtils.java

+ 146 - 7
authorize-sdk/src/main/java/com/xy/utils/AuthorizeUtils.java

@@ -9,17 +9,25 @@ import com.xy.collections.list.JList;
 import com.xy.config.AuthorizeConfig;
 import com.xy.consts.CommConsts;
 import com.xy.entity.SysMenuJoinSysRoleMenu;
+import com.xy.enums.SaTokenEnum;
 import com.xy.error.CommRuntimeException;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.experimental.Accessors;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+import javax.servlet.http.HttpServletRequest;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+/**
+ * 权限工具类
+ */
 @Component
 @AllArgsConstructor
 public class AuthorizeUtils {
@@ -55,6 +63,18 @@ public class AuthorizeUtils {
         return r.getData();
     }
 
+    private static Tuple.Tuple2<String, Long> getHttpServletRequest() {
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        String token = request.getHeader(CommConsts.TOKEN_NAME);
+        if (StringUtils.isEmpty(token)) {
+            throw new CommRuntimeException(SaTokenEnum.NO_LOGIN.getKey(), SaTokenEnum.NO_LOGIN.getName());
+        }
+        String sysId = request.getHeader(CommConsts.SYS_ID);
+        if (StringUtils.isEmpty(sysId)) {
+            throw new CommRuntimeException(SaTokenEnum.NO_AUTHORITY.getKey(), SaTokenEnum.NO_AUTHORITY.getName());
+        }
+        return new Tuple.Tuple2<>(token, Long.valueOf(sysId));
+    }
 
     /**
      * 获取菜单
@@ -93,6 +113,16 @@ public class AuthorizeUtils {
         return sysMenuJoinSysRoleMenus;
     }
 
+    /**
+     * 获取菜单
+     *
+     * @return
+     */
+    public static List<SysMenuJoinSysRoleMenu> getMenu() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getMenu(tuple2.getV1(), tuple2.getV2());
+    }
+
     /**
      * 获取菜单树
      *
@@ -112,6 +142,16 @@ public class AuthorizeUtils {
         return tops;
     }
 
+    /**
+     * 获取菜单树
+     *
+     * @return
+     */
+    public static List<SysMenuJoinSysRoleMenu> getMenuTree() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getMenuTree(tuple2.getV1(), tuple2.getV2());
+    }
+
     /**
      * 判断数据查询权限
      *
@@ -151,6 +191,17 @@ public class AuthorizeUtils {
         return false;
     }
 
+    /**
+     * 判断数据查询权限
+     *
+     * @param uri
+     * @return
+     */
+    public static boolean getDataAuth(String uri) {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getDataAuth(tuple2.getV1(), tuple2.getV2(), uri);
+    }
+
     /**
      * 获取所有系统ID
      *
@@ -178,6 +229,16 @@ public class AuthorizeUtils {
         return list;
     }
 
+    /**
+     * 获取所有系统ID
+     *
+     * @return
+     */
+    public static List<Long> getSystemIds() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getSystemIds(tuple2.getV1());
+    }
+
     /**
      * 获取所有角色ID
      *
@@ -207,6 +268,16 @@ public class AuthorizeUtils {
         return list;
     }
 
+    /**
+     * 获取所有角色ID
+     *
+     * @return
+     */
+    public static List<Long> getRoleIds() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getRoleIds(tuple2.getV1(), tuple2.getV2());
+    }
+
     /**
      * 获取所有角色code
      *
@@ -239,13 +310,11 @@ public class AuthorizeUtils {
     /**
      * 获取所有角色code
      *
-     * @param longId   token
-     * @param systemId 系统id
      * @return
      */
-    public static List<String> getRoleCodes(Long longId, Long systemId) {
-        String token = StpUtil.getTokenValueByLoginId(longId);
-        return getRoleCodes(token, systemId);
+    public static List<String> getRoleCodes() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getRoleCodes(tuple2.getV1(), tuple2.getV2());
     }
 
     /**
@@ -275,6 +344,16 @@ public class AuthorizeUtils {
         return false;
     }
 
+    /**
+     * 是否是管理员
+     *
+     * @return
+     */
+    public static Boolean isAdmin() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return isAdmin(tuple2.getV1(), tuple2.getV2());
+    }
+
     /**
      * 获取所有菜单接口uri
      *
@@ -315,6 +394,16 @@ public class AuthorizeUtils {
         return list.comparing();
     }
 
+    /**
+     * 获取所有菜单接口uri
+     *
+     * @return
+     */
+    public static List<String> getMenuInterfaceUri() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getMenuInterfaceUri(tuple2.getV1(), tuple2.getV2());
+    }
+
     /**
      * 登录鉴权
      *
@@ -343,6 +432,16 @@ public class AuthorizeUtils {
         return true;
     }
 
+    /**
+     * 登录鉴权
+     *
+     * @return
+     */
+    public static boolean authByLogin() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return authByLogin(tuple2.getV1());
+    }
+
     /**
      * 接口鉴权
      *
@@ -371,6 +470,17 @@ public class AuthorizeUtils {
         return true;
     }
 
+    /**
+     * 接口鉴权
+     *
+     * @param uri
+     * @return
+     */
+    public static boolean authByInterface(String uri) {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return authByInterface(tuple2.getV1(), tuple2.getV2(), uri);
+    }
+
     /**
      * 数据鉴权
      *
@@ -395,6 +505,17 @@ public class AuthorizeUtils {
         return getDataAuth(token, systemId, uri);
     }
 
+    /**
+     * 数据鉴权
+     *
+     * @param uri
+     * @return
+     */
+    public static boolean authByData(String uri) {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return authByData(tuple2.getV1(), tuple2.getV2(), uri);
+    }
+
     /**
      * 获取登录id
      *
@@ -424,9 +545,19 @@ public class AuthorizeUtils {
     }
 
     /**
-     * token续签
+     * 获取登录id
      *
-     * @param token token
+     * @param tClass
+     * @param <T>
+     * @return
+     */
+    public static <T> T getLoginId(Class<T> tClass) {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        return getLoginId(tuple2.getV1(), tClass);
+    }
+
+    /**
+     * token续签
      */
     public static void renewTimeout(String token) {
         AuthorizeConfig authorizeConfig = getAuthorizeConfig();
@@ -439,6 +570,14 @@ public class AuthorizeUtils {
         StpUtil.renewTimeout(token, CommConsts.TOKEN_TIMEOUT);
     }
 
+    /**
+     * token续签
+     */
+    public static void renewTimeout() {
+        Tuple.Tuple2<String, Long> tuple2 = getHttpServletRequest();
+        renewTimeout(tuple2.getV1());
+    }
+
     @Data
     @Accessors(chain = true)
     public static class CacheEntity implements Serializable {