-
Notifications
You must be signed in to change notification settings - Fork 0
/
yunzhijia.go
158 lines (138 loc) · 3.63 KB
/
yunzhijia.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type YzjResponse[T any] struct {
Data T `json:"data"`
ErrorCode int `json:"errorCode"`
Success bool `json:"success"`
}
type TicketContainer struct {
Appids string `json:"appids"`
}
type ClockInFlowFlow struct {
HasRule bool `json:"hasRule"`
Rest bool `json:"rest"`
SignDataList []struct {
TimePoint struct {
WorkTime int64 `json:"workTime"`
ClockInTime int64 `json:"clockInTime"`
TimePointType string `json:"timePointType"`
} `json:"timePoint"`
} `json:"signDataList"`
WorkHoursStr string `json:"workHoursStr"`
}
type YunZhiJia struct {
token string
oid string
appid string
}
type ClockInTimeType string
const (
ClockInTimeTypeStart = ClockInTimeType("START_WORK")
ClockInTimeTypeEnd = ClockInTimeType("END_WORK")
)
func NewYunZhiJia(token, oid, appid string) *YunZhiJia {
return &YunZhiJia{token: token, oid: oid, appid: appid}
}
func (y *YunZhiJia) ClockInFlowForDate(date string) (*ClockInFlowFlow, error) {
ticket, err := fetchTicket(y.token)
if err != nil {
log.Printf("fetch ticket error: %v", err)
return nil, err
}
flow, err := fetchClockInFlow(y.oid, y.appid, ticket, date)
if err != nil {
log.Printf("fetch clock in flow error: %v", err)
return nil, err
}
return flow, nil
}
func (y *YunZhiJia) ClockInFlow() (*ClockInFlowFlow, error) {
today := time.Now()
date := fmt.Sprintf("%d-%02d-%02d", today.Year(), today.Month(), today.Day())
return y.ClockInFlowForDate(date)
}
func (y *YunZhiJia) IsClockInToday(t ClockInTimeType) (bool, error) {
flow, err := y.ClockInFlow()
if err != nil {
return false, err
}
//if !flow.HasRule {
// return true, nil
//}
//if flow.Rest {
// return true, nil
//}
//log.Printf("type: %s, flow: %+v", t, flow)
for _, v := range flow.SignDataList {
if v.TimePoint.TimePointType == string(t) {
if v.TimePoint.ClockInTime == 0 {
return false, nil
}
//log.Printf("%s: %+v", t, v.TimePoint)
if t == ClockInTimeTypeStart {
return v.TimePoint.ClockInTime <= v.TimePoint.WorkTime, nil
} else {
return v.TimePoint.ClockInTime >= v.TimePoint.WorkTime, nil
}
}
}
return true, nil
}
func fetchTicket(token string) (string, error) {
req, err := http.NewRequest("POST", "https://do.yunzhijia.com/cloudwork/batchticket/tickets", nil)
if err != nil {
return "", err
}
req.Header.Add("openToken", token)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
data := YzjResponse[TicketContainer]{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", err
}
if data.Data.Appids == "" {
return "", fmt.Errorf("fetch ticket error: %v", data)
}
return data.Data.Appids, nil
}
func fetchClockInFlow(oid, appid, ticket, date string) (*ClockInFlowFlow, error) {
body := map[string]string{
"date": date,
"oid": oid,
}
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
url := fmt.Sprintf("https://www.yunzhijia.com/gateway/smartatt-core/mobile/statistics/getClockInFlow?appId=%s&ticket=%s", appid, ticket)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data := YzjResponse[ClockInFlowFlow]{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return &data.Data, nil
}