diff --git a/src/main/java/com/zhangmeng/online/exam/ui/api/model/UserDataLoad.java b/src/main/java/com/zhangmeng/online/exam/ui/api/model/UserDataLoad.java index 347ea5c..de6a335 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/api/model/UserDataLoad.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/api/model/UserDataLoad.java @@ -6,6 +6,9 @@ import com.alibaba.fastjson2.JSONObject; import com.zhangmeng.online.exam.ui.api.DataLoad; import com.zhangmeng.online.exam.ui.api.form.UserForm; import com.zhangmeng.online.exam.ui.controller.UserEditController; +import com.zhangmeng.online.exam.ui.module.Role; +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.HttpUtils; import javafx.beans.property.SimpleStringProperty; @@ -13,6 +16,8 @@ import javafx.beans.property.SimpleStringProperty; import java.util.*; import com.zhangmeng.online.exam.ui.api.ApiUtils; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.scene.Parent; import javafx.scene.control.MenuItem; import javafx.stage.Stage; @@ -52,7 +57,7 @@ public class UserDataLoad implements DataLoad { userMapList.add(userMap); } - ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名","手机号码", "电子邮箱" ), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total); + ApiUtils.DataView dataView = new ApiUtils.DataView(Arrays.asList("序号", "用户名", "手机号码", "电子邮箱"), userMapList).setDataLoad(this).setPageNum(pageNum).setPageSize(pageSize).setTotal(total); dataView.setDataLoad(this); return dataView; } @@ -71,9 +76,9 @@ public class UserDataLoad implements DataLoad { public Map saveForm(Map map) { String result = HttpUtils.POST(ApiUtils.API_URL + "/user/save", map); JSONObject jsonObject = JSON.parseObject(result); - if (jsonObject.getIntValue("code") == 200){ + if (jsonObject.getIntValue("code") == 200) { AlertUtils.alert_msg("保存成功!"); - }else { + } else { AlertUtils.alert_warning("保存失败!"); } return null; @@ -85,15 +90,15 @@ public class UserDataLoad implements DataLoad { map.put("id", id); String result = HttpUtils.POST(ApiUtils.API_URL + "/user/delete", map); JSONObject jsonObject = JSON.parseObject(result); - if (jsonObject.getIntValue("code") == 200){ + if (jsonObject.getIntValue("code") == 200) { AlertUtils.alert_msg("删除成功!"); - }else { + } else { AlertUtils.alert_warning("删除失败!"); } } @Override - public void editData(String id,Stage stage) { + public void editData(String id, Stage stage) { UserForm userForm = new UserForm(); UserEditController controller = userForm.getController(); @@ -110,7 +115,40 @@ public class UserDataLoad implements DataLoad { String phone = data.getString("phone"); controller.phoneField.setText(phone); String password = data.getString("password"); + String role_ids = data.getString("role_ids"); + List roles = new ArrayList<>(); + List allRoles = RoleService.getAllRoles(); + User user = new User(); + user.setId(Integer.parseInt(id)); + user.setUsername(username); + user.setEmail(email); + user.setPhone(phone); + user.setPassword(password); + if (role_ids != null && !role_ids.equals("null")) { + String[] role_id_arr = role_ids.split(","); + for (Role allRole : allRoles) { + Role role = new Role(); + role.setId(allRole.getId()); + role.setSelected(false); + for (String role_id : role_id_arr) { + if (allRole.getId() == Integer.parseInt(role_id)) { + role.setSelected(true); + user.getRoles().add(role); + break; + } + } + role.setName(allRole.getName()); + role.setDescription(allRole.getDescription()); + role.setStatus(allRole.getStatus()); + role.setPermissions(allRole.getPermissions()); + roles.add(role); + } + } + controller.passwordField.setText(password); + controller.roleListView.setItems(FXCollections.observableArrayList(roles)); + + controller.setUser(user); AlertUtils.alert("编辑用户信息", (Parent) userForm, stage); } @@ -125,9 +163,9 @@ public class UserDataLoad implements DataLoad { map.put("newValue", newValue); String result = HttpUtils.POST(ApiUtils.API_URL + "/user/update", map); JSONObject jsonObject = JSON.parseObject(result); - if (jsonObject.getIntValue("code") == 200){ + if (jsonObject.getIntValue("code") == 200) { AlertUtils.alert_msg("更新成功!"); - }else { + } else { AlertUtils.alert_warning("更新失败!"); } @@ -138,7 +176,6 @@ public class UserDataLoad implements DataLoad { public List getContextMenu() { - return null; } diff --git a/src/main/java/com/zhangmeng/online/exam/ui/controller/UserEditController.java b/src/main/java/com/zhangmeng/online/exam/ui/controller/UserEditController.java index 8c58d08..21cf65f 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/controller/UserEditController.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/controller/UserEditController.java @@ -3,6 +3,7 @@ package com.zhangmeng.online.exam.ui.controller; import com.zhangmeng.online.exam.ui.module.Role; import com.zhangmeng.online.exam.ui.module.User; import com.zhangmeng.online.exam.ui.service.LogService; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -42,7 +43,7 @@ public class UserEditController { public TextField emailField; @FXML - public ComboBox statusComboBox; + public ComboBox statusComboBox; @FXML public ListView roleListView; @@ -53,43 +54,50 @@ public class UserEditController { @FXML public void initialize() { // 初始化状态下拉框 - statusComboBox.getItems().addAll("启用", "禁用"); - statusComboBox.setValue("启用"); + /** + * DISABLED("已停用"), + * VALID("正常"), + * LOCKED("锁定"); + */ + PaperEditController.Status status1 = new PaperEditController.Status("已停用", 0); + PaperEditController.Status status2 = new PaperEditController.Status("正常", 1); + PaperEditController.Status status3 = new PaperEditController.Status("已锁定", 2); + statusComboBox.getItems().addAll(status2, status1,status3); + statusComboBox.setValue(status2); - // 初始化角色列表 - roleListView.setItems(FXCollections.observableArrayList(RoleService.getAllRoles())); - - Callback, ListCell> call = CheckBoxListCell.forListView(new Callback>() { + statusComboBox.setConverter(new StringConverter() { @Override - public ObservableValue call(Role param) { - return param.selectedProperty(); - } - }, new StringConverter() { - @Override - public String toString(Role object) { - if (object != null) { - System.out.println("姓名:" + object.getName() + " 是否选择:" + object.isSelected()); - return object.getName(); - } - return null; + public String toString(PaperEditController.Status object) { + return object.getName(); } @Override - public Role fromString(String string) { + public PaperEditController.Status fromString(String string) { return null; } }); - - roleListView.setCellFactory(call); - roleListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { + statusComboBox.setCellFactory(new Callback, ListCell>() { @Override - public void changed(ObservableValue observable, Role oldValue, Role newValue) { - System.out.println("姓名:" + newValue.getName() + " 是否选择:" + newValue.isSelected()); + public ListCell call(ListView statusListView) { + return new ListCell<>() { + @Override + protected void updateItem(PaperEditController.Status item, boolean empty) { + super.updateItem(item, empty); + if (item != null && !empty) { + HBox hBox = new HBox(); + Label label = new Label(item.getName()); + hBox.getChildren().add(label); + setGraphic(hBox); + } + } + }; } }); - + // 初始化角色列表 + roleListView.setItems(FXCollections.observableArrayList(RoleService.getAllRoles())); + roleListView.setCellFactory(CheckBoxListCell.forListView(Role::selectedProperty)); } public void setUser(User user) { @@ -98,7 +106,7 @@ public class UserEditController { usernameField.setText(user.getUsername()); phoneField.setText(user.getPhone()); emailField.setText(user.getEmail()); - statusComboBox.setValue(user.getStatus()); + // 设置已有角色的选中状态 user.getRoles().forEach(role -> { @@ -131,7 +139,7 @@ public class UserEditController { // 更新基本信息 user.setPhone(phoneField.getText()); user.setEmail(emailField.getText()); - user.setStatus(statusComboBox.getValue()); + user.setStatus(String.valueOf(statusComboBox.getValue().getValue())); // 更新角色 user.getRoles().clear(); @@ -141,13 +149,13 @@ public class UserEditController { // 保存用户 if (user.getId() == 0) { - UserService.createUser(user); LogService.log("用户管理", "创建", "创建用户:" + user.getUsername()); } else { - UserService.updateUser(user); LogService.log("用户管理", "更新", "更新用户:" + user.getUsername()); } + UserService.saveUser(user); + saveClicked = true; closeDialog(); } catch (Exception e) { @@ -180,12 +188,14 @@ public class UserEditController { errorMessage.append("邮箱不能为空!\n"); } - Role role = roleListView.getSelectionModel().getSelectedItem(); - - if (role == null) { + if (roleListView.getItems().stream().noneMatch(Role::isSelected)) { errorMessage.append("至少选择一个角色!\n"); } +// for (Role role : roleListView.getItems()) { +// System.out.println("角色名称:" + role.getName() + " 是否选择:" + role.isSelected()); +// } + if (errorMessage.length() == 0) { return true; } else { diff --git a/src/main/java/com/zhangmeng/online/exam/ui/controller/UserListController.java b/src/main/java/com/zhangmeng/online/exam/ui/controller/UserListController.java deleted file mode 100644 index 4755bc9..0000000 --- a/src/main/java/com/zhangmeng/online/exam/ui/controller/UserListController.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.zhangmeng.online.exam.ui.controller; - -import javafx.event.ActionEvent; - -/** - * @author zm - * @date 2025/3/31 16:34 - * @version: 1.0 - */ -public class UserListController { - public void handleSearch(ActionEvent actionEvent) { - - } - - public void handleAdd(ActionEvent actionEvent) { - - } -} diff --git a/src/main/java/com/zhangmeng/online/exam/ui/module/Role.java b/src/main/java/com/zhangmeng/online/exam/ui/module/Role.java index 830ad33..2a42d0a 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/module/Role.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/module/Role.java @@ -98,6 +98,10 @@ public class Role { this.selected.set(selected); } + public SimpleBooleanProperty getSelected() { + return selected; + } + @Override public String toString() { return name; diff --git a/src/main/java/com/zhangmeng/online/exam/ui/service/UserService.java b/src/main/java/com/zhangmeng/online/exam/ui/service/UserService.java index aa2fe0c..5513406 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/service/UserService.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/service/UserService.java @@ -6,6 +6,8 @@ import com.alibaba.fastjson2.JSONObject; import com.zhangmeng.online.exam.ui.api.ApiUtils; import com.zhangmeng.online.exam.ui.module.MenuData; 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.fxml.FXMLLoader; import javafx.scene.Parent; @@ -13,12 +15,14 @@ import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TreeItem; import javafx.stage.Stage; - +import com.zhangmeng.online.exam.ui.module.Role; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import static com.zhangmeng.online.exam.ui.utils.FxUtils.User_DynamicTableComponent; + /** * 模块 * @author zm @@ -88,11 +92,34 @@ public class UserService { } - public static void createUser(User user) { - } + public static void saveUser(User user) { - public static void updateUser(User user) { + Map map = new HashMap<>(); + map.put("id", user.getId()); + map.put("username", user.getUsername()); + map.put("name", user.getName()); + map.put("email", user.getEmail()); + map.put("status", user.getStatus()); + map.put("password", user.getPassword()); + map.put("salt", user.getSalt()); + map.put("phone", user.getPhone()); + map.put("avatarUrl", user.getAvatarUrl()); + map.put("token", user.getToken()); + map.put("role_ids", user.getRoles().stream().map(Role::getId).toArray()); + String result = HttpUtils.POST(ApiUtils.API_URL + "/user/save", map); + JSONObject jsonObject = JSON.parseObject(result); + if (jsonObject.getIntValue("code") == 200){ + if (user.getId() == 0){ + AlertUtils.alert_msg("保存成功!"); + }else { + AlertUtils.alert_msg("更新成功!"); + } + + FxUtils.FX_BEANS.get(User_DynamicTableComponent).flushData(); + }else { + AlertUtils.alert_warning("保存失败!"); + } } } diff --git a/src/main/resources/fxml/user-edit.fxml b/src/main/resources/fxml/user-edit.fxml index cc2f0f7..9651fcc 100644 --- a/src/main/resources/fxml/user-edit.fxml +++ b/src/main/resources/fxml/user-edit.fxml @@ -22,11 +22,11 @@