Selaa lähdekoodia

feature:000.000.005:后端完成基于角色接口权限管理;
前端调整图表详情的展示效果;

yang yi 1 kuukausi sitten
vanhempi
säilyke
70ca31606c

+ 2 - 2
BI_front/src/views/chartManage.vue

@@ -97,7 +97,7 @@
       <template #header>数据分析详情</template>
       <template #default>
         <el-row>
-          <el-col :span="12">
+          <el-col :span="15">
             <el-card>
               <template #header>数据图表</template>
               <template #default>
@@ -105,7 +105,7 @@
               </template>
             </el-card>
           </el-col>
-          <el-col :span="12">
+          <el-col :span="9">
             <el-card>
               <template #header>分析结论</template>
               <template #default>

+ 4 - 5
BI_front/src/views/myChart.vue

@@ -35,7 +35,6 @@
       <el-table-column label="图表名称" align="center" prop="name" />
       <el-table-column label="分析目标" align="center" prop="analysisTarget" />
       <el-table-column label="图标类型" align="center" prop="chartType" />
-      <!--<el-table-column label="生成的分析结论" align="center" prop="analysisConclusion" />-->
       <el-table-column label="创建用户ID" align="center" prop="userId" />
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
         <template #default="scope">
@@ -89,7 +88,7 @@
       <template #header>数据分析详情</template>
       <template #default>
         <el-row>
-          <el-col :span="12">
+          <el-col :span="15">
             <el-card>
               <template #header>数据图表</template>
               <template #default>
@@ -97,7 +96,7 @@
               </template>
             </el-card>
           </el-col>
-          <el-col :span="12">
+          <el-col :span="9">
             <el-card>
               <template #header>分析结论</template>
               <template #default>
@@ -122,6 +121,7 @@
 import { listChart, delChart } from "@/common/api/chart";
 import {ElMessage} from "element-plus";
 import * as echarts from 'echarts';
+import {getUserId} from "../common/utils/auth.js";
 
 
 export default {
@@ -149,8 +149,6 @@ export default {
         pageNum: 1,
         pageSize: 10,
         name: null,
-        analysisTarget: null,
-        chartType: null,
         userId: null,
       },
       //图表的配置对象
@@ -194,6 +192,7 @@ export default {
     /** 查询图表管理列表 */
     getList() {
       this.loading = true;
+      this.queryParams.userId = getUserId();
       listChart(this.queryParams).then(response => {
         this.chartList = response.data.records;
         this.total = response.data.total;

+ 9 - 10
serve/src/main/java/space/anyi/BI/controller/ChartController.java

@@ -5,6 +5,7 @@ import cn.hutool.poi.excel.ExcelUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -36,12 +37,14 @@ public class ChartController {
     private final static Logger log = LoggerFactory.getLogger(ChartController.class);
     @Resource
     private ChartService chartService;
+    @PreAuthorize("@ps.hasRole('用户')")
     @GetMapping("/getChartById/{id}")
     @ResponseBody
     public ResponseResult getChartById(@PathVariable("id") Long id){
         Chart chart = chartService.getById(id);
         return ResponseResult.okResult(chart);
     }
+    @PreAuthorize("@ps.hasRole('管理员')")
     @PostMapping("/add")
     @ResponseBody
     public ResponseResult addChart(@RequestBody Chart chart){
@@ -52,33 +55,29 @@ public class ChartController {
         chartService.save(chart);
         return ResponseResult.okResult();
     }
+    @PreAuthorize("@ps.hasRole('用户')")
     @DeleteMapping("/{ids}")
     @ResponseBody
     public ResponseResult deleteChartById(@PathVariable List<Long> ids){
         chartService.removeByIds(ids);
         return ResponseResult.okResult();
     }
+    @PreAuthorize("@ps.hasRole('管理员')")
     @PutMapping("/update")
     @ResponseBody
     public ResponseResult updateChart(@RequestBody Chart chart){
         chartService.updateById(chart);
         return ResponseResult.okResult();
     }
+    @PreAuthorize("@ps.hasRole('用户')")
     @GetMapping("/list")
     @ResponseBody
-    public ResponseResult page(Integer pageNum,Integer pageSize){
-        Page<Chart> page = chartService.page(new Page<Chart>(pageNum, pageSize));
-        List<ChartVO> voList = new ArrayList<>();
-        for (Chart chart : page.getRecords()) {
-            ChartVO chartVO = BeanCopyUtil.copyBean(chart, ChartVO.class);
-            chartVO.setId(chart.getId().toString());
-            chartVO.setUserId(chart.getUserId().toString());
-            voList.add(chartVO);
-        }
-        PageVO pageVO = new PageVO(voList, page.getTotal());
+    public ResponseResult page(Integer pageNum,Integer pageSize,String name,Long UserId){
+        PageVO pageVO  = chartService.getChartPage(pageNum,pageSize,name,UserId);
         return ResponseResult.okResult(pageVO);
     }
 
+    @PreAuthorize("@ps.hasRole('用户')")
     @PostMapping("/generateChartByAI")
     @ResponseBody
     public ResponseResult generateChartByAI(ChartDTO chartDTO, MultipartFile file) throws IOException {

+ 7 - 0
serve/src/main/java/space/anyi/BI/controller/UserController.java

@@ -2,6 +2,7 @@ package space.anyi.BI.controller;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import space.anyi.BI.entity.LoginUserDetails;
 import space.anyi.BI.entity.ResponseResult;
@@ -41,6 +42,7 @@ public class UserController {
         return userService.register(user);
     }
 
+    @PreAuthorize("@ps.hasRole('用户')")
     @GetMapping("/getUserInfo")
     public ResponseResult getUserInfo(){
         LoginUserDetails loginUser = SecurityUtils.getLoginUser();
@@ -52,6 +54,7 @@ public class UserController {
         System.out.println("user = " + user);
         return ResponseResult.okResult(user);
     }
+    @PreAuthorize("@ps.hasRole('管理员')")
     @GetMapping("/page")
     @ResponseBody
     public ResponseResult page(Integer currentPage,Integer pageSize,String userName,String userAccount,String userRole){
@@ -59,6 +62,7 @@ public class UserController {
         PageVO page = userService.pageByUsernameAndUseraccount(currentPage, pageSize, userName, userAccount,userRole);
         return ResponseResult.okResult(page);
     }
+    @PreAuthorize("@ps.hasRole('用户')")
     @GetMapping("/{id}")
     @ResponseBody
     public ResponseResult getUserById(@PathVariable Long id){
@@ -68,6 +72,7 @@ public class UserController {
         return ResponseResult.okResult(userVO);
     }
 
+    @PreAuthorize("@ps.hasRole('管理员')")
     @DeleteMapping("/{ids}")
     @ResponseBody
     public ResponseResult deleteUserById(@PathVariable List<Long> ids){
@@ -75,6 +80,7 @@ public class UserController {
         return ResponseResult.okResult();
     }
 
+    @PreAuthorize("@ps.hasRole('管理员')")
     @PutMapping
     @ResponseBody
     public ResponseResult updateUser(@RequestBody UserDTO userDTO){
@@ -83,6 +89,7 @@ public class UserController {
         return ResponseResult.okResult();
     }
 
+    @PreAuthorize("@ps.hasRole('管理员')")
     @PostMapping
     @ResponseBody
     public ResponseResult addUser(@RequestBody UserDTO userDTO){

+ 3 - 0
serve/src/main/java/space/anyi/BI/service/ChartService.java

@@ -5,6 +5,7 @@ import space.anyi.BI.entity.Chart;
 import com.baomidou.mybatisplus.extension.service.IService;
 import space.anyi.BI.entity.dto.ChartDTO;
 import space.anyi.BI.entity.vo.ChartVO;
+import space.anyi.BI.entity.vo.PageVO;
 
 /**
 * @author 杨逸
@@ -14,4 +15,6 @@ import space.anyi.BI.entity.vo.ChartVO;
 public interface ChartService extends IService<Chart> {
 
     ChartVO generateChartByAI(ChartDTO chartDTO, MultipartFile file);
+
+    PageVO getChartPage(Integer pageNum, Integer pageSize, String name, Long userId);
 }

+ 12 - 0
serve/src/main/java/space/anyi/BI/service/PermissionService.java

@@ -0,0 +1,12 @@
+package space.anyi.BI.service;
+
+/**
+ * @ProjectName: serve
+ * @FileName: PermissionService
+ * @Author: 杨逸
+ * @Data:2024/12/6 21:43
+ * @Description:
+ */
+public interface PermissionService {
+    boolean hasRole(String role);
+}

+ 27 - 1
serve/src/main/java/space/anyi/BI/service/impl/ChartServiceImpl.java

@@ -1,14 +1,17 @@
 package space.anyi.BI.service.impl;
 
 import cn.hutool.core.util.IdUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
-import space.anyi.BI.BIApplication;
 import space.anyi.BI.entity.Chart;
 import space.anyi.BI.entity.dto.ChartDTO;
 import space.anyi.BI.entity.vo.ChartVO;
+import space.anyi.BI.entity.vo.PageVO;
 import space.anyi.BI.entity.xinghuo.HttpRequestData;
 import space.anyi.BI.entity.xinghuo.HttpResponseData;
 import space.anyi.BI.exception.SystemException;
@@ -21,6 +24,8 @@ import space.anyi.BI.util.ExcelUtils;
 import space.anyi.BI.util.SecurityUtils;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
 * @author 杨逸
@@ -31,6 +36,27 @@ import java.io.IOException;
 public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
     implements ChartService{
     private final static Logger log = LoggerFactory.getLogger(ChartServiceImpl.class);
+
+    @Override
+    public PageVO getChartPage(Integer pageNum, Integer pageSize, String name, Long userId) {
+        if (!SecurityUtils.isAdmin()){
+            userId = SecurityUtils.getUserId();
+        }
+        LambdaQueryWrapper<Chart>  lambdaQueryWrapper = new LambdaQueryWrapper<Chart>()
+                .like(StringUtils.hasText(name), Chart::getName, name)
+                .eq(userId != null, Chart::getUserId, userId);
+        Page<Chart> page = page(new Page<Chart>(pageNum, pageSize),lambdaQueryWrapper);
+        List<ChartVO> voList = new ArrayList<>();
+        for (Chart chart : page.getRecords()) {
+            ChartVO chartVO = BeanCopyUtil.copyBean(chart, ChartVO.class);
+            chartVO.setId(chart.getId().toString());
+            chartVO.setUserId(chart.getUserId().toString());
+            voList.add(chartVO);
+        }
+        PageVO pageVO = new PageVO(voList, page.getTotal());
+        return pageVO;
+    }
+
     @Override
     public ChartVO generateChartByAI(ChartDTO chartDTO, MultipartFile file) {
         //读数据

+ 25 - 0
serve/src/main/java/space/anyi/BI/service/impl/PermissionServiceImpl.java

@@ -0,0 +1,25 @@
+package space.anyi.BI.service.impl;
+
+import org.springframework.stereotype.Service;
+import space.anyi.BI.service.PermissionService;
+import space.anyi.BI.util.SecurityUtils;
+
+/**
+ * @ProjectName: serve
+ * @FileName: PermissionServiceImpl
+ * @Author: 杨逸
+ * @Data:2024/12/6 21:45
+ * @Description:
+ */
+@Service("ps")
+public class PermissionServiceImpl implements PermissionService {
+    @Override
+    public boolean hasRole(String role) {
+        String userRole = SecurityUtils.getLoginUser().getUser().getUserRole();
+        if ("管理员".equals(userRole))return true;
+        if (userRole.equals(role)){
+            return true;
+        }
+        return false;
+    }
+}

+ 2 - 3
serve/src/main/java/space/anyi/BI/util/SecurityUtils.java

@@ -44,9 +44,8 @@ public class SecurityUtils {
     }
 
     public static Boolean isAdmin(){
-        Long id = getLoginUser().getUser().getId();
-        return id != null && 1 == id;
-        //return id != null && 1L == id || 14787164048668L == id;
+        String userRole = getLoginUser().getUser().getUserRole();
+        return "管理员".equals(userRole);
     }
 
     public static Long getUserId() {