wechat

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2022 License: MIT Imports: 3 Imported by: 0

README

wechat SDK.

license version golang

项目介绍

wechat包是一个使用golang开发的微信开放平台sdk。针对微信开放平台(公众号、小程序、企业微信、开放平台等)提供的接口进行了统一的封装, 方便使用。目前该项目处于维护中,欢迎pr。

快速开始

安装并引入wechat包
安装
$ go get -u github.com/fbbyqsyea/wechat
引入
import "github.com/fbbyqsyea/wechat"
简单使用
    // 直接使用包名称调用函数
    resp, err := wechat.GetAccessToken(appid, secret)
    if err != nil {
        panic(err)
    }
    fmt.Println("response: ", resp)
    accessToken = resp.AccessToken

    // 使用wechat Client实例调用
    wc := wechat.NewClient(wechat.Config{
        OfficialAccountAccessToken: accessToken,
    })
    resp, err := wc.GetApiDomainIp()
    if err != nil {
        panic(err)
    }
    fmt.Println("response: ", resp)

// rowspan="9"

接口列表

组别 接口 函数 是否已实现
接口令牌 微信接口调用凭证】令牌 GetAccessToken
微信IP 微信 API 接口 IP地址 GetApiDomainIp
微信callback IP地址 GetCallbackIp
网络检测 回调地址网络信息(dns、ping) GetNetworkDetection
OpenApi管理 清空接口的每日调用接口次数 ClearQuota
查询接口的每日调用接口的额度以及调用次数 GetQuota
查询调用接口报错返回的rid详情信息 ParseRid
公众号菜单 创建公众号菜单 CreateMenu
获取公众号当前菜单信息 GetMenu
删除公众号菜单 DelMenu
用户 创建标签 AddTag
获取公众号已创建的标签 GetMenu
编辑标签信息 UpdateTag
删除标签 DelTag
获取标签下粉丝列表 GetTagUsers
批量为用户打标签 BatchTagging
批量为用户取消标签 BatchUnTagging
获取用户身上的标签列表 GetUserTags
用户基本信息 GetUserInfo
批量获取用户基本信息 BatchGetUserInfo
帐号的关注者列表 GetUserList
公众号的黑名单列表 GetBlackList
批量拉黑用户 BatchBlackList
批量取消拉黑用户 BatchUnBlackList
账户 生成公众号二维码 CreateQrCode
生成短key GenShorten
根据短key还原数据 FetchShorten

单元测试

httpx工具包单元测试

httpx 工具包是用来发送http请求的简单类库。使用如下命令测试httpx工具包是否正常工作。

$  go test github.com/fbbyqsyea/wechat/utils/httpx -test.v
=== RUN   TestGet
    httpx_test.go:27: &{map[] { gzip  httpbin.org Go-http-client/1.1} 218.79.138.235 http://httpbin.org/get}
--- PASS: TestGet (2.78s)
=== RUN   TestPost
    httpx_test.go:52: &{map[request:post] { gzip  httpbin.org Go-http-client/1.1} 218.79.138.235 http://httpbin.org/post}
--- PASS: TestPost (1.54s)
PASS
ok      github.com/fbbyqsyea/wechat/utils/httpx 4.327s
wechat包单元测试
$ go test github.com/fbbyqsyea/wechat/ -test.v -args appid secret
=== RUN   TestGetAccessToken
    wechat_test.go:18: xxxx xxxxx
    wechat_test.go:23: TestGetAccessToken:  &{{0 } 57_zMWU3Gp3kScrY9sczPs1s0mrpFPleQEW0pZJ9wM-8_QaHPd3xMJiBxiax1KqSs_1pMheWB1is9PuS8k-rKU6-lIyXsjy1fQnmt7DAyEH0hnh9ZZ2vZyWuAfHVW9u99SOnW8U6bxuhFR9IzBLMJFaAGACZV 7200}
--- PASS: TestGetAccessToken (0.18s)
=== RUN   TestClient
    wechat_test.go:35: TestClient:  &{{0 } [101.89.47.18 101.91.34.103 109.244.129.223 109.244.145.152 109.244.184.250 112.60.20.154 116.128.170.42 116.128.184.169 117.144.228.18 117.144.228.62 119.147.6.237 120.232.65.161 157.148.36.94 157.255.218.109 183.2.143.222 203.205.239.82 203.205.239.94 81.69.216.43]}
--- PASS: TestClient (0.07s)
PASS
ok      github.com/fbbyqsyea/wechat     0.255s
offiaccount包单元测试
$ go test github.com/fbbyqsyea/wechat/offiaccount -test.v -args accesstoken
=== RUN   TestGetApiDomainIp
    offiaccount_test.go:17: TestGetApiDomainIp:  &{{42001 access_token expired rid: 62906d95-02aa1074-1284afe2} []}
--- PASS: TestGetApiDomainIp (0.07s)
=== RUN   TestGetCallbackIp
    offiaccount_test.go:25: TestGetCallbackIp:  &{{42001 access_token expired rid: 62906d95-6cf7a965-4c7fa659} []}
--- PASS: TestGetCallbackIp (0.02s)
=== RUN   TestGetNetworkDetection
    offiaccount_test.go:33: &{{42001 access_token expired rid: 62906d95-0990b6cd-5cc9c777} [] []}
--- PASS: TestGetNetworkDetection (0.01s)
=== RUN   TestGetQuota
    offiaccount_test.go:49: TestGetQuota:  &{{42001 access_token expired rid: 62906d95-69ba6c5d-7389e885} {0 0 0}}
--- PASS: TestGetQuota (0.03s)
=== RUN   TestGetMenu
    offiaccount_test.go:57: TestGetMenu:  &{{42001 access_token expired rid: 62906d95-5edaa2c1-4c4d16d1} 0 {[]}}
--- PASS: TestGetMenu (0.02s)
...........................
PASS
ok      github.com/fbbyqsyea/wechat/offiaccount 0.529s

Documentation

Index

Constants

View Source
const (
	// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
	GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
)

Variables

This section is empty.

Functions

func AddTag added in v0.0.4

func AddTag(accessToken, tagName string) (*offiaccount.TagResponse, error)

创建标签 一个公众号,最多可以创建100个标签。

accessToken 接口调用凭证、令牌

tagName 标签名称

func BatchBlackList added in v0.0.4

func BatchBlackList(accessToken string, openIdList []string) (*offiaccount.Response, error)

批量拉黑用户

accessToken 接口调用凭证、令牌

openIdList 需要拉黑的openid 一次只能拉黑20个用户

func BatchGetUserInfo added in v0.0.4

func BatchGetUserInfo(accessToken string, openIds []string) (*offiaccount.UserInfoListResponse, error)

批量获取用户基本信息

accessToken 接口调用凭证、令牌

openIds 微信公众号用户唯一标识 最多100个

func BatchTagging added in v0.0.4

func BatchTagging(accessToken string, tagId int64, openIdList []string) (*offiaccount.Response, error)

批量为用户打标签

accessToken 接口调用凭证、令牌

openIdList 微信用户openid列表

func BatchUnBlackList added in v0.0.4

func BatchUnBlackList(accessToken string, openIdList []string) (*offiaccount.Response, error)

批量取消拉黑用户

accessToken 接口调用凭证、令牌

openIdList 需要拉黑的openid 一次只能拉黑20个用户

func BatchUnTagging added in v0.0.4

func BatchUnTagging(accessToken string, tagId int64, openIdList []string) (*offiaccount.Response, error)

批量为用户取消标签

accessToken 接口调用凭证、令牌

tagId 标签ID

openIdList 微信用户openid列表

func ClearQuota added in v0.0.3

func ClearQuota(accessToken, appid string) (*offiaccount.Response, error)

用于清空公众号/小程序/第三方平台等接口的每日调用接口次数。

accessToken 接口调用凭证、令牌

appid 要被清空的账号的appid

func CreateMenu added in v0.0.3

func CreateMenu(accessToken string, menu map[string]interface{}) (*offiaccount.Response, error)

创建公众号菜单

accessToken 接口调用凭证、令牌

menu 需要发布的公众号菜单信息

func CreateQrCode added in v0.0.4

func CreateQrCode(accessToken string, QrType string, sceneKey interface{}, expireSeconds int64) (*offiaccount.QrCodeResponse, error)

生成公众号二维码

accessToken 接口调用凭证、令牌

QrType 二维码类型 enum(QR_SCENE, QR_STR_SCENE, QR_LIMIT_SCENE, QR_LIMIT_STR_SCENE)

sceneKey 二维码参数 整数参数请传整数 字符串参数请传字符串

expireSeconds 有效时长 永久二维码请填写0

func DelMenu added in v0.0.3

func DelMenu(accessToken string) (*offiaccount.Response, error)

删除公众号菜单(默认菜单和全部的个性化菜单)

accessToken 接口调用凭证、令牌

func DelTag added in v0.0.4

func DelTag(accessToken string, tagId int64) (*offiaccount.Response, error)

删除标签

accessToken 接口调用凭证、令牌

tagId 标签ID

func FetchShorten added in v0.0.4

func FetchShorten(accessToken, shortKey string) (*offiaccount.FetchShortenResponse, error)

根据短key还原数据

accessToken 接口调用凭证、令牌

shortKey 短key

func GenShorten added in v0.0.4

func GenShorten(accessToken, longData string, expireSeconds int64) (*offiaccount.ShortenResponse, error)

生成短key

accessToken 接口调用凭证、令牌

longData 原始数据 4k以内

expireSeconds 有效时长 永久二维码请填写0

func GetApiDomainIp

func GetApiDomainIp(accessToken string) (*offiaccount.IpAddressResponse, error)

获取微信 API 接口 IP地址

accessToken 接口调用凭证、令牌

func GetBlackList added in v0.0.4

func GetBlackList(accessToken, nextOpenId string) (*offiaccount.BlackListResponse, error)

公众号的黑名单列表

accessToken 接口调用凭证、令牌

nextOpenId 拉取列表最后一个用户的openid

func GetCallbackIp

func GetCallbackIp(accessToken string) (*offiaccount.IpAddressResponse, error)

获取微信callback IP地址

accessToken 接口调用凭证、令牌

func GetMenu added in v0.0.3

func GetMenu(accessToken string) (*offiaccount.MenuResponse, error)

获取公众号当前菜单信息

accessToken 接口调用凭证、令牌

func GetNetworkDetection

func GetNetworkDetection(accessToken, action, checkOperator string) (*offiaccount.NetworkDetectionResponse, error)

获取公众号回调地址网络信息。dns、ping丢包等

accessToken 接口调用凭证、令牌

action 执行的检测动作,允许的值:dns(做域名解析)、ping(做 ping 检测)、all(dns和 ping 都做)

checkOperator 指定平台从某个运营商进行检测,允许的值:CHINANET(电信出口)、UNICOM(联通出口)、CAP(腾讯自建出口)、DEFAULT(根据 ip 来选择运营商)

func GetQuota added in v0.0.3

func GetQuota(accessToken, cgiPath string) (*offiaccount.QuotaResponse, error)

用于查询公众号/小程序/第三方平台等接口的每日调用接口的额度以及调用次数。

accessToken 接口调用凭证、令牌

cgiPath api的请求地址,例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错

func GetTagUsers added in v0.0.4

func GetTagUsers(accessToken string, tagId int64, nextOpenId string) (*offiaccount.TagUserResponse, error)

获取标签下粉丝列表

accessToken 接口调用凭证、令牌

nextOpenId 拉取列表最后一个用户的openid

func GetTags added in v0.0.4

func GetTags(accessToken string) (*offiaccount.TagsResponse, error)

获取公众号已创建的标签

accessToken 接口调用凭证、令牌

func GetUserInfo added in v0.0.4

func GetUserInfo(accessToken, openId string) (*offiaccount.UserInfoResponse, error)

用户基本信息

accessToken 接口调用凭证、令牌

openId 微信公众号用户唯一标识

func GetUserList added in v0.0.4

func GetUserList(accessToken, nextOpenId string) (*offiaccount.UserListResponse, error)

帐号的关注者列表

accessToken 接口调用凭证、令牌

nextOpenId 拉取列表最后一个用户的openid

func GetUserTags added in v0.0.4

func GetUserTags(accessToken, openId string) (*offiaccount.UserTagsResponse, error)

获取用户身上的标签列表

accessToken 接口调用凭证、令牌

openId 微信公众号用户唯一标识

func ParseRid added in v0.0.3

func ParseRid(accessToken, rid string) (*offiaccount.RidResponse, error)

本接口用于查询调用公众号/小程序/第三方平台等接口报错返回的 rid 详情信息,辅助开发者高效定位问题。

accessToken 接口调用凭证、令牌

rid 接口异常返回的rid参数

func UpdateTag added in v0.0.4

func UpdateTag(accessToken string, tagId int64, tagName string) (*offiaccount.Response, error)

编辑标签信息

accessToken 接口调用凭证、令牌

tagId 标签ID

tagName 标签名称

Types

type AccessTokenResponse added in v0.0.3

type AccessTokenResponse struct {
	offiaccount.Response
	AccessToken string `json:"access_token"` // 接口调用凭证、令牌
	ExpiresIn   int64  `json:"expires_in"`   // 凭证有效时间,单位:秒
}

func GetAccessToken

func GetAccessToken(appid, secret string) (*AccessTokenResponse, error)

获取公众号/小程序的接口调用凭证

appid 公众号唯一appid

secret 第三方用户唯一凭证密钥,即appsecret

type Client added in v0.0.4

type Client struct {
	*offiaccount.OffiAccountAccess
}

微信客户端 内嵌offiaccount 可以使用此实例调用offiaccount的所有method。

func NewClient added in v0.0.4

func NewClient(cfg Config) *Client

微信客户端实例

type Config added in v0.0.4

type Config struct {
	OfficialAccountAccessToken string // 微信公众号接口访问令牌
}

微信配置信息

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL