用户管理 2025年4月8日12:20:26
parent
e61157b33c
commit
be73b40372
|
|
@ -6,6 +6,9 @@ import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
import com.zhangmeng.online.exam.ui.api.DataLoad;
|
||||||
import com.zhangmeng.online.exam.ui.api.form.UserForm;
|
import com.zhangmeng.online.exam.ui.api.form.UserForm;
|
||||||
import com.zhangmeng.online.exam.ui.controller.UserEditController;
|
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.AlertUtils;
|
||||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||||
import javafx.beans.property.SimpleStringProperty;
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
|
@ -13,6 +16,8 @@ import javafx.beans.property.SimpleStringProperty;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.control.MenuItem;
|
import javafx.scene.control.MenuItem;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
@ -110,7 +115,40 @@ public class UserDataLoad implements DataLoad {
|
||||||
String phone = data.getString("phone");
|
String phone = data.getString("phone");
|
||||||
controller.phoneField.setText(phone);
|
controller.phoneField.setText(phone);
|
||||||
String password = data.getString("password");
|
String password = data.getString("password");
|
||||||
|
String role_ids = data.getString("role_ids");
|
||||||
|
List<Role> roles = new ArrayList<>();
|
||||||
|
List<Role> 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.passwordField.setText(password);
|
||||||
|
controller.roleListView.setItems(FXCollections.observableArrayList(roles));
|
||||||
|
|
||||||
|
controller.setUser(user);
|
||||||
AlertUtils.alert("编辑用户信息", (Parent) userForm, stage);
|
AlertUtils.alert("编辑用户信息", (Parent) userForm, stage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,7 +176,6 @@ public class UserDataLoad implements DataLoad {
|
||||||
public List<MenuItem> getContextMenu() {
|
public List<MenuItem> getContextMenu() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.Role;
|
||||||
import com.zhangmeng.online.exam.ui.module.User;
|
import com.zhangmeng.online.exam.ui.module.User;
|
||||||
import com.zhangmeng.online.exam.ui.service.LogService;
|
import com.zhangmeng.online.exam.ui.service.LogService;
|
||||||
|
import javafx.beans.property.SimpleBooleanProperty;
|
||||||
import javafx.beans.value.ChangeListener;
|
import javafx.beans.value.ChangeListener;
|
||||||
import javafx.beans.value.ObservableValue;
|
import javafx.beans.value.ObservableValue;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
|
|
@ -42,7 +43,7 @@ public class UserEditController {
|
||||||
public TextField emailField;
|
public TextField emailField;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public ComboBox<String> statusComboBox;
|
public ComboBox<PaperEditController.Status> statusComboBox;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public ListView<Role> roleListView;
|
public ListView<Role> roleListView;
|
||||||
|
|
@ -53,43 +54,50 @@ public class UserEditController {
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
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);
|
||||||
|
|
||||||
|
statusComboBox.setConverter(new StringConverter<PaperEditController.Status>() {
|
||||||
|
@Override
|
||||||
|
public String toString(PaperEditController.Status object) {
|
||||||
|
return object.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaperEditController.Status fromString(String string) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
statusComboBox.setCellFactory(new Callback<ListView<PaperEditController.Status>, ListCell<PaperEditController.Status>>() {
|
||||||
|
@Override
|
||||||
|
public ListCell<PaperEditController.Status> call(ListView<PaperEditController.Status> 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.setItems(FXCollections.observableArrayList(RoleService.getAllRoles()));
|
||||||
|
roleListView.setCellFactory(CheckBoxListCell.forListView(Role::selectedProperty));
|
||||||
Callback<ListView<Role>, ListCell<Role>> call = CheckBoxListCell.forListView(new Callback<Role, ObservableValue<Boolean>>() {
|
|
||||||
@Override
|
|
||||||
public ObservableValue<Boolean> call(Role param) {
|
|
||||||
return param.selectedProperty();
|
|
||||||
}
|
|
||||||
}, new StringConverter<Role>() {
|
|
||||||
@Override
|
|
||||||
public String toString(Role object) {
|
|
||||||
if (object != null) {
|
|
||||||
System.out.println("姓名:" + object.getName() + " 是否选择:" + object.isSelected());
|
|
||||||
return object.getName();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Role fromString(String string) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
roleListView.setCellFactory(call);
|
|
||||||
roleListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Role>() {
|
|
||||||
@Override
|
|
||||||
public void changed(ObservableValue<? extends Role> observable, Role oldValue, Role newValue) {
|
|
||||||
System.out.println("姓名:" + newValue.getName() + " 是否选择:" + newValue.isSelected());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUser(User user) {
|
public void setUser(User user) {
|
||||||
|
|
@ -98,7 +106,7 @@ public class UserEditController {
|
||||||
usernameField.setText(user.getUsername());
|
usernameField.setText(user.getUsername());
|
||||||
phoneField.setText(user.getPhone());
|
phoneField.setText(user.getPhone());
|
||||||
emailField.setText(user.getEmail());
|
emailField.setText(user.getEmail());
|
||||||
statusComboBox.setValue(user.getStatus());
|
|
||||||
|
|
||||||
// 设置已有角色的选中状态
|
// 设置已有角色的选中状态
|
||||||
user.getRoles().forEach(role -> {
|
user.getRoles().forEach(role -> {
|
||||||
|
|
@ -131,7 +139,7 @@ public class UserEditController {
|
||||||
// 更新基本信息
|
// 更新基本信息
|
||||||
user.setPhone(phoneField.getText());
|
user.setPhone(phoneField.getText());
|
||||||
user.setEmail(emailField.getText());
|
user.setEmail(emailField.getText());
|
||||||
user.setStatus(statusComboBox.getValue());
|
user.setStatus(String.valueOf(statusComboBox.getValue().getValue()));
|
||||||
|
|
||||||
// 更新角色
|
// 更新角色
|
||||||
user.getRoles().clear();
|
user.getRoles().clear();
|
||||||
|
|
@ -141,13 +149,13 @@ public class UserEditController {
|
||||||
|
|
||||||
// 保存用户
|
// 保存用户
|
||||||
if (user.getId() == 0) {
|
if (user.getId() == 0) {
|
||||||
UserService.createUser(user);
|
|
||||||
LogService.log("用户管理", "创建", "创建用户:" + user.getUsername());
|
LogService.log("用户管理", "创建", "创建用户:" + user.getUsername());
|
||||||
} else {
|
} else {
|
||||||
UserService.updateUser(user);
|
|
||||||
LogService.log("用户管理", "更新", "更新用户:" + user.getUsername());
|
LogService.log("用户管理", "更新", "更新用户:" + user.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UserService.saveUser(user);
|
||||||
|
|
||||||
saveClicked = true;
|
saveClicked = true;
|
||||||
closeDialog();
|
closeDialog();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -180,12 +188,14 @@ public class UserEditController {
|
||||||
errorMessage.append("邮箱不能为空!\n");
|
errorMessage.append("邮箱不能为空!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Role role = roleListView.getSelectionModel().getSelectedItem();
|
if (roleListView.getItems().stream().noneMatch(Role::isSelected)) {
|
||||||
|
|
||||||
if (role == null) {
|
|
||||||
errorMessage.append("至少选择一个角色!\n");
|
errorMessage.append("至少选择一个角色!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// for (Role role : roleListView.getItems()) {
|
||||||
|
// System.out.println("角色名称:" + role.getName() + " 是否选择:" + role.isSelected());
|
||||||
|
// }
|
||||||
|
|
||||||
if (errorMessage.length() == 0) {
|
if (errorMessage.length() == 0) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -98,6 +98,10 @@ public class Role {
|
||||||
this.selected.set(selected);
|
this.selected.set(selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SimpleBooleanProperty getSelected() {
|
||||||
|
return selected;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||||
import com.zhangmeng.online.exam.ui.module.MenuData;
|
import com.zhangmeng.online.exam.ui.module.MenuData;
|
||||||
import com.zhangmeng.online.exam.ui.module.User;
|
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 com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
|
|
@ -13,12 +15,14 @@ import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.TreeItem;
|
import javafx.scene.control.TreeItem;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import com.zhangmeng.online.exam.ui.module.Role;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.zhangmeng.online.exam.ui.utils.FxUtils.User_DynamicTableComponent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模块
|
* 模块
|
||||||
* @author zm
|
* @author zm
|
||||||
|
|
@ -88,11 +92,34 @@ public class UserService {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createUser(User user) {
|
|
||||||
|
|
||||||
|
public static void saveUser(User user) {
|
||||||
|
|
||||||
|
Map<String, Object> 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("更新成功!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateUser(User user) {
|
FxUtils.FX_BEANS.get(User_DynamicTableComponent).flushData();
|
||||||
|
}else {
|
||||||
|
AlertUtils.alert_warning("保存失败!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,11 @@
|
||||||
|
|
||||||
<!-- 密码 -->
|
<!-- 密码 -->
|
||||||
<Label text="密码:" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
|
<Label text="密码:" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
|
||||||
<PasswordField fx:id="passwordField" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
|
<PasswordField fx:id="passwordField" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
|
||||||
|
|
||||||
<!-- 手机号 -->
|
<!-- 手机号 -->
|
||||||
<Label text="姓名:" GridPane.rowIndex="2" GridPane.columnIndex="0"/>
|
<Label text="手机号:" GridPane.rowIndex="2" GridPane.columnIndex="0"/>
|
||||||
<TextField fx:id="phoneField" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
|
<TextField fx:id="phoneField" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
|
||||||
|
|
||||||
<!-- 邮箱 -->
|
<!-- 邮箱 -->
|
||||||
<Label text="邮箱:" GridPane.rowIndex="3" GridPane.columnIndex="0"/>
|
<Label text="邮箱:" GridPane.rowIndex="3" GridPane.columnIndex="0"/>
|
||||||
|
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
<?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.UserListController">
|
|
||||||
<padding>
|
|
||||||
<Insets top="10" right="10" bottom="10" left="10"/>
|
|
||||||
</padding>
|
|
||||||
<children>
|
|
||||||
<HBox spacing="10" alignment="CENTER_LEFT">
|
|
||||||
<TextField fx:id="searchField" promptText="输入关键字搜索..." HBox.hgrow="ALWAYS"/>
|
|
||||||
<Button text="搜索" onAction="#handleSearch"/>
|
|
||||||
<Region HBox.hgrow="ALWAYS"/>
|
|
||||||
<Button fx:id="addButton" text="添加用户" onAction="#handleAdd"/>
|
|
||||||
</HBox>
|
|
||||||
<TableView fx:id="userTable" VBox.vgrow="ALWAYS">
|
|
||||||
<columns>
|
|
||||||
<TableColumn fx:id="idColumn" text="ID" prefWidth="50"/>
|
|
||||||
<TableColumn fx:id="usernameColumn" text="用户名" prefWidth="100"/>
|
|
||||||
<TableColumn fx:id="nameColumn" text="姓名" prefWidth="100"/>
|
|
||||||
<TableColumn fx:id="emailColumn" text="邮箱" prefWidth="150"/>
|
|
||||||
<TableColumn fx:id="statusColumn" text="状态" prefWidth="80"/>
|
|
||||||
<TableColumn fx:id="actionColumn" text="操作" prefWidth="150"/>
|
|
||||||
</columns>
|
|
||||||
</TableView>
|
|
||||||
</children>
|
|
||||||
</VBox>
|
|
||||||
Loading…
Reference in New Issue