From a3ce22df79d6bdadf63d0aef04241ae0907a804b Mon Sep 17 00:00:00 2001 From: silenceper Date: Sun, 24 May 2020 01:41:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=9C=8D=E5=8A=A1=E5=99=A8IP=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++-- cache/redis.go | 2 +- officialaccount/basic/basic.go | 52 +++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ef55760..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,6 +61,8 @@ server.Send() - 提交issue,描述需要贡献的内容 - 完成更改后,提交PR +## 公众号 +![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 +}