enterprise/common/weixin/qyweixin_checkin.go

129 lines
3.4 KiB
Go
Raw Normal View History

2023-08-13 21:24:54 +08:00
package weixin
import (
"encoding/json"
butil "enterprise/base/util"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gorm.io/gorm/utils"
"math"
"strings"
"time"
)
type UserCheckIn struct {
UserId string
Exception string
Rawdata string
StartTime int64
EndTime int64
}
type QyWeixinCheckin struct {
QyWeixin
}
func NewQyWeixinCheckin(corpId, secret, agent string) *QyWeixinCheckin {
return &QyWeixinCheckin{
QyWeixin: QyWeixin{
CorpId: corpId,
Secret: secret,
Agent: agent,
},
}
}
func (q *QyWeixinCheckin) GetCheckinEmployee(groupIds []string) ([]string, error) {
reqUrl := fmt.Sprintf("%s?access_token=%s", urlGetCheckinRlue, q.GetToken())
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte("{}"))
if err != nil {
return nil, err
}
result := make(map[string]interface{})
if err := json.Unmarshal(rspBody, &result); err != nil {
log.Errorf("http url[%s] result[%s] error :%s", reqUrl, string(rspBody), err.Error())
return nil, err
}
if cast.ToInt(result["errcode"]) != 0 {
log.Errorf("http url[%s] result[%s] error ", reqUrl, string(rspBody))
return nil, errors.New(string(rspBody))
}
resultUser := make([]string, 0)
groups := cast.ToSlice(result["group"])
for _, group := range groups {
g := cast.ToStringMap(group)
if !utils.Contains(groupIds, cast.ToString(g["groupid"])) {
continue
}
ranges := cast.ToStringMap(g["range"])
userid := cast.ToStringSlice(ranges["userid"])
resultUser = append(resultUser, userid...)
}
return resultUser, nil
}
func (q *QyWeixinCheckin) GetCheckinData(day, userId string) (*UserCheckIn, error) {
dayTime, _ := time.ParseInLocation("2006-01-02", day, time.Local)
reqData := make(map[string]interface{})
reqData["opencheckindatatype"] = 1
reqData["starttime"] = dayTime.Unix()
reqData["endtime"] = dayTime.Unix() + 86400
reqData["useridlist"] = []string{userId}
reqUrl := fmt.Sprintf("%s?access_token=%s", urlGetCheckinData, q.GetToken())
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(goutil.EncodeJSON(reqData)))
if err != nil {
return nil, err
}
result := make(map[string]interface{})
if err := json.Unmarshal(rspBody, &result); err != nil {
log.Errorf("http url[%s] result[%s] error :%s", reqUrl, string(rspBody), err.Error())
return nil, err
}
if cast.ToInt(result["errcode"]) != 0 {
log.Errorf("http url[%s] result[%s] error ", reqUrl, string(rspBody))
return nil, errors.New(string(rspBody))
}
checkindatas := cast.ToSlice(result["checkindata"])
userData := new(UserCheckIn)
userData.UserId = userId
exception := make([]string, 0)
isException := false
startTime := int64(math.MaxInt64)
endTime := int64(0)
for _, checkdata := range checkindatas {
c := cast.ToStringMap(checkdata)
exceptionType := cast.ToString(c["exception_type"])
checkinTime := cast.ToInt64(c["checkin_time"])
if exceptionType != "" {
isException = true
}
exception = append(exception, exceptionType)
if checkinTime < startTime {
startTime = checkinTime
}
if checkinTime > endTime {
endTime = checkinTime
}
}
if startTime != int64(math.MaxInt64) {
userData.StartTime = startTime
}
userData.EndTime = endTime
userData.Rawdata = goutil.EncodeJSON(checkindatas)
if isException {
userData.Exception = strings.Join(exception, ",")
}
if userData.EndTime == 0 && userData.StartTime == 0 {
return nil, nil
}
return userData, nil
}