密码输入优化
This commit is contained in:
parent
478d88f3c4
commit
fd2baf7d33
|
@ -79,8 +79,8 @@ public class KeypadServiceImpl implements KeypadService {
|
|||
// 适当延长等待时间
|
||||
while (true) {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
String password = webSocketClient.getPassword();
|
||||
if (password.length() >= plaintextParam.getIntValue("MAXLength")) {
|
||||
Boolean confirm = webSocketClient.getConfirm();
|
||||
if (confirm) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ public class KeypadServiceImpl implements KeypadService {
|
|||
e.printStackTrace();
|
||||
}
|
||||
String password = webSocketClient.getPassword();
|
||||
return Result.ok(password);
|
||||
return Result.ok("密码输入完成!", password);
|
||||
} else {
|
||||
return Result.error("密码输入异常!详情:" + connectResult.getDesc());
|
||||
}
|
||||
|
|
|
@ -1,18 +1,21 @@
|
|||
package com.dpkj.modules.keypad.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.websocket.*;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ClientEndpoint
|
||||
@Slf4j
|
||||
public class WebSocketClient {
|
||||
|
||||
private Session session;
|
||||
// 密码
|
||||
private String password = "";
|
||||
// 是否确认
|
||||
private boolean confirm = false;
|
||||
|
||||
public WebSocketClient(String uri) {
|
||||
try {
|
||||
|
@ -25,11 +28,13 @@ public class WebSocketClient {
|
|||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
System.out.println("Connected to WebSocket server");
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String message) {
|
||||
System.out.println("Received message: " + message);
|
||||
|
||||
// 解析外层 JSON 对象
|
||||
JSONObject outerObj = JSONObject.parseObject(message);
|
||||
|
@ -41,24 +46,38 @@ public class WebSocketClient {
|
|||
Integer keyValue = paramObj.getInteger("Key");
|
||||
|
||||
if (keyValue != null) {
|
||||
Integer i = parseAsciiToDigit(keyValue);
|
||||
if (i != -1) {
|
||||
password += i.toString();
|
||||
// 更正
|
||||
if (keyValue == 8) {
|
||||
password = password.substring(0, password.length() - 1);
|
||||
// 取消
|
||||
} else if (keyValue == 27) {
|
||||
password = "";
|
||||
// 确认
|
||||
} else if (keyValue == 13) {
|
||||
confirm = true;
|
||||
} else if (keyValue == 2) {
|
||||
password += "00";
|
||||
} else {
|
||||
password += KEYPAD_MAPPING.get("0x" + Integer.toHexString(keyValue));
|
||||
}
|
||||
} else {
|
||||
log.info("按键事件返回值,未找到 Key 字段或其值为空。");
|
||||
System.out.println("未找到 Key 字段或其值为空。");
|
||||
}
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session, CloseReason closeReason) {
|
||||
|
||||
System.out.println("Connection closed: " + closeReason.getReasonPhrase());
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public Boolean getConfirm() {
|
||||
return confirm;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
if (session != null && session.isOpen()) {
|
||||
|
@ -69,14 +88,28 @@ public class WebSocketClient {
|
|||
}
|
||||
}
|
||||
|
||||
public static Integer parseAsciiToDigit(Integer asciiValue) {
|
||||
// 检查输入的 ASCII 值是否在 '0' 到 '9' 的范围内
|
||||
if (asciiValue >= 48 && asciiValue <= 57) {
|
||||
// 将 ASCII 值转换为对应的数字
|
||||
return asciiValue - 48;
|
||||
} else {
|
||||
// 如果输入的 ASCII 值不在 '0' 到 '9' 的范围内,返回 -1 表示无效输入
|
||||
return -1;
|
||||
}
|
||||
private static final Map<String, String> KEYPAD_MAPPING = new HashMap<>();
|
||||
|
||||
static {
|
||||
// 初始化映射关系
|
||||
KEYPAD_MAPPING.put("0x30", "0");
|
||||
KEYPAD_MAPPING.put("0x31", "1");
|
||||
KEYPAD_MAPPING.put("0x32", "2");
|
||||
KEYPAD_MAPPING.put("0x33", "3");
|
||||
KEYPAD_MAPPING.put("0x34", "4");
|
||||
KEYPAD_MAPPING.put("0x35", "5");
|
||||
KEYPAD_MAPPING.put("0x36", "6");
|
||||
KEYPAD_MAPPING.put("0x37", "7");
|
||||
KEYPAD_MAPPING.put("0x38", "8");
|
||||
KEYPAD_MAPPING.put("0x39", "9");
|
||||
KEYPAD_MAPPING.put("0x08", "BACKSPACE"); // 更正按键
|
||||
KEYPAD_MAPPING.put("0x10", "CLEAR");
|
||||
KEYPAD_MAPPING.put("0x0D", "ENTER"); // 确认按键
|
||||
KEYPAD_MAPPING.put("0x1B", "CANCEL"); // 取消
|
||||
KEYPAD_MAPPING.put("0xA1", "HELP"); // 帮助
|
||||
KEYPAD_MAPPING.put("0x2E", "DECPOINT");
|
||||
KEYPAD_MAPPING.put("0x02", "00");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue