gosdk/qyweixin/app_approve.go

200 lines
5.5 KiB
Go
Raw Normal View History

2024-01-22 22:34:59 +08:00
package qyweixin
import (
"encoding/json"
"fmt"
2024-07-12 16:26:05 +08:00
"git.u8t.cn/open/gosdk/util"
2024-01-22 22:34:59 +08:00
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
)
type Applyer struct {
Userid string `json:"userid"`
Partyid string `json:"partyid"`
}
type Option struct {
Key string `json:"key"`
Value []struct {
Text string `json:"text"`
Lang string `json:"lang"`
} `json:"value"`
}
type Selector struct {
Type string `json:"type"`
Options []*Option `json:"options"`
}
type Vacation struct {
Selector *Selector `json:"selector"`
Attendance struct {
DateRange struct {
NewBegin int64 `json:"new_begin"`
NewEnd int64 `json:"new_end"`
NewDuration int64 `json:"new_duration"`
Type string `json:"type"`
} `json:"date_range"`
} `json:"attendance"`
}
type ApplyValue struct {
Text string `json:"text"`
Selector *Selector `json:"selector"`
Children []interface{} `json:"children"`
Date struct {
Type string `json:"type"`
Timestamp string `json:"s_timestamp"`
} `json:"date"`
NewMoney string `json:"new_Money"`
Files []struct {
FileId string `json:"file_id"`
} `json:"files"`
Vacation *Vacation `json:"vacation"`
PunchCorrection interface{} `json:"punch_correction"`
2024-03-09 20:04:21 +08:00
BankAccount interface{} `json:"bank_account"`
2024-01-22 22:34:59 +08:00
}
type ApplyContent struct {
Control string `json:"control"`
Title []struct {
Text string `json:"text"`
Lang string `json:"lang"`
} `json:"title"`
Value *ApplyValue `json:"value"`
}
type ApproveDetail struct {
SpNo string `json:"sp_no"`
SpName string `json:"sp_name"`
SpStatus int `json:"sp_status"`
TemplateID string `json:"template_id"`
ApplyTime int64 `json:"apply_time"`
Applyer *Applyer `json:"applyer"`
ApplyData struct {
Contents []*ApplyContent `json:"contents"`
} `json:"apply_data"`
}
type ApproveDetailRsp struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Info *ApproveDetail `json:"info"`
}
type AppApprove struct {
App
}
func (d *ApproveDetail) GetValue(title string) string {
for _, content := range d.ApplyData.Contents {
2024-07-04 18:26:21 +08:00
isEqual := false
for _, ti := range content.Title {
if ti.Text == title {
isEqual = true
}
}
if !isEqual {
2024-01-22 22:34:59 +08:00
continue
}
var value string
if content.Control == "Selector" {
value = content.Value.Selector.Options[0].Value[0].Text
} else if content.Control == "Text" || content.Control == "Textarea" {
value = content.Value.Text
} else if content.Control == "Date" {
value = content.Value.Date.Timestamp
} else if content.Control == "Money" {
value = content.Value.NewMoney
} else if content.Control == "File" {
value = content.Value.Files[0].FileId
} else if content.Control == "Vacation" { //请假 请假类型,开始时间,结束时间,请假时长
tp := content.Value.Vacation.Selector.Options[0].Value[0].Text
value = tp + "," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewBegin) +
"," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewEnd) +
"," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration)
} else if content.Control == "PunchCorrection" { //补卡:日期,时间,状态
mp := cast.ToStringMap(content.Value.PunchCorrection)
ddate := cast.ToString(mp["daymonthyear"])
dtime := cast.ToString(mp["time"])
if ddate == "" {
ddate = dtime
}
value = ddate + "," + dtime + "," + cast.ToString(mp["state"])
2024-03-09 20:04:21 +08:00
} else if content.Control == "BankAccount" {
mp := cast.ToStringMap(content.Value.BankAccount)
value = cast.ToString(mp["account_type"]) + "," + cast.ToString(mp["account_name"]) + "," + cast.ToString(mp["account_number"])
2024-01-22 22:34:59 +08:00
}
return value
}
return ""
}
func (d *ApproveDetail) String() string {
return goutil.EncodeJSONIndent(d)
}
func (d *ApproveDetail) GetUserid() string {
return d.Applyer.Userid
}
2024-01-22 22:47:35 +08:00
func NewAppApprove(cfg *AppConfig) *AppApprove {
2024-01-22 22:34:59 +08:00
return &AppApprove{
2024-01-22 22:47:35 +08:00
App: *NewApp(cfg),
2024-01-22 22:34:59 +08:00
}
}
func (q *AppApprove) GetDetail(spNo string) (*ApproveDetail, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token=%s", q.GetToken())
reqParam := fmt.Sprintf(`{"sp_no":"%s"}`, spNo)
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(reqParam))
if err != nil {
return nil, err
}
var rsp ApproveDetailRsp
2024-03-09 20:04:21 +08:00
fmt.Println("spno: %s, detail: %s", spNo, string(rspBody))
2024-01-22 22:34:59 +08:00
if err := json.Unmarshal(rspBody, &rsp); err != nil {
log.Errorf("get body[%s] json error :%s", string(rspBody), err.Error())
return nil, err
}
return rsp.Info, nil
}
func (q *AppApprove) GetList(start, end int64, templateId string) ([]string, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token=%s", q.GetToken())
reqParam := make(map[string]interface{})
reqParam["starttime"] = cast.ToString(start)
reqParam["endtime"] = cast.ToString(end)
reqParam["new_cursor"] = ""
reqParam["size"] = 100
filters := make([]interface{}, 0)
if templateId != "" {
filters = append(filters, map[string]interface{}{
"key": "template_id",
"value": templateId,
})
}
filters = append(filters, map[string]interface{}{
"key": "sp_status",
"value": "2",
})
reqParam["filters"] = filters
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(goutil.EncodeJSON(reqParam)))
if err != nil {
log.Errorf("httpPost error :%s", err.Error())
return nil, err
}
result, err := q.GetResult(rspBody)
if err != nil {
return nil, err
}
return cast.ToStringSlice(result["sp_no_list"]), nil
}