|
|
|
@ -7,135 +7,135 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 微信后台推送的ticke
|
|
|
|
|
func (app *App) getComponentVerifyTicketCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:component_verify_ticket:%v", app.componentAppId)
|
|
|
|
|
func (c *Client) getComponentVerifyTicketCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:component_verify_ticket:%v", c.config.ComponentAppId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetComponentVerifyTicket 设置微信后台推送的ticke
|
|
|
|
|
func (app *App) SetComponentVerifyTicket(componentVerifyTicket string) string {
|
|
|
|
|
func (c *Client) SetComponentVerifyTicket(componentVerifyTicket string) string {
|
|
|
|
|
if componentVerifyTicket == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
app.redis.Db.Set(context.Background(), app.getComponentVerifyTicketCacheKeyName(), componentVerifyTicket, time.Hour*12)
|
|
|
|
|
return app.GetComponentVerifyTicket()
|
|
|
|
|
c.config.RedisClient.Db.Set(context.Background(), c.getComponentVerifyTicketCacheKeyName(), componentVerifyTicket, time.Hour*12)
|
|
|
|
|
return c.GetComponentVerifyTicket()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetComponentVerifyTicket 获取微信后台推送的ticke
|
|
|
|
|
func (app *App) GetComponentVerifyTicket() string {
|
|
|
|
|
if app.redis.Db == nil {
|
|
|
|
|
return app.componentVerifyTicket
|
|
|
|
|
func (c *Client) GetComponentVerifyTicket() string {
|
|
|
|
|
if c.config.RedisClient.Db == nil {
|
|
|
|
|
return c.config.ComponentVerifyTicket
|
|
|
|
|
}
|
|
|
|
|
result, _ := app.redis.Db.Get(context.Background(), app.getComponentVerifyTicketCacheKeyName()).Result()
|
|
|
|
|
result, _ := c.config.RedisClient.Db.Get(context.Background(), c.getComponentVerifyTicketCacheKeyName()).Result()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 令牌
|
|
|
|
|
func (app *App) getComponentAccessTokenCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:component_access_token:%v", app.componentAppId)
|
|
|
|
|
func (c *Client) getComponentAccessTokenCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:component_access_token:%v", c.config.ComponentAppId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetComponentAccessToken 设置令牌
|
|
|
|
|
func (app *App) SetComponentAccessToken(componentAccessToken string) string {
|
|
|
|
|
func (c *Client) SetComponentAccessToken(componentAccessToken string) string {
|
|
|
|
|
if componentAccessToken == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
app.redis.Db.Set(context.Background(), app.getComponentAccessTokenCacheKeyName(), componentAccessToken, time.Second*7200)
|
|
|
|
|
return app.GetComponentAccessToken()
|
|
|
|
|
c.config.RedisClient.Db.Set(context.Background(), c.getComponentAccessTokenCacheKeyName(), componentAccessToken, time.Second*7200)
|
|
|
|
|
return c.GetComponentAccessToken()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetComponentAccessToken 获取令牌
|
|
|
|
|
func (app *App) GetComponentAccessToken() string {
|
|
|
|
|
if app.redis.Db == nil {
|
|
|
|
|
return app.componentAccessToken
|
|
|
|
|
func (c *Client) GetComponentAccessToken() string {
|
|
|
|
|
if c.config.RedisClient.Db == nil {
|
|
|
|
|
return c.config.ComponentAccessToken
|
|
|
|
|
}
|
|
|
|
|
result, _ := app.redis.Db.Get(context.Background(), app.getComponentAccessTokenCacheKeyName()).Result()
|
|
|
|
|
result, _ := c.config.RedisClient.Db.Get(context.Background(), c.getComponentAccessTokenCacheKeyName()).Result()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MonitorComponentAccessToken 监控令牌
|
|
|
|
|
func (app *App) MonitorComponentAccessToken() string {
|
|
|
|
|
func (c *Client) MonitorComponentAccessToken() string {
|
|
|
|
|
// 查询
|
|
|
|
|
componentAccessToken := app.GetComponentAccessToken()
|
|
|
|
|
componentAccessToken := c.GetComponentAccessToken()
|
|
|
|
|
// 判断
|
|
|
|
|
result := app.CgiBinGetApiDomainIp(componentAccessToken)
|
|
|
|
|
result := c.CgiBinGetApiDomainIp(componentAccessToken)
|
|
|
|
|
if len(result.Result.IpList) > 0 {
|
|
|
|
|
return componentAccessToken
|
|
|
|
|
}
|
|
|
|
|
// 重新获取
|
|
|
|
|
return app.SetComponentAccessToken(app.CgiBinComponentApiComponentToken().Result.ComponentAccessToken)
|
|
|
|
|
return c.SetComponentAccessToken(c.CgiBinComponentApiComponentToken().Result.ComponentAccessToken)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 授权方令牌
|
|
|
|
|
func (app *App) getAuthorizerAccessTokenCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:authorizer_access_token:%v:%v", app.componentAppId, app.authorizerAppid)
|
|
|
|
|
func (c *Client) getAuthorizerAccessTokenCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:authorizer_access_token:%v:%v", c.config.ComponentAppId, c.config.AuthorizerAppid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAuthorizerAccessToken 设置授权方令牌
|
|
|
|
|
func (app *App) SetAuthorizerAccessToken(authorizerAccessToken string) string {
|
|
|
|
|
func (c *Client) SetAuthorizerAccessToken(authorizerAccessToken string) string {
|
|
|
|
|
if authorizerAccessToken == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
app.redis.Db.Set(context.Background(), app.getAuthorizerAccessTokenCacheKeyName(), authorizerAccessToken, time.Hour*2)
|
|
|
|
|
return app.GetComponentAccessToken()
|
|
|
|
|
c.config.RedisClient.Db.Set(context.Background(), c.getAuthorizerAccessTokenCacheKeyName(), authorizerAccessToken, time.Hour*2)
|
|
|
|
|
return c.GetComponentAccessToken()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAuthorizerAccessToken 获取授权方令牌
|
|
|
|
|
func (app *App) GetAuthorizerAccessToken() string {
|
|
|
|
|
if app.redis.Db == nil {
|
|
|
|
|
return app.authorizerAccessToken
|
|
|
|
|
func (c *Client) GetAuthorizerAccessToken() string {
|
|
|
|
|
if c.config.RedisClient.Db == nil {
|
|
|
|
|
return c.config.AuthorizerAccessToken
|
|
|
|
|
}
|
|
|
|
|
result, _ := app.redis.Db.Get(context.Background(), app.getAuthorizerAccessTokenCacheKeyName()).Result()
|
|
|
|
|
result, _ := c.config.RedisClient.Db.Get(context.Background(), c.getAuthorizerAccessTokenCacheKeyName()).Result()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MonitorAuthorizerAccessToken 监控授权方令牌
|
|
|
|
|
func (app *App) MonitorAuthorizerAccessToken(authorizerRefreshToken string) string {
|
|
|
|
|
func (c *Client) MonitorAuthorizerAccessToken(authorizerRefreshToken string) string {
|
|
|
|
|
// 查询
|
|
|
|
|
authorizerAccessToken := app.GetAuthorizerAccessToken()
|
|
|
|
|
authorizerAccessToken := c.GetAuthorizerAccessToken()
|
|
|
|
|
// 判断
|
|
|
|
|
if authorizerAccessToken != "" {
|
|
|
|
|
return authorizerAccessToken
|
|
|
|
|
}
|
|
|
|
|
// 重新获取
|
|
|
|
|
return app.SetAuthorizerAccessToken(app.CgiBinComponentApiAuthorizerToken(authorizerRefreshToken).Result.AuthorizerAccessToken)
|
|
|
|
|
return c.SetAuthorizerAccessToken(c.CgiBinComponentApiAuthorizerToken(authorizerRefreshToken).Result.AuthorizerAccessToken)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 预授权码
|
|
|
|
|
func (app *App) getPreAuthCodeCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:pre_auth_code:%v", app.componentAppId)
|
|
|
|
|
func (c *Client) getPreAuthCodeCacheKeyName() string {
|
|
|
|
|
return fmt.Sprintf("wechat_open:pre_auth_code:%v", c.config.ComponentAppId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetPreAuthCode 设置预授权码
|
|
|
|
|
func (app *App) SetPreAuthCode(preAuthCode string) string {
|
|
|
|
|
func (c *Client) SetPreAuthCode(preAuthCode string) string {
|
|
|
|
|
if preAuthCode == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
app.redis.Db.Set(context.Background(), app.getPreAuthCodeCacheKeyName(), preAuthCode, time.Second*1700)
|
|
|
|
|
return app.GetComponentAccessToken()
|
|
|
|
|
c.config.RedisClient.Db.Set(context.Background(), c.getPreAuthCodeCacheKeyName(), preAuthCode, time.Second*1700)
|
|
|
|
|
return c.GetComponentAccessToken()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPreAuthCode 获取预授权码
|
|
|
|
|
func (app *App) GetPreAuthCode() string {
|
|
|
|
|
if app.redis.Db == nil {
|
|
|
|
|
return app.authorizerAccessToken
|
|
|
|
|
func (c *Client) GetPreAuthCode() string {
|
|
|
|
|
if c.config.RedisClient.Db == nil {
|
|
|
|
|
return c.config.AuthorizerAccessToken
|
|
|
|
|
}
|
|
|
|
|
result, _ := app.redis.Db.Get(context.Background(), app.getPreAuthCodeCacheKeyName()).Result()
|
|
|
|
|
result, _ := c.config.RedisClient.Db.Get(context.Background(), c.getPreAuthCodeCacheKeyName()).Result()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DelPreAuthCode 删除预授权码
|
|
|
|
|
func (app *App) DelPreAuthCode() error {
|
|
|
|
|
return app.redis.Db.Del(context.Background(), app.getPreAuthCodeCacheKeyName()).Err()
|
|
|
|
|
func (c *Client) DelPreAuthCode() error {
|
|
|
|
|
return c.config.RedisClient.Db.Del(context.Background(), c.getPreAuthCodeCacheKeyName()).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MonitorPreAuthCode 监控预授权码
|
|
|
|
|
func (app *App) MonitorPreAuthCode() string {
|
|
|
|
|
func (c *Client) MonitorPreAuthCode() string {
|
|
|
|
|
// 查询
|
|
|
|
|
preAuthCode := app.GetPreAuthCode()
|
|
|
|
|
preAuthCode := c.GetPreAuthCode()
|
|
|
|
|
// 判断
|
|
|
|
|
if preAuthCode != "" {
|
|
|
|
|
return preAuthCode
|
|
|
|
|
}
|
|
|
|
|
// 重新获取
|
|
|
|
|
return app.SetPreAuthCode(app.CgiBinComponentApiCreatePreAuthCoden().Result.PreAuthCode)
|
|
|
|
|
return c.SetPreAuthCode(c.CgiBinComponentApiCreatePreAuthCoden().Result.PreAuthCode)
|
|
|
|
|
}
|
|
|
|
|