package weixin import ( "encoding/json" butil "enterprise/base/util" "fmt" log "github.com/sirupsen/logrus" "github.com/spf13/cast" "time" ) var ( urlGetToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" urlGetCheckinRlue = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcorpcheckinoption" urlGetCheckinData = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata" urlConvertOpenid = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid" urlGetUser = "https://qyapi.weixin.qq.com/cgi-bin/user/get" urlGetDepartmentUser = "https://qyapi.weixin.qq.com/cgi-bin/user/list" ) type QyWeixin struct { CorpId string Secret string Agent string Token string tokenExpire int64 } type UserInfo struct { UserId string RealName string } func NewQyWeixin(corpId, secret, agent string) *QyWeixin { return &QyWeixin{ CorpId: corpId, Secret: secret, Agent: agent, } } func (q *QyWeixin) GetToken() string { if time.Now().Unix() <= q.tokenExpire-600 { return q.Token } q.refreshToken() return q.Token } func (q *QyWeixin) GetResult(rspBody []byte) (map[string]interface{}, error) { result := make(map[string]interface{}) if err := json.Unmarshal(rspBody, &result); err != nil { log.Errorf("result[%s] error :%s", string(rspBody), err.Error()) return nil, err } if cast.ToInt(result["errcode"]) != 0 { log.Errorf("result[%s] error ", string(rspBody)) return nil, fmt.Errorf("%d:%s", cast.ToInt(result["errcode"]), cast.ToString(result["errmsg"])) } return result, nil } func (q *QyWeixin) GetOpenid(userid string) (string, error) { if err := q.refreshToken(); err != nil { return "", err } reqUrl := fmt.Sprintf("%s?access_token=%s", urlConvertOpenid, q.GetToken()) rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(fmt.Sprintf(`{"userid" : "%s"}`, userid))) if err != nil { log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error()) return "", err } result, err := q.GetResult(rspBody) if err != nil { return "", err } return cast.ToString(result["openid"]), nil } func (q *QyWeixin) GetUserInfo(userid string) (*UserInfo, error) { if err := q.refreshToken(); err != nil { return nil, err } reqUrl := fmt.Sprintf("%s?access_token=%s&userid=%s", urlGetUser, q.GetToken(), userid) rspBody, err := butil.HttpGet(reqUrl, nil) if err != nil { log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error()) return nil, err } result, err := q.GetResult(rspBody) if err != nil { return nil, err } userInfo := new(UserInfo) userInfo.UserId = userid userInfo.RealName = cast.ToString(result["name"]) return userInfo, nil } func (q *QyWeixin) GetDepartmentUserid(departmentId int) ([]string, error) { if err := q.refreshToken(); err != nil { return nil, err } reqUrl := fmt.Sprintf("%s?access_token=%s&department_id=%d", urlGetDepartmentUser, q.GetToken(), departmentId) rspBody, err := butil.HttpGet(reqUrl, nil) if err != nil { log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error()) return nil, err } result, err := q.GetResult(rspBody) if err != nil { return nil, err } userids := make([]string, 0) userlist := cast.ToSlice(result["userlist"]) for _, u := range userlist { user := cast.ToStringMap(u) userids = append(userids, cast.ToString(user["userid"])) } return userids, nil } func (q *QyWeixin) refreshToken() error { if time.Now().Unix() <= q.tokenExpire-600 { return nil } reqUrl := fmt.Sprintf("%s?corpid=%s&corpsecret=%s", urlGetToken, q.CorpId, q.Secret) rspBody, err := butil.HttpGet(reqUrl, nil) if err != nil { log.Errorf("http url[%s] error :%s", reqUrl, err.Error()) return err } result, err := q.GetResult(rspBody) if err != nil { return err } q.Token = cast.ToString(result["access_token"]) q.tokenExpire = time.Now().Unix() + cast.ToInt64(result["expires_in"]) return nil }