Jelajahi Sumber

添加获取微信服务器IP接口

silenceper 6 tahun lalu
induk
melakukan
a3ce22df79
3 mengubah file dengan 56 tambahan dan 4 penghapusan
  1. 4 2
      README.md
  2. 1 1
      cache/redis.go
  3. 51 1
      officialaccount/basic/basic.go

+ 4 - 2
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
 

+ 1 - 1
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 实例化

+ 51 - 1
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
+}