1. 程式人生 > >通達OA辦公軟體PC網頁端簽到

通達OA辦公軟體PC網頁端簽到


import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Map;

//實現開啟瀏覽器並跳到指定網址的類 
public class SignIn {
	
	public static void openURL(String browser,String userName,String passWord) {
		
		String head = "http://124.130.134.82:8686/logincheck.php?UNAME=";
		String tail = "&PASSWORD=";
		String logUrl = head+userName+tail+passWord;
		
		try {
			//	日期格式化
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//	獲取當前時間
			Date nowTime = new Date();
			String time = df.format(nowTime);
			//	判斷今天是周幾
			int weekDay = dayForWeek(time);
			if (weekDay != 7) {
				//	處理瀏覽器路徑
				Map map = System.getenv();
				for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {
					String value = (String) map.get((String) itr.next());
					if (value.contains("firefox.exe")) {
						browser = value;
						break;
					} else if (value.contains("chrome.exe")) {
						browser = value;
					}
				}
				int a = 5 ;
				int b = 6 ;
				int c = 5;
				int d = 6;
				//	登入
				for (int i = 0; i < 5; i++) {//進行五次嘗試
					
					if (a != b) {
						Process process = Runtime.getRuntime().exec(new String[] { browser, logUrl });
						a = process.waitFor();
						b = process.exitValue();
					}
					//	簽到
					String signInUrlOne = "http://伺服器IP:埠/general/attendance/personal/duty/submit.php?REGISTER_TYPE=1&USER_DUTP=1";
					String signInUrlTwo = "http://伺服器IP:埠/general/attendance/personal/duty/submit.php?REGISTER_TYPE=2&USER_DUTP=1";
					String signInUrlThree = "http://伺服器IP:埠/general/attendance/personal/duty/submit.php?REGISTER_TYPE=3&USER_DUTP=1";
					String signInUrlFour = "http://伺服器IP:埠/general/attendance/personal/duty/submit.php?REGISTER_TYPE=4&USER_DUTP=1";
					
					//	判斷時間為(早上籤到,中午簽退,中午簽到,下午簽退)
					
					if (c != d) {
						SimpleDateFormat clock = new SimpleDateFormat("HH:mm");//設定時間格式
						String now = clock.format(nowTime);
						//	早上籤到
						String beginTimeOne = "07:00";
						String endTimeOne = "08:00";
						if (belongCalendar(now, beginTimeOne, endTimeOne)) {
							Process one = Runtime.getRuntime().exec(new String[] { browser, signInUrlOne });
							System.out.println("早上籤到");
							c = one.waitFor();
							d = one.exitValue();
						}
						//	中午簽退
						String beginTimeTwo = "12:10";
						String endTimeTwo = "12:25";
						if (belongCalendar(now, beginTimeTwo, endTimeTwo)) {
							Process two = Runtime.getRuntime().exec(new String[] { browser, signInUrlTwo });
							System.out.println("中午簽退");
							c = two.waitFor();
							d = two.exitValue();
						}
						//	中午簽到
						String beginTimeThree = "12:30";
						String endTimeThree = "13:00";
						if (belongCalendar(now, beginTimeThree, endTimeThree)) {
							Process three = Runtime.getRuntime().exec(new String[] { browser, signInUrlThree });
							System.out.println("中午簽到");
							c = three.waitFor();
							d = three.exitValue();
						}
						//	下午簽退
						String beginTimeFour = "17:50";
						String endTimeFour = "24:00";
						if (belongCalendar(now, beginTimeFour, endTimeFour)) {
							Process four = Runtime.getRuntime().exec(new String[] { browser, signInUrlFour });
							System.out.println("下午簽退");
							c = four.waitFor();
							d = four.exitValue();
						}
					}
				}
				
				System.out.println("時間:"+time+",簽到");
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 判斷輸入的日期是星期幾
	 * @param pTime
	 * @return
	 * @throws Throwable
	 */
	public static int dayForWeek(String time) throws Throwable {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date tmpDate = format.parse(time);
		Calendar cal = new GregorianCalendar();
		cal.set(tmpDate.getYear(), tmpDate.getMonth(), tmpDate.getDay());
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 判斷時間是否在時間段內
	 * @param nowTime
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean belongCalendar(String nowTime, String beginTime, String endTime) {
		
		SimpleDateFormat clock = new SimpleDateFormat("HH:mm");//設定時間格式
		Calendar date = Calendar.getInstance();
		Calendar begin = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		
		try {
			
			Date now = clock.parse(nowTime);
			Date begins = clock.parse(beginTime);
			Date ends = clock.parse(endTime);
			
			date.setTime(now);
			begin.setTime(begins);
			end.setTime(ends);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

}

以上就是具體的操作流程,前提是你得知道你們公司的伺服器IP以及埠


public class Demo {

	public static void main(String[] args) {
		//	瀏覽器地址(注意自己的瀏覽器安裝位置)
		String browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
		//	"D:\\DevelopmentTools\\FireFoxDeveloperEdition\\firefox.exe";//	火狐瀏覽器
		//	"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";//	谷歌瀏覽器
		// 登入網址	後面要拼接
		//String logUrl = "http://伺服器IP:埠/logincheck.php?UNAME=使用者名稱&PASSWORD=密碼";
		
                //    這裡可以填寫多個賬戶進行登陸
		String [] userNameList = {"使用者名稱"};
		String [] passWordList = {"密碼"};
		
		for (int i = 0 ;i<userNameList.length ;i++ ) {
			SignIn.openURL(browser,userNameList[i],passWordList[i]);
		}
		
	}
	
}