63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package channel
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"message-pusher/common"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type wechatCorpAccountResponse struct {
|
|
ErrorCode int `json:"errcode"`
|
|
ErrorMessage string `json:"errmsg"`
|
|
AccessToken string `json:"access_token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
}
|
|
|
|
type WeChatCorpAccountTokenStoreItem struct {
|
|
CorpId string
|
|
CorpSecret string
|
|
AgentId string
|
|
AccessToken string
|
|
}
|
|
|
|
func (i *WeChatCorpAccountTokenStoreItem) Key() string {
|
|
return i.CorpId + i.AgentId + i.CorpSecret
|
|
}
|
|
|
|
func (i *WeChatCorpAccountTokenStoreItem) Token() string {
|
|
return i.AccessToken
|
|
}
|
|
|
|
func (i *WeChatCorpAccountTokenStoreItem) Refresh() {
|
|
// https://work.weixin.qq.com/api/doc/90000/90135/91039
|
|
client := http.Client{
|
|
Timeout: 5 * time.Second,
|
|
}
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
|
|
i.CorpId, i.CorpSecret), nil)
|
|
if err != nil {
|
|
common.SysError(err.Error())
|
|
return
|
|
}
|
|
responseData, err := client.Do(req)
|
|
if err != nil {
|
|
common.SysError("failed to refresh access token: " + err.Error())
|
|
return
|
|
}
|
|
defer responseData.Body.Close()
|
|
var res wechatCorpAccountResponse
|
|
err = json.NewDecoder(responseData.Body).Decode(&res)
|
|
if err != nil {
|
|
common.SysError("failed to decode wechatCorpAccountResponse: " + err.Error())
|
|
return
|
|
}
|
|
if res.ErrorCode != 0 {
|
|
common.SysError(res.ErrorMessage)
|
|
return
|
|
}
|
|
i.AccessToken = res.AccessToken
|
|
common.SysLog("access token refreshed")
|
|
}
|