Commit 5eb0441b authored by youxiaoji's avatar youxiaoji

* [当信息以满足提交条件时,不再返回form信息]

parent 8fdca284
...@@ -72,7 +72,7 @@ public class VisitorAppointmentTool { ...@@ -72,7 +72,7 @@ public class VisitorAppointmentTool {
// 登录状态有效期(毫秒),设置为30分钟 // 登录状态有效期(毫秒),设置为30分钟
private static final long LOGIN_VALIDITY_PERIOD = 30 * 60 * 1000; private static final long LOGIN_VALIDITY_PERIOD = 30 * 60 * 1000;
public VisitorAppointmentTool(UserToken userToken,AgentService agentService, InfoCollectorService infoCollectorService, UserSseService userSseService) { public VisitorAppointmentTool(UserToken userToken, AgentService agentService, InfoCollectorService infoCollectorService, UserSseService userSseService) {
this.agentService = agentService; this.agentService = agentService;
this.infoCollectorService = infoCollectorService; this.infoCollectorService = infoCollectorService;
this.ssoToken = "d10bc61aa4e00dcc6f08de64ca42012814fdbcee9b88aa977f7fb07d3a4018f4"; this.ssoToken = "d10bc61aa4e00dcc6f08de64ca42012814fdbcee9b88aa977f7fb07d3a4018f4";
...@@ -140,13 +140,14 @@ public class VisitorAppointmentTool { ...@@ -140,13 +140,14 @@ public class VisitorAppointmentTool {
log.error("海信SSO认证工具的Playwright资源释放失败: ", e); log.error("海信SSO认证工具的Playwright资源释放失败: ", e);
} }
} }
@Tool(description = "提交访客预约申请") @Tool(description = "提交访客预约申请")
public String submitAppointmentApply(JSONObject jsonObject){ public String submitAppointmentApply(JSONObject jsonObject) {
sharedContext.tracing().start(new Tracing.StartOptions() sharedContext.tracing().start(new Tracing.StartOptions()
.setScreenshots(true) .setScreenshots(true)
.setSnapshots(true) .setSnapshots(true)
.setSources(true)); .setSources(true));
log.info("jsonObject = {}",jsonObject); log.info("jsonObject = {}", jsonObject);
JSONArray jsonArray = infoCollectorService.getInfo(pageId); JSONArray jsonArray = infoCollectorService.getInfo(pageId);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
...@@ -168,31 +169,31 @@ public class VisitorAppointmentTool { ...@@ -168,31 +169,31 @@ public class VisitorAppointmentTool {
log.info("当前页面URL: {}", currentUrl); log.info("当前页面URL: {}", currentUrl);
String parkValue = ""; String parkValue = "";
List<JSONObject> dateJson = new ArrayList<>(); List<JSONObject> dateJson = new ArrayList<>();
for(int i=0;i<jsonArray.size();i++){ for (int i = 0; i < jsonArray.size(); i++) {
log.info("index {} ",i); log.info("index {} ", i);
JSONObject obj = jsonArray.getJSONObject(i); JSONObject obj = jsonArray.getJSONObject(i);
log.info("json {}",obj); log.info("json {}", obj);
String fieldName = obj.getString("field_name"); String fieldName = obj.getString("field_name");
String fieldValue = jsonObject.getString(obj.getString("code")); String fieldValue = jsonObject.getString(obj.getString("code"));
if(fieldName.compareToIgnoreCase("访问园区") == 0){ if (fieldName.compareToIgnoreCase("访问园区") == 0) {
parkValue = fieldValue; parkValue = fieldValue;
continue; continue;
} }
if(fieldName.compareToIgnoreCase("接访员工手机号")== 0 || fieldName.compareToIgnoreCase("接访员工姓名")==0){ if (fieldName.compareToIgnoreCase("接访员工手机号") == 0 || fieldName.compareToIgnoreCase("接访员工姓名") == 0) {
continue; continue;
} }
if(fieldName.contains("日期")) { if (fieldName.contains("日期")) {
dateJson.add(obj); dateJson.add(obj);
//fieldLocator.evaluate("el => el.value = '"+fieldValue+"'");; //fieldLocator.evaluate("el => el.value = '"+fieldValue+"'");;
}else { } else {
Locator fieldLocator = page.locator(".van-cell") Locator fieldLocator = page.locator(".van-cell")
.filter(new Locator.FilterOptions().setHasText(Pattern.compile("^" + fieldName + "$"))) .filter(new Locator.FilterOptions().setHasText(Pattern.compile("^" + fieldName + "$")))
.locator("input"); .locator("input");
Locator.FillOptions fillOptions = new Locator.FillOptions(); Locator.FillOptions fillOptions = new Locator.FillOptions();
fillOptions.setForce(true); fillOptions.setForce(true);
fieldLocator .fill(fieldValue, fillOptions); fieldLocator.fill(fieldValue, fillOptions);
} }
//saveScreenShot(page.screenshot(),"ipark"); //saveScreenShot(page.screenshot(),"ipark");
} }
...@@ -205,39 +206,39 @@ public class VisitorAppointmentTool { ...@@ -205,39 +206,39 @@ public class VisitorAppointmentTool {
page.getByText(parkValue, new Page.GetByTextOptions().setExact(false)).click(); page.getByText(parkValue, new Page.GetByTextOptions().setExact(false)).click();
page.getByText("确认", new Page.GetByTextOptions().setExact(true)).click(); page.getByText("确认", new Page.GetByTextOptions().setExact(true)).click();
page.locator(".van-overlay").waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN)); page.locator(".van-overlay").waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN));
for(JSONObject tmp:dateJson){ for (JSONObject tmp : dateJson) {
String fieldName = tmp.getString("field_name"); String fieldName = tmp.getString("field_name");
String fieldValue = jsonObject.getString(tmp.getString("code")); String fieldValue = jsonObject.getString(tmp.getString("code"));
String[] values = fieldValue.split("-"); String[] values = fieldValue.split("-");
page.locator(".van-cell") page.locator(".van-cell")
.filter(new Locator.FilterOptions().setHasText(Pattern.compile("^"+fieldName+"$"))) .filter(new Locator.FilterOptions().setHasText(Pattern.compile("^" + fieldName + "$")))
.locator("input").click(); .locator("input").click();
page.locator(".van-overlay").first().waitFor(waitUntilOptions); page.locator(".van-overlay").first().waitFor(waitUntilOptions);
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[0]+"年")).click(); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[0] + "年")).click();
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[1]+"月")).click(); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[1] + "月")).click();
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[2]+"日")).click(); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(values[2] + "日")).click();
// 点击确认 // 点击确认
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确认")).click(); page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确认")).click();
page.locator(".van-overlay").first().waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN)); page.locator(".van-overlay").first().waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.HIDDEN));
} }
saveScreenShot(page.screenshot(),"ipark"); saveScreenShot(page.screenshot(), "ipark");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("提交")) page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("提交"))
.click(); .click();
page.waitForTimeout(3000); page.waitForTimeout(3000);
if(page.url().compareToIgnoreCase(destUrl) == 0){ if (page.url().compareToIgnoreCase(destUrl) == 0) {
log.info("有必填项未提供"); log.info("有必填项未提供");
} }
return "提交成功"; return "提交成功";
}catch (Exception e){ } catch (Exception e) {
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
String errorMsg = "获取海信出差申请页面内容失败: " + e.getMessage(); String errorMsg = "获取海信出差申请页面内容失败: " + e.getMessage();
log.error("获取海信海信出差申请内容失败,耗时: {} ms", endTime - startTime, e); log.error("获取海信海信出差申请内容失败,耗时: {} ms", endTime - startTime, e);
return errorMsg; return errorMsg;
}finally { } finally {
// 释放页面资源 // 释放页面资源
if (page != null) { if (page != null) {
try { try {
...@@ -250,15 +251,16 @@ public class VisitorAppointmentTool { ...@@ -250,15 +251,16 @@ public class VisitorAppointmentTool {
.setPath(Paths.get("trace1.zip"))); .setPath(Paths.get("trace1.zip")));
} }
} }
private void saveScreenShot(byte[] bytes,String suffix){
private void saveScreenShot(byte[] bytes, String suffix) {
// 生成一个唯一的文件名,防止覆盖 // 生成一个唯一的文件名,防止覆盖
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
String fileName = "screenshot_" + timestamp +"_"+suffix+ ".png"; String fileName = "screenshot_" + timestamp + "_" + suffix + ".png";
try { try {
// Paths.get() 指定存储路径,默认在项目根目录 // Paths.get() 指定存储路径,默认在项目根目录
Files.write(Paths.get(fileName), bytes); Files.write(Paths.get(fileName), bytes);
log.info("截图已保存至: {}" , fileName); log.info("截图已保存至: {}", fileName);
} catch (IOException e) { } catch (IOException e) {
log.info("保存截图失败: " + e.getMessage()); log.info("保存截图失败: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
...@@ -266,23 +268,25 @@ public class VisitorAppointmentTool { ...@@ -266,23 +268,25 @@ public class VisitorAppointmentTool {
} }
@Tool(description = "如果在用户信息中有任何与访客预约相关的信息,调用这个工具来保存访客预约信息") @Tool(description = "如果在用户信息中有任何与访客预约相关的信息,调用这个工具来保存访客预约信息")
public String applyInfoSave(@ToolParam(required = true) JSONObject infos,ToolContext toolContext) throws IOException { public String applyInfoSave(@ToolParam(required = true) JSONObject infos, ToolContext toolContext) throws IOException {
log.info("applyInfoSave(infos={})", infos); log.info("applyInfoSave(infos={})", infos);
infos.keySet().forEach(key -> { infos.keySet().forEach(key -> {
infoCollectorService.saveValue(key, infos.get(key)); infoCollectorService.saveValue(key, infos.get(key));
}); });
Set<String> keys = infoCollectorService.findLackInfo(pageId); Set<String> keys = infoCollectorService.findLackInfo(pageId);
if (!keys.isEmpty()) {
JSONArray jsonArray = infoCollectorService.getInfo(pageId); JSONArray jsonArray = infoCollectorService.getInfo(pageId);
JSONArray lackJson = new JSONArray(); JSONArray lackJson = new JSONArray();
for(int i=0;i<jsonArray.size();i++){ for (int i = 0; i < jsonArray.size(); i++) {
JSONObject tmp = jsonArray.getJSONObject(i); JSONObject tmp = jsonArray.getJSONObject(i);
if(keys.contains(tmp.getString("field_name"))){ if (keys.contains(tmp.getString("field_name"))) {
lackJson.add(tmp.getJSONObject("pangea_json")); lackJson.add(tmp.getJSONObject("pangea_json"));
} }
} }
JSONObject formMessage = new JSONObject(); JSONObject formMessage = new JSONObject();
formMessage.put("coms", lackJson); formMessage.put("coms", lackJson);
sendFormMessage(formMessage,toolContext); sendFormMessage(formMessage, toolContext);
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (keys.isEmpty()) { if (keys.isEmpty()) {
sb.append("用户已提交全部数据,提示用户提交申请"); sb.append("用户已提交全部数据,提示用户提交申请");
...@@ -298,7 +302,8 @@ public class VisitorAppointmentTool { ...@@ -298,7 +302,8 @@ public class VisitorAppointmentTool {
} }
return sb.toString(); return sb.toString();
} }
private void sendFormMessage(JSONObject formMessage,ToolContext toolContext) throws IOException {
private void sendFormMessage(JSONObject formMessage, ToolContext toolContext) throws IOException {
String emitterId = toolContext.getContext().get("emitterId").toString(); String emitterId = toolContext.getContext().get("emitterId").toString();
SseEmitter sseEmitter = userSseService.getEmitter(emitterId); SseEmitter sseEmitter = userSseService.getEmitter(emitterId);
log.info("Send Form Message {}", formMessage); log.info("Send Form Message {}", formMessage);
...@@ -314,18 +319,18 @@ public class VisitorAppointmentTool { ...@@ -314,18 +319,18 @@ public class VisitorAppointmentTool {
public String getAppointmentApplyNecessaryInfo(ToolContext toolContext) { public String getAppointmentApplyNecessaryInfo(ToolContext toolContext) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
if(infoCollectorService.exists(pageId)){ if (infoCollectorService.exists(pageId)) {
JSONArray jsonArray = infoCollectorService.getInfo(pageId); JSONArray jsonArray = infoCollectorService.getInfo(pageId);
JSONArray lackJson = new JSONArray(); JSONArray lackJson = new JSONArray();
for(int i=0;i<jsonArray.size();i++){ for (int i = 0; i < jsonArray.size(); i++) {
JSONObject tmp = jsonArray.getJSONObject(i); JSONObject tmp = jsonArray.getJSONObject(i);
lackJson.add(tmp.getJSONObject("pangea_json")); lackJson.add(tmp.getJSONObject("pangea_json"));
} }
JSONObject formMessage = new JSONObject(); JSONObject formMessage = new JSONObject();
formMessage.put("coms", lackJson); formMessage.put("coms", lackJson);
try { try {
sendFormMessage(formMessage,toolContext); sendFormMessage(formMessage, toolContext);
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
stringBuilder.append(formMessage.toJSONString()); stringBuilder.append(formMessage.toJSONString());
...@@ -346,7 +351,7 @@ public class VisitorAppointmentTool { ...@@ -346,7 +351,7 @@ public class VisitorAppointmentTool {
log.info("正在访问业务系统页面: {}", destUrl); log.info("正在访问业务系统页面: {}", destUrl);
String faviconUrl = "https://vrms-proxy.hisense.com/favicon.ico"; String faviconUrl = "https://vrms-proxy.hisense.com/favicon.ico";
page.navigate(faviconUrl); page.navigate(faviconUrl);
page.evaluate("() => sessionStorage.setItem('Access-Token', '"+iparkToken+"')"); page.evaluate("() => sessionStorage.setItem('Access-Token', '" + iparkToken + "')");
page.navigate(destUrl, new Page.NavigateOptions().setWaitUntil(WaitUntilState.NETWORKIDLE)); page.navigate(destUrl, new Page.NavigateOptions().setWaitUntil(WaitUntilState.NETWORKIDLE));
// 检查是否重定向到了SSO登录页面 // 检查是否重定向到了SSO登录页面
String currentUrl = page.url(); String currentUrl = page.url();
...@@ -368,7 +373,7 @@ public class VisitorAppointmentTool { ...@@ -368,7 +373,7 @@ public class VisitorAppointmentTool {
// 提取页面内容 // 提取页面内容
String content = stringBuilder.toString(); String content = stringBuilder.toString();
JSONObject formMessage = generateJson(jsonArray); JSONObject formMessage = generateJson(jsonArray);
sendFormMessage(formMessage,toolContext); sendFormMessage(formMessage, toolContext);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
log.info("成功获取海信出差申请页面内容,耗时: {} ms", endTime - startTime); log.info("成功获取海信出差申请页面内容,耗时: {} ms", endTime - startTime);
log.info("用户需要提交的信息包括:{}", formMessage); log.info("用户需要提交的信息包括:{}", formMessage);
...@@ -399,7 +404,7 @@ public class VisitorAppointmentTool { ...@@ -399,7 +404,7 @@ public class VisitorAppointmentTool {
JSONObject tmp = jsonArray.getJSONObject(i); JSONObject tmp = jsonArray.getJSONObject(i);
JSONObject obj = buildComponents(tmp); JSONObject obj = buildComponents(tmp);
tmp.put("code", obj.getJSONObject("props").getString("name")); tmp.put("code", obj.getJSONObject("props").getString("name"));
tmp.put("pangea_json",obj); tmp.put("pangea_json", obj);
components.add(obj); components.add(obj);
} }
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment