Last active
March 27, 2019 05:56
-
-
Save showsky/751df37df72506c3f814f0de05f2e504 to your computer and use it in GitHub Desktop.
Apollo Leave System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function curl_get($url, $cookie = NULL) { | |
| $header_array = array( | |
| 'Referer: https://hrm.mayohr.com/ta/supervisor/shiftscheduleapprovelist', | |
| 'Content-Type: application/json', | |
| 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' | |
| ); | |
| if (isset($cookie)) { | |
| $header_array[] = 'Cookie: ' . $cookie; | |
| } | |
| $options = array( | |
| CURLOPT_URL => $url, | |
| CURLOPT_RETURNTRANSFER => TRUE, | |
| CURLOPT_COOKIEJAR => 'cookie', | |
| CURLOPT_COOKIEFILE => 'cookie', | |
| CURLOPT_VERBOSE => FALSE, | |
| CURLOPT_SSL_VERIFYPEER => FALSE, | |
| ); | |
| $options[CURLOPT_HTTPHEADER] = $header_array; | |
| $ch = curl_init(); | |
| curl_setopt_array($ch, $options); | |
| if ( ! $result = curl_exec($ch)) { | |
| trigger_error(curl_error($ch)); | |
| } | |
| switch (curl_getinfo($ch, CURLINFO_HTTP_CODE)) { | |
| case 200: | |
| curl_close($ch); | |
| return $result; | |
| default: | |
| return FALSE; | |
| } | |
| } | |
| function curl_post($url, array $post = array(), $cookie = NULL) { | |
| $header_array = array( | |
| 'https://auth.mayohr.com/HRM/Account/Login?original_target=https%3A%2F%2Fhrm.mayohr.com%2Ftube&lang=zh-tw', | |
| 'application/x-www-form-urlencoded; charset=UTF-8', | |
| 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' | |
| ); | |
| if (isset($cookie)) { | |
| $header_array[] = 'Cookie: ' . $cookie; | |
| } | |
| $options = array( | |
| CURLOPT_POST => TRUE, | |
| CURLOPT_URL => $url, | |
| CURLOPT_RETURNTRANSFER => TRUE, | |
| CURLOPT_COOKIEJAR => 'cookie', | |
| CURLOPT_COOKIEFILE => 'cookie', | |
| CURLOPT_VERBOSE => FALSE, | |
| CURLOPT_SSL_VERIFYPEER => FALSE, | |
| ); | |
| $options[CURLOPT_HTTPHEADER] = $header_array; | |
| if (count($post) > 0) { | |
| $options[CURLOPT_POSTFIELDS] = http_build_query($post); | |
| } | |
| $ch = curl_init(); | |
| curl_setopt_array($ch, $options); | |
| if ( ! $result = curl_exec($ch)) { | |
| trigger_error(curl_error($ch)); | |
| } | |
| switch (curl_getinfo($ch, CURLINFO_HTTP_CODE)) { | |
| case 200: | |
| curl_close($ch); | |
| return $result; | |
| default: | |
| return FALSE; | |
| } | |
| } | |
| function time_zone_convert($time) { | |
| $date_time = new DateTime($time); | |
| $time_zone = new DateTimeZone('Asia/Taipei'); | |
| $date_time->setTimezone($time_zone); | |
| return $date_time->format('Y-m-d H:i:s'); | |
| } | |
| function read_cookie() { | |
| $cookie = NULL; | |
| if (file_exists('cookie.txt')) { | |
| $cookie = file_get_contents('cookie.txt'); | |
| } | |
| if ( ! empty($cookie)) { | |
| return $cookie; | |
| } | |
| $cookie = readline('Input Cookie: '); | |
| file_put_contents('cookie.txt', $cookie); | |
| return $cookie; | |
| } | |
| function read_month() { | |
| $month = readline('Input month: '); | |
| $month = intval($month); | |
| if ($month == 0) { | |
| return date('n'); | |
| } | |
| return $month; | |
| } | |
| function login($username, $password) { | |
| $url = 'https://auth.mayohr.com/Token'; | |
| $post_array = array( | |
| 'userName' => $username, | |
| 'password' => $password, | |
| 'grant_type' => 'password' | |
| ); | |
| $result = curl_post($url, $post_array); | |
| return $result; | |
| } | |
| function verify_json($json) { | |
| if ($json === FALSE || isset($json['Error'])) { | |
| return FALSE; | |
| } | |
| return TRUE; | |
| } | |
| function parse_dep_json($json) { | |
| $result = array(); | |
| $json = json_decode($json, TRUE); | |
| foreach ($json['Data'] as $key => $vlaue) { | |
| $result[$key] = $vlaue['DepartmentId']; | |
| } | |
| return $result; | |
| } | |
| function parse_json($json) { | |
| $result = array(); | |
| $json = json_decode($json, TRUE); | |
| if ( ! isset($json['Data']['Employees'])) { | |
| return $result; | |
| } | |
| $data = $json['Data']['Employees']; | |
| foreach ($data as $key1 => $value1) { | |
| $name = $value1['ChineseName']; | |
| $leave = array(); | |
| foreach ($value1['Calendars'] as $key2 => $value2) { | |
| if (isset($value2['LeaveSheets'])) { | |
| foreach ($value2['LeaveSheets'] as $key3 => $value3) { | |
| $leave[] = array( | |
| 'LeaveStartDatetime' => time_zone_convert($value3['LeaveStartDatetime']), | |
| 'LeaveEndDatetime' => time_zone_convert($value3['LeaveEndDatetime']), | |
| 'TotalHours' => $value3['TotalHours'] | |
| ); | |
| } | |
| } | |
| } | |
| if (count($leave) > 0) { | |
| $result[] = array( | |
| 'name' => $name, | |
| 'leave' => $leave | |
| ); | |
| } | |
| } | |
| return $result; | |
| } | |
| function group_by_date($data) { | |
| $result = array(); | |
| foreach ($data as $value1) { | |
| $name = $value1['name']; | |
| foreach ($value1['leave'] as $value2) { | |
| $date = new DateTime($value2['LeaveStartDatetime']); | |
| $result[$date->format('Y-m-d')][$name] = array( | |
| 'LeaveStartDatetime' => $value2['LeaveStartDatetime'], | |
| 'LeaveEndDatetime' => $value2['LeaveEndDatetime'], | |
| 'TotalHours' => $value2['TotalHours'] | |
| ); | |
| } | |
| } | |
| return $result; | |
| } | |
| ########### Start ############# | |
| $cookie = read_cookie(); | |
| $month = read_month(); | |
| // 部門 | |
| $url = 'https://pt.mayohr.com/api/departments'; | |
| $json = curl_get($url, $cookie); | |
| if ( ! verify_json($json)) { | |
| exit('Login error'); | |
| } | |
| $dep_result = parse_dep_json($json); | |
| // 請假人員 | |
| $url = 'https://pt.mayohr.com/api/EmployeeCalendars/deptScheduling?'; | |
| $parameter_array = array( | |
| 'year' => date('Y'), | |
| 'month' => $month, | |
| 'departmentId' => NULL | |
| ); | |
| $result_data = array(); | |
| foreach ($dep_result as $value) { | |
| $parameter_array['departmentId'] = $value; | |
| $json = curl_get($url . http_build_query($parameter_array), $cookie); | |
| if ( ! verify_json($json)) { | |
| exit('Login error'); | |
| } | |
| $result = parse_json($json); | |
| if (count($result) > 0) { | |
| $result_data = array_merge($result_data, $result); | |
| } | |
| usleep(300); | |
| } | |
| // 轉換 | |
| $group_by_date = group_by_date($result_data); | |
| var_dump($group_by_date); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy your cookie to cli input