添加验证码登录
parent
8851fa2651
commit
4c4f09234b
|
|
@ -1,13 +1,28 @@
|
|||
package com.zhangmeng.online.exam.ui.admin;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.zhangmeng.online.exam.ui.api.ApiUtils;
|
||||
import com.zhangmeng.online.exam.ui.components.ExamButtonComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.ExamComponent;
|
||||
import com.zhangmeng.online.exam.ui.components.ShortAnswerComponent;
|
||||
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
|
||||
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.concurrent.ScheduledService;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.Window;
|
||||
import javafx.util.Duration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
|
|
@ -16,24 +31,13 @@ import javafx.stage.Window;
|
|||
*/
|
||||
public class LoginPage extends AnchorPane {
|
||||
|
||||
/**
|
||||
* <columnConstraints>
|
||||
* <ColumnConstraints hgrow="SOMETIMES" maxWidth="229.0" minWidth="10.0" prefWidth="38.0" />
|
||||
* <ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="248.0" />
|
||||
* </columnConstraints>
|
||||
* <rowConstraints>
|
||||
* <RowConstraints maxHeight="174.0" minHeight="10.0" prefHeight="60.0" vgrow="SOMETIMES" />
|
||||
* <RowConstraints maxHeight="206.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
|
||||
* <RowConstraints maxHeight="238.0" minHeight="10.0" prefHeight="52.0" vgrow="SOMETIMES" />
|
||||
* </rowConstraints>
|
||||
* <children>
|
||||
* <Label text="账号" />
|
||||
* <Label text="密码" GridPane.rowIndex="1" />
|
||||
* <TextField GridPane.columnIndex="1" />
|
||||
* <Button mnemonicParsing="false" prefHeight="23.0" prefWidth="178.0" text="登录" GridPane.columnIndex="1" GridPane.rowIndex="2" />
|
||||
* <PasswordField GridPane.columnIndex="1" GridPane.rowIndex="1" />
|
||||
* </children>
|
||||
*/
|
||||
|
||||
private TextField username;
|
||||
private PasswordField password;
|
||||
private TextField verificationCode;
|
||||
|
||||
private Button button1;
|
||||
|
||||
public LoginPage() {
|
||||
|
||||
ColumnConstraints column1 = new ColumnConstraints();
|
||||
|
|
@ -61,60 +65,179 @@ public class LoginPage extends AnchorPane {
|
|||
row2.setVgrow(Priority.SOMETIMES);
|
||||
|
||||
RowConstraints row3 = new RowConstraints();
|
||||
row3.setMaxHeight(238.0);
|
||||
row3.setMaxHeight(206.0);
|
||||
row3.setMinHeight(10.0);
|
||||
row3.setPrefHeight(52.0);
|
||||
row3.setPrefHeight(47.0);
|
||||
row3.setVgrow(Priority.SOMETIMES);
|
||||
|
||||
RowConstraints row4 = new RowConstraints();
|
||||
row4.setMaxHeight(206.0);
|
||||
row4.setMinHeight(10.0);
|
||||
row4.setPrefHeight(47.0);
|
||||
row4.setVgrow(Priority.SOMETIMES);
|
||||
|
||||
// <GridPane layoutX="485.0" layoutY="59.0" prefHeight="159.0" prefWidth="275.0" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="125.0" AnchorPane.rightAnchor="127.0" AnchorPane.topAnchor="59.0">
|
||||
GridPane gridPane = new GridPane();
|
||||
gridPane.getColumnConstraints().addAll(column1, column2);
|
||||
gridPane.getRowConstraints().addAll(row1, row2, row3);
|
||||
gridPane.getRowConstraints().addAll(row1, row2, row3, row4);
|
||||
gridPane.setLayoutX(485.0);
|
||||
gridPane.setLayoutY(59.0);
|
||||
gridPane.setPrefHeight(159.0);
|
||||
gridPane.setPrefWidth(275.0);
|
||||
AnchorPane.setBottomAnchor(gridPane, 97.0);
|
||||
gridPane.setPrefHeight(200);
|
||||
gridPane.setPrefWidth(300);
|
||||
AnchorPane.setBottomAnchor(gridPane, 90.0);
|
||||
AnchorPane.setLeftAnchor(gridPane, 125.0);
|
||||
AnchorPane.setRightAnchor(gridPane, 127.0);
|
||||
AnchorPane.setTopAnchor(gridPane, 59.0);
|
||||
|
||||
Label label1 = new Label("账号");
|
||||
Label label2 = new Label("密码");
|
||||
TextField textField1 = new TextField();
|
||||
PasswordField passwordField1 = new PasswordField();
|
||||
Button button1 = new Button("登录");
|
||||
username = new TextField();
|
||||
password = new PasswordField();
|
||||
button1 = new Button("登录");
|
||||
button1.setMnemonicParsing(false);
|
||||
button1.setPrefHeight(23.0);
|
||||
button1.setPrefWidth(178.0);
|
||||
|
||||
gridPane.add(label1, 0, 0);
|
||||
gridPane.add(label2, 0, 1);
|
||||
gridPane.add(textField1, 1, 0);
|
||||
gridPane.add(passwordField1, 1, 1);
|
||||
gridPane.add(button1, 1, 2);
|
||||
gridPane.add(username, 1, 0);
|
||||
gridPane.add(password, 1, 1);
|
||||
|
||||
|
||||
//验证码
|
||||
ImageView imageView = new ImageView(new Image(ApiUtils.API_URL + "/verificationCode/generate"));
|
||||
imageView.setFitHeight(30);
|
||||
imageView.setFitWidth(100);
|
||||
verificationCode = new TextField();
|
||||
// verificationCode.setPrefWidth(100);
|
||||
// verificationCode.setPrefHeight(23);
|
||||
|
||||
Label label3 = new Label("验证码");
|
||||
gridPane.add(label3, 0, 2);
|
||||
imageView.setOnMouseClicked(event -> {
|
||||
imageView.setImage(new Image(ApiUtils.API_URL + "/verificationCode/generate"));
|
||||
});
|
||||
|
||||
HBox hBox = new HBox(10);
|
||||
hBox.setAlignment(Pos.CENTER);
|
||||
hBox.getChildren().addAll(verificationCode, imageView);
|
||||
gridPane.add(hBox, 1, 2);
|
||||
|
||||
gridPane.add(button1, 1, 3);
|
||||
getChildren().add(gridPane);
|
||||
|
||||
username.setText("admin");
|
||||
password.setText("123456");
|
||||
|
||||
button1.setOnAction(event -> {
|
||||
|
||||
Scene scene = button1.getScene();
|
||||
Stage window = (Stage) scene.getWindow();
|
||||
window.close();
|
||||
|
||||
Stage stage = new Stage();
|
||||
// IndexPage shortAnswerComponent = new IndexPage();
|
||||
|
||||
PaperPage paperPage = new PaperPage();
|
||||
scene = new Scene(paperPage, 1280, 720);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("在线考试系统");
|
||||
stage.show();
|
||||
|
||||
|
||||
login();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void login() {
|
||||
|
||||
}
|
||||
//TODO 登录逻辑
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("username", username.getText());
|
||||
params.put("password", password.getText());
|
||||
params.put("captcha", verificationCode.getText());
|
||||
String response = HttpUtils.POST(ApiUtils.API_URL + "/login", params);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
if (jsonObject.getIntValue("code") == 2001) {
|
||||
|
||||
Map<String,Object> data = (Map<String, Object>) jsonObject.get("data");
|
||||
Object token = data.get("token");
|
||||
HttpUtils.USER_INFO.put("token",token);
|
||||
Alert alert = AlertUtils.alert_msg(jsonObject.getString("message"));
|
||||
|
||||
MyScheduledService myService = new MyScheduledService();
|
||||
//myService.setDelay(Duration.seconds(5));//延迟
|
||||
myService.setPeriod(Duration.millis(500));//时间周期
|
||||
myService.setRestartOnFailure(true);
|
||||
myService.setMaximumFailureCount(4);//设置最大失败次数
|
||||
|
||||
myService.progressProperty().addListener(new ChangeListener<Number>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
||||
System.out.println("progressProperty:" + newValue.intValue());
|
||||
}
|
||||
});
|
||||
|
||||
myService.valueProperty().addListener(new ChangeListener<Number>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
||||
if (newValue != null){
|
||||
System.out.println("progressProperty:" + newValue.intValue());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
myService.lastValueProperty().addListener(new ChangeListener<Number>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
||||
|
||||
if (newValue != null){
|
||||
System.out.println("lastValueProperty:" + newValue.intValue());
|
||||
if (newValue.intValue() == 3){
|
||||
myService.cancel();
|
||||
alert.close();
|
||||
success();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
myService.start();
|
||||
|
||||
|
||||
} else {
|
||||
AlertUtils.alert_warning(jsonObject.getString("message"));
|
||||
}
|
||||
}
|
||||
|
||||
private void success(){
|
||||
Scene scene = button1.getScene();
|
||||
Stage window = (Stage) scene.getWindow();
|
||||
window.close();
|
||||
|
||||
Stage stage = new Stage();
|
||||
// IndexPage shortAnswerComponent = new IndexPage();
|
||||
|
||||
PaperPage paperPage = new PaperPage();
|
||||
scene = new Scene(paperPage, 1280, 720);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("在线考试系统");
|
||||
stage.show();
|
||||
|
||||
}
|
||||
|
||||
class MyScheduledService extends ScheduledService<Number> {
|
||||
|
||||
int sum = 0 ;
|
||||
|
||||
@Override
|
||||
protected Task<Number> createTask() {
|
||||
|
||||
return new Task<Number>() {
|
||||
@Override
|
||||
protected Number call() throws Exception {
|
||||
sum = sum + 1;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateValue(Number value) {
|
||||
|
||||
super.updateValue(value);
|
||||
// System.out.println("updateValue");
|
||||
if (value.intValue() == 10){
|
||||
MyScheduledService.this.cancel();
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -48,20 +48,22 @@ public class AlertUtils {
|
|||
/**
|
||||
* 警告弹窗
|
||||
*/
|
||||
public static void alert_warning(String msg) {
|
||||
public static Alert alert_warning(String msg) {
|
||||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||||
alert.initModality(Modality.APPLICATION_MODAL);
|
||||
alert.setContentText(msg);
|
||||
alert.setHeaderText(null);
|
||||
alert.show();
|
||||
return alert;
|
||||
}
|
||||
|
||||
public static void alert_msg(String msg) {
|
||||
public static Alert alert_msg(String msg) {
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.initModality(Modality.APPLICATION_MODAL);
|
||||
alert.setContentText(msg);
|
||||
alert.setHeaderText(null);
|
||||
alert.show();
|
||||
return alert;
|
||||
}
|
||||
|
||||
public static void alert(String title, Parent node, Stage primaryStage) {
|
||||
|
|
|
|||
|
|
@ -18,20 +18,25 @@ import java.util.Map;
|
|||
*/
|
||||
public class HttpUtils<T> {
|
||||
|
||||
public static Map<String, Object> USER_INFO = new HashMap<>();
|
||||
|
||||
public static <T> T GET(String url, Class<T> clazz) {
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
return JSONObject.parseObject(response, clazz);
|
||||
}
|
||||
|
||||
public static String GET(String url) {
|
||||
url = url + "?token=" + USER_INFO.get("token");
|
||||
return HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
}
|
||||
|
||||
public static String GET(String url, Map<String, Object> params) {
|
||||
params.put("token",USER_INFO.get("token"));
|
||||
return HttpUtil.get(url, params);
|
||||
}
|
||||
|
||||
public static String POST(String url, Map<String, Object> params) {
|
||||
params.put("token",USER_INFO.get("token"));
|
||||
return HttpUtil.post(url, params);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.PasswordField?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.image.ImageView?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.ColumnConstraints?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
|
|
@ -11,10 +12,11 @@
|
|||
|
||||
<AnchorPane prefHeight="315.0" prefWidth="527.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.online.exam.ui.admin.LoginController">
|
||||
<children>
|
||||
<GridPane layoutX="485.0" layoutY="59.0" prefHeight="159.0" prefWidth="275.0" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="125.0" AnchorPane.rightAnchor="127.0" AnchorPane.topAnchor="59.0">
|
||||
<GridPane layoutX="485.0" layoutY="59.0" prefHeight="159.0" prefWidth="364.0" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="125.0" AnchorPane.rightAnchor="42.0" AnchorPane.topAnchor="59.0">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="229.0" minWidth="10.0" prefWidth="38.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="248.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="229.0" minWidth="10.0" prefWidth="54.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="236.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="80.0" />
|
||||
</columnConstraints>
|
||||
<rowConstraints>
|
||||
<RowConstraints maxHeight="174.0" minHeight="10.0" prefHeight="60.0" vgrow="SOMETIMES" />
|
||||
|
|
@ -24,10 +26,14 @@
|
|||
<children>
|
||||
<Label text="账号" />
|
||||
<Label text="密码" GridPane.rowIndex="1" />
|
||||
<TextField GridPane.columnIndex="1" />
|
||||
<Button mnemonicParsing="false" prefHeight="23.0" prefWidth="178.0" text="登录" GridPane.columnIndex="1" GridPane.rowIndex="2" />
|
||||
<PasswordField GridPane.columnIndex="1" GridPane.rowIndex="1" />
|
||||
<TextField prefHeight="23.0" prefWidth="100.0" GridPane.columnIndex="1" />
|
||||
<PasswordField prefHeight="22.0" prefWidth="159.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
|
||||
<Label text="验证码" GridPane.rowIndex="2" />
|
||||
<TextField prefHeight="23.0" prefWidth="121.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
|
||||
</children>
|
||||
</GridPane>
|
||||
<Button layoutX="175.0" layoutY="231.0" mnemonicParsing="false" prefHeight="23.0" prefWidth="178.0" text="登录" />
|
||||
<ImageView fitHeight="23.0" fitWidth="107.0" layoutX="424.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true" />
|
||||
<ImageView fitHeight="22.0" fitWidth="107.0" layoutX="442.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
|
|
|
|||
Loading…
Reference in New Issue