123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- package main
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "math"
- "net/http"
- "strconv"
- "strings"
- "time"
- )
- const (
- OnDutyCheckTime = 120048879
- OnDutyCheckResult = 120048880
- OffDutyCheckTime = 120048881
- OffDutyCheckResult = 120048882
- AttendanceApprove = 120048891
- )
- type DingTalk struct {
- token string
- agentId string
- }
- func (dt *DingTalk) refreshToken() {
- resp, err := http.Get("https://oapi.dingtalk.com/gettoken?appkey=dinglvrfy5p9zq79ojaj&appsecret=7Tcy-xRj8m3jVM9cNZo62XlbFaxfONgRE0LKImUrka4DgEOt16RN9TJh9cZR63Vl")
- if err == nil {
- result := make(map[string]any)
- err := json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- dt.token = result["access_token"].(string)
- dt.agentId = "1485668405"
- }
- } else {
- panic(any(err))
- }
- }
- }
- func (dt *DingTalk) getEmployeeIdByUnionId(unionId string) string {
- var param io.Reader = nil
- args := make(map[string]any)
- args["unionid"] = unionId
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- userResult := result["result"].(map[string]any)
- userId := userResult["userid"].(string)
- return userId
- }
- panic(any("获取员工[" + unionId + "]信息失败"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getEmployeeIdByAuthCode(authCode string) string {
- var param io.Reader = nil
- args := make(map[string]any)
- args["clientId"] = "dinglvrfy5p9zq79ojaj"
- args["clientSecret"] = "7Tcy-xRj8m3jVM9cNZo62XlbFaxfONgRE0LKImUrka4DgEOt16RN9TJh9cZR63Vl"
- args["code"] = authCode
- args["refreshToken"] = authCode
- args["grantType"] = "authorization_code"
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- var token = ""
- resp, err := http.Post("https://api.dingtalk.com/v1.0/oauth2/userAccessToken", "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if result["accessToken"] != nil {
- token = result["accessToken"].(string)
- req, _ := http.NewRequest("GET", "https://api.dingtalk.com/v1.0/contact/users/me", nil)
- req.Header.Set("x-acs-dingtalk-access-token", token)
- resp, err := (&http.Client{}).Do(req)
- if err != nil {
- panic(any("无法获取员工信息。"))
- } else {
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if result["unionId"] != nil {
- unionId := result["unionId"].(string)
- id := dt.getEmployeeIdByUnionId(unionId)
- return id
- } else {
- panic(any("无法获取员工信息。"))
- }
- } else {
- panic(any("无法获取员工信息。"))
- }
- }
- } else {
- panic(any("无法获取员工Token。"))
- }
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getIsLeaderById(id string) bool {
- var param io.Reader = nil
- args := make(map[string]any)
- args["userid"] = id
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- userResult := result["result"].(map[string]any)
- leaderInDeps := userResult["leader_in_dept"].([]any)
- for i := 0; i < len(leaderInDeps); i++ {
- leaderInDep := leaderInDeps[i].(map[string]any)
- isLeader := leaderInDep["leader"].(bool)
- if isLeader {
- return true
- }
- }
- return false
- }
- panic(any("获取员工[" + id + "]信息失败"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getEmployeeNameById(id string) string {
- var param io.Reader = nil
- args := make(map[string]any)
- args["userid"] = id
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- userResult := result["result"].(map[string]any)
- userName := userResult["name"].(string)
- return userName
- }
- panic(any("获取员工[" + id + "]信息失败"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getEmployeeNameByAuthCode(authCode string) string {
- var param io.Reader = nil
- args := make(map[string]any)
- args["clientId"] = "dinglvrfy5p9zq79ojaj"
- args["clientSecret"] = "7Tcy-xRj8m3jVM9cNZo62XlbFaxfONgRE0LKImUrka4DgEOt16RN9TJh9cZR63Vl"
- args["code"] = authCode
- args["refreshToken"] = authCode
- args["grantType"] = "authorization_code"
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- var token = ""
- resp, err := http.Post("https://api.dingtalk.com/v1.0/oauth2/userAccessToken", "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if result["accessToken"] != nil {
- token = result["accessToken"].(string)
- req, _ := http.NewRequest("GET", "https://api.dingtalk.com/v1.0/contact/users/me", nil)
- req.Header.Set("x-acs-dingtalk-access-token", token)
- resp, err := (&http.Client{}).Do(req)
- if err != nil {
- panic(any("无法获取员工信息。"))
- } else {
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if result["unionId"] != nil {
- unionId := result["unionId"].(string)
- id := dt.getEmployeeIdByUnionId(unionId)
- name := dt.getEmployeeNameById(id)
- return name
- } else {
- panic(any("无法获取员工信息。"))
- }
- } else {
- panic(any("无法获取员工信息。"))
- }
- }
- } else {
- panic(any("无法获取员工Token。"))
- }
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getDepartments(parentId int) []Department {
- departments := make([]Department, 0)
- var param io.Reader = nil
- if parentId != 0 {
- args := make(map[string]any)
- args["dept_id"] = parentId
- json, err := json.Marshal(args)
- if err == nil {
- param = bytes.NewReader(json)
- } else {
- panic(any(err))
- }
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- resultData := readBody(resp)
- err := json.Unmarshal(resultData, &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- depList := result["result"].([]any)
- if depList != nil {
- for i := 0; i < len(depList); i++ {
- dep := depList[i].(map[string]any)
- depId := int(dep["dept_id"].(float64))
- depName := dep["name"].(string)
- parentDepId := int(dep["parent_id"].(float64))
- departments = append(departments, Department{depId, depName, parentDepId})
- }
- }
- } else {
- fmt.Println(string(resultData))
- }
- } else {
- fmt.Println(err.Error())
- }
- }
- return departments
- }
- func (dt *DingTalk) getAttendanceRules(userId string, userName string) AttendanceRules {
- var param io.Reader = nil
- args := make(map[string]any)
- args["userid"] = userId
- data, err := json.Marshal(args)
- if err == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(err))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/attendance/getusergroup?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err := json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- groupResult := result["result"].(map[string]any)
- if groupResult != nil {
- if groupResult["name"] == nil {
- return AttendanceRules{"", time.Unix(0, 0), time.Unix(0, 0)}
- }
- groupName := groupResult["name"].(string)
- classes := groupResult["classes"].([]any)
- if len(classes) > 0 {
- sections := classes[0].(map[string]any)["sections"].([]any)
- if len(sections) > 0 {
- times := sections[0].(map[string]any)["times"].([]any)
- start := dateTimeToTime(strToDateTime(times[0].(map[string]any)["check_time"].(string)))
- end := dateTimeToTime(strToDateTime(times[1].(map[string]any)["check_time"].(string)))
- return AttendanceRules{groupName, start, end}
- }
- } else {
- return AttendanceRules{groupName, time.Unix(0, 0), time.Unix(0, 0)}
- }
- }
- }
- }
- panic(any("无法获取员工[" + userName + "]的考勤班次信息。"))
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getEmployeeCount() int {
- var param io.Reader = nil
- args := make(map[string]any)
- args["only_active"] = true
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/user/count?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- countResult := result["result"].(map[string]any)
- count := int(math.Floor(countResult["count"].(float64)))
- return count
- }
- panic(any("获取员工数量失败。"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getEmployee(userId string, depId int) Employee {
- var param io.Reader = nil
- args := make(map[string]any)
- args["userid"] = userId
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- userResult := result["result"].(map[string]any)
- userName := userResult["name"].(string)
- title := ""
- if userResult["title"] != nil {
- title = userResult["title"].(string)
- }
- email := ""
- if userResult["email"] != nil {
- email = userResult["email"].(string)
- }
- attendanceRules := dt.getAttendanceRules(userId, userName)
- isStruggle := false
- return Employee{userId, userName, title, depId, email, attendanceRules, false, isStruggle}
- }
- panic(any("获取员工[" + userId + "]信息失败"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- // 获取部门员工列表
- func (dt *DingTalk) getEmployees(depId int) []Employee {
- employees := make([]Employee, 0)
- var param io.Reader = nil
- if depId != 0 {
- args := make(map[string]any)
- args["dept_id"] = depId
- json, err := json.Marshal(args)
- if err == nil {
- param = bytes.NewReader(json)
- } else {
- panic(any(err))
- }
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/user/listid?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err := json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- resultData := result["result"].(map[string]any)
- if resultData != nil {
- userIdList := resultData["userid_list"].([]any)
- for i := 0; i < len(userIdList); i++ {
- employees = append(employees, dt.getEmployee(userIdList[i].(string), depId))
- }
- return employees
- }
- }
- panic(any("无法获取部门[" + strconv.Itoa(depId) + "]员工列表"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) sendTextMessage(userIds []string, content string) {
- var param io.Reader = nil
- args := make(map[string]any)
- args["agent_id"] = dt.agentId
- args["userid_list"] = strings.Join(userIds, ",")
- msg := make(map[string]any)
- msg["msgtype"] = "text"
- text := make(map[string]any)
- text["content"] = content
- msg["text"] = text
- args["msg"] = msg
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) != 0 {
- panic(any("发送消息失败"))
- }
- } else {
- panic(any(err))
- }
- }
- }
- func (dt *DingTalk) sendUrlMessage(userIds []string, title string, text string, imageUrl string, contentUrl string) {
- var param io.Reader = nil
- args := make(map[string]any)
- args["agent_id"] = dt.agentId
- args["userid_list"] = strings.Join(userIds, ",")
- msg := make(map[string]any)
- msg["msgtype"] = "link"
- link := make(map[string]any)
- link["messageUrl"] = contentUrl
- link["picUrl"] = imageUrl
- link["title"] = title
- link["text"] = text
- msg["link"] = link
- args["msg"] = msg
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) != 0 {
- panic(any("发送消息失败"))
- }
- } else {
- panic(any(err))
- }
- }
- }
- func (dt *DingTalk) sendActionCard(userIds []string, title string, markdown string, singleTitle string, singleUrl string) {
- var param io.Reader = nil
- args := make(map[string]any)
- args["agent_id"] = dt.agentId
- args["userid_list"] = strings.Join(userIds, ",")
- msg := make(map[string]any)
- msg["msgtype"] = "action_card"
- actionCard := make(map[string]any)
- actionCard["title"] = title
- actionCard["markdown"] = markdown
- actionCard["single_title"] = singleTitle
- actionCard["single_url"] = singleUrl
- msg["action_card"] = actionCard
- args["msg"] = msg
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) != 0 {
- panic(any("发送消息失败"))
- }
- } else {
- panic(any(err))
- }
- }
- }
- func getOrCreateAttendanceData(list []*AttendanceData, index int, employeeId string, day time.Time) *AttendanceData {
- if list[index] == nil {
- list[index] = new(AttendanceData)
- list[index].EmployeeId = employeeId
- list[index].Day = day
- }
- return list[index]
- }
- func (dt *DingTalk) getEmployeeAttendanceDataList(employeeId string, start time.Time, end time.Time) []AttendanceData {
- var list []*AttendanceData
- var param io.Reader = nil
- args := make(map[string]any)
- args["column_id_list"] = strconv.Itoa(OnDutyCheckTime) + "," + strconv.Itoa(OnDutyCheckResult) + "," + strconv.Itoa(OffDutyCheckTime) + "," + strconv.Itoa(OffDutyCheckResult) + "," + strconv.Itoa(AttendanceApprove)
- args["from_date"] = start.Format("2006-01-02 15:04:05")
- args["to_date"] = end.Format("2006-01-02 15:04:05")
- args["userid"] = employeeId
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/attendance/getcolumnval?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err = json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- resultData := result["result"].(map[string]any)
- if resultData != nil {
- columns := resultData["column_vals"].([]any)
- for i := 0; i < len(columns); i++ {
- column := columns[i].(map[string]any)
- columnId := int(column["column_vo"].(map[string]any)["id"].(float64))
- columnsValues := column["column_vals"].([]any)
- if list == nil {
- list = make([]*AttendanceData, len(columnsValues))
- }
- var attendanceData *AttendanceData
- for i := 0; i < len(columnsValues); i++ {
- dayValues := columnsValues[i].(map[string]any)
- switch columnId {
- case OnDutyCheckTime:
- day := dateTimeToDate(strToDateTime(dayValues["date"].(string)))
- attendanceData = getOrCreateAttendanceData(list, i, employeeId, day)
- attendanceData.Start = dayValues["value"].(string)
- case OnDutyCheckResult:
- day := dateTimeToDate(strToDateTime(dayValues["date"].(string)))
- attendanceData = getOrCreateAttendanceData(list, i, employeeId, day)
- attendanceData.StartResult = dayValues["value"].(string)
- case OffDutyCheckTime:
- day := dateTimeToDate(strToDateTime(dayValues["date"].(string)))
- attendanceData = getOrCreateAttendanceData(list, i, employeeId, day)
- attendanceData.End = dayValues["value"].(string)
- case OffDutyCheckResult:
- day := dateTimeToDate(strToDateTime(dayValues["date"].(string)))
- attendanceData = getOrCreateAttendanceData(list, i, employeeId, day)
- attendanceData.EndResult = dayValues["value"].(string)
- case AttendanceApprove:
- day := dateTimeToDate(strToDateTime(dayValues["date"].(string)))
- attendanceData = getOrCreateAttendanceData(list, i, employeeId, day)
- attendanceData.Approve = dayValues["value"].(string)
- }
- }
- }
- for i := 0; i < len(list); i++ {
- attendanceData := list[i]
- attendanceData.State = 出勤
- if attendanceData.Start == "" || attendanceData.End == "" {
- if attendanceData.StartResult == "出差" || attendanceData.EndResult == "出差" {
- attendanceData.State = 出差
- }
- if attendanceData.StartResult == "外出" || attendanceData.EndResult == "外出" {
- attendanceData.State = 外出
- }
- if attendanceData.StartResult == "缺卡" || attendanceData.EndResult == "缺卡" {
- attendanceData.State = 缺卡
- }
- //不管外出还是缺卡,出差还是外出,只要有请假就是请假
- if attendanceData.StartResult == "请假" || attendanceData.EndResult == "请假" {
- attendanceData.State = 请假
- }
- if attendanceData.StartResult == "加班" || attendanceData.EndResult == "加班" {
- attendanceData.State = 加班
- }
- if attendanceData.StartResult == "" && attendanceData.EndResult == "" && attendanceData.Approve == "" {
- attendanceData.State = 休息
- }
- }
- //Check approve
- if strings.Contains(attendanceData.Approve, "出差") {
- attendanceData.State = 出差
- }
- if strings.Contains(attendanceData.Approve, "外出") {
- attendanceData.State = 外出
- }
- if strings.Contains(attendanceData.Approve, "缺卡") {
- attendanceData.State = 缺卡
- }
- if strings.Contains(attendanceData.Approve, "请假") {
- attendanceData.State = 请假
- }
- if strings.Contains(attendanceData.Approve, "加班") {
- attendanceData.State = 加班
- }
- if strings.Contains(attendanceData.Start, "昨日") {
- startTime := dateTimeToTime(strToTime("00:01"))
- attendanceData.StartTime = startTime
- } else {
- startTime := dateTimeToTime(strToTime(attendanceData.Start))
- attendanceData.StartTime = startTime
- }
- if strings.Contains(attendanceData.End, "次日") {
- endTime := dateTimeToTime(strToTime("23:59"))
- attendanceData.EndTime = endTime
- } else {
- endTime := dateTimeToTime(strToTime(attendanceData.End))
- attendanceData.EndTime = endTime
- }
- }
- result := make([]AttendanceData, 0)
- for i := 0; i < len(list); i++ {
- result = append(result, *list[i])
- }
- return result
- }
- }
- panic(any("无法获取员工[" + employeeId + "]的考勤信息。"))
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
- func (dt *DingTalk) getLeaveQuota(lq map[string]any) LeaveQuota {
- leaveQuota := LeaveQuota{}
- leaveQuota.Id = lq["userid"].(string)
- leaveCode := lq["leave_code"].(string)
- if leaveCode == "6a6c631c-65cf-434c-97fa-335c9df7cb81" {
- leaveQuota.Quota = int(lq["quota_num_per_day"].(float64))
- leaveQuota.Used = int(lq["used_num_per_day"].(float64))
- leaveQuota.LeaveType = "年假"
- leaveQuota.Unit = 天
- } else {
- leaveQuota.Quota = int(lq["quota_num_per_hour"].(float64))
- leaveQuota.Used = int(lq["used_num_per_hour"].(float64))
- leaveQuota.LeaveType = "调休"
- leaveQuota.Unit = 小时
- }
- return leaveQuota
- }
- func (dt *DingTalk) getLeaveQuotas(employeeId string, leaveType int) []LeaveQuota {
- leaveQuotas := make([]LeaveQuota, 0)
- args := make(map[string]any)
- args["offset"] = 0
- args["size"] = 100
- args["userids"] = employeeId
- if leaveType == 年假 {
- args["leave_code"] = "6a6c631c-65cf-434c-97fa-335c9df7cb81"
- } else {
- args["leave_code"] = "12d81490-04e3-4848-920e-7f87e5955451"
- }
- args["op_userid"] = "14033025461181119"
- var param io.Reader = nil
- data, dataErr := json.Marshal(args)
- if dataErr == nil {
- param = bytes.NewReader(data)
- } else {
- panic(any(dataErr))
- }
- resp, err := http.Post("https://oapi.dingtalk.com/topapi/attendance/vacation/quota/list?access_token="+dt.token, "application/json;charset=utf-8", param)
- if err == nil {
- result := make(map[string]any)
- err := json.Unmarshal(readBody(resp), &result)
- if err == nil {
- if int((result["errcode"]).(float64)) == 0 {
- resultData := result["result"].(map[string]any)
- if resultData != nil {
- if resultData["leave_quotas"] != nil {
- lqs := resultData["leave_quotas"].([]any)
- for i := 0; i < len(lqs); i++ {
- lq := lqs[i].(map[string]any)
- leaveQuotas = append(leaveQuotas, dt.getLeaveQuota(lq))
- }
- }
- return leaveQuotas
- } else {
- panic(any("无法获得假期信息"))
- }
- } else {
- panic(any("无法获得假期信息"))
- }
- } else {
- panic(any(err))
- }
- } else {
- panic(any(err))
- }
- }
|