- refactor weixinapi
This commit is contained in:
56
platformapi/weixinapi/cgibin.go
Normal file
56
platformapi/weixinapi/cgibin.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package weixinapi
|
||||
|
||||
import "git.rosy.net.cn/baseapi/utils"
|
||||
|
||||
func (a *API) CBSetToken(newToken string) bool {
|
||||
curToken := a.CBGetToken()
|
||||
if curToken != newToken {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.token = newToken
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *API) CBGetToken() string {
|
||||
a.locker.RLock()
|
||||
defer a.locker.RUnlock()
|
||||
return a.token
|
||||
}
|
||||
|
||||
func (a *API) CBRetrieveToken() (tokenInfo *TokenInfo, err error) {
|
||||
result, err := a.AccessAPI("cgi-bin/token", utils.Params2Map("grant_type", "client_credential"), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokenInfo = &TokenInfo{
|
||||
AccessToken: utils.Interface2String(result["access_token"]),
|
||||
ExpiresIn: int(utils.MustInterface2Int64(result["expires_in"])),
|
||||
}
|
||||
return tokenInfo, nil
|
||||
}
|
||||
|
||||
func (a *API) CBRefreshToken() (tokenInfo *TokenInfo, err error) {
|
||||
if tokenInfo, err = a.CBRetrieveToken(); err == nil {
|
||||
a.CBSetToken(tokenInfo.AccessToken)
|
||||
}
|
||||
return tokenInfo, err
|
||||
}
|
||||
|
||||
func (a *API) CBMessageTemplateSend(userOpenID, templateID, downloadURL string, miniProgram, data interface{}) (err error) {
|
||||
bodyJson := map[string]interface{}{
|
||||
"touser": userOpenID,
|
||||
"template_id": templateID,
|
||||
"url": downloadURL,
|
||||
"data": data,
|
||||
}
|
||||
if downloadURL != "" {
|
||||
bodyJson["url"] = downloadURL
|
||||
}
|
||||
if miniProgram != nil {
|
||||
bodyJson["miniprogram"] = miniProgram
|
||||
}
|
||||
_, err = a.AccessAPI("cgi-bin/message/template/send", nil, string(utils.MustMarshal(bodyJson)))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user