enterprise/common/weixin/qyweixin.go

91 lines
2.5 KiB
Go
Raw Normal View History

2023-08-09 22:00:55 +08:00
package weixin
2023-08-04 18:27:37 +08:00
import (
"encoding/json"
butil "enterprise/base/util"
"errors"
"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"
2023-08-09 22:00:55 +08:00
urlConvertOpenid = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid"
2023-08-04 18:27:37 +08:00
)
type QyWeixin struct {
2023-08-13 21:24:54 +08:00
CorpId string
Secret string
Agent string
Token string
2023-08-04 18:27:37 +08:00
tokenExpire int64
}
2023-08-13 21:24:54 +08:00
func NewQyWeixin(corpId, secret, agent string) *QyWeixin {
2023-08-04 18:27:37 +08:00
return &QyWeixin{
2023-08-13 21:24:54 +08:00
CorpId: corpId,
Secret: secret,
Agent: agent,
2023-08-04 18:27:37 +08:00
}
}
2023-08-13 21:24:54 +08:00
func (q *QyWeixin) GetToken() string {
2023-08-04 18:27:37 +08:00
if time.Now().Unix() <= q.tokenExpire-600 {
2023-08-13 21:24:54 +08:00
return q.Token
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
q.refreshToken()
return q.Token
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
func (q *QyWeixin) GetOpenid(userid string) (string, error) {
2023-08-04 18:27:37 +08:00
if err := q.refreshToken(); err != nil {
2023-08-13 21:24:54 +08:00
return "", err
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
reqUrl := fmt.Sprintf("%s?access_token=%s", urlConvertOpenid, q.GetToken())
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(fmt.Sprintf(`{"userid" : "%s"}`, userid)))
2023-08-04 18:27:37 +08:00
if err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("httpPost url[%s] error :%s", reqUrl, err.Error())
return "", err
2023-08-04 18:27:37 +08:00
}
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())
2023-08-13 21:24:54 +08:00
return "", err
2023-08-04 18:27:37 +08:00
}
if cast.ToInt(result["errcode"]) != 0 {
log.Errorf("http url[%s] result[%s] error ", reqUrl, string(rspBody))
2023-08-13 21:24:54 +08:00
return "", errors.New(string(rspBody))
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
return cast.ToString(result["openid"]), nil
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
func (q *QyWeixin) refreshToken() error {
if time.Now().Unix() <= q.tokenExpire-600 {
return nil
2023-08-04 18:27:37 +08:00
}
2023-08-13 21:24:54 +08:00
reqUrl := fmt.Sprintf("%s?corpid=%s&corpsecret=%s", urlGetToken, q.CorpId, q.Secret)
rspBody, err := butil.HttpGet(reqUrl, nil)
2023-08-04 18:27:37 +08:00
if err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("http url[%s] error :%s", reqUrl, err.Error())
return err
2023-08-04 18:27:37 +08:00
}
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())
2023-08-13 21:24:54 +08:00
return err
2023-08-04 18:27:37 +08:00
}
if cast.ToInt(result["errcode"]) != 0 {
log.Errorf("http url[%s] result[%s] error ", reqUrl, string(rspBody))
2023-08-13 21:24:54 +08:00
return errors.New(string(rspBody))
2023-08-04 18:56:44 +08:00
}
2023-08-09 22:00:55 +08:00
2023-08-13 21:24:54 +08:00
q.Token = cast.ToString(result["access_token"])
q.tokenExpire = time.Now().Unix() + cast.ToInt64(result["expires_in"])
return nil
2023-08-09 22:00:55 +08:00
}