Explorar el Código

templeDesice 2022/8/30

lwhhszx hace 3 años
padre
commit
a3e7b5192f

+ 35 - 8
PAS/src/main/java/cn/cslg/pas/common/config/InnerInterceptor/LizzMybatisIntercepts.java

@@ -1,9 +1,11 @@
 package cn.cslg.pas.common.config.InnerInterceptor;
 
+import cn.cslg.pas.common.model.PersonnelVO;
+import cn.cslg.pas.common.utils.CacheUtils;
 import cn.cslg.pas.common.utils.SecurityUtils.SecurityUtils;
 import cn.cslg.pas.common.utils.auth.TreeUtils;
-import cn.cslg.pas.domain.DataSource;
-import cn.dev33.satoken.stp.StpUtil;
+import cn.cslg.pas.common.model.DataSource;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
@@ -33,7 +35,9 @@ import java.util.Properties;
 
 @Slf4j
 public class LizzMybatisIntercepts implements InnerInterceptor {
-
+    @Lazy//懒加载,当调用时注入
+    @Autowired
+    private  CacheUtils cacheUtils ;
 
     @Value("${authorUrl}")
     private String url;
@@ -49,29 +53,52 @@ public class LizzMybatisIntercepts implements InnerInterceptor {
             SecurityUtils.cleanDataScope(); //当第一次进来后删除线程保存的functionId,避免后续使用的查询sql进来
             Integer functionId =Integer.parseInt(maps.get("functionId").toString());
             String token =maps.get("token").toString();
+            //查询数据规则
+            //设定formdata类型参数
             RequestBody requestBody =  new FormBody.Builder()
                     .add("loginId", maps.get("loginId").toString())
                     .add("functionId",functionId.toString())
                     .build();
+            //建立连接
             OkHttpClient okHttpClient = new OkHttpClient();
             Request request = new Request.Builder()
                     .url(url+"/permission/api/data/queryDataRule")
                     .post(requestBody)
                     .addHeader("Cookie",token)
                     .build();
+            //获得请求返回
             String resBody = okHttpClient.newCall(request).execute().body().string();
+            //处理请求返回
             JSONArray jsonArray = JSONArray.parseArray(resBody);
             String  sqls ="";
+            // 查询字典数据
+            //设定formdata类型参数
+            RequestBody reBodySource =  new FormBody.Builder()
+                    .add("tableName", "local")
+                    .build();
+            //建立连接
+            OkHttpClient okHttpClientSou = new OkHttpClient();
+            Request requestSou = new Request.Builder()
+                    .url(url+"/permission/api/data/getDataSource")
+                    .post(reBodySource)
+                    .addHeader("Cookie",token)
+                    .build();
+            //获得请求返回
+            String resSource = okHttpClientSou.newCall(requestSou).execute().body().string();
+
+            JSONArray jsonArray1 = JSON.parseArray(resSource);
+            // 获得字典
+            List<DataSource> dataSources=jsonArray1.toJavaList(DataSource.class);
              //拼接所有角色的限制条件
             if(jsonArray.size()>0){
+                PersonnelVO personnelVO =cacheUtils.getLoginUserPersonnel(3);
             for(int i=0;i<jsonArray.size();i++){
-                List<DataSource> dataSources =new ArrayList<>();
-                String sql1= TreeUtils.reSql(JSONObject.parseObject( jsonArray.get(i).toString()) ,dataSources);
+                String sql1= TreeUtils.reSql(JSONObject.parseObject( jsonArray.get(i).toString()) ,dataSources,personnelVO);
                 sqls += jsonArray.size()!=i+1 ?  sql1+" OR ":sql1;
             }
-            if(Localsql.contains("desc")){
-             Localsql=   Localsql.replace("desc","");
-                Localsql+=" And (" +sqls+") desc";
+            // 根据sql语句结构,将拼接sql放入合适的位置
+            if(Localsql.contains("order by")){
+             Localsql=   Localsql.replace("order by","And (" +sqls+") order by");
             }
             else{
                 Localsql+=" And (" +sqls+")";

+ 9 - 13
PAS/src/main/java/cn/cslg/pas/domain/DataSource.java

@@ -1,8 +1,6 @@
-package cn.cslg.pas.domain;
+package cn.cslg.pas.common.model;
 
-import cn.cslg.pas.common.model.BaseEntity;
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 
@@ -12,39 +10,37 @@ import lombok.experimental.Accessors;
  * @description 数据权限类 数据库对应实体
  */
 
-@lombok.Data
+@Data
 @Accessors(chain = true)
-@EqualsAndHashCode(callSuper = true)
-@TableName(value = "DATA_SOURCE")
-public class DataSource extends BaseEntity<DataSource> {
+
+public class DataSource  {
     /**
      * 数据字典名字
      */
-    @TableField(value = "DATA_SOURCE_NAME")
     private String dataSourceName;
 
     /**
      * 数据字典描述
      */
-    @TableField(value = "DATA_SOURCE_DESCRIPTION")
     private String dataSourceDescription;
 
     /**
      * 数据字典数据库
      */
-    @TableField(value = "DATA_SOURCE_DATABASE")
     private String dataSourceDataBase;
 
     /**
      * 数据字典表格
      */
-    @TableField(value = "DATA_SOURCE_TABLE")
     private String dataSourceTable;
 
     /**
      * 数据字典字段
      */
-    @TableField(value = "DATA_SOURCE_FIELD")
     private String dataSourceField;
 
+    private  Integer id;
+    private Integer isDelete;
+
+
 }

+ 1 - 1
PAS/src/main/java/cn/cslg/pas/common/utils/CacheUtils.java

@@ -49,7 +49,7 @@ public class CacheUtils {
         }
     }
 
-    public PersonnelVO getLoginUserPersonnel(Object userId) {
+    public  PersonnelVO getLoginUserPersonnel(Object userId) {
         String json = redisUtil.get(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + userId);
         if (StringUtils.isEmpty(json)) {
             throw new NotLoginException("无数据", "user", "");

+ 23 - 25
PAS/src/main/java/cn/cslg/pas/common/utils/auth/TreeUtils.java

@@ -1,21 +1,23 @@
 package cn.cslg.pas.common.utils.auth;
 
-import cn.cslg.pas.domain.DataSource;
+import cn.cslg.pas.common.model.DataSource;
+import cn.cslg.pas.common.model.PersonnelVO;
+import cn.cslg.pas.common.utils.CacheUtils;
+import cn.cslg.pas.common.utils.RedisUtil;
 import com.alibaba.fastjson.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.Resource;
 import java.lang.reflect.Field;
 import java.util.List;
 
 @Component
 public class TreeUtils {
-    /**
-     * 分页查询设置返回参数 (当前页 每页条数 总条数)
-     */
-    public static   String reSql(JSONObject jsonObject,List<DataSource> dataSource)  {
-
-     String sql=  recursionTree(jsonObject,dataSource);
+    public static   String reSql(JSONObject jsonObject,List<DataSource> dataSource,PersonnelVO personnelVO) throws NoSuchFieldException, IllegalAccessException {
+        String sql ="";
+       if (jsonObject.containsKey("left")&&jsonObject.containsKey("right")){
+      sql=  recursionTree(jsonObject,dataSource,personnelVO);}
         return sql;
 
     }
@@ -33,25 +35,25 @@ public class TreeUtils {
      * @param jsonObject jsaonObject
      * @return
      */
-    public static  String recursionTree(JSONObject jsonObject, List<DataSource> dataSource)  {
+    public static  String recursionTree(JSONObject jsonObject, List<DataSource> dataSource,PersonnelVO personnelVO) throws NoSuchFieldException, IllegalAccessException {
         String str1 = "";
         String str2 = "";
         JSONObject jsonLeft =jsonObject.getJSONObject("left");
         JSONObject jsonRight =jsonObject.getJSONObject("right");
         if(jsonLeft.containsKey("left")){
-            str1 = recursionTree(jsonLeft,dataSource);
+            str1 = recursionTree(jsonLeft,dataSource,personnelVO);
         }
         else{
-            String field =distinguishFields(jsonLeft.get("field").toString(),dataSource);
-            String value=distinguishFields(jsonLeft.get("value").toString(),dataSource);
+            String field =jsonLeft.get("field").toString();
+            String value=distinguishFields(jsonLeft.get("value").toString(),dataSource,personnelVO);
             str1 = field+jsonLeft.get("opr").toString()+value;
         }
         if(jsonRight.containsKey("right")){
-            str2= recursionTree( jsonRight,dataSource);
+            str2= recursionTree( jsonRight,dataSource,personnelVO);
         }
         else{
-            String field =distinguishFields(jsonRight.get("field").toString(),dataSource);
-            String value=distinguishFields(jsonRight.get("value").toString(),dataSource);
+            String field =jsonRight.get("field").toString();
+            String value=distinguishFields(jsonRight.get("value").toString(),dataSource,personnelVO);
             str2 =field+jsonRight.get("opr").toString()+value;
         }
         String sql ="("+ str1+") "+ jsonObject.get("logicOpr")+" ("+str2+")";
@@ -88,22 +90,18 @@ public class TreeUtils {
     };
 
     //对field部分进行计算 sql查询
-    public static String distinguishFields(String field, List<DataSource> dataSources){
+    public static String distinguishFields(String field, List<DataSource> dataSources,PersonnelVO personnelVO) throws NoSuchFieldException, IllegalAccessException {
         String tem ="";
         String reField =field;
         for(DataSource dataSource :dataSources){
-            if(field.equals(dataSource.getDataSourceField()) ){
-                tem =dataSource.getDataSourceDataBase();
+            if(field.equals(dataSource.getDataSourceField())){
+                Class personClass =personnelVO.getClass();
+                Field dataField = personClass.getDeclaredField(field);
+                dataField.setAccessible(true);
+                reField =dataField.get(personnelVO).toString();
                 break;
             }}
-        switch (tem){
-            case "1":
-                reField="";
-                break;
-            case "2":
-                reField="1";
-                break;
-        }
+
         return reField;
     }
 

+ 1 - 1
PAS/src/main/java/cn/cslg/pas/service/OAuth2Service.java

@@ -162,7 +162,7 @@ public class OAuth2Service {
         PersonnelVO personnelVO = com.alibaba.fastjson2.JSONObject.parseObject(token, PersonnelVO.class);
 
 
-        return Response.success(token);
+        return Response.success(personnelVO.getToken());
     }
 
     public String changePwd(String oldPassword, String newPassword) {