parent
b2ee634db1
commit
3250e2402f
@ -1,5 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "1.0.153"
|
||||
return "1.0.154"
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package wechatopen
|
||||
|
||||
// MonitorAuthorizerAccessToken 授权方access_token 监控
|
||||
//func MonitorAuthorizerAccessToken(ctx context.Context, c *Client, authorizerRefreshToken string) (string, error) {
|
||||
// authorizerAccessToken := GetAuthorizerAccessToken(ctx, c) // 查询
|
||||
// // 判断
|
||||
// result, err := c.CgiBinGetCallBackIp(ctx, authorizerAccessToken)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// if len(result.Result.IpList) > 0 {
|
||||
// return authorizerAccessToken, err
|
||||
// }
|
||||
// // 重新获取
|
||||
// resp, err := c.CgiBinComponentApiAuthorizerToken(ctx, authorizerRefreshToken)
|
||||
// if resp.Result.AuthorizerRefreshToken == "" {
|
||||
// return authorizerAccessToken, err
|
||||
// }
|
||||
// return SetAuthorizerAccessToken(ctx, c, resp.Result.AuthorizerAccessToken), err
|
||||
//}
|
@ -0,0 +1,38 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/dtapps/go-library/utils/gojson"
|
||||
"github.com/dtapps/go-library/utils/gorequest"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CgiBinGetCallBackIpResponse struct {
|
||||
IpList []string `json:"ip_list,omitempty"`
|
||||
}
|
||||
|
||||
type CgiBinGetCallBackIpResult struct {
|
||||
Result CgiBinGetCallBackIpResponse // 结果
|
||||
Body []byte // 内容
|
||||
Http gorequest.Response // 请求
|
||||
}
|
||||
|
||||
func NewCgiBinGetCallBackIpResult(result CgiBinGetCallBackIpResponse, body []byte, http gorequest.Response) *CgiBinGetCallBackIpResult {
|
||||
return &CgiBinGetCallBackIpResult{Result: result, Body: body, Http: http}
|
||||
}
|
||||
|
||||
// CgiBinGetCallBackIp 获取微信callback IP地址
|
||||
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_the_WeChat_server_IP_address.html
|
||||
func (c *Client) CgiBinGetCallBackIp(ctx context.Context, authorizerAccessToken string, notMustParams ...gorequest.Params) (*CgiBinGetCallBackIpResult, error) {
|
||||
// 参数
|
||||
params := gorequest.NewParamsWith(notMustParams...)
|
||||
// 请求
|
||||
request, err := c.request(ctx, apiUrl+"/cgi-bin/get_api_domain_ip?access_token="+authorizerAccessToken, params, http.MethodGet)
|
||||
if err != nil {
|
||||
return NewCgiBinGetCallBackIpResult(CgiBinGetCallBackIpResponse{}, request.ResponseBody, request), err
|
||||
}
|
||||
// 定义
|
||||
var response CgiBinGetCallBackIpResponse
|
||||
err = gojson.Unmarshal(request.ResponseBody, &response)
|
||||
return NewCgiBinGetCallBackIpResult(response, request.ResponseBody, request), err
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func (c *Client) checkAuthorizerIsConfig(ctx context.Context) error {
|
||||
authorizerAppid := c.GetAuthorizerAppid(ctx)
|
||||
if authorizerAppid == "" {
|
||||
return errors.New("请配置 authorizerAppid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) checkAuthorizerConfig(ctx context.Context) error {
|
||||
authorizerAppid := c.GetAuthorizerAppid(ctx)
|
||||
if authorizerAppid == "" {
|
||||
return errors.New("请配置 authorizerAppid")
|
||||
}
|
||||
authorizerAccessToken := GetAuthorizerAccessToken(ctx, c)
|
||||
if authorizerAccessToken == "" {
|
||||
return errors.New("请配置 authorizerAccessToken")
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package wechatopen
|
||||
|
||||
// MonitorComponentAccessToken 第三方平台access_token 监控
|
||||
//func MonitorComponentAccessToken(ctx context.Context, c *Client) (string, error) {
|
||||
// componentAccessToken := GetComponentAccessToken(ctx, c) // 查询
|
||||
// // 判断
|
||||
// result, err := c.CgiBinGetApiDomainIp(ctx, componentAccessToken)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// if len(result.Result.IpList) > 0 {
|
||||
// return componentAccessToken, err
|
||||
// }
|
||||
// // 重新获取
|
||||
// resp, err := c.CgiBinComponentApiComponentToken(ctx)
|
||||
// if resp.Result.ComponentAccessToken == "" {
|
||||
// return componentAccessToken, err
|
||||
// }
|
||||
// return SetComponentAccessToken(ctx, c, resp.Result.ComponentAccessToken), err
|
||||
//}
|
@ -0,0 +1,17 @@
|
||||
package wechatopen
|
||||
|
||||
// MonitorComponentPreAuthCode 第三方平台预授权码 监控
|
||||
//func MonitorComponentPreAuthCode(ctx context.Context, c *Client) (string, error) {
|
||||
// // 查询
|
||||
// preAuthCode := GetComponentPreAuthCode(ctx, c)
|
||||
// // 判断
|
||||
// if preAuthCode != "" {
|
||||
// return preAuthCode, nil
|
||||
// }
|
||||
// // 重新获取
|
||||
// resp, err := c.CgiBinComponentApiCreatePreAuthCoden(ctx)
|
||||
// if resp.Result.PreAuthCode == "" {
|
||||
// return preAuthCode, err
|
||||
// }
|
||||
// return SetComponentPreAuthCode(ctx, c, resp.Result.PreAuthCode), err
|
||||
//}
|
@ -0,0 +1 @@
|
||||
package wechatopen
|
@ -1,40 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// GetComponentAppId 第三方平台appid
|
||||
func (c *Client) GetComponentAppId(ctx context.Context) string {
|
||||
return c.config.componentAppId
|
||||
}
|
||||
|
||||
// GetComponentAppSecret 第三方平台app_secret
|
||||
func (c *Client) GetComponentAppSecret(ctx context.Context) string {
|
||||
return c.config.componentAppSecret
|
||||
}
|
||||
|
||||
// GetMessageToken 第三方平台消息令牌
|
||||
func (c *Client) GetMessageToken(ctx context.Context) string {
|
||||
return c.config.messageToken
|
||||
}
|
||||
|
||||
// GetMessageKey 第三方平台消息密钥
|
||||
func (c *Client) GetMessageKey(ctx context.Context) string {
|
||||
return c.config.messageKey
|
||||
}
|
||||
|
||||
// GetAuthorizerAppid 授权方appid
|
||||
func (c *Client) GetAuthorizerAppid(ctx context.Context) string {
|
||||
return c.config.authorizerAppid
|
||||
}
|
||||
|
||||
// GetAuthorizerAccessToken 授权方access_token
|
||||
func (c *Client) GetAuthorizerAccessToken(ctx context.Context) string {
|
||||
return c.config.authorizerAccessToken
|
||||
}
|
||||
|
||||
// GetAuthorizerRefreshToken 授权方refresh_token
|
||||
func (c *Client) GetAuthorizerRefreshToken(ctx context.Context) string {
|
||||
return c.config.authorizerRefreshToken
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetAuthorizerAccessTokenCacheKeyName 授权方access_token 缓存名称
|
||||
func GetAuthorizerAccessTokenCacheKeyName(ctx context.Context, c *Client) string {
|
||||
return c.cache.authorizerAccessTokenPrefix + c.GetComponentAppId(ctx) + ":" + c.GetAuthorizerAppid(ctx)
|
||||
}
|
||||
|
||||
// GetAuthorizerAccessToken 授权方access_token
|
||||
func GetAuthorizerAccessToken(ctx context.Context, c *Client) string {
|
||||
if c.config.authorizerAccessToken != "" {
|
||||
return c.config.authorizerAccessToken
|
||||
}
|
||||
if c.cache.redisClient.GetDb() == nil {
|
||||
return c.config.authorizerAccessToken
|
||||
}
|
||||
result, _ := c.cache.redisClient.Get(ctx, GetAuthorizerAccessTokenCacheKeyName(ctx, c)).Result()
|
||||
return result
|
||||
}
|
||||
|
||||
// SetAuthorizerAccessToken 授权方access_token
|
||||
func SetAuthorizerAccessToken(ctx context.Context, c *Client, authorizerAccessToken string) string {
|
||||
if authorizerAccessToken == "" {
|
||||
return ""
|
||||
}
|
||||
c.cache.redisClient.Set(ctx, GetAuthorizerAccessTokenCacheKeyName(ctx, c), authorizerAccessToken, time.Hour*2)
|
||||
return GetAuthorizerAccessToken(ctx, c)
|
||||
}
|
||||
|
||||
// DelAuthorizerAccessToken 授权方access_token 删除
|
||||
func DelAuthorizerAccessToken(ctx context.Context, c *Client) error {
|
||||
return c.cache.redisClient.Del(ctx, GetAuthorizerAccessTokenCacheKeyName(ctx, c)).Err()
|
||||
}
|
||||
|
||||
// MonitorAuthorizerAccessToken 授权方access_token 监控
|
||||
func MonitorAuthorizerAccessToken(ctx context.Context, c *Client, authorizerRefreshToken string) (string, error) {
|
||||
// 查询
|
||||
authorizerAccessToken := GetAuthorizerAccessToken(ctx, c)
|
||||
// 判断
|
||||
if authorizerAccessToken != "" {
|
||||
return authorizerAccessToken, nil
|
||||
}
|
||||
// 重新获取
|
||||
resp, err := c.CgiBinComponentApiAuthorizerToken(ctx, authorizerRefreshToken)
|
||||
if resp.Result.AuthorizerRefreshToken == "" {
|
||||
return authorizerAccessToken, errors.New(fmt.Sprintf("获取失败:%v", err))
|
||||
}
|
||||
return SetAuthorizerAccessToken(ctx, c, resp.Result.AuthorizerAccessToken), err
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetComponentAccessTokenCacheKeyName 第三方平台access_token 缓存名称
|
||||
func GetComponentAccessTokenCacheKeyName(ctx context.Context, c *Client) string {
|
||||
return c.cache.componentAccessTokenPrefix + c.GetComponentAppId(ctx)
|
||||
}
|
||||
|
||||
// GetComponentAccessToken 第三方平台access_token 获取
|
||||
func GetComponentAccessToken(ctx context.Context, c *Client) string {
|
||||
if c.cache.redisClient.GetDb() == nil {
|
||||
return c.config.componentAccessToken
|
||||
}
|
||||
result, _ := c.cache.redisClient.GetDb().Get(ctx, GetComponentAccessTokenCacheKeyName(ctx, c)).Result()
|
||||
return result
|
||||
}
|
||||
|
||||
// SetComponentAccessToken 第三方平台access_token 设置
|
||||
func SetComponentAccessToken(ctx context.Context, c *Client, componentAccessToken string) string {
|
||||
if componentAccessToken == "" {
|
||||
return ""
|
||||
}
|
||||
c.cache.redisClient.Set(ctx, GetComponentAccessTokenCacheKeyName(ctx, c), componentAccessToken, time.Second*7200)
|
||||
return GetComponentAccessToken(ctx, c)
|
||||
}
|
||||
|
||||
// DelComponentAccessToken 第三方平台access_token 删除
|
||||
func DelComponentAccessToken(ctx context.Context, c *Client) error {
|
||||
return c.cache.redisClient.Del(ctx, GetComponentAccessTokenCacheKeyName(ctx, c)).Err()
|
||||
}
|
||||
|
||||
// MonitorComponentAccessToken 第三方平台access_token 监控
|
||||
func MonitorComponentAccessToken(ctx context.Context, c *Client) (string, error) {
|
||||
// 查询
|
||||
componentAccessToken := GetComponentAccessToken(ctx, c)
|
||||
// 判断
|
||||
result, err := c.CgiBinGetApiDomainIp(ctx, componentAccessToken)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(result.Result.IpList) > 0 {
|
||||
return componentAccessToken, err
|
||||
}
|
||||
// 重新获取
|
||||
resp, err := c.CgiBinComponentApiComponentToken(ctx)
|
||||
return SetComponentAccessToken(ctx, c, resp.Result.ComponentAccessToken), err
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetComponentVerifyTicketCacheKeyName 第三方平台推送ticket 缓存名称
|
||||
func GetComponentVerifyTicketCacheKeyName(ctx context.Context, c *Client) string {
|
||||
return c.cache.componentVerifyTicketPrefix + c.GetComponentAppId(ctx)
|
||||
}
|
||||
|
||||
// GetComponentVerifyTicket 第三方平台推送ticket 获取
|
||||
func GetComponentVerifyTicket(ctx context.Context, c *Client) string {
|
||||
if c.cache.redisClient.GetDb() == nil {
|
||||
return c.config.componentVerifyTicket
|
||||
}
|
||||
result, _ := c.cache.redisClient.Get(ctx, GetComponentVerifyTicketCacheKeyName(ctx, c)).Result()
|
||||
return result
|
||||
}
|
||||
|
||||
// SetComponentVerifyTicket 第三方平台推送ticket 设置
|
||||
func SetComponentVerifyTicket(ctx context.Context, c *Client, componentVerifyTicket string) string {
|
||||
if componentVerifyTicket == "" {
|
||||
return ""
|
||||
}
|
||||
c.cache.redisClient.Set(ctx, GetComponentVerifyTicketCacheKeyName(ctx, c), componentVerifyTicket, time.Hour*12)
|
||||
return GetComponentVerifyTicket(ctx, c)
|
||||
}
|
||||
|
||||
// DelComponentVerifyTicket 第三方平台推送ticket 删除
|
||||
func DelComponentVerifyTicket(ctx context.Context, c *Client) error {
|
||||
return c.cache.redisClient.Del(ctx, GetComponentVerifyTicketCacheKeyName(ctx, c)).Err()
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetPreAuthCodeCacheKeyName 第三方平台预授权码 缓存名称
|
||||
func GetPreAuthCodeCacheKeyName(ctx context.Context, c *Client) string {
|
||||
return c.cache.preAuthCodePrefix + c.GetComponentAppId(ctx)
|
||||
}
|
||||
|
||||
// GetPreAuthCode 第三方平台预授权码 获取
|
||||
func GetPreAuthCode(ctx context.Context, c *Client) string {
|
||||
if c.cache.redisClient.GetDb() == nil {
|
||||
return c.config.authorizerAccessToken
|
||||
}
|
||||
result, _ := c.cache.redisClient.Get(ctx, GetPreAuthCodeCacheKeyName(ctx, c)).Result()
|
||||
return result
|
||||
}
|
||||
|
||||
// SetPreAuthCode 第三方平台预授权码 设置
|
||||
func SetPreAuthCode(ctx context.Context, c *Client, preAuthCode string) string {
|
||||
if preAuthCode == "" {
|
||||
return ""
|
||||
}
|
||||
c.cache.redisClient.Set(ctx, GetPreAuthCodeCacheKeyName(ctx, c), preAuthCode, time.Second*1700)
|
||||
return GetPreAuthCode(ctx, c)
|
||||
}
|
||||
|
||||
// DelPreAuthCode 第三方平台预授权码 删除
|
||||
func DelPreAuthCode(ctx context.Context, c *Client) error {
|
||||
return c.cache.redisClient.Del(ctx, GetPreAuthCodeCacheKeyName(ctx, c)).Err()
|
||||
}
|
||||
|
||||
// MonitorPreAuthCode 第三方平台预授权码 监控
|
||||
func MonitorPreAuthCode(ctx context.Context, c *Client) (string, error) {
|
||||
// 查询
|
||||
preAuthCode := GetPreAuthCode(ctx, c)
|
||||
// 判断
|
||||
if preAuthCode != "" {
|
||||
return preAuthCode, nil
|
||||
}
|
||||
// 重新获取
|
||||
resp, err := c.CgiBinComponentApiCreatePreAuthCoden(ctx)
|
||||
return SetPreAuthCode(ctx, c, resp.Result.PreAuthCode), err
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package wechatopen
|
||||
|
||||
import "context"
|
||||
|
||||
// SetAuthorizerAccessToken 授权方access_token
|
||||
func (c *Client) SetAuthorizerAccessToken(ctx context.Context, authorizerAccessToken string) string {
|
||||
if authorizerAccessToken == "" {
|
||||
return ""
|
||||
}
|
||||
c.config.authorizerAccessToken = authorizerAccessToken
|
||||
return c.config.authorizerAccessToken
|
||||
}
|
||||
|
||||
// SetAuthorizerRefreshToken 授权方refresh_token
|
||||
func (c *Client) SetAuthorizerRefreshToken(ctx context.Context, authorizerRefreshToken string) string {
|
||||
if authorizerRefreshToken == "" {
|
||||
return ""
|
||||
}
|
||||
c.config.authorizerRefreshToken = authorizerRefreshToken
|
||||
return c.config.authorizerRefreshToken
|
||||
}
|
Loading…
Reference in new issue