package cn.minoa.view.mine; import java.io.File; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import cn.minoa.MainApp; import cn.minoa.dataRequestInterface.OrderInfo; import cn.minoa.dataRequestInterface.ResponseData; import cn.minoa.model.Person; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.ListView; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.Pane; import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.scene.control.Alert.AlertType; public class WrittenEmailController { private MainApp mainApp; private Stage dialogStage; @FXML private TextField receiverField; @FXML private TextField emailTitleField; @FXML private TextArea emailContentTextArea; @FXML private ListView emailFiles; //与之对应的数据结构 private List emailFilesList; public WrittenEmailController() { // TODO Auto-generated constructor stub emailFilesList=new LinkedList(); } @FXML private void initialize() { // 在fxml被载入时候被自动调用 receiverField.setPromptText("请输入邮件地址(格式:昵称@pkusz.edu.cn)"); emailTitleField.setPromptText("请输入邮件主题..."); emailContentTextArea.setPromptText("请输入邮件内容..."); } /* * @param mainApp */ public void setMainApp(MainApp mainApp) { this.mainApp = mainApp; } public void setStage(Stage stage) { this.dialogStage = stage; } /* * 从弹窗获取要添加的附件文件路径,将其加入到附件列表中,并显示到前端 */ @FXML private void addFilesToEmail() { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("选择上传文件"); Stage selectFileStage = new Stage(); File file = fileChooser.showOpenDialog(selectFileStage); if (file != null) { //文件不为空,且不是文件夹 if(file.isFile()) { if(!isSelected(file.getAbsolutePath())) { emailFilesList.add(file.getAbsolutePath()); emailFiles.getItems().add(new emailFileListItem(file.getAbsolutePath()).getPaneItem()); }else { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("附件添加重复"); alert.setHeaderText("你选择的该文件已加入附件列表"); alert.setContentText("请确保你选中的文件是未加入附件列表的文件。"); alert.showAndWait(); } }else { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("附件类型错误"); alert.setHeaderText("你的文件类型错误"); alert.setContentText("请确保你选中的是一个文件而非文件夹。"); alert.showAndWait(); } } } //判断邮件地址是否符合要求 private boolean isRightEmailPath(String emailPath){ //取尾部字段:@pkusz.edu.cn [length=13] if(emailPath.length()<=13){ return false; } String tailPath=emailPath.substring(emailPath.length()-13); System.out.println("tailPath: "+tailPath); if(tailPath.equals("@pkusz.edu.cn")){ return true; }else{ return false; } } //取邮件地址中的昵称字段 private String getUserInEmailPath(String emailPath){ return emailPath.substring(0,emailPath.length()-13); } //根据文件路径判断该选中文件是否已经加入到列表中 private boolean isSelected(String absolutePath) { boolean flag=false; //... return flag; } //发送邮件 @FXML private void handleSendEmail() { if(isRightContent()) { // 请求数据 OrderInfo orderInfo = new OrderInfo(); ResponseData responseData = new ResponseData(); Long seqLong = mainApp.getNewDataReqId(); orderInfo.setSeq(seqLong); orderInfo.setJsonString(getSendApproval()); System.out.println("getSendApproval: " + orderInfo.getJsonString()); mainApp.minoaDataAPI.executeOrder("/sendApproval", orderInfo); responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong); JSONObject jsonObject = responseData.praseRequestData(); responseData.printSelf(); // 解析数据 Integer codeInteger = null; try { codeInteger = jsonObject.getInt("code"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (codeInteger == 200) { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("发送成功"); alert.setHeaderText("邮件发送成功"); alert.setContentText("你的邮件已经发送到对方的邮件箱中。"); alert.showAndWait(); exit(); }else { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("发送失败"); alert.setHeaderText("邮件发送失败"); alert.setContentText("登录信息过期,邮件发送失败,请尝试重新登录。"); alert.showAndWait(); } } } @FXML private void exit() { this.dialogStage.close(); } //验证邮件格式 private boolean isRightContent() { //验证接收人是否是联系人列表中的一个 String receiverRawName=receiverField.getText().trim(); if(!isRightEmailPath(receiverRawName)){ //弹窗提示 // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("地址后缀错误"); alert.setHeaderText("你的地址后缀错误"); alert.setContentText("请确保你输入的地址后缀是@pkusz.edu.cn,并保证前面有用户名。"); alert.showAndWait(); return false; } String receiverName=getUserInEmailPath(receiverRawName); Integer flag=0; ObservableList persons=mainApp.contactListData; for (Iterator iterator = persons.iterator(); iterator.hasNext();) { Person person = (Person) iterator.next(); if(person.getUserName().equals(receiverName)) { flag++; } } if(flag==0) { //弹窗提示 // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("收件人错误"); alert.setHeaderText("你的收件人错误"); alert.setContentText("请确保你输入的收件人姓名是系统的实际用户。"); alert.showAndWait(); return false; } //验证标题是否为空 if(emailTitleField.getText().trim().equals("")) { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("邮件标题错误"); alert.setHeaderText("你的邮件标题错误"); alert.setContentText("邮件标题不能为空。"); alert.showAndWait(); return false; } //验证内容是否为空 if(emailContentTextArea.getText().trim().equals("")) { // 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/ Alert alert = new Alert(AlertType.WARNING); alert.setTitle("邮件内容错误"); alert.setHeaderText("你的邮件内容错误"); alert.setContentText("邮件内容不能为空。"); alert.showAndWait(); return false; } return true; } //发送邮件命令 private String getSendApproval() { JSONObject detailJsonObject = new JSONObject(); String titleString=emailTitleField.getText().trim(); String contentString=emailContentTextArea.getText().trim(); //根据已经添加的文件附件列表制作一个arraylist JSONArray filesArray=new JSONArray(); for(int i=0;i