From 2e94f6104325d6c1c8c0251473cf8ffd918d57b6 Mon Sep 17 00:00:00 2001 From: silenceper Date: Sun, 24 May 2020 01:43:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++--- cache/redis.go | 2 +- officialaccount/basic/basic.go | 52 +++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5e0d1f9..2e60a6d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # WeChat SDK for Go -[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=release-2.0)](https://travis-ci.org/silenceper/wechat) +![Go](https://github.com/silenceper/wechat/workflows/Go/badge.svg?branch=release-2.0) [![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat) -[![GoDoc](http://godoc.org/github.com/silenceper/wechat?status.svg)](http://godoc.org/github.com/silenceper/wechat) +[![GoDoc](http://godoc.org/github.com/silenceper/wechat?status.svg)](https://pkg.go.dev/github.com/silenceper/wechat/v2?tab=doc) 使用Golang开发的微信SDK,简单、易用。 @@ -61,9 +61,8 @@ server.Send() - 提交issue,描述需要贡献的内容 - 完成更改后,提交PR -## 关注公众号 - -![学点程序](https://silenceper.oss-cn-beijing.aliyuncs.com/qrcode/search_study_program.png) +## 公众号 +![img](https://silenceper.oss-cn-beijing.aliyuncs.com/qrcode/search_study_program.png) ## License diff --git a/cache/redis.go b/cache/redis.go index 7440b9e..bf422f0 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -19,7 +19,7 @@ type RedisOpts struct { Database int `yml:"database" json:"database"` MaxIdle int `yml:"max_idle" json:"max_idle"` MaxActive int `yml:"max_active" json:"max_active"` - IdleTimeout int32 `yml:"idle_timeout" json:"idle_timeout"` //second + IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` //second } //NewRedis 实例化 diff --git a/officialaccount/basic/basic.go b/officialaccount/basic/basic.go index dae392b..52296b0 100644 --- a/officialaccount/basic/basic.go +++ b/officialaccount/basic/basic.go @@ -1,6 +1,18 @@ package basic -import "github.com/silenceper/wechat/v2/officialaccount/context" +import ( + "fmt" + + "github.com/silenceper/wechat/v2/officialaccount/context" + "github.com/silenceper/wechat/v2/util" +) + +var ( + //获取微信服务器IP地址 + //文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_the_WeChat_server_IP_address.html + getCallbackIPURL = "https://api.weixin.qq.com/cgi-bin/getcallbackip" + getAPIDomainIPURL = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip" +) //Basic struct type Basic struct { @@ -13,3 +25,41 @@ func NewBasic(context *context.Context) *Basic { basic.Context = context return basic } + +//IPListRes 获取微信服务器IP地址 返回结果 +type IPListRes struct { + util.CommonError + IPList []string `json:"ip_list"` +} + +//GetCallbackIP 获取微信callback IP地址 +func (basic *Basic) GetCallbackIP() ([]string, error) { + ak, err := basic.GetAccessToken() + if err != nil { + return nil, err + } + url := fmt.Sprintf("%s?access_token=%s", getCallbackIPURL, ak) + data, err := util.HTTPGet(url) + if err != nil { + return nil, err + } + ipListRes := &IPListRes{} + err = util.DecodeWithError(data, ipListRes, "GetCallbackIP") + return ipListRes.IPList, err +} + +//GetAPIDomainIP 获取微信API接口 IP地址 +func (basic *Basic) GetAPIDomainIP() ([]string, error) { + ak, err := basic.GetAccessToken() + if err != nil { + return nil, err + } + url := fmt.Sprintf("%s?access_token=%s", getAPIDomainIPURL, ak) + data, err := util.HTTPGet(url) + if err != nil { + return nil, err + } + ipListRes := &IPListRes{} + err = util.DecodeWithError(data, ipListRes, "GetAPIDomainIP") + return ipListRes.IPList, err +}