2025年4月10日16:25:22
parent
00e6a814c9
commit
a50e70cd7b
|
|
@ -6,12 +6,17 @@ import com.zhangmeng.online.exam.ui.api.form.base.Form;
|
|||
import com.zhangmeng.online.exam.ui.api.model.UserDataLoad;
|
||||
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
|
||||
import com.zhangmeng.online.exam.ui.controller.RoleEditController;
|
||||
import com.zhangmeng.online.exam.ui.controller.UserEditController;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/3/15 11:38
|
||||
|
|
@ -19,44 +24,23 @@ import javafx.stage.Stage;
|
|||
*/
|
||||
public class RoleForm extends Form {
|
||||
|
||||
private RoleEditController controller;
|
||||
|
||||
public RoleForm() {
|
||||
Label label1 = new Label("类型:");
|
||||
label1.setLayoutX(135.0);
|
||||
label1.setLayoutY(84.0);
|
||||
Label label2 = new Label("角色名称:");
|
||||
label2.setLayoutX(130.0);
|
||||
label2.setLayoutY(145.0);
|
||||
Label label3 = new Label("角色描述:");
|
||||
label3.setLayoutX(123.0);
|
||||
label3.setLayoutY(209.0);
|
||||
try {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/role-edit.fxml"));
|
||||
this.getChildren().add(fxmlLoader.load());
|
||||
this.controller = fxmlLoader.getController();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
ChoiceBox choiceBox = new ChoiceBox();
|
||||
choiceBox.setLayoutX(200);
|
||||
choiceBox.setLayoutY(80.0);
|
||||
choiceBox.setPrefHeight(23.0);
|
||||
choiceBox.setPrefWidth(229.0);
|
||||
choiceBox.getItems().addAll("管理员", "教师", "学生");
|
||||
choiceBox.setTooltip(new Tooltip("请选择类型"));
|
||||
|
||||
TextField textField1 = new TextField();
|
||||
textField1.setLayoutX(200.0);
|
||||
textField1.setLayoutY(141.0);
|
||||
textField1.setPrefHeight(23.0);
|
||||
textField1.setPrefWidth(229.0);
|
||||
|
||||
TextField textField2 = new TextField();
|
||||
textField2.setLayoutX(200);
|
||||
textField2.setLayoutY(205.0);
|
||||
textField2.setPrefHeight(23.0);
|
||||
textField2.setPrefWidth(229.0);
|
||||
|
||||
Button button1 = new Button("保存");
|
||||
button1.setLayoutX(267.0);
|
||||
button1.setLayoutY(289.0);
|
||||
button1.setMnemonicParsing(false);
|
||||
button1.setText("保存");
|
||||
|
||||
this.getChildren().addAll(label1, label2, label3, choiceBox, textField1, textField2, button1);
|
||||
public RoleEditController getController() {
|
||||
return controller;
|
||||
}
|
||||
|
||||
public void setController(RoleEditController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import com.zhangmeng.online.exam.ui.module.User;
|
|||
import com.zhangmeng.online.exam.ui.service.PermissionService;
|
||||
import com.zhangmeng.online.exam.ui.service.RoleService;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.FxUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
|
|
@ -25,6 +26,8 @@ import javafx.stage.Stage;
|
|||
import java.util.*;
|
||||
|
||||
import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Permission_DynamicTableComponent;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Role_DynamicTableComponent;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -80,7 +83,16 @@ public class PermissionDataLoad implements DataLoad {
|
|||
|
||||
@Override
|
||||
public void deleteData(String id) {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/permission/delete", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
AlertUtils.alert_msg("删除成功!");
|
||||
FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
|
||||
} else {
|
||||
AlertUtils.alert_warning("删除失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -5,15 +5,34 @@ import com.alibaba.fastjson2.JSONArray;
|
|||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||
import com.zhangmeng.online.exam.ui.api.form.RoleForm;
|
||||
import com.zhangmeng.online.exam.ui.api.form.UserForm;
|
||||
import com.zhangmeng.online.exam.ui.components.PaperViewComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.PermissionChooseComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.callBack.PaperViewComponentCallBackImpl;
|
||||
import com.zhangmeng.online.exam.ui.controller.PermissionChooseController;
|
||||
import com.zhangmeng.online.exam.ui.controller.RoleEditController;
|
||||
import com.zhangmeng.online.exam.ui.controller.UserEditController;
|
||||
import com.zhangmeng.online.exam.ui.module.Permission;
|
||||
import com.zhangmeng.online.exam.ui.module.Role;
|
||||
import com.zhangmeng.online.exam.ui.module.RoleType;
|
||||
import com.zhangmeng.online.exam.ui.module.User;
|
||||
import com.zhangmeng.online.exam.ui.service.RoleService;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.FxUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.ContextMenu;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Role_DynamicTableComponent;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -38,9 +57,13 @@ public class RoleDataLoad implements DataLoad {
|
|||
JSONObject role = (JSONObject) datum;
|
||||
Map<String, SimpleStringProperty> roleMap = new HashMap<>();
|
||||
roleMap.put("序号", new SimpleStringProperty(role.getString("id")));
|
||||
roleMap.put("id", new SimpleStringProperty(role.getString("id")));
|
||||
roleMap.put("角色名称", new SimpleStringProperty(role.getString("name")));
|
||||
roleMap.put("角色名称_name", new SimpleStringProperty(role.getString("name")));
|
||||
roleMap.put("描述", new SimpleStringProperty(role.getString("desc")));
|
||||
roleMap.put("描述_desc", new SimpleStringProperty(role.getString("desc")));
|
||||
roleMap.put("角色类型", new SimpleStringProperty(role.getString("type_name")));
|
||||
roleMap.put("角色类型_type_name", new SimpleStringProperty(role.getString("type_name")));
|
||||
roleMapList.add(roleMap);
|
||||
}
|
||||
return new ApiUtils.DataView(Arrays.asList("序号", "角色名称", "描述", "角色类型"), roleMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total);
|
||||
|
|
@ -63,12 +86,34 @@ public class RoleDataLoad implements DataLoad {
|
|||
|
||||
@Override
|
||||
public void deleteData(String id) {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/role/delete", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
AlertUtils.alert_msg("删除成功!");
|
||||
FxUtils.FX_BEANS.get(Role_DynamicTableComponent).flushData();
|
||||
} else {
|
||||
AlertUtils.alert_warning("删除失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editData(String id, Stage stage) {
|
||||
RoleForm userForm = new RoleForm();
|
||||
RoleEditController controller = userForm.getController();
|
||||
|
||||
Role role = RoleService.getRoleById(id);
|
||||
List<RoleType> roleTypes = RoleService.getRoleTypes();
|
||||
for (RoleType roleType : roleTypes) {
|
||||
if (roleType.getType().equals(role.getTypeName())) {
|
||||
controller.roleComboBox.setValue(roleType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
controller.setRole(role);
|
||||
AlertUtils.alert("编辑用户信息", (Parent) userForm, stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -78,7 +123,37 @@ public class RoleDataLoad implements DataLoad {
|
|||
|
||||
@Override
|
||||
public List<MenuItem> getContextMenu() {
|
||||
return null;
|
||||
|
||||
MenuItem group_volumes = new MenuItem("设置权限");
|
||||
group_volumes.setOnAction(event -> {
|
||||
Map<String, SimpleStringProperty> map = ApiUtils.selectedItem(group_volumes);
|
||||
String id = map.get("id").getValue();
|
||||
ContextMenu parentPopup = group_volumes.getParentPopup();
|
||||
BorderPane indexPage = (BorderPane) parentPopup.getStyleableParent();
|
||||
Stage stage = (Stage) indexPage.getScene().getWindow();
|
||||
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
context.put("id", id);
|
||||
|
||||
PermissionChooseComponent paperViewComponent = new PermissionChooseComponent(context);
|
||||
|
||||
PermissionChooseController permissionChooseController = paperViewComponent.getPermissionChooseController();
|
||||
permissionChooseController.permissionChooseVBox.prefWidthProperty().bind(paperViewComponent.widthProperty());
|
||||
permissionChooseController.permissionChooseVBox.prefHeightProperty().bind(paperViewComponent.heightProperty());
|
||||
permissionChooseController.permissionChooseVBox.setStyle("-fx-border-width: 0.5px; -fx-border-color: transparent;");
|
||||
|
||||
Role role = RoleService.getRoleById(id);
|
||||
|
||||
//查询权限
|
||||
List<Permission> permissions = RoleService.getPermissionsByRole(role);
|
||||
role.setPermissions(permissions);
|
||||
|
||||
permissionChooseController.setRole(role);
|
||||
AlertUtils.alert("设置权限", paperViewComponent, 649, 800, stage, true);
|
||||
|
||||
});
|
||||
|
||||
return Arrays.asList(group_volumes);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package com.zhangmeng.online.exam.ui.components;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.api.form.base.Form;
|
||||
import com.zhangmeng.online.exam.ui.controller.PermissionChooseController;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/10 11:13
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class PermissionChooseComponent extends Form {
|
||||
|
||||
private PermissionChooseController controller;
|
||||
|
||||
private Map<String, Object> context;
|
||||
|
||||
public Map<String, Object> getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public void setContext(Map<String, Object> context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public PermissionChooseController getPermissionChooseController() {
|
||||
return controller;
|
||||
}
|
||||
|
||||
public PermissionChooseComponent(Map<String, Object> context) {
|
||||
this.context = context;
|
||||
try {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/permission-choose.fxml"));
|
||||
this.getChildren().add(fxmlLoader.load());
|
||||
this.controller = fxmlLoader.getController();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setController(PermissionChooseController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
package com.zhangmeng.online.exam.ui.controller;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.module.Permission;
|
||||
import com.zhangmeng.online.exam.ui.module.Role;
|
||||
import com.zhangmeng.online.exam.ui.service.LogService;
|
||||
import com.zhangmeng.online.exam.ui.service.RoleService;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.CheckBoxTreeItem;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TreeView;
|
||||
import javafx.scene.control.cell.CheckBoxTreeCell;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Stage;
|
||||
import com.zhangmeng.online.exam.ui.service.PermissionService;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/10 11:14
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class PermissionChooseController {
|
||||
|
||||
@FXML
|
||||
public VBox permissionChooseVBox;
|
||||
|
||||
@FXML
|
||||
private Label titleLabel;
|
||||
|
||||
@FXML
|
||||
private TreeView<PermissionWrapper> permissionTree;
|
||||
|
||||
private Role role;
|
||||
private boolean saveClicked = false;
|
||||
|
||||
private static final ObservableList<Permission> permissions = FXCollections.observableArrayList();
|
||||
|
||||
// 添加一个包装类来存储Permission对象
|
||||
private static class PermissionWrapper {
|
||||
private final Permission permission;
|
||||
|
||||
public PermissionWrapper(Permission permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return permission.getName();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
initializePermissionTree();
|
||||
}
|
||||
|
||||
private void initializePermissionTree() {
|
||||
// 使用CheckBoxTreeItem作为根节点
|
||||
CheckBoxTreeItem<PermissionWrapper> root = new CheckBoxTreeItem<>(new PermissionWrapper(null));
|
||||
root.setValue(new PermissionWrapper(null) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "所有权限";
|
||||
}
|
||||
});
|
||||
|
||||
// 加载系统权限
|
||||
for (Permission permission : PermissionService.getRootPermissions()) {
|
||||
CheckBoxTreeItem<PermissionWrapper> item = createPermissionTreeItem(permission);
|
||||
root.getChildren().add(item);
|
||||
}
|
||||
|
||||
root.setExpanded(true);
|
||||
permissionTree.setRoot(root);
|
||||
permissionTree.setCellFactory(CheckBoxTreeCell.forTreeView());
|
||||
|
||||
}
|
||||
|
||||
private CheckBoxTreeItem<PermissionWrapper> createPermissionTreeItem(Permission permission) {
|
||||
CheckBoxTreeItem<PermissionWrapper> item = new CheckBoxTreeItem<>(new PermissionWrapper(permission));
|
||||
// item.selectedProperty().addListener((observable, oldValue, newValue) -> {
|
||||
// if (newValue) {
|
||||
// System.out.println("selected: " + permission.getName());
|
||||
// } else {
|
||||
// System.out.println("unselected: " + permission.getName());
|
||||
// }
|
||||
// });
|
||||
|
||||
for (Permission child : permission.getChildren()) {
|
||||
item.getChildren().add(createPermissionTreeItem(child));
|
||||
}
|
||||
|
||||
item.setExpanded(true);
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setRole(Role role) {
|
||||
this.role = role;
|
||||
titleLabel.setText("设置角色权限 - " + role.getName());
|
||||
|
||||
// 设置已有权限的选中状态
|
||||
setPermissionChecked((CheckBoxTreeItem<PermissionWrapper>) permissionTree.getRoot());
|
||||
}
|
||||
|
||||
private void setPermissionChecked(CheckBoxTreeItem<PermissionWrapper> item) {
|
||||
Permission permission = item.getValue().getPermission();
|
||||
if (permission != null && role.hasPermission(permission.getId())) {
|
||||
item.setSelected(true);
|
||||
}
|
||||
|
||||
item.getChildren().forEach(child ->
|
||||
setPermissionChecked((CheckBoxTreeItem<PermissionWrapper>) child)
|
||||
);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleSave() {
|
||||
try {
|
||||
permissions.clear();
|
||||
// 收集选中的权限
|
||||
collectSelectedPermissions((CheckBoxTreeItem<PermissionWrapper>) permissionTree.getRoot());
|
||||
|
||||
if (role != null) {
|
||||
// 清除现有权限
|
||||
role.getPermissions().clear();
|
||||
role.getPermissions().addAll(permissions);
|
||||
}
|
||||
for (Permission permission : permissions) {
|
||||
System.out.println(permission.getName());
|
||||
}
|
||||
LogService.log("角色管理", "更新", "更新角色权限:" + role.getName());
|
||||
|
||||
RoleService.updatePermission(role.getId(), permissions);
|
||||
|
||||
saveClicked = true;
|
||||
closeDialog();
|
||||
} catch (Exception e) {
|
||||
LogService.log("角色管理", "更新", "更新角色权限失败", e);
|
||||
showError("保存失败", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void collectSelectedPermissions(CheckBoxTreeItem<PermissionWrapper> item) {
|
||||
|
||||
if (item.isSelected() || item.isIndeterminate() ) {
|
||||
Permission permission = item.getValue().getPermission();
|
||||
if (permission != null) {
|
||||
permissions.add(permission);
|
||||
}
|
||||
}
|
||||
|
||||
item.getChildren().forEach(child ->
|
||||
collectSelectedPermissions((CheckBoxTreeItem<PermissionWrapper>) child)
|
||||
);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleCancel() {
|
||||
closeDialog();
|
||||
}
|
||||
|
||||
private void closeDialog() {
|
||||
Stage stage = (Stage) titleLabel.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
public boolean isSaveClicked() {
|
||||
return saveClicked;
|
||||
}
|
||||
|
||||
private void showError(String title, String message) {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.setTitle(title);
|
||||
alert.setHeaderText(null);
|
||||
alert.setContentText(message);
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package com.zhangmeng.online.exam.ui.controller;
|
||||
|
||||
import com.zhangmeng.online.exam.ui.module.Permission;
|
||||
import com.zhangmeng.online.exam.ui.module.Role;
|
||||
import com.zhangmeng.online.exam.ui.module.RoleType;
|
||||
import com.zhangmeng.online.exam.ui.service.RoleService;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.FxUtils;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/10 10:20
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class RoleEditController {
|
||||
|
||||
@FXML
|
||||
public ComboBox<RoleType> roleComboBox;
|
||||
|
||||
@FXML
|
||||
public TextField rolenameField;
|
||||
|
||||
@FXML
|
||||
public TextField descriptionField;
|
||||
|
||||
private Role role;
|
||||
|
||||
public void setRole(Role role) {
|
||||
this.role = role;
|
||||
this.rolenameField.setText(role.getName());
|
||||
this.descriptionField.setText(role.getDescription());
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
roleComboBox.getItems().addAll(RoleService.getRoleTypes());
|
||||
roleComboBox.setConverter(new StringConverter<RoleType>() {
|
||||
@Override
|
||||
public String toString(RoleType object) {
|
||||
if (object == null){
|
||||
return null;
|
||||
}
|
||||
return object.getDesc();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoleType fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void handleSave(ActionEvent actionEvent) {
|
||||
|
||||
RoleType roleType = roleComboBox.getSelectionModel().getSelectedItem();
|
||||
if (roleType == null){
|
||||
AlertUtils.alert_warning("请选择角色类型");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rolenameField.getText().trim().isEmpty()){
|
||||
AlertUtils.alert_warning("请输入角色名称");
|
||||
return;
|
||||
}
|
||||
|
||||
if (descriptionField.getText().trim().isEmpty()){
|
||||
AlertUtils.alert_warning("请输入角色描述");
|
||||
return;
|
||||
}
|
||||
|
||||
String roleId = role == null ? "": String.valueOf(role.getId());
|
||||
|
||||
boolean flag = RoleService.saveRole(roleId, rolenameField.getText(), descriptionField.getText(), roleType.getType());
|
||||
if (flag){
|
||||
AlertUtils.alert_msg("保存成功");
|
||||
closeDialog();
|
||||
//TODO 刷新角色列表
|
||||
FxUtils.FX_BEANS.get(FxUtils.Role_DynamicTableComponent).flushData();
|
||||
}else{
|
||||
AlertUtils.alert_warning("保存失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void handleCancel(ActionEvent actionEvent) {
|
||||
closeDialog();
|
||||
}
|
||||
|
||||
private void closeDialog() {
|
||||
Stage stage = (Stage) roleComboBox.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,9 @@ public class Role {
|
|||
private String name;
|
||||
private String description;
|
||||
private String status;
|
||||
|
||||
private String typeName;
|
||||
|
||||
private List<Permission> permissions;
|
||||
private SimpleBooleanProperty selected = new SimpleBooleanProperty(false);
|
||||
|
||||
|
|
@ -30,6 +33,14 @@ public class Role {
|
|||
public Role() {
|
||||
}
|
||||
|
||||
public String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public void setTypeName(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
// Getters and setters
|
||||
public int getId() {
|
||||
return id;
|
||||
|
|
@ -81,9 +92,9 @@ public class Role {
|
|||
permissions.remove(permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String permissionCode) {
|
||||
public boolean hasPermission(int permissionCode) {
|
||||
return permissions.stream()
|
||||
.anyMatch(p -> p.getCode().equals(permissionCode));
|
||||
.anyMatch(p -> p.getId() == permissionCode );
|
||||
}
|
||||
|
||||
public BooleanProperty selectedProperty() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
package com.zhangmeng.online.exam.ui.module;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/10 10:31
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class RoleType {
|
||||
|
||||
private String type;
|
||||
|
||||
private String desc;
|
||||
|
||||
private int index;
|
||||
|
||||
public RoleType(String type, String desc, int index) {
|
||||
this.type = type;
|
||||
this.desc = desc;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public RoleType() {
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
|
|
@ -114,9 +114,9 @@ public class User {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean hasPermission(String permissionCode) {
|
||||
public boolean hasPermission(int permissionId) {
|
||||
return roles.stream()
|
||||
.anyMatch(role -> role.hasPermission(permissionCode));
|
||||
.anyMatch(role -> role.hasPermission(permissionId));
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import com.alibaba.fastjson2.JSONArray;
|
|||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.module.Permission;
|
||||
import com.zhangmeng.online.exam.ui.module.Role;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.FxUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
|
|
@ -17,7 +16,6 @@ import java.util.Map;
|
|||
|
||||
import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.Permission_DynamicTableComponent;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.User_DynamicTableComponent;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -35,7 +33,7 @@ public class PermissionService {
|
|||
params.put("pageSize", ApiUtils.PAGE_SIZE);
|
||||
params.put("isTop", true);
|
||||
|
||||
String userListData = HttpUtils.GET(API_URL + "/permission/list",params);
|
||||
String userListData = HttpUtils.GET(API_URL + "/permission/list", params);
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
Integer total = jsonObject.getInteger("total");
|
||||
|
|
@ -54,7 +52,7 @@ public class PermissionService {
|
|||
permission.setId(Integer.parseInt(id));
|
||||
permission.setName(name);
|
||||
permission.setUrl(url);
|
||||
if (sort != null){
|
||||
if (sort != null) {
|
||||
permission.setSort(Integer.parseInt(sort));
|
||||
}
|
||||
permission.setParentId(Integer.parseInt(parentId));
|
||||
|
|
@ -66,18 +64,18 @@ public class PermissionService {
|
|||
|
||||
}
|
||||
|
||||
public static void savePermission(Map<String, Object> params,boolean isEdit) {
|
||||
public static void savePermission(Map<String, Object> params, boolean isEdit) {
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/permission/save", params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (jsonObject.getIntValue("code") == 200){
|
||||
if (!isEdit){
|
||||
if (jsonObject.getIntValue("code") == 200) {
|
||||
if (!isEdit) {
|
||||
AlertUtils.alert_msg("保存成功!");
|
||||
FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_msg("更新成功!");
|
||||
FxUtils.FX_BEANS.get(Permission_DynamicTableComponent).flushData();
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
AlertUtils.alert_warning("保存失败!");
|
||||
}
|
||||
}
|
||||
|
|
@ -99,10 +97,69 @@ public class PermissionService {
|
|||
permission.setId(Integer.parseInt(id));
|
||||
permission.setName(name);
|
||||
permission.setUrl(url);
|
||||
permission.setSort(Integer.parseInt(sort==null?"0":sort));
|
||||
permission.setSort(Integer.parseInt(sort == null ? "0" : sort));
|
||||
permission.setParentId(Integer.parseInt(parentId));
|
||||
permission.setDescription(desc);
|
||||
|
||||
return permission;
|
||||
}
|
||||
|
||||
public static List<Permission> getRootPermissions() {
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("pageNum", ApiUtils.PAGE_NUM);
|
||||
params.put("pageSize", ApiUtils.PAGE_SIZE);
|
||||
|
||||
String userListData = HttpUtils.GET(API_URL + "/permission/getRootPermissions", params);
|
||||
JSONObject jsonObject = JSON.parseObject(userListData);
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
Integer total = jsonObject.getInteger("total");
|
||||
List<Permission> roles = new ArrayList<>();
|
||||
for (Object datum : data) {
|
||||
JSONObject permissionmap = (JSONObject) datum;
|
||||
|
||||
String id = permissionmap.getString("id");
|
||||
String name = permissionmap.getString("name");
|
||||
String desc = permissionmap.getString("desc");
|
||||
String url = permissionmap.getString("url");
|
||||
String parentId = permissionmap.getString("parentId");
|
||||
String sort = permissionmap.getString("sort");
|
||||
|
||||
JSONArray children = permissionmap.getJSONArray("children");
|
||||
List<Permission> childrenList = new ArrayList<>();
|
||||
for (Object child : children) {
|
||||
JSONObject childmap = (JSONObject) child;
|
||||
Permission permission = new Permission();
|
||||
permission.setId(Integer.parseInt(childmap.getString("id")));
|
||||
permission.setName(childmap.getString("name"));
|
||||
permission.setUrl(childmap.getString("url"));
|
||||
String parentId1 = childmap.getString("parentId");
|
||||
if (sort != null) {
|
||||
permission.setSort(Integer.parseInt(sort));
|
||||
}
|
||||
permission.setDescription(childmap.getString("desc"));
|
||||
if (parentId1 != null) {
|
||||
permission.setParentId(Integer.parseInt(parentId1));
|
||||
}
|
||||
childrenList.add(permission);
|
||||
}
|
||||
|
||||
Permission permission = new Permission();
|
||||
permission.setId(Integer.parseInt(id));
|
||||
permission.setName(name);
|
||||
permission.setUrl(url);
|
||||
if (sort != null) {
|
||||
permission.setSort(Integer.parseInt(sort));
|
||||
}
|
||||
if (parentId != null) {
|
||||
permission.setParentId(Integer.parseInt(parentId));
|
||||
}
|
||||
|
||||
permission.setChildren(childrenList);
|
||||
permission.setDescription(desc);
|
||||
roles.add(permission);
|
||||
}
|
||||
|
||||
return roles;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,15 @@ import com.alibaba.fastjson2.JSON;
|
|||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.module.Permission;
|
||||
import com.zhangmeng.online.exam.ui.module.Role;
|
||||
import com.zhangmeng.online.exam.ui.module.RoleType;
|
||||
import com.zhangmeng.online.exam.ui.module.User;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.FxUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.collections.ObservableList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -15,6 +20,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;
|
||||
import static com.zhangmeng.online.exam.ui.utils.FxUtils.User_DynamicTableComponent;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -48,4 +54,129 @@ public class RoleService {
|
|||
|
||||
return roles;
|
||||
}
|
||||
|
||||
public static Role getRoleById(String id) {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getRoleById", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
JSONObject data = jsonObject.getJSONObject("data");
|
||||
|
||||
String name = data.getString("name");
|
||||
String desc = data.getString("desc");
|
||||
String type_name = data.getString("type_name");
|
||||
|
||||
Role role = new Role();
|
||||
role.setId(Integer.parseInt(id));
|
||||
role.setName(name);
|
||||
role.setDescription(desc);
|
||||
role.setTypeName(type_name);
|
||||
role.setPermissions(new ArrayList<>());
|
||||
return role;
|
||||
}
|
||||
|
||||
public static List<RoleType> getRoleTypes() {
|
||||
|
||||
String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getRoleTypes");
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
JSONArray roleTypes = jsonObject.getJSONArray("data");
|
||||
int index = 0;
|
||||
List<RoleType> roleTypeList = new ArrayList<>();
|
||||
for (Object obj : roleTypes) {
|
||||
JSONObject roleTypeMap = (JSONObject) obj;
|
||||
String type = roleTypeMap.getString("type_name");
|
||||
String desc = roleTypeMap.getString("type_desc");
|
||||
// int index = roleTypeMap.getInteger("index");
|
||||
RoleType roleType = new RoleType(type, desc, index);
|
||||
index++;
|
||||
roleTypeList.add(roleType);
|
||||
}
|
||||
return roleTypeList;
|
||||
}
|
||||
|
||||
public static boolean saveRole(String id, String name, String desc, String type) {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
map.put("name", name);
|
||||
map.put("desc", desc);
|
||||
map.put("type", type);
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/role/saveRole", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
return jsonObject.getIntValue("code") == 200;
|
||||
}
|
||||
|
||||
public static boolean updatePermission(int id, ObservableList<Permission> permissions) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
StringBuilder ids = new StringBuilder();
|
||||
for (Permission permission : permissions) {
|
||||
ids.append(permission.getId()).append(",");
|
||||
}
|
||||
map.put("permissionIds", ids.toString());
|
||||
String result = HttpUtils.POST(ApiUtils.API_URL + "/role/updatePermission", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
return jsonObject.getIntValue("code") == 200;
|
||||
}
|
||||
|
||||
public static List<Permission> getPermissionsByRole(Role role) {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("roleId", role.getId());
|
||||
String result = HttpUtils.GET(ApiUtils.API_URL + "/role/getPermissionsByRole", map);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
List<Permission> permissionList = new ArrayList<>();
|
||||
|
||||
JSONArray data = jsonObject.getJSONArray("data");
|
||||
Integer total = jsonObject.getInteger("total");
|
||||
|
||||
for (Object datum : data) {
|
||||
JSONObject permissionmap = (JSONObject) datum;
|
||||
|
||||
String id = permissionmap.getString("id");
|
||||
String name = permissionmap.getString("name");
|
||||
String desc = permissionmap.getString("desc");
|
||||
String url = permissionmap.getString("url");
|
||||
String parentId = permissionmap.getString("parentId");
|
||||
String sort = permissionmap.getString("sort");
|
||||
|
||||
JSONArray children = permissionmap.getJSONArray("children");
|
||||
List<Permission> childrenList = new ArrayList<>();
|
||||
for (Object child : children) {
|
||||
JSONObject childmap = (JSONObject) child;
|
||||
Permission permission = new Permission();
|
||||
permission.setId(Integer.parseInt(childmap.getString("id")));
|
||||
permission.setName(childmap.getString("name"));
|
||||
permission.setUrl(childmap.getString("url"));
|
||||
String parentId1 = childmap.getString("parentId");
|
||||
if (sort != null) {
|
||||
permission.setSort(Integer.parseInt(sort));
|
||||
}
|
||||
permission.setDescription(childmap.getString("desc"));
|
||||
if (parentId1 != null) {
|
||||
permission.setParentId(Integer.parseInt(parentId1));
|
||||
}
|
||||
childrenList.add(permission);
|
||||
}
|
||||
|
||||
Permission permission = new Permission();
|
||||
permission.setId(Integer.parseInt(id));
|
||||
permission.setName(name);
|
||||
permission.setUrl(url);
|
||||
if (sort != null) {
|
||||
permission.setSort(Integer.parseInt(sort));
|
||||
}
|
||||
if (parentId != null) {
|
||||
permission.setParentId(Integer.parseInt(parentId));
|
||||
}
|
||||
|
||||
permission.setChildren(childrenList);
|
||||
permission.setDescription(desc);
|
||||
permissionList.add(permission);
|
||||
}
|
||||
|
||||
return permissionList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
|
||||
<VBox fx:id="permissionChooseVBox" spacing="10" xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="com.zhangmeng.online.exam.ui.controller.PermissionChooseController">
|
||||
<padding>
|
||||
<Insets top="10" right="10" bottom="10" left="10"/>
|
||||
</padding>
|
||||
|
||||
<Label fx:id="titleLabel" style="-fx-font-size: 14px; -fx-font-weight: bold;"/>
|
||||
|
||||
<TreeView fx:id="permissionTree" VBox.vgrow="ALWAYS"/>
|
||||
|
||||
<HBox spacing="10" alignment="CENTER_RIGHT">
|
||||
<Button text="保存" onAction="#handleSave"/>
|
||||
<Button text="取消" onAction="#handleCancel"/>
|
||||
</HBox>
|
||||
</VBox>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
|
||||
<VBox spacing="10" xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="com.zhangmeng.online.exam.ui.controller.RoleEditController">
|
||||
<padding>
|
||||
<Insets top="10" right="10" bottom="10" left="10"/>
|
||||
</padding>
|
||||
|
||||
<GridPane hgap="10" vgap="10">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints minWidth="80" prefWidth="100"/>
|
||||
<ColumnConstraints hgrow="ALWAYS"/>
|
||||
</columnConstraints>
|
||||
|
||||
|
||||
<Label text="角色类型:" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
|
||||
<ComboBox fx:id="roleComboBox" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
|
||||
|
||||
<!-- 权限名称 -->
|
||||
<Label text="角色名称:" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
|
||||
<TextField fx:id="rolenameField" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
|
||||
|
||||
<!-- 描述 -->
|
||||
<Label text="角色描述:" GridPane.rowIndex="2" GridPane.columnIndex="0"/>
|
||||
<TextField fx:id="descriptionField" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
|
||||
|
||||
</GridPane>
|
||||
|
||||
<HBox spacing="10" alignment="CENTER_RIGHT">
|
||||
<Button text="保存" onAction="#handleSave" defaultButton="true"/>
|
||||
<Button text="取消" onAction="#handleCancel" cancelButton="true"/>
|
||||
</HBox>
|
||||
</VBox>
|
||||
Loading…
Reference in New Issue