新增企业微信
This commit is contained in:
122
platformapi/enterprise_wechat/wechat_client.go
Normal file
122
platformapi/enterprise_wechat/wechat_client.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package enterprise_wechat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func New(corpId, corpSecret string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
|
||||
return &API{
|
||||
corpId: corpId,
|
||||
corpSecret: corpSecret,
|
||||
locker: sync.RWMutex{},
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取token
|
||||
type EnterpriseToken struct {
|
||||
PublicCode
|
||||
AccessToken string `json:"access_token"` // 权限说明
|
||||
ExpiresIn int64 `json:"expires_in"` // 过期时间
|
||||
}
|
||||
|
||||
func (a *API) SetToken(token string) {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.accessToken = token
|
||||
}
|
||||
|
||||
func (a *API) SetExpiresInTime(int2 int64) {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.expiresIn = time.Now().Unix() + int2
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
func (a *API) GetAccessToken() (tokenInfo *EnterpriseToken, err error) {
|
||||
parameter := make(map[string]interface{}, 2)
|
||||
parameter["corpid"] = a.corpId
|
||||
parameter["corpsecret"] = a.corpSecret
|
||||
|
||||
result, err := a.AccessAPI(WeChatBaseApi, GetToken, http.MethodGet, parameter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accessToken := &EnterpriseToken{}
|
||||
if err := utils.Map2StructByJson(result, &accessToken, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if accessToken.ErrCode != 0 {
|
||||
return nil, errors.New(accessToken.ErrMsg)
|
||||
}
|
||||
a.SetToken(accessToken.AccessToken)
|
||||
a.SetExpiresInTime(accessToken.ExpiresIn)
|
||||
return accessToken, err
|
||||
}
|
||||
|
||||
// 数据发送
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
bizParams["access_token"] = a.accessToken
|
||||
data, err := json.Marshal(bizParams) // 序列化
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 全路径请求参数
|
||||
fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||
|
||||
// 发送请求
|
||||
sendUrl := func() *http.Request {
|
||||
var request *http.Request
|
||||
if http.MethodPost == method {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
}
|
||||
|
||||
// 数据解析
|
||||
dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if utils.MustInterface2Int64(jsonResult1["errcode"]) != 0 {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["errmsg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["errcode"])))
|
||||
baseapi.SugarLogger.Debugf("enterprise wechat AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return errLevel, err
|
||||
}
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) CheckAccessTokenExpiresIn() {
|
||||
if a.expiresIn == 0 || a.expiresIn <= time.Now().Unix() {
|
||||
a.GetAccessToken()
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user