parent
61d1c2f975
commit
b40a8afea0
@ -1,5 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "1.0.136"
|
||||
return "1.0.137"
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
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.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
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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()
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: redis:6.2-alpine
|
@ -0,0 +1,23 @@
|
||||
package gocron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFailedToConnectToRedis = errors.New("gocron: failed to connect to redis")
|
||||
ErrFailedToObtainLock = errors.New("gocron: failed to obtain lock")
|
||||
ErrFailedToReleaseLock = errors.New("gocron: failed to release lock")
|
||||
)
|
||||
|
||||
// Locker represents the required interface to lock jobs when running multiple schedulers.
|
||||
type Locker interface {
|
||||
// Lock if an error is returned by lock, the job will not be scheduled.
|
||||
Lock(ctx context.Context, key string) (Lock, error)
|
||||
}
|
||||
|
||||
// Lock represents an obtained lock
|
||||
type Lock interface {
|
||||
Unlock(ctx context.Context) error
|
||||
}
|
Loading…
Reference in new issue