mirror of
https://gitee.com/willfree/minoaClientWindows_gradle.git
synced 2026-06-17 11:40:39 +08:00
1179 lines
52 KiB
Java
1179 lines
52 KiB
Java
package cn.minoa.view.login;
|
||
|
||
import java.io.File;
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
|
||
import cn.minoa.dataRequestInterface.MinoaDataAPI;
|
||
import javafx.scene.image.Image;
|
||
import javafx.scene.image.ImageView;
|
||
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 cn.minoa.model.SingleMessage;
|
||
import cn.minoa.model.SingleNotice;
|
||
import javafx.collections.FXCollections;
|
||
import javafx.collections.ObservableList;
|
||
import javafx.fxml.FXML;
|
||
import javafx.scene.control.Alert;
|
||
import javafx.scene.control.Alert.AlertType;
|
||
import javafx.scene.control.PasswordField;
|
||
import javafx.scene.control.TextField;
|
||
|
||
public class LoginOverviewController {
|
||
|
||
private MainApp mainApp;
|
||
private OrderInfo orderInfo;
|
||
|
||
public LoginOverviewController() {
|
||
orderInfo = new OrderInfo();
|
||
}
|
||
|
||
@FXML
|
||
private TextField userNameFiled;
|
||
@FXML
|
||
private PasswordField passwordFiled;
|
||
@FXML
|
||
private ImageView loginHeadPhoto;
|
||
@FXML
|
||
private ImageView miniLogo;
|
||
@FXML
|
||
private ImageView settingImage;
|
||
|
||
@FXML
|
||
private void initialize() {
|
||
// 在fxml被载入时候被自动调用
|
||
miniLogo.setImage(new Image("file:resources/images/logo_mini.png"));
|
||
settingImage.setImage(new Image("file:resources/images/setting_gear.png"));
|
||
loginHeadPhoto.setImage(new Image("file:resources/images/ic_launcher-web.png"));
|
||
}
|
||
|
||
/*
|
||
* @param mainApp
|
||
*/
|
||
public void setMainApp(MainApp mainApp) {
|
||
this.mainApp = mainApp;
|
||
}
|
||
|
||
@FXML
|
||
private void handleSetting() {
|
||
// 弹出注册界面
|
||
mainApp.showSettingOverview();
|
||
}
|
||
|
||
@FXML
|
||
private void handleRegister() {
|
||
// 弹出注册界面
|
||
mainApp.showRegisterOverview();
|
||
}
|
||
|
||
@FXML
|
||
private void handleRegisterByFlip() {
|
||
// 从登录页翻转到注册界面
|
||
mainApp.showRegisterOverviewByFlip();
|
||
}
|
||
|
||
@FXML
|
||
private void handleFindPassword() {
|
||
// 找回密码
|
||
// 。。。
|
||
}
|
||
|
||
@FXML
|
||
private void handleLogin() {
|
||
// 实例化
|
||
mainApp.minoaDataAPI = new MinoaDataAPI(mainApp);
|
||
// 登录验证
|
||
String loginString = getLoginJsonString();
|
||
boolean isRight = checkPass(loginString);
|
||
if (isRight) {
|
||
mainApp.iniShow();
|
||
} else {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(AlertType.WARNING);
|
||
alert.setTitle("Wrong UserName or Password");
|
||
alert.setHeaderText("Please correct UserName or Password.");
|
||
alert.setContentText("Please correct UserName or Password.");
|
||
alert.showAndWait();
|
||
}
|
||
}
|
||
|
||
private boolean checkPass(String loginJsonString) {
|
||
System.out.println("checking your password...");
|
||
boolean flag = false;
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(loginJsonString);
|
||
mainApp.minoaDataAPI.executeOrder("/login", orderInfo);
|
||
// 根据结果判断是否登录成功
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// responseData.printSelf();
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
System.out.println("login 200");
|
||
flag = true;
|
||
// 初始化Loginer的信息
|
||
updateLoginer(jsonObject);
|
||
System.out.println("ini loginer (include the head photo) success");
|
||
// 初始化其它系统信息
|
||
iniLoginerData();
|
||
// 打印输出
|
||
mainApp.loginer.printSelf();
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
// 初始化登录者的一些系统信息
|
||
private void iniLoginerData() {
|
||
// 初始化联系人列表信息及其头像
|
||
updateContactList();
|
||
System.out.println("ini contact list (include the head photos) success");
|
||
// 初始化与所有联系人的消息记录列表
|
||
updateMessageLog();
|
||
System.out.println("ini message log success");
|
||
// 初始化通知列表
|
||
updateNoticeList();
|
||
System.out.println("ini notice success");
|
||
// 开启[消息列表/通知/审批]的线程循环监听
|
||
System.out.println("Creating Thread to Bind News...");
|
||
mainApp.creatThreadToRefreshMessageLog();
|
||
System.out.println("Created Thread to Bind News.");
|
||
}
|
||
|
||
// 请求服务器某文件的大小:返回-1表示没有该文件或登录失效
|
||
public Integer getServerFileLength(String filename) {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getFileInfoJson(filename));
|
||
mainApp.minoaDataAPI.executeOrder("/fileInfo", orderInfo);
|
||
// 获取头像信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
System.out.println("getServerFileLength-fileInfo-JSON: " + jsonObject.toString());
|
||
responseData.printSelf();
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
Integer fileSize= dataJsonObject.getInt("fileSize");
|
||
return fileSize;
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
return -1;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
// 下载文件[返回0:已经成功接收所有回执数据并写入本地文件]
|
||
public Integer downloadFile(String filename) {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getFileInfoJson(filename));
|
||
mainApp.minoaDataAPI.executeOrder("/fileInfo", orderInfo);
|
||
// 获取头像信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
System.out.println("fileInfo-JSON: " + jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
Integer sliceNumInteger;
|
||
sliceNumInteger = dataJsonObject.getInt("sliceNum");
|
||
// 根据分片信息获取每个分片,并合并成一个完整比特流
|
||
Integer flagInteger = 0;
|
||
for (int i = 0; i < sliceNumInteger; i++) {
|
||
flagInteger += downLoadFileBySlice(filename, i);
|
||
}
|
||
System.out.println("下载分片的消息回执[0表示全部数据分片已经被写入本地]: " + flagInteger);
|
||
return flagInteger;
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
return 1;
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
/*
|
||
* @param filename 要下载的文件的“服务器路径+文件名称”,即写入请求的名称
|
||
*
|
||
* @param localDir 要下载文件到的本地地址
|
||
*
|
||
* @param localName 要下载的文件重命名之后的本地名称
|
||
*
|
||
* @return 0表示已经成功接收所有回执数据并写入本地文件,否则出现error
|
||
*
|
||
* 目的是下载头像到指定路径,并重命名之。
|
||
*/
|
||
private Integer downloadRenamedFileToSpecifiedDir(String filename, String localDir, String localName) {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getFileInfoJson(filename));
|
||
mainApp.minoaDataAPI.executeOrder("/fileInfo", orderInfo);
|
||
// 获取头像信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
System.out.println("fileInfo-JSON: " + jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
Integer sliceNumInteger;
|
||
sliceNumInteger = dataJsonObject.getInt("sliceNum");
|
||
// 根据分片信息获取每个分片,并合并成一个完整比特流
|
||
Integer flagInteger = 0;
|
||
for (int i = 0; i < sliceNumInteger; i++) {
|
||
flagInteger += downLoadFileBySlice(filename, i, localDir, localName);
|
||
}
|
||
System.out.println("下载分片的消息回执[0表示全部数据分片已经被写入本地]: " + flagInteger);
|
||
return flagInteger;
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
return 1;
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
/*
|
||
* @param filename 要下载的文件的“服务器路径+文件名称”,即写入请求的名称
|
||
*
|
||
* @param localDir 要下载文件到的本地地址
|
||
*
|
||
* @return 0表示已经成功接收所有回执数据并写入本地文件,否则出现error
|
||
*
|
||
* .下载服务器文件到本地指定目录下
|
||
*/
|
||
public Integer downloadFileToSpecifiedDir(String filename, String localDir) {
|
||
// 下载到本地的文件名称
|
||
String localName = getPureFileName(filename);
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getFileInfoJson(filename));
|
||
mainApp.minoaDataAPI.executeOrder("/fileInfo", orderInfo);
|
||
// 获取文件信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
System.out.println("fileInfo-JSON: " + jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
Integer sliceNumInteger;
|
||
sliceNumInteger = dataJsonObject.getInt("sliceNum");
|
||
// 根据分片信息获取每个分片,并合并成一个完整比特流
|
||
Integer flagInteger = 0;
|
||
for (int i = 0; i < sliceNumInteger; i++) {
|
||
flagInteger += downLoadFileBySlice(filename, i, localDir, localName);
|
||
}
|
||
System.out.println("下载分片的消息回执[0表示全部数据分片已经被写入本地]: " + flagInteger);
|
||
return flagInteger;
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
return 1;
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
// 请求文件分片数据=>返回0表示分片数据被成功下载
|
||
public Integer downLoadFileBySlice(String filename, Integer sliceNo, String localDir, String localName) {
|
||
Integer flagInteger;
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getDownloadFileJson(filename, sliceNo));
|
||
mainApp.minoaDataAPI.executeOrder("/downloadFile", orderInfo, localDir, localName);
|
||
// 获取头像信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// JSONObject jsonObject=responseData.praseRequestData();
|
||
System.out.println("downloadFileSlice-JSON: " + responseData.getResponseJsonDataString());
|
||
flagInteger = Integer.parseInt(responseData.getResponseJsonDataString());
|
||
return flagInteger;
|
||
}
|
||
|
||
// 生成本地文件=>没有用到
|
||
// public void creatLocalFile(String filename,byte[] fileBytes) {
|
||
// FileOutputStream out;
|
||
// try {
|
||
// out = new FileOutputStream(new File(filename));
|
||
// try {
|
||
// out.write(fileBytes);
|
||
// out.flush();
|
||
// System.out.println("download success");
|
||
// out.close();
|
||
// } catch (IOException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// }
|
||
// } catch (FileNotFoundException e) {
|
||
// // TODO Auto-generated catch block
|
||
// e.printStackTrace();
|
||
// }
|
||
// }
|
||
|
||
// 根据包含路径的文件名获取纯文件名[linux下]
|
||
public String getPureFileName(String pathfilename) {
|
||
// 标记最后一个/的下标
|
||
int indexLeft = -1;
|
||
for (int i = 0; i < pathfilename.length(); i++) {
|
||
if (pathfilename.charAt(i) == '/') {
|
||
indexLeft = i;
|
||
}
|
||
}
|
||
if (indexLeft == -1) {
|
||
System.out.println("pure name[linux]: " + pathfilename);
|
||
return pathfilename;
|
||
} else {
|
||
System.out.println("pure name[linux]: " + pathfilename.substring(indexLeft + 1, pathfilename.length()));
|
||
return pathfilename.substring(indexLeft + 1, pathfilename.length());
|
||
}
|
||
}
|
||
|
||
// 根据包含路径的文件名获取纯文件名
|
||
// public String getPureFileName(String pathfilename) {
|
||
// // 文件名称切割
|
||
// while (pathfilename.indexOf('/') > 0) {
|
||
// pathfilename = pathfilename.substring(pathfilename.indexOf('/') + 1, pathfilename.length());
|
||
// }
|
||
// System.out.println("pure file name: " + pathfilename);
|
||
// return pathfilename;
|
||
// }
|
||
|
||
// 合并两个Byte[]
|
||
// public Byte[] byteMerger(Byte[] bt1, Byte[] bt2){
|
||
// Byte[] bt3 = new Byte[bt1.length+bt2.length];
|
||
// System.arraycopy(bt1, 0, bt3, 0, bt1.length);
|
||
// System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
|
||
// return bt3;
|
||
// }
|
||
|
||
// 请求文件分片数据=>返回0表示分片数据被成功下载
|
||
private Integer downLoadFileBySlice(String filename, Integer sliceNo) {
|
||
Integer flagInteger;
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getDownloadFileJson(filename, sliceNo));
|
||
mainApp.minoaDataAPI.executeOrder("/downloadFile", orderInfo);
|
||
// 获取头像信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// JSONObject jsonObject=responseData.praseRequestData();
|
||
System.out.println("downloadFileSlice-JSON: " + responseData.getResponseJsonDataString());
|
||
flagInteger = Integer.parseInt(responseData.getResponseJsonDataString());
|
||
return flagInteger;
|
||
}
|
||
|
||
// 获取要下载的文件信息
|
||
public String getFileInfoJson(String filename) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/fileInfo";
|
||
String usernameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
String filenameString = filename;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", usernameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("filename", filenameString);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 获取要下载的文件信息
|
||
private String getDownloadFileJson(String filename, Integer sliceNo) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/downloadFile";
|
||
String usernameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
String filenameString = filename;
|
||
Integer sliceNoInteger = sliceNo;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", usernameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("filename", filenameString);
|
||
jsonObject.put("sliceNo", sliceNoInteger);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 从前端界面获得登录数据并转换成json格式
|
||
private String getLoginJsonString() {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/login";
|
||
String userNameString = userNameFiled.getText().trim();
|
||
String passWordString = passwordFiled.getText().trim();
|
||
if (userNameString == null || passWordString == null) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(AlertType.WARNING);
|
||
alert.setTitle("UserName or Password is null");
|
||
alert.setHeaderText("Please correct UserName or Password.");
|
||
alert.setContentText("Please correct UserName or Password.");
|
||
alert.showAndWait();
|
||
}
|
||
mainApp.loginer.setUserName(userNameString);
|
||
mainApp.loginer.setPassWord(passWordString);
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", userNameString);
|
||
jsonObject.put("password", passWordString);
|
||
} catch (Exception e) {
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 根据请求到的json数据更新loginer的信息
|
||
private void updateLoginer(JSONObject loginerJsonObject) {
|
||
String dataString;
|
||
try {
|
||
dataString = loginerJsonObject.getString("data");
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
String uuid = dataJsonObject.getString("uuid");
|
||
mainApp.loginer.setUuid(uuid);
|
||
// 初始化登录者头像
|
||
String avatarString = dataJsonObject.getString("avatar");
|
||
if (!isHaveHeadPhoto(mainApp.loginer.getUserName() + ".png")) {
|
||
System.out.println("downloading " + mainApp.loginer.getUserName() + "'s head photo file...");
|
||
downloadRenamedFileToSpecifiedDir(avatarString, mainApp.userHeadPhotoBinPath, mainApp.loginer.getUserName() + ".png");
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
// 拿到获取用户列表的json格式字符串
|
||
private String getGetUsersJson() {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/getUsers";
|
||
String userNameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", userNameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 初始化联系人列表信息
|
||
private void updateContactList() {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getGetUsersJson());
|
||
mainApp.minoaDataAPI.executeOrder("/getUsers", orderInfo);
|
||
// 获取用户列表信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
// System.out.println("getUsers-JSON: "+jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
JSONArray dataJsonArray = new JSONArray(dataString);
|
||
String userCacheString;
|
||
JSONObject userCacheJsonObject;
|
||
String userNameCacheString;
|
||
// System.out.println("data: "+dataString);
|
||
// 遍历每个用户联系人,将其加入mainApp的联系人列表中
|
||
for (int i = 0; i < dataJsonArray.length(); i++) {
|
||
userCacheString = dataJsonArray.getString(i);
|
||
userCacheJsonObject = new JSONObject(userCacheString);
|
||
userNameCacheString = userCacheJsonObject.getString("username");
|
||
// System.out.println("user-"+i+": "+userNameCacheString);
|
||
mainApp.contactListData.add(new Person(userNameCacheString));
|
||
// 初始化联系人头像到本地=》目前接口没返回头像路径
|
||
String avatarString = userCacheJsonObject.getString("avatar");
|
||
if (!isHaveHeadPhoto(userNameCacheString + ".png")) {
|
||
System.out.println("downloading " + userNameCacheString + "'s head photo file...");
|
||
downloadRenamedFileToSpecifiedDir(avatarString, mainApp.userHeadPhotoBinPath, userNameCacheString + ".png");
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
//判断是否已经有本地头像
|
||
boolean isHaveHeadPhoto(String headPhotoString) {
|
||
File file = new File(mainApp.userHeadPhotoBinPath + headPhotoString);
|
||
if (file.exists()) {
|
||
System.out.println(headPhotoString + " has existed.");
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 请求通知消息
|
||
private String getGetInformJson() {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/getInform";
|
||
String usernameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
Integer pageNoInteger = 0;
|
||
Integer pageSizeInteger = 100;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", usernameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("pageNo", pageNoInteger);
|
||
jsonObject.put("pageSize", pageSizeInteger);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 请求每个通知的数据分片的JSON
|
||
public String getGetDataSlice(String dataId, Integer sliceNo) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/getDataSlice";
|
||
String usernameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
Integer sliceNoInteger = sliceNo;
|
||
String dataIdString = dataId;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", usernameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("dataId", dataIdString);
|
||
jsonObject.put("sliceNo", sliceNoInteger);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
//重新加载所有通知列表信息
|
||
public void reloadNoticeList(){
|
||
mainApp.noticeList.clear();
|
||
updateNoticeList();
|
||
}
|
||
|
||
// 初始化通知列表信息
|
||
private void updateNoticeList() {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getGetInformJson());
|
||
mainApp.minoaDataAPI.executeOrder("/getInform", orderInfo);
|
||
// 获取用户列表信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
System.out.println("通知答复:");
|
||
responseData.printSelf();
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
System.out.println("getInform-JSON: " + jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
if (dataString.equals("")) {
|
||
//数据为空
|
||
} else {
|
||
JSONArray dataJsonArray = new JSONArray(dataString);
|
||
String singleNoticeCacheString;
|
||
JSONObject singleNoticeCacheJsonObject;
|
||
Integer noticeIdInteger;
|
||
String usernameCacheString;
|
||
String titleCacheString;
|
||
String contentCacheString;
|
||
String starttimeCacheString;
|
||
String endtimeCacheString;
|
||
String attachementCacheString;
|
||
// 遍历每个用户联系人,将其加入mainApp的联系人列表中
|
||
for (int i = 0; i < dataJsonArray.length(); i++) {
|
||
singleNoticeCacheString = dataJsonArray.getString(i);
|
||
singleNoticeCacheJsonObject = new JSONObject(singleNoticeCacheString);
|
||
//拿到通知ID
|
||
noticeIdInteger = singleNoticeCacheJsonObject.getInt("id");
|
||
// 拿到新闻发布人
|
||
usernameCacheString = singleNoticeCacheJsonObject.getString("proposer");
|
||
// 拿到content
|
||
// JSONObject detailJsonObject = singleNoticeCacheJsonObject.getJSONObject("detail");
|
||
String detailString = singleNoticeCacheJsonObject.getString("detail");
|
||
System.out.println("通知详细: " + detailString);
|
||
JSONObject detailJsonObject = new JSONObject(detailString);
|
||
contentCacheString = detailJsonObject.getString("content");
|
||
// 拿到title
|
||
titleCacheString = detailJsonObject.getString("title");
|
||
// 拿到starttime、endtime
|
||
starttimeCacheString = detailJsonObject.getString("startTime");
|
||
endtimeCacheString = detailJsonObject.getString("endTime");
|
||
// 拿到attachment
|
||
attachementCacheString = detailJsonObject.getString("attachment");
|
||
// System.out.println("user-"+i+": "+usernameCacheString);
|
||
// 新建一个notice
|
||
SingleNotice singleNotice = new SingleNotice();
|
||
singleNotice.setNoticeId(noticeIdInteger);
|
||
singleNotice.setContent(contentCacheString);
|
||
singleNotice.setUsername(usernameCacheString);
|
||
singleNotice.setTitle(titleCacheString);
|
||
singleNotice.setStarttime(starttimeCacheString);
|
||
singleNotice.setEndtime(endtimeCacheString);
|
||
singleNotice.setAttachment(attachementCacheString);
|
||
mainApp.noticeList.add(singleNotice);
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
} else if (codeInteger == 201) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
if (dataString.equals("")) {
|
||
//数据为空
|
||
} else {
|
||
JSONObject dataJsonObject = new JSONObject(dataString);
|
||
String dataIdString = dataJsonObject.getString("dataId");
|
||
Integer dataSizeInteger = dataJsonObject.getInt("dataSize");
|
||
Integer sliceNumInteger = dataJsonObject.getInt("sliceNum");
|
||
Integer sliceSizeInteger = dataJsonObject.getInt("sliceSize");
|
||
byte[] noticelistByte = new byte[dataSizeInteger];
|
||
for (Integer sliceNo = 0; sliceNo < sliceNumInteger; sliceNo++) {
|
||
// 发送数据请求
|
||
Long seqLongT = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLongT);
|
||
System.out.println("getDataSlice: " + getGetDataSlice(dataIdString, sliceNo));
|
||
orderInfo.setJsonString(getGetDataSlice(dataIdString, sliceNo));
|
||
mainApp.minoaDataAPI.executeOrder("/getDataSlice", orderInfo);
|
||
// 获取用户列表信息
|
||
ResponseData responseDataCache = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLongT);
|
||
byte[] byteCache = responseDataCache.getFileSlice();
|
||
System.out.println("byteCache size:" + byteCache.length);
|
||
System.out.println("byteCache:" + byteCache);
|
||
if (sliceNo == (sliceNumInteger - 1)) {
|
||
for (int x = (sliceNo * sliceSizeInteger); x < dataSizeInteger; x++) {
|
||
noticelistByte[x] = byteCache[x - (sliceNo * sliceSizeInteger)];
|
||
}
|
||
} else {
|
||
for (int x = (sliceNo * sliceSizeInteger); x < (sliceNo * sliceSizeInteger + 7000); x++) {
|
||
noticelistByte[x] = byteCache[x - (sliceNo * sliceSizeInteger)];
|
||
}
|
||
}
|
||
}
|
||
try {
|
||
String cacheString = new String(noticelistByte, "UTF-8");
|
||
System.out.println(cacheString);
|
||
//解析通知列表
|
||
JSONArray dataJsonArray = new JSONArray(cacheString);
|
||
String singleNoticeCacheString;
|
||
JSONObject singleNoticeCacheJsonObject;
|
||
String usernameCacheString;
|
||
String titleCacheString;
|
||
String contentCacheString;
|
||
String starttimeCacheString;
|
||
String endtimeCacheString;
|
||
String attachementCacheString;
|
||
// 遍历每个用户联系人,将其加入mainApp的联系人列表中
|
||
for (int i = 0; i < dataJsonArray.length(); i++) {
|
||
singleNoticeCacheString = dataJsonArray.getString(i);
|
||
singleNoticeCacheJsonObject = new JSONObject(singleNoticeCacheString);
|
||
// 拿到新闻发布人
|
||
usernameCacheString = singleNoticeCacheJsonObject.getString("proposer");
|
||
// 拿到content
|
||
String detailString = singleNoticeCacheJsonObject.getString("detail");
|
||
JSONObject detailJsonObject = new JSONObject(detailString);
|
||
contentCacheString = detailJsonObject.getString("content");
|
||
// 拿到title
|
||
titleCacheString = detailJsonObject.getString("title");
|
||
// 拿到starttime、endtime
|
||
starttimeCacheString = detailJsonObject.getString("startTime");
|
||
endtimeCacheString = detailJsonObject.getString("endTime");
|
||
// 拿到attachment
|
||
attachementCacheString = detailJsonObject.getString("attachment");
|
||
// System.out.println("user-"+i+": "+usernameCacheString);
|
||
// 新建一个notice
|
||
SingleNotice singleNotice = new SingleNotice();
|
||
singleNotice.setContent(contentCacheString);
|
||
singleNotice.setUsername(usernameCacheString);
|
||
singleNotice.setTitle(titleCacheString);
|
||
singleNotice.setStarttime(starttimeCacheString);
|
||
singleNotice.setEndtime(endtimeCacheString);
|
||
singleNotice.setAttachment(attachementCacheString);
|
||
mainApp.noticeList.add(singleNotice);
|
||
}
|
||
} catch (UnsupportedEncodingException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
// 初始化与所有联系人的往来消息列表
|
||
public void updateMessageLog() {
|
||
System.out.println("ini message log start...");
|
||
// 清空旧的联系人消息清单
|
||
mainApp.messageLogMap.clear();
|
||
// 获取联系人清单
|
||
ObservableList<Person> contactList = mainApp.getContactListData();
|
||
// 遍历联系人,获取与他们的聊天消息记录
|
||
for (Person person : contactList) {
|
||
// 新建当前用户的消息记录列表
|
||
ObservableList<SingleMessage> messageLogByPersonCacheList = FXCollections.observableArrayList();
|
||
mainApp.messageLogMap.put(person.getUserName(), messageLogByPersonCacheList);
|
||
// 赋值该消息记录列表(当前时间之前的五条消息记录)
|
||
fullMessageLogByPerson(person);
|
||
}
|
||
// 为了容错"/getNews"轮询到自己发出的消息,这里在消息记录表中增加一个自己
|
||
ObservableList<SingleMessage> messageLogByMyselfList = FXCollections.observableArrayList();
|
||
mainApp.messageLogMap.put(mainApp.loginer.getUserName(), messageLogByMyselfList);
|
||
// 赋值登录时未读消息记录列表(循环请求getNews,并将联系人头像标红)
|
||
System.out.println("fullIniNotReadMessageListWhenLoginByGetNews-start");
|
||
for (int i = 0; i < 1; i++) { // 只请求一次getNews有时候拿不到新消息,这里多发送几次请求。
|
||
fullNotReadMessageListByGetNews();
|
||
}
|
||
System.out.println("fullIniNotReadMessageListWhenLoginByGetNews-end");
|
||
}
|
||
|
||
// 赋值该消息记录列表(循环请求getNews,并将联系人头像标红)
|
||
private void fullNotReadMessageListByGetNews() {
|
||
// 发送数据请求
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
OrderInfo orderInfo = new OrderInfo();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getGetNewsJson());
|
||
mainApp.minoaDataAPI.executeOrder("/getNews", orderInfo);
|
||
// 获取回复消息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// System.out.println("ini-getNews-responseData: "+responseData.getResponseJsonDataString());
|
||
JSONObject jsonObject = responseData.praseRequestData();
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
if (!dataString.equals("")) {
|
||
JSONArray dataJsonArray = new JSONArray(dataString);
|
||
String newsCacheString;
|
||
JSONObject newsCacheJsonObject;
|
||
Integer typeCodeInteger;
|
||
String contentString;
|
||
// 遍历每个新消息或者通知或者审批,将其更新到前端内存数据中
|
||
for (int i = 0; i < dataJsonArray.length(); i++) {
|
||
newsCacheString = dataJsonArray.getString(i);
|
||
newsCacheJsonObject = new JSONObject(newsCacheString);
|
||
typeCodeInteger = newsCacheJsonObject.getInt("typeCode");
|
||
contentString = newsCacheJsonObject.getString("content");
|
||
if (typeCodeInteger == 0) {
|
||
// 处理新消息
|
||
handleNewSingleMessage(contentString);
|
||
} else if (typeCodeInteger == 1) {
|
||
// 处理新通知
|
||
} else if (typeCodeInteger == 2) {
|
||
// 处理新审批
|
||
}
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取是否有新消息产生的json字符串
|
||
private String getGetNewsJson() {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/getNews";
|
||
String userNameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", userNameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// 处理一条接收到的新消息
|
||
private void handleNewSingleMessage(String singleMessageString) {
|
||
try {
|
||
// System.out.println("handleNewSingleMessage start");
|
||
JSONObject singleMessageJsonObject = new JSONObject(singleMessageString);
|
||
String fromString;
|
||
JSONObject fromJsonObject;
|
||
String fromNameString;
|
||
Integer idInteger;
|
||
String msgString;
|
||
Long timeLong;
|
||
// 解析发送方姓名
|
||
fromString = singleMessageJsonObject.getString("from");
|
||
fromJsonObject = new JSONObject(fromString);
|
||
fromNameString = fromJsonObject.getString("username");
|
||
// 解析id
|
||
idInteger = singleMessageJsonObject.getInt("id");
|
||
// 解析内容
|
||
msgString = singleMessageJsonObject.getString("msg");
|
||
// 解析时间
|
||
timeLong = singleMessageJsonObject.getLong("time");
|
||
// 构建一条消息对象
|
||
SingleMessage singleMessage = new SingleMessage();
|
||
singleMessage.setFromName(fromNameString);
|
||
singleMessage.setMsg(msgString);
|
||
singleMessage.setTime(timeLong);
|
||
singleMessage.setToName(mainApp.loginer.getUserName());
|
||
singleMessage.setMsgId(idInteger);
|
||
// 加入登录未读消息列表中
|
||
mainApp.iniNotReadMessageListWhenLogin.addNotReadMessage(singleMessage);
|
||
;
|
||
System.out.println("Login-iniNotReadMessageListWhenLogin addSingleMessage success: " + singleMessageString);
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
// 填充登录者与传入的联系人的消息记录[填充当前时间节点之前的五条消息]
|
||
private void fullMessageLogByPerson(Person person) {
|
||
if (person != null) {
|
||
// 发送请求,更新与该用户的消息记录
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
orderInfo.setJsonString(getGetRTMsgLogJson(person));
|
||
mainApp.minoaDataAPI.executeOrder("/getRTMsgLog", orderInfo);
|
||
// 获取用户列表信息
|
||
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// System.out.println(responseData.getResponseJsonDataString());
|
||
JSONObject jsonObject = responseData.praseLoginData();
|
||
// System.out.println("getRTMsgLog-JSON: "+jsonObject.toString());
|
||
Integer codeInteger = null;
|
||
try {
|
||
codeInteger = jsonObject.getInt("code");
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
if (codeInteger == 200) {
|
||
// 赋值新的消息列表
|
||
String dataString;
|
||
try {
|
||
dataString = jsonObject.getString("data");
|
||
// System.out.println("getRTMsgLog-data: "+dataString);
|
||
if (dataString.equals("null") || dataString.equals("")) {
|
||
// 没有消息
|
||
} else {
|
||
JSONArray dataJsonArray = new JSONArray(dataString);
|
||
String singleMessageCacheString;
|
||
// 遍历每个用户联系人,将其消息记录加入mainApp的消息列表中【倒序是为了让时间顺序】
|
||
for (int i = dataJsonArray.length() - 1; i >= 0; i--) {
|
||
singleMessageCacheString = dataJsonArray.getString(i);
|
||
// System.out.println("singleMessage-"+i+": "+singleMessageCacheString);
|
||
praseAndAddSigleMessage(singleMessageCacheString, person);
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
} else {
|
||
// 用户未登录,提示重新登录
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(AlertType.WARNING);
|
||
alert.setTitle("登录信息过期");
|
||
alert.setHeaderText("登录信息过期");
|
||
alert.setContentText("你的登录信息过期,请重新登录。");
|
||
alert.showAndWait();
|
||
}
|
||
System.out.println("ini message log with " + person.getUserName() + " success");
|
||
}
|
||
}
|
||
|
||
// 解析单条消息记录并将其加入与特定联系人的消息记录里
|
||
private void praseAndAddSigleMessage(String singleMessageString, Person person) {
|
||
try {
|
||
JSONObject singleMessageJsonObject = new JSONObject(singleMessageString);
|
||
// formName
|
||
String fromString = singleMessageJsonObject.getString("from");
|
||
JSONObject fromJsonObject = new JSONObject(fromString);
|
||
String fromNameString = fromJsonObject.getString("username");
|
||
// toName
|
||
String toString = singleMessageJsonObject.getString("to");
|
||
JSONObject toJsonObject = new JSONObject(toString);
|
||
String toNameString = toJsonObject.getString("username");
|
||
// time
|
||
Long timeLong = singleMessageJsonObject.getLong("time");
|
||
// msg
|
||
String msgString = singleMessageJsonObject.getString("msg");
|
||
// id
|
||
Integer idInteger = singleMessageJsonObject.getInt("id");
|
||
// 构造类
|
||
SingleMessage singleMessage = new SingleMessage();
|
||
singleMessage.setFromName(fromNameString);
|
||
singleMessage.setToName(toNameString);
|
||
singleMessage.setTime(timeLong);
|
||
singleMessage.setMsg(msgString);
|
||
singleMessage.setMsgId(idInteger);
|
||
// 加入列表
|
||
mainApp.messageLogMap.get(person.getUserName()).add(singleMessage);
|
||
// System.out.println(person.getUserName()+"的消息记录数目是:"+mainApp.messageLogMap.get(person.getUserName()).size());
|
||
} catch (JSONException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
// 获取精确到秒的时间戳
|
||
private static Long getSecondTimestampTwo(Date date) {
|
||
if (null == date) {
|
||
return (long) 0;
|
||
}
|
||
String timestamp = String.valueOf(date.getTime() / 1000);
|
||
return Long.valueOf(timestamp);
|
||
}
|
||
|
||
// 创建一个代表2019年9月1号的时间戳【精确到秒】
|
||
public static Long get20190901() throws ParseException {
|
||
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||
Date testData = dataFormat.parse("2019-09-01");
|
||
return getSecondTimestampTwo(testData);
|
||
}
|
||
|
||
// 创建一个当前时间的时间戳【精确到秒】
|
||
public static Long getNowTimeStamp() {
|
||
Date date = new Date();
|
||
return getSecondTimestampTwo(date);
|
||
}
|
||
|
||
// "获取当前时间节点之前与该选中人的5条互发消息"[数据库给的数据是按照时间倒序的]
|
||
private String getGetRTMsgLogJson(Person selectedPerson) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/getRTMsgLog";
|
||
String userNameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
String peerString = selectedPerson.getUserName();
|
||
Long timestampLong = null;
|
||
timestampLong = getNowTimeStamp();
|
||
// 期望获取的信息条数
|
||
Integer limitInteger = 5;
|
||
// 0表示时间节点之后,1表示之前
|
||
Integer directInteger = 1;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", userNameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("peer", peerString);
|
||
jsonObject.put("timestamp", timestampLong);
|
||
jsonObject.put("limit", limitInteger);
|
||
jsonObject.put("direct", directInteger);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
// //"获取2019年9月1日之后与该选中人的100条互发消息"
|
||
// private String getGetRTMsgLogJson(Person selectedPerson) {
|
||
// JSONObject jsonObject =new JSONObject();
|
||
// String commandString="/getRTMsgLog";
|
||
// String userNameString=mainApp.loginer.getUserName();
|
||
// String uuidString=mainApp.loginer.getUuid();
|
||
// String peerString=selectedPerson.getUserName();
|
||
// Long timestampLong = null;
|
||
// try {
|
||
// timestampLong=get20190901();
|
||
// } catch (ParseException e1) {
|
||
// // TODO Auto-generated catch block
|
||
// e1.printStackTrace();
|
||
// }
|
||
// //期望获取的信息条数
|
||
// Integer limitInteger=100;
|
||
// //0表示时间节点之后,1表示之前
|
||
// Integer directInteger=0;
|
||
// try {
|
||
// jsonObject.put("command", commandString);
|
||
// jsonObject.put("username", userNameString);
|
||
// jsonObject.put("uuid", uuidString);
|
||
// jsonObject.put("peer", peerString);
|
||
// jsonObject.put("timestamp", timestampLong);
|
||
// jsonObject.put("limit", limitInteger);
|
||
// jsonObject.put("direct", directInteger);
|
||
// } catch (Exception e) {
|
||
// // TODO: handle exception
|
||
// System.out.println("exception: " + e.getMessage());
|
||
// }
|
||
// return jsonObject.toString();
|
||
// }
|
||
|
||
//获取上传文件的json格式
|
||
public String getUploadFile(String filename, Integer offset) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
String commandString = "/uploadFile";
|
||
String usernameString = mainApp.loginer.getUserName();
|
||
String uuidString = mainApp.loginer.getUuid();
|
||
String filenameString = filename;
|
||
Integer offsetInteger = offset;
|
||
try {
|
||
jsonObject.put("command", commandString);
|
||
jsonObject.put("username", usernameString);
|
||
jsonObject.put("uuid", uuidString);
|
||
jsonObject.put("filename", filenameString);
|
||
jsonObject.put("offset", offsetInteger);
|
||
} catch (Exception e) {
|
||
// TODO: handle exception
|
||
System.out.println("exception: " + e.getMessage());
|
||
}
|
||
// 调试输出
|
||
System.out.println("getUploadFile: "+jsonObject.toString());
|
||
return jsonObject.toString();
|
||
}
|
||
|
||
/*
|
||
* .文件上传操作
|
||
* @param serverpath 将该文件上传到的指定服务器路径
|
||
* @param file 要上传的本地文件
|
||
* @return 返回0表示传输成功;返回-1表示传输失败
|
||
*/
|
||
public Integer sendFileToServer(File file, String serverpath) {
|
||
//获取要上传文件的文件长度,计算上传次数
|
||
long filelen = file.length();
|
||
int cycle = (int) filelen / mainApp.sliceSize;
|
||
int remainder=(int)filelen%mainApp.sliceSize;
|
||
if((cycle>0)&&(remainder==0)) {
|
||
cycle--;
|
||
}
|
||
System.out.println("file-len: " + filelen);
|
||
System.out.println("cycle: " + cycle);
|
||
//发送上传命令给上传接口,接口自行取本地数据
|
||
int flagSuccess = 0; //记录收到200回复的次数
|
||
for (int i = 0; i <= cycle; i++) {
|
||
OrderInfo orderInfo = new OrderInfo();
|
||
ResponseData responseData = new ResponseData();
|
||
Long seqLong = mainApp.getNewDataReqId();
|
||
orderInfo.setSeq(seqLong);
|
||
//此时传入的文件路径是windows下的绝对路径
|
||
orderInfo.setJsonString(getUploadFile(file.getAbsolutePath(), i * mainApp.sliceSize));
|
||
|
||
mainApp.minoaDataAPI.executeOrder("/uploadFile", orderInfo, serverpath);
|
||
responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
||
// System.out.println("上传文件的数据回执(200表示成功):"+responseData.getResponseJsonDataString());
|
||
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) {
|
||
flagSuccess++;
|
||
}
|
||
}
|
||
//每个分片都上传成功
|
||
if (flagSuccess == (cycle + 1)) {
|
||
System.out.println("已成功将文件" + file.getAbsolutePath() + "上传至服务器" + serverpath + "目录下。");
|
||
return 0;
|
||
} else {
|
||
System.out.println("由于未知原因,本地文件" + file.getAbsolutePath() + "上传服务器失败。");
|
||
return -1;
|
||
}
|
||
}
|
||
}
|