gosdk/qyweixin/app_hr.go

116 lines
3.3 KiB
Go

package qyweixin
import (
"fmt"
"git.u8t.cn/open/gosdk/util"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"time"
)
var (
urlQyWeixinHrGetAllField = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_fields"
urlQyWeixinHrGetStaffInfo = "https://qyapi.weixin.qq.com/cgi-bin/hr/get_staff_info"
)
type AppHr struct {
App
config *AppConfig
}
type StaffInfo struct {
UserName string
RealName string
Phone string
StaffType string
Idno string
Salary float64
Stock float64
EntryDate string
BirthDate string
OfficialDate string
BankName string
BankCard string
}
func NewAppHr(cfg *AppConfig) *AppHr {
return &AppHr{
App: *NewApp(cfg),
}
}
func (h *AppHr) GetStaffInfo(userId string) (*StaffInfo, error) {
reqUrl := fmt.Sprintf("%s?access_token=%s", urlQyWeixinHrGetStaffInfo, h.GetToken())
reqBody := make(map[string]interface{})
reqBody["userid"] = userId
reqBody["get_all"] = true
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(goutil.EncodeJSON(reqBody)))
if err != nil {
return nil, err
}
staff := new(StaffInfo)
result, err := h.GetResult(rspBody)
if err != nil {
return nil, err
}
fieldMap := make(map[string]map[string]interface{})
for _, fieldInfo := range cast.ToSlice(result["field_info"]) {
fi := cast.ToStringMap(fieldInfo)
fieldMap[cast.ToString(fi["fieldid"])] = fi
}
userInfo, err := h.GetUserInfo(userId)
if err != nil {
log.Errorf("GetUserInfo error:%s", err.Error())
return nil, err
}
staff.UserName = userId
staff.RealName = userInfo.RealName
staff.Salary = cast.ToFloat64(h.getFieldValue(fieldMap["20001"]))
staff.Stock = cast.ToFloat64(h.getFieldValue(fieldMap["20002"]))
staff.Phone = cast.ToString(h.getFieldValue(fieldMap["17003"]))
staff.StaffType = cast.ToString(h.getFieldValue(fieldMap["12003"]))
staff.Idno = cast.ToString(h.getFieldValue(fieldMap["11015"]))
staff.BankName = cast.ToString(h.getFieldValue(fieldMap["13001"]))
staff.BankCard = cast.ToString(h.getFieldValue(fieldMap["13002"]))
staff.EntryDate = time.Unix(cast.ToInt64(h.getFieldValue(fieldMap["12018"])), 0).Format("2006-01-02")
staff.BirthDate = time.Unix(cast.ToInt64(h.getFieldValue(fieldMap["11005"])), 0).Format("2006-01-02")
staff.OfficialDate = time.Unix(cast.ToInt64(h.getFieldValue(fieldMap["12023"])), 0).Format("2006-01-02")
//fmt.Println(goutil.EncodeJSON(staff))
return staff, nil
}
func (h *AppHr) getFieldValue(fieldInfo map[string]interface{}) string {
valueType := cast.ToInt(fieldInfo["value_type"])
if valueType == 1 {
return cast.ToString(fieldInfo["value_string"])
} else if valueType == 2 {
return cast.ToString(fieldInfo["value_uint64"])
} else if valueType == 3 {
return cast.ToString(fieldInfo["value_uint32"])
} else if valueType == 4 {
return cast.ToString(fieldInfo["value_int64"])
} else if valueType == 5 {
moble := cast.ToStringMap(fieldInfo["value_mobile"])
return cast.ToString(moble["value_mobile"])
}
return ""
}
func (h *AppHr) GetAllField() ([]byte, error) {
reqUrl := fmt.Sprintf("%s?access_token=%s", urlQyWeixinHrGetAllField, h.GetToken())
rspBody, err := util.HttpGet(reqUrl, nil)
if err != nil {
return nil, err
}
result, err := h.GetResult(rspBody)
if err != nil {
return nil, err
}
fmt.Println(goutil.EncodeJSONIndent(result))
return rspBody, err
}