mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 21:02:25 +08:00
Compare commits
96 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7fdcf9534 | ||
|
|
05907d152e | ||
|
|
398f2ec6ae | ||
|
|
e8fb058740 | ||
|
|
d5a67eaf29 | ||
|
|
e5f0d5eab7 | ||
|
|
2eae660002 | ||
|
|
c0da806e03 | ||
|
|
185baa5d12 | ||
|
|
bf42c188cb | ||
|
|
53b0f26688 | ||
|
|
430277c947 | ||
|
|
71e3ddaab3 | ||
|
|
0505969439 | ||
|
|
3014901b48 | ||
|
|
2e191c0a44 | ||
|
|
4c35924b8c | ||
|
|
ed508654a1 | ||
|
|
a18fe5b58a | ||
|
|
a6eb2eedc3 | ||
|
|
404d522d09 | ||
|
|
6f2bd492fc | ||
|
|
811d71c35b | ||
|
|
1fe872be66 | ||
|
|
ff38e6bac0 | ||
|
|
c956f416a5 | ||
|
|
5fea6d5965 | ||
|
|
a8763dd6c9 | ||
|
|
df9889d982 | ||
|
|
7fe7e21cb2 | ||
|
|
a5c6eb300d | ||
|
|
868b31cb3d | ||
|
|
b5c70cc206 | ||
|
|
69d0b94fdf | ||
|
|
c14c020a3c | ||
|
|
fe31f04640 | ||
|
|
2ccc15b050 | ||
|
|
3d7d60644f | ||
|
|
5d9705ddc8 | ||
|
|
63f2226974 | ||
|
|
3a1221e7ed | ||
|
|
6c06c05233 | ||
|
|
2af3f42055 | ||
|
|
c4cb394d80 | ||
|
|
681e86e3d4 | ||
|
|
6fe797765f | ||
|
|
74e965b207 | ||
|
|
0e23fa3fee | ||
|
|
351cf65621 | ||
|
|
f3e3564178 | ||
|
|
5e8e16444c | ||
|
|
880ab20a6b | ||
|
|
66369c9541 | ||
|
|
58092a21de | ||
|
|
01f83c20d5 | ||
|
|
972dec0406 | ||
|
|
b599e93c5b | ||
|
|
f77ee8dd2e | ||
|
|
c89d24990c | ||
|
|
e6a61a5ec7 | ||
|
|
4326efae54 | ||
|
|
5d3f4be9a2 | ||
|
|
9b5bb79a72 | ||
|
|
a4a388aef7 | ||
|
|
7a4230414d | ||
|
|
239fe1d758 | ||
|
|
3f7690cf24 | ||
|
|
2e94f61043 | ||
|
|
a3ce22df79 | ||
|
|
f9a2c15361 | ||
|
|
3360ee7752 | ||
|
|
9c0cde64e5 | ||
|
|
748fe17e72 | ||
|
|
340e1a3c5b | ||
|
|
165d8f4d96 | ||
|
|
5557975960 | ||
|
|
7842eed32f | ||
|
|
d69578bedc | ||
|
|
fad1b1839b | ||
|
|
a8abc23893 | ||
|
|
c372d7a83c | ||
|
|
85b0a114dc | ||
|
|
ea1b2d92d0 | ||
|
|
d91d1dd630 | ||
|
|
b147370c1d | ||
|
|
293d47c6fb | ||
|
|
aa75de8144 | ||
|
|
2423b58855 | ||
|
|
2c7d3aaf3e | ||
|
|
ebf85640f7 | ||
|
|
b6bba6ab66 | ||
|
|
50588e3d93 | ||
|
|
9084163132 | ||
|
|
51cbfab087 | ||
|
|
7e8c94a6c4 | ||
|
|
15ebd71a04 |
44
.github/workflows/go.yml
vendored
Normal file
44
.github/workflows/go.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
name: Go
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master,release-* ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master,release-* ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
golangci:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [1.15.x]
|
||||||
|
name: golangci-lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v2
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.31
|
||||||
|
build:
|
||||||
|
name: Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
services:
|
||||||
|
redis:
|
||||||
|
image: redis
|
||||||
|
ports:
|
||||||
|
- 6379:6379
|
||||||
|
options: --entrypoint redis-server
|
||||||
|
memcached:
|
||||||
|
image: memcached
|
||||||
|
ports:
|
||||||
|
- 11211:11211
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ^1.13
|
||||||
|
id: go
|
||||||
|
- name: Test
|
||||||
|
run: go test -v -race ./...
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -26,4 +26,4 @@ _testmain.go
|
|||||||
.vscode/
|
.vscode/
|
||||||
vendor
|
vendor
|
||||||
.idea/
|
.idea/
|
||||||
examples/tcb/*
|
example/*
|
||||||
61
.golangci.yml
Normal file
61
.golangci.yml
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
linters:
|
||||||
|
# please, do not use `enable-all`: it's deprecated and will be removed soon.
|
||||||
|
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- bodyclose
|
||||||
|
- deadcode
|
||||||
|
- depguard
|
||||||
|
- dogsled
|
||||||
|
- dupl
|
||||||
|
- errcheck
|
||||||
|
- funlen
|
||||||
|
- goconst
|
||||||
|
# - gocritic
|
||||||
|
- gocyclo
|
||||||
|
- gofmt
|
||||||
|
- goimports
|
||||||
|
- golint
|
||||||
|
- goprintffuncname
|
||||||
|
- gosimple
|
||||||
|
- govet
|
||||||
|
- ineffassign
|
||||||
|
- interfacer
|
||||||
|
- misspell
|
||||||
|
- nolintlint
|
||||||
|
- rowserrcheck
|
||||||
|
- scopelint
|
||||||
|
- staticcheck
|
||||||
|
- structcheck
|
||||||
|
- stylecheck
|
||||||
|
- typecheck
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
- unused
|
||||||
|
- varcheck
|
||||||
|
- whitespace
|
||||||
|
|
||||||
|
issues:
|
||||||
|
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||||
|
exclude-rules:
|
||||||
|
- path: _test\.go
|
||||||
|
linters:
|
||||||
|
- gomnd
|
||||||
|
|
||||||
|
# https://github.com/go-critic/go-critic/issues/926
|
||||||
|
- linters:
|
||||||
|
- gocritic
|
||||||
|
text: "unnecessaryDefer:"
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
funlen:
|
||||||
|
lines: 66
|
||||||
|
statements: 40
|
||||||
|
|
||||||
|
issues:
|
||||||
|
include:
|
||||||
|
- EXC0002 # disable excluding of issues about comments from golint
|
||||||
|
exclude-rules:
|
||||||
|
- linters:
|
||||||
|
- stylecheck
|
||||||
|
text: "ST1000:"
|
||||||
20
.travis.yml
20
.travis.yml
@@ -1,20 +0,0 @@
|
|||||||
language: go
|
|
||||||
|
|
||||||
go:
|
|
||||||
- 1.13.x
|
|
||||||
- 1.12.x
|
|
||||||
- 1.11.x
|
|
||||||
- 1.10.x
|
|
||||||
|
|
||||||
services:
|
|
||||||
- memcached
|
|
||||||
- redis-server
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/)
|
|
||||||
- go get golang.org/x/lint/golint
|
|
||||||
|
|
||||||
script:
|
|
||||||
- go test -v -race ./...
|
|
||||||
- go vet ./...
|
|
||||||
- golint -set_exit_status $(go list ./...)
|
|
||||||
671
README.md
671
README.md
@@ -1,646 +1,73 @@
|
|||||||
# WeChat SDK for Go
|
# WeChat SDK for Go
|
||||||
[](https://travis-ci.org/silenceper/wechat)
|

|
||||||
[](https://goreportcard.com/report/github.com/silenceper/wechat)
|
[](https://goreportcard.com/report/github.com/silenceper/wechat)
|
||||||
[](http://godoc.org/github.com/silenceper/wechat)
|
[](https://pkg.go.dev/github.com/silenceper/wechat/v2?tab=doc)
|
||||||
|
|
||||||
使用Golang开发的微信SDK,简单、易用。
|
使用Golang开发的微信SDK,简单、易用。
|
||||||
|
>当前版本为v2版本
|
||||||
|
|
||||||
|
|
||||||
|
## 文档 && 例子
|
||||||
|
[Wechat SDK 2.0 文档](http://silenceper.com/wechat)
|
||||||
|
|
||||||
|
[Wechat SDK 2.0 例子](https://github.com/gowechat/example)
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
```
|
||||||
|
import "github.com/silenceper/wechat/v2"
|
||||||
|
```
|
||||||
|
|
||||||
以下是一个处理消息接收以及回复的例子:
|
以下是一个微信公众号处理消息接收以及回复的例子:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//使用memcache保存access_token,也可选择redis或自定义cache
|
//使用memcache保存access_token,也可选择redis或自定义cache
|
||||||
memCache=cache.NewMemcache("127.0.0.1:11211")
|
wc := wechat.NewWechat()
|
||||||
|
memory := cache.NewMemory()
|
||||||
//配置微信参数
|
cfg := &offConfig.Config{
|
||||||
config := &wechat.Config{
|
AppID: "xxx",
|
||||||
AppID: "xxxx",
|
AppSecret: "xxx",
|
||||||
AppSecret: "xxxx",
|
Token: "xxx",
|
||||||
Token: "xxxx",
|
//EncodingAESKey: "xxxx",
|
||||||
EncodingAESKey: "xxxx",
|
Cache: memory,
|
||||||
Cache: memCache
|
|
||||||
}
|
}
|
||||||
wc := wechat.NewWechat(config)
|
officialAccount := wc.GetOfficialAccount(cfg)
|
||||||
|
|
||||||
// 传入request和responseWriter
|
// 传入request和responseWriter
|
||||||
server := wc.GetServer(request, responseWriter)
|
server := officialAccount.GetServer(req, rw)
|
||||||
|
//设置接收消息的处理方法
|
||||||
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
||||||
|
|
||||||
//回复消息:演示回复用户发送的消息
|
//回复消息:演示回复用户发送的消息
|
||||||
text := message.NewText(msg.Content)
|
text := message.NewText(msg.Content)
|
||||||
return &message.Reply{message.MsgTypeText, text}
|
return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
||||||
})
|
})
|
||||||
|
|
||||||
server.Serve()
|
//处理消息接收以及回复
|
||||||
|
err := server.Serve()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//发送回复的消息
|
||||||
server.Send()
|
server.Send()
|
||||||
|
|
||||||
```
|
```
|
||||||
完整代码:[examples/http/http.go](./examples/http/http.go)
|
|
||||||
|
|
||||||
#### 和主流框架配合使用
|
## 目录说明
|
||||||
|
- officialaccount: 微信公众号API
|
||||||
主要是request和responseWriter在不同框架中获取方式可能不一样:
|
- miniprogram: 小程序API
|
||||||
|
- minigame:小游戏API
|
||||||
- Beego: [./examples/beego/beego.go](./examples/beego/beego.go)
|
- pay:微信支付API
|
||||||
- Gin Framework: [./examples/gin/gin.go](./examples/gin/gin.go)
|
- openplatform:开放平台API
|
||||||
|
- work:企业微信
|
||||||
## 交流群:
|
- aispeech:智能对话
|
||||||

|
|
||||||
>关注公众号并回复“入群”
|
## 贡献
|
||||||
|
- 提交issue,描述需要贡献的内容
|
||||||
## 基本配置
|
- 完成更改后,提交PR
|
||||||
|
|
||||||
```go
|
## 公众号
|
||||||
memcache := cache.NewMemcache("127.0.0.1:11211")
|

|
||||||
|
|
||||||
wcConfig := &wechat.Config{
|
|
||||||
AppID: cfg.AppID,
|
|
||||||
AppSecret: cfg.AppSecret,
|
|
||||||
Token: cfg.Token,
|
|
||||||
EncodingAESKey: cfg.EncodingAESKey,//消息加解密时用到
|
|
||||||
Cache: memcache,
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Cache 设置**
|
|
||||||
|
|
||||||
Cache主要用来保存全局access_token以及js-sdk中的ticket:
|
|
||||||
默认采用memcache存储。当然也可以直接实现`cache/cache.go`中的接口
|
|
||||||
|
|
||||||
|
|
||||||
## 基本API使用
|
|
||||||
|
|
||||||
- [消息管理](#消息管理)
|
|
||||||
- 接收普通消息
|
|
||||||
- 接收事件推送
|
|
||||||
- 被动回复消息
|
|
||||||
- 回复文本消息
|
|
||||||
- 回复图片消息
|
|
||||||
- 回复视频消息
|
|
||||||
- 回复音乐消息
|
|
||||||
- 回复图文消息
|
|
||||||
- [自定义菜单](#自定义菜单)
|
|
||||||
- 自定义菜单创建接口
|
|
||||||
- 自定义菜单查询接口
|
|
||||||
- 自定义菜单删除接口
|
|
||||||
- 自定义菜单事件推送
|
|
||||||
- 个性化菜单接口
|
|
||||||
- 添加个性化菜单
|
|
||||||
- 删除个性化菜单
|
|
||||||
- 测试个性化菜单匹配结果
|
|
||||||
- 获取公众号菜单配置
|
|
||||||
- [微信网页开发](#微信网页开发)
|
|
||||||
- Oauth2 授权
|
|
||||||
- 发起授权
|
|
||||||
- 通过code换取access_token
|
|
||||||
- 拉取用户信息
|
|
||||||
- 刷新access_token
|
|
||||||
- 检验access_token是否有效
|
|
||||||
- 获取js-sdk配置
|
|
||||||
- [素材管理](#素材管理)
|
|
||||||
- [小程序开发](#小程序开发)
|
|
||||||
- [小程序-云开发](./tcb)
|
|
||||||
|
|
||||||
## 消息管理
|
|
||||||
|
|
||||||
通过`wechat.GetServer(request,responseWriter)`获取到server对象之后
|
|
||||||
|
|
||||||
调用`SetMessageHandler(func(msg message.MixMessage){})`设置消息的处理函数,函数参数为message.MixMessage 结构如下:
|
|
||||||
|
|
||||||
```go
|
|
||||||
//MixMessage 存放所有微信发送过来的消息和事件
|
|
||||||
type MixMessage struct {
|
|
||||||
CommonToken
|
|
||||||
|
|
||||||
//基本消息
|
|
||||||
MsgID int64 `xml:"MsgId"`
|
|
||||||
Content string `xml:"Content"`
|
|
||||||
PicURL string `xml:"PicUrl"`
|
|
||||||
MediaID string `xml:"MediaId"`
|
|
||||||
Format string `xml:"Format"`
|
|
||||||
ThumbMediaID string `xml:"ThumbMediaId"`
|
|
||||||
LocationX float64 `xml:"Location_X"`
|
|
||||||
LocationY float64 `xml:"Location_Y"`
|
|
||||||
Scale float64 `xml:"Scale"`
|
|
||||||
Label string `xml:"Label"`
|
|
||||||
Title string `xml:"Title"`
|
|
||||||
Description string `xml:"Description"`
|
|
||||||
URL string `xml:"Url"`
|
|
||||||
|
|
||||||
//事件相关
|
|
||||||
Event string `xml:"Event"`
|
|
||||||
EventKey string `xml:"EventKey"`
|
|
||||||
Ticket string `xml:"Ticket"`
|
|
||||||
Latitude string `xml:"Latitude"`
|
|
||||||
Longitude string `xml:"Longitude"`
|
|
||||||
Precision string `xml:"Precision"`
|
|
||||||
|
|
||||||
MenuID string `xml:"MenuId"`
|
|
||||||
|
|
||||||
//扫码事件
|
|
||||||
ScanCodeInfo struct {
|
|
||||||
ScanType string `xml:"ScanType"`
|
|
||||||
ScanResult string `xml:"ScanResult"`
|
|
||||||
} `xml:"ScanCodeInfo"`
|
|
||||||
|
|
||||||
//发图事件
|
|
||||||
SendPicsInfo struct {
|
|
||||||
Count int32 `xml:"Count"`
|
|
||||||
PicList []EventPic `xml:"PicList>item"`
|
|
||||||
} `xml:"SendPicsInfo"`
|
|
||||||
|
|
||||||
//发送地理位置事件
|
|
||||||
SendLocationInfo struct {
|
|
||||||
LocationX float64 `xml:"Location_X"`
|
|
||||||
LocationY float64 `xml:"Location_Y"`
|
|
||||||
Scale float64 `xml:"Scale"`
|
|
||||||
Label string `xml:"Label"`
|
|
||||||
Poiname string `xml:"Poiname"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
具体参数请参考微信文档:[接收普通消息
|
|
||||||
](http://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html)
|
|
||||||
|
|
||||||
### 接收普通消息
|
|
||||||
```go
|
|
||||||
server.SetMessageHandler(func(v message.MixMessage) *message.Reply {
|
|
||||||
switch v.MsgType {
|
|
||||||
//文本消息
|
|
||||||
case message.MsgTypeText:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//图片消息
|
|
||||||
case message.MsgTypeImage:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//语音消息
|
|
||||||
case message.MsgTypeVoice:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//视频消息
|
|
||||||
case message.MsgTypeVideo:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//小视频消息
|
|
||||||
case message.MsgTypeShortVideo:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//地理位置消息
|
|
||||||
case message.MsgTypeLocation:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//链接消息
|
|
||||||
case message.MsgTypeLink:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//事件推送消息
|
|
||||||
case message.MsgTypeEvent:
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### 接收事件推送
|
|
||||||
```go
|
|
||||||
//事件推送消息
|
|
||||||
case message.MsgTypeEvent:
|
|
||||||
switch v.Event {
|
|
||||||
//EventSubscribe 订阅
|
|
||||||
case message.EventSubscribe:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//取消订阅
|
|
||||||
case message.EventUnsubscribe:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
//用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
|
|
||||||
case message.EventScan:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 上报地理位置事件
|
|
||||||
case message.EventLocation:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 点击菜单拉取消息时的事件推送
|
|
||||||
case message.EventClick:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 点击菜单跳转链接时的事件推送
|
|
||||||
case message.EventView:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 扫码推事件的事件推送
|
|
||||||
case message.EventScancodePush:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 扫码推事件且弹出“消息接收中”提示框的事件推送
|
|
||||||
case message.EventScancodeWaitmsg:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 弹出系统拍照发图的事件推送
|
|
||||||
case message.EventPicSysphoto:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 弹出拍照或者相册发图的事件推送
|
|
||||||
case message.EventPicPhotoOrAlbum:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 弹出微信相册发图器的事件推送
|
|
||||||
case message.EventPicWeixin:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
// 弹出地理位置选择器的事件推送
|
|
||||||
case message.EventLocationSelect:
|
|
||||||
//do something
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### 被动回复消息
|
|
||||||
|
|
||||||
回复消息需要返回 `*message.Reply` 对象结构体如下:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type Reply struct {
|
|
||||||
MsgType MsgType //消息类型
|
|
||||||
MsgData interface{} //消息结构
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
注意:`return nil`表示什么也不做
|
|
||||||
|
|
||||||
#### 回复文本消息
|
|
||||||
```go
|
|
||||||
text := message.NewText("回复文本消息")
|
|
||||||
return &message.Reply{message.MsgTypeText, text}
|
|
||||||
```
|
|
||||||
#### 回复图片消息
|
|
||||||
```go
|
|
||||||
//mediaID 可通过素材管理-上上传多媒体文件获得
|
|
||||||
image :=message.NewImage("mediaID")
|
|
||||||
return &message.Reply{message.MsgTypeImage, image}
|
|
||||||
```
|
|
||||||
#### 回复视频消息
|
|
||||||
```go
|
|
||||||
video := message.NewVideo("mediaID", "视频标题", "视频描述")
|
|
||||||
return &message.Reply{message.MsgTypeVideo, video}
|
|
||||||
```
|
|
||||||
#### 回复音乐消息
|
|
||||||
```go
|
|
||||||
music := message.NewMusic("title", "description", "musicURL", "hQMusicURL", "thumbMediaID")
|
|
||||||
return &message.Reply{message.MsgTypeMusic,music}
|
|
||||||
```
|
|
||||||
**字段说明:**
|
|
||||||
|
|
||||||
Title:音乐标题
|
|
||||||
|
|
||||||
Description:音乐描述
|
|
||||||
|
|
||||||
MusicURL:音乐链接
|
|
||||||
|
|
||||||
HQMusicUrl:高质量音乐链接,WIFI环境优先使用该链接播放音乐
|
|
||||||
|
|
||||||
ThumbMediaId:缩略图的媒体id,通过素材管理接口上传多媒体文件,得到的id
|
|
||||||
|
|
||||||
#### 回复图文消息
|
|
||||||
|
|
||||||
```go
|
|
||||||
articles := make([]*message.Article, 1)
|
|
||||||
|
|
||||||
article := new(message.Article)
|
|
||||||
article.Title = "标题"
|
|
||||||
article.Description = "描述信息信息信息"
|
|
||||||
article.PicURL = "http://ww1.sinaimg.cn/large/65209136gw1f7vhjw95eqj20wt0zk40z.jpg"
|
|
||||||
article.URL = "https://github.com/silenceper/wechat"
|
|
||||||
articles[0] = article
|
|
||||||
|
|
||||||
news := message.NewNews(articles)
|
|
||||||
return &message.Reply{message.MsgTypeNews,news}
|
|
||||||
```
|
|
||||||
**字段说明:**
|
|
||||||
|
|
||||||
Title:图文消息标题
|
|
||||||
|
|
||||||
Description:图文消息描述
|
|
||||||
|
|
||||||
PicUrl :图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
|
|
||||||
|
|
||||||
Url :点击图文消息跳转链接
|
|
||||||
|
|
||||||
|
|
||||||
## 自定义菜单
|
|
||||||
|
|
||||||
通过` wechat.GetMenu()`获取menu的实例
|
|
||||||
|
|
||||||
### 自定义菜单创建接口
|
|
||||||
|
|
||||||
以下是一个创建二级菜单的例子
|
|
||||||
|
|
||||||
```go
|
|
||||||
mu := wc.GetMenu()
|
|
||||||
|
|
||||||
buttons := make([]*menu.Button, 1)
|
|
||||||
btn := new(menu.Button)
|
|
||||||
|
|
||||||
//创建click类型菜单
|
|
||||||
btn.SetClickButton("name", "key123")
|
|
||||||
buttons[0] = btn
|
|
||||||
|
|
||||||
//设置btn为二级菜单
|
|
||||||
btn2 := new(menu.Button)
|
|
||||||
btn2.SetSubButton("subButton", buttons)
|
|
||||||
|
|
||||||
buttons2 := make([]*menu.Button, 1)
|
|
||||||
buttons2[0] = btn2
|
|
||||||
|
|
||||||
//发送请求
|
|
||||||
err := mu.SetMenu(buttons2)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("err= %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
**创建其他类型的菜单:**
|
|
||||||
|
|
||||||
```go
|
|
||||||
//SetViewButton view类型
|
|
||||||
func (btn *Button) SetViewButton(name, url string)
|
|
||||||
|
|
||||||
// SetScanCodePushButton 扫码推事件
|
|
||||||
func (btn *Button) SetScanCodePushButton(name, key string)
|
|
||||||
|
|
||||||
//SetScanCodeWaitMsgButton 设置 扫码推事件且弹出"消息接收中"提示框
|
|
||||||
func (btn *Button) SetScanCodeWaitMsgButton(name, key string)
|
|
||||||
|
|
||||||
//SetPicSysPhotoButton 设置弹出系统拍照发图按钮
|
|
||||||
func (btn *Button) SetPicSysPhotoButton(name, key string)
|
|
||||||
|
|
||||||
//SetPicPhotoOrAlbumButton 设置弹出拍照或者相册发图类型按钮
|
|
||||||
func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) {
|
|
||||||
|
|
||||||
// SetPicWeixinButton 设置弹出微信相册发图器类型按钮
|
|
||||||
func (btn *Button) SetPicWeixinButton(name, key string)
|
|
||||||
|
|
||||||
// SetLocationSelectButton 设置 弹出地理位置选择器 类型按钮
|
|
||||||
func (btn *Button) SetLocationSelectButton(name, key string)
|
|
||||||
|
|
||||||
//SetMediaIDButton 设置 下发消息(除文本消息) 类型按钮
|
|
||||||
func (btn *Button) SetMediaIDButton(name, mediaID string)
|
|
||||||
|
|
||||||
//SetViewLimitedButton 设置 跳转图文消息URL 类型按钮
|
|
||||||
func (btn *Button) SetViewLimitedButton(name, mediaID string) {
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### 自定义菜单查询接口
|
|
||||||
|
|
||||||
```go
|
|
||||||
mu := wc.GetMenu()
|
|
||||||
resMenu,err:=mu.GetMenu()
|
|
||||||
```
|
|
||||||
>返回结果 resMenu 结构参考 ./menu/menu.go 中ResMenu 结构体
|
|
||||||
|
|
||||||
### 自定义菜单删除接口
|
|
||||||
|
|
||||||
```go
|
|
||||||
mu := wc.GetMenu()
|
|
||||||
err:=mu.DeleteMenu()
|
|
||||||
```
|
|
||||||
|
|
||||||
### 自定义菜单事件推送
|
|
||||||
|
|
||||||
请参考 消息管理 - 事件推送
|
|
||||||
|
|
||||||
### 个性化菜单接口
|
|
||||||
**添加个性化菜单**
|
|
||||||
|
|
||||||
```go
|
|
||||||
|
|
||||||
func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error
|
|
||||||
```
|
|
||||||
|
|
||||||
**删除个性化菜单**
|
|
||||||
|
|
||||||
```go
|
|
||||||
//删除个性化菜单
|
|
||||||
func (menu *Menu) DeleteConditional(menuID int64) error
|
|
||||||
|
|
||||||
```
|
|
||||||
**测试个性化菜单匹配结果**
|
|
||||||
|
|
||||||
```go
|
|
||||||
//菜单匹配
|
|
||||||
func (menu *Menu) MenuTryMatch(userID string) (buttons []Button, err error) {
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### 获取公众号菜单配置
|
|
||||||
|
|
||||||
```go
|
|
||||||
//获取自定义菜单配置接口
|
|
||||||
func (menu *Menu) GetCurrentSelfMenuInfo() (resSelfMenuInfo ResSelfMenuInfo, err error)
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## 微信网页开发
|
|
||||||
|
|
||||||
### Oauth2 授权
|
|
||||||
|
|
||||||
具体授权流程请参考微信文档:[网页授权](http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html)
|
|
||||||
|
|
||||||
**1.发起授权**
|
|
||||||
|
|
||||||
```go
|
|
||||||
oauth := wc.GetOauth()
|
|
||||||
err := oauth.Redirect("跳转的绝对地址", "snsapi_userinfo", "123dd123")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
> 如果不希望直接跳转,可通过 oauth.GetRedirectURL 获取跳转的url
|
|
||||||
|
|
||||||
**2.通过code换取access_token**
|
|
||||||
|
|
||||||
```go
|
|
||||||
code := c.Query("code")
|
|
||||||
resToken, err := oauth.GetUserAccessToken(code)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
**3.拉取用户信息(需scope为 snsapi_userinfo)**
|
|
||||||
|
|
||||||
```go
|
|
||||||
//getUserInfo
|
|
||||||
userInfo, err := oauth.GetUserInfo(resToken.AccessToken, resToken.OpenID)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(userInfo)
|
|
||||||
|
|
||||||
```
|
|
||||||
**刷新access_token**
|
|
||||||
|
|
||||||
```go
|
|
||||||
func (oauth *Oauth) RefreshAccessToken(refreshToken string) (result ResAccessToken, err error)
|
|
||||||
|
|
||||||
```
|
|
||||||
**检验access_token是否有效**
|
|
||||||
|
|
||||||
```go
|
|
||||||
func (oauth *Oauth) CheckAccessToken(accessToken, openID string) (b bool, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 获取js-sdk配置
|
|
||||||
|
|
||||||
```go
|
|
||||||
js := wc.GetJs()
|
|
||||||
cfg, err := js.GetConfig("传入需要的调用js-sdk的url地址")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(cfg)
|
|
||||||
```
|
|
||||||
其中返回的cfg结构体如下:
|
|
||||||
|
|
||||||
```go
|
|
||||||
type Config struct {
|
|
||||||
AppID string `json:"app_id"`
|
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
NonceStr string `json:"nonce_str"`
|
|
||||||
Signature string `json:"signature"`
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## 素材管理
|
|
||||||
|
|
||||||
[素材管理API](https://godoc.org/github.com/silenceper/wechat/material#Material)
|
|
||||||
|
|
||||||
## 小程序开发
|
|
||||||
|
|
||||||
获取小程序操作对象
|
|
||||||
|
|
||||||
``` go
|
|
||||||
memCache=cache.NewMemcache("127.0.0.1:11211")
|
|
||||||
config := &wechat.Config{
|
|
||||||
AppID: "xxx",
|
|
||||||
AppSecret: "xxx",
|
|
||||||
Cache: memCache=cache.NewMemcache("127.0.0.1:11211"),
|
|
||||||
}
|
|
||||||
wc := wechat.NewWechat(config)
|
|
||||||
|
|
||||||
wxa := wc.GetMiniProgram()
|
|
||||||
```
|
|
||||||
|
|
||||||
### 小程序登录凭证校验
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) Code2Session(jsCode string) (result ResCode2Session, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 小程序数据统计
|
|
||||||
|
|
||||||
**获取用户访问小程序日留存**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisDailyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序月留存**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisMonthlyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序周留存**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisWeeklyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序数据概况**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisDailySummary(beginDate, endDate string) (result ResAnalysisDailySummary, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序数据日趋势**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisDailyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序数据月趋势**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisMonthlyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户访问小程序数据周趋势**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisWeeklyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取小程序新增或活跃用户的画像分布数据**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisUserPortrait(beginDate, endDate string) (result ResAnalysisUserPortrait, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取用户小程序访问分布数据**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisVisitDistribution(beginDate, endDate string) (result ResAnalysisVisitDistribution, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取小程序页面访问数据**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetAnalysisVisitPage(beginDate, endDate string) (result ResAnalysisVisitPage, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 小程序二维码生成
|
|
||||||
|
|
||||||
**获取小程序二维码,适用于需要的码数量较少的业务场景**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取小程序码,适用于需要的码数量较少的业务场景**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetWXACode(coderParams QRCoder) (response []byte, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
**获取小程序码,适用于需要的码数量极多的业务场景**
|
|
||||||
|
|
||||||
``` go
|
|
||||||
func (wxa *MiniProgram) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error)
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
更多API使用请参考 godoc :
|
|
||||||
[https://godoc.org/github.com/silenceper/wechat](https://godoc.org/github.com/silenceper/wechat)
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
5
aispeech/README.md
Normal file
5
aispeech/README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# 智能对话
|
||||||
|
|
||||||
|
[官方文档](https://developers.weixin.qq.com/doc/aispeech/platform/INTERFACEDOCUMENT.html)
|
||||||
|
|
||||||
|
## 快速入门
|
||||||
2
cache/cache.go
vendored
2
cache/cache.go
vendored
@@ -2,7 +2,7 @@ package cache
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
//Cache interface
|
// Cache interface
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Get(key string) interface{}
|
Get(key string) interface{}
|
||||||
Set(key string, val interface{}, timeout time.Duration) error
|
Set(key string, val interface{}, timeout time.Duration) error
|
||||||
|
|||||||
10
cache/memcache.go
vendored
10
cache/memcache.go
vendored
@@ -7,18 +7,18 @@ import (
|
|||||||
"github.com/bradfitz/gomemcache/memcache"
|
"github.com/bradfitz/gomemcache/memcache"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Memcache struct contains *memcache.Client
|
// Memcache struct contains *memcache.Client
|
||||||
type Memcache struct {
|
type Memcache struct {
|
||||||
conn *memcache.Client
|
conn *memcache.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewMemcache create new memcache
|
// NewMemcache create new memcache
|
||||||
func NewMemcache(server ...string) *Memcache {
|
func NewMemcache(server ...string) *Memcache {
|
||||||
mc := memcache.New(server...)
|
mc := memcache.New(server...)
|
||||||
return &Memcache{mc}
|
return &Memcache{mc}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get return cached value
|
// Get return cached value
|
||||||
func (mem *Memcache) Get(key string) interface{} {
|
func (mem *Memcache) Get(key string) interface{} {
|
||||||
var err error
|
var err error
|
||||||
var item *memcache.Item
|
var item *memcache.Item
|
||||||
@@ -40,7 +40,7 @@ func (mem *Memcache) IsExist(key string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set cached value with key and expire time.
|
// Set cached value with key and expire time.
|
||||||
func (mem *Memcache) Set(key string, val interface{}, timeout time.Duration) (err error) {
|
func (mem *Memcache) Set(key string, val interface{}, timeout time.Duration) (err error) {
|
||||||
var data []byte
|
var data []byte
|
||||||
if data, err = json.Marshal(val); err != nil {
|
if data, err = json.Marshal(val); err != nil {
|
||||||
@@ -51,7 +51,7 @@ func (mem *Memcache) Set(key string, val interface{}, timeout time.Duration) (er
|
|||||||
return mem.conn.Set(item)
|
return mem.conn.Set(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete delete value in memcache.
|
// Delete delete value in memcache.
|
||||||
func (mem *Memcache) Delete(key string) error {
|
func (mem *Memcache) Delete(key string) error {
|
||||||
return mem.conn.Delete(key)
|
return mem.conn.Delete(key)
|
||||||
}
|
}
|
||||||
|
|||||||
16
cache/memcache_test.go
vendored
16
cache/memcache_test.go
vendored
@@ -3,6 +3,9 @@ package cache
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bradfitz/gomemcache/memcache"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMemcache(t *testing.T) {
|
func TestMemcache(t *testing.T) {
|
||||||
@@ -16,13 +19,22 @@ func TestMemcache(t *testing.T) {
|
|||||||
if !mem.IsExist("username") {
|
if !mem.IsExist("username") {
|
||||||
t.Error("IsExist Error")
|
t.Error("IsExist Error")
|
||||||
}
|
}
|
||||||
|
exists := mem.IsExist("unknown-key")
|
||||||
|
assert.Equal(t, false, exists)
|
||||||
|
|
||||||
name := mem.Get("username").(string)
|
name := mem.Get("username").(string)
|
||||||
if name != "silenceper" {
|
if name != "" {
|
||||||
t.Error("get Error")
|
if name != "silenceper" {
|
||||||
|
t.Error("get Error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
data := mem.Get("unknown-key")
|
||||||
|
assert.Nil(t, data)
|
||||||
|
|
||||||
if err = mem.Delete("username"); err != nil {
|
if err = mem.Delete("username"); err != nil {
|
||||||
t.Errorf("delete Error , err=%v", err)
|
t.Errorf("delete Error , err=%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = mem.Delete("unknown-key")
|
||||||
|
assert.Equal(t, memcache.ErrCacheMiss, err)
|
||||||
}
|
}
|
||||||
|
|||||||
6
cache/memory.go
vendored
6
cache/memory.go
vendored
@@ -62,13 +62,13 @@ func (mem *Memory) Set(key string, val interface{}, timeout time.Duration) (err
|
|||||||
|
|
||||||
//Delete delete value in memcache.
|
//Delete delete value in memcache.
|
||||||
func (mem *Memory) Delete(key string) error {
|
func (mem *Memory) Delete(key string) error {
|
||||||
return mem.deleteKey(key)
|
mem.deleteKey(key)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteKey
|
// deleteKey
|
||||||
func (mem *Memory) deleteKey(key string) error {
|
func (mem *Memory) deleteKey(key string) {
|
||||||
mem.Lock()
|
mem.Lock()
|
||||||
defer mem.Unlock()
|
defer mem.Unlock()
|
||||||
delete(mem.data, key)
|
delete(mem.data, key)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
12
cache/redis.go
vendored
12
cache/redis.go
vendored
@@ -19,7 +19,7 @@ type RedisOpts struct {
|
|||||||
Database int `yml:"database" json:"database"`
|
Database int `yml:"database" json:"database"`
|
||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||||
MaxActive int `yml:"max_active" json:"max_active"`
|
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 实例化
|
//NewRedis 实例化
|
||||||
@@ -45,6 +45,11 @@ func NewRedis(opts *RedisOpts) *Redis {
|
|||||||
return &Redis{pool}
|
return &Redis{pool}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//SetRedisPool 设置redis连接池
|
||||||
|
func (r *Redis) SetRedisPool(pool *redis.Pool) {
|
||||||
|
r.conn = pool
|
||||||
|
}
|
||||||
|
|
||||||
//SetConn 设置conn
|
//SetConn 设置conn
|
||||||
func (r *Redis) SetConn(conn *redis.Pool) {
|
func (r *Redis) SetConn(conn *redis.Pool) {
|
||||||
r.conn = conn
|
r.conn = conn
|
||||||
@@ -90,10 +95,7 @@ func (r *Redis) IsExist(key string) bool {
|
|||||||
|
|
||||||
a, _ := conn.Do("EXISTS", key)
|
a, _ := conn.Do("EXISTS", key)
|
||||||
i := a.(int64)
|
i := a.(int64)
|
||||||
if i > 0 {
|
return i > 0
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Delete 删除
|
//Delete 删除
|
||||||
|
|||||||
1
cache/redis_test.go
vendored
1
cache/redis_test.go
vendored
@@ -10,6 +10,7 @@ func TestRedis(t *testing.T) {
|
|||||||
Host: "127.0.0.1:6379",
|
Host: "127.0.0.1:6379",
|
||||||
}
|
}
|
||||||
redis := NewRedis(opts)
|
redis := NewRedis(opts)
|
||||||
|
redis.SetConn(redis.conn)
|
||||||
var err error
|
var err error
|
||||||
timeoutDuration := 1 * time.Second
|
timeoutDuration := 1 * time.Second
|
||||||
|
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
//AccessTokenURL 获取access_token的接口
|
|
||||||
AccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ResAccessToken struct
|
|
||||||
type ResAccessToken struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessTokenFunc 获取 access token 的函数签名
|
|
||||||
type GetAccessTokenFunc func(ctx *Context) (accessToken string, err error)
|
|
||||||
|
|
||||||
//SetAccessTokenLock 设置读写锁(一个appID一个读写锁)
|
|
||||||
func (ctx *Context) SetAccessTokenLock(l *sync.RWMutex) {
|
|
||||||
ctx.accessTokenLock = l
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetGetAccessTokenFunc 设置自定义获取accessToken的方式, 需要自己实现缓存
|
|
||||||
func (ctx *Context) SetGetAccessTokenFunc(f GetAccessTokenFunc) {
|
|
||||||
ctx.accessTokenFunc = f
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessToken 获取access_token
|
|
||||||
func (ctx *Context) GetAccessToken() (accessToken string, err error) {
|
|
||||||
ctx.accessTokenLock.Lock()
|
|
||||||
defer ctx.accessTokenLock.Unlock()
|
|
||||||
|
|
||||||
if ctx.accessTokenFunc != nil {
|
|
||||||
return ctx.accessTokenFunc(ctx)
|
|
||||||
}
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("access_token_%s", ctx.AppID)
|
|
||||||
val := ctx.Cache.Get(accessTokenCacheKey)
|
|
||||||
if val != nil {
|
|
||||||
accessToken = val.(string)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//从微信服务器获取
|
|
||||||
var resAccessToken ResAccessToken
|
|
||||||
resAccessToken, err = ctx.GetAccessTokenFromServer()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
accessToken = resAccessToken.AccessToken
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetAccessTokenFromServer 强制从微信服务器获取token
|
|
||||||
func (ctx *Context) GetAccessTokenFromServer() (resAccessToken ResAccessToken, err error) {
|
|
||||||
url := fmt.Sprintf("%s?grant_type=client_credential&appid=%s&secret=%s", AccessTokenURL, ctx.AppID, ctx.AppSecret)
|
|
||||||
var body []byte
|
|
||||||
body, err = util.HTTPGet(url)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(body, &resAccessToken)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if resAccessToken.ErrMsg != "" {
|
|
||||||
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrCode, resAccessToken.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("access_token_%s", ctx.AppID)
|
|
||||||
expires := resAccessToken.ExpiresIn - 1500
|
|
||||||
err = ctx.Cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestContext_SetCustomAccessTokenFunc(t *testing.T) {
|
|
||||||
ctx := Context{
|
|
||||||
accessTokenLock: new(sync.RWMutex),
|
|
||||||
}
|
|
||||||
f := func(ctx *Context) (accessToken string, err error) {
|
|
||||||
return "fake token", nil
|
|
||||||
}
|
|
||||||
ctx.SetGetAccessTokenFunc(f)
|
|
||||||
res, err := ctx.GetAccessToken()
|
|
||||||
if res != "fake token" || err != nil {
|
|
||||||
t.Error("expect fake token but error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_NoSetCustomAccessTokenFunc(t *testing.T) {
|
|
||||||
ctx := Context{
|
|
||||||
accessTokenLock: new(sync.RWMutex),
|
|
||||||
}
|
|
||||||
|
|
||||||
if ctx.accessTokenFunc != nil {
|
|
||||||
t.Error("error accessTokenFunc")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var testdata = `{"authorizer_info":{"nick_name":"就爱浪","head_img":"http:\/\/wx.qlogo.cn\/mmopen\/xPKCxELaaj6hiaTZGv19oQPBJibb7hBoKmNOjQibCNOUycE8iaBhiaHOA6eC8hadQSAUZTuHUJl4qCIbCQGjSWialicfzWh4mdxuejY\/0","service_type_info":{"id":1},"verify_type_info":{"id":-1},"user_name":"gh_dcdbaa6f1687","alias":"ckeyer","qrcode_url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/FribWCoIzQbAX7R1PQ8iaxGonqKp0doYD2ibhC0uhx11LrRcblASiazsbQJTJ4icQnMzfH7G0SUPuKbibTA8Cs4uk5WQ\/0","business_info":{"open_pay":0,"open_shake":0,"open_scan":0,"open_card":0,"open_store":0},"idc":1,"principal_name":"个人","signature":"不折腾会死。"},"authorization_info":{"authorizer_appid":"yyyyy","authorizer_refresh_token":"xxxx","func_info":[{"funcscope_category":{"id":1}},{"funcscope_category":{"id":15}},{"funcscope_category":{"id":4}},{"funcscope_category":{"id":7}},{"funcscope_category":{"id":2}},{"funcscope_category":{"id":3}},{"funcscope_category":{"id":11}},{"funcscope_category":{"id":6}},{"funcscope_category":{"id":5}},{"funcscope_category":{"id":8}},{"funcscope_category":{"id":13}},{"funcscope_category":{"id":9}},{"funcscope_category":{"id":12}},{"funcscope_category":{"id":22}},{"funcscope_category":{"id":23}},{"funcscope_category":{"id":24},"confirm_info":{"need_confirm":0,"already_confirm":0,"can_confirm":0}},{"funcscope_category":{"id":26}},{"funcscope_category":{"id":27},"confirm_info":{"need_confirm":0,"already_confirm":0,"can_confirm":0}},{"funcscope_category":{"id":33},"confirm_info":{"need_confirm":0,"already_confirm":0,"can_confirm":0}},{"funcscope_category":{"id":35}}]}}`
|
|
||||||
|
|
||||||
// TestDecode
|
|
||||||
func TestDecode(t *testing.T) {
|
|
||||||
var ret struct {
|
|
||||||
AuthorizerInfo *AuthorizerInfo `json:"authorizer_info"`
|
|
||||||
AuthorizationInfo *AuthBaseInfo `json:"authorization_info"`
|
|
||||||
}
|
|
||||||
json.Unmarshal([]byte(testdata), &ret)
|
|
||||||
t.Logf("%+v", ret.AuthorizerInfo)
|
|
||||||
t.Logf("%+v", ret.AuthorizationInfo)
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Context struct
|
|
||||||
type Context struct {
|
|
||||||
AppID string
|
|
||||||
AppSecret string
|
|
||||||
Token string
|
|
||||||
EncodingAESKey string
|
|
||||||
PayMchID string
|
|
||||||
PayNotifyURL string
|
|
||||||
PayKey string
|
|
||||||
|
|
||||||
Cache cache.Cache
|
|
||||||
|
|
||||||
Writer http.ResponseWriter
|
|
||||||
Request *http.Request
|
|
||||||
|
|
||||||
//accessTokenLock 读写锁 同一个AppID一个
|
|
||||||
accessTokenLock *sync.RWMutex
|
|
||||||
|
|
||||||
//jsAPITicket 读写锁 同一个AppID一个
|
|
||||||
jsAPITicketLock *sync.RWMutex
|
|
||||||
|
|
||||||
//accessTokenFunc 自定义获取 access token 的方法
|
|
||||||
accessTokenFunc GetAccessTokenFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query returns the keyed url query value if it exists
|
|
||||||
func (ctx *Context) Query(key string) string {
|
|
||||||
value, _ := ctx.GetQuery(key)
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetQuery is like Query(), it returns the keyed url query value
|
|
||||||
func (ctx *Context) GetQuery(key string) (string, bool) {
|
|
||||||
req := ctx.Request
|
|
||||||
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
|
|
||||||
return values[0], true
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetJsAPITicketLock 设置jsAPITicket的lock
|
|
||||||
func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
|
|
||||||
ctx.jsAPITicketLock = lock
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetJsAPITicketLock 获取jsAPITicket 的lock
|
|
||||||
func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
|
|
||||||
return ctx.jsAPITicketLock
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
//qyAccessTokenURL 获取access_token的接口
|
|
||||||
qyAccessTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
|
|
||||||
)
|
|
||||||
|
|
||||||
//ResQyAccessToken struct
|
|
||||||
type ResQyAccessToken struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetQyAccessTokenLock 设置读写锁(一个appID一个读写锁)
|
|
||||||
func (ctx *Context) SetQyAccessTokenLock(l *sync.RWMutex) {
|
|
||||||
ctx.accessTokenLock = l
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetQyAccessToken 获取access_token
|
|
||||||
func (ctx *Context) GetQyAccessToken() (accessToken string, err error) {
|
|
||||||
ctx.accessTokenLock.Lock()
|
|
||||||
defer ctx.accessTokenLock.Unlock()
|
|
||||||
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("qy_access_token_%s", ctx.AppID)
|
|
||||||
val := ctx.Cache.Get(accessTokenCacheKey)
|
|
||||||
if val != nil {
|
|
||||||
accessToken = val.(string)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//从微信服务器获取
|
|
||||||
var resQyAccessToken ResQyAccessToken
|
|
||||||
resQyAccessToken, err = ctx.GetQyAccessTokenFromServer()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
accessToken = resQyAccessToken.AccessToken
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetQyAccessTokenFromServer 强制从微信服务器获取token
|
|
||||||
func (ctx *Context) GetQyAccessTokenFromServer() (resQyAccessToken ResQyAccessToken, err error) {
|
|
||||||
log.Printf("GetQyAccessTokenFromServer")
|
|
||||||
url := fmt.Sprintf(qyAccessTokenURL, ctx.AppID, ctx.AppSecret)
|
|
||||||
var body []byte
|
|
||||||
body, err = util.HTTPGet(url)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(body, &resQyAccessToken)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if resQyAccessToken.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("get qy_access_token error : errcode=%v , errormsg=%v", resQyAccessToken.ErrCode, resQyAccessToken.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
qyAccessTokenCacheKey := fmt.Sprintf("qy_access_token_%s", ctx.AppID)
|
|
||||||
expires := resQyAccessToken.ExpiresIn - 1500
|
|
||||||
err = ctx.Cache.Set(qyAccessTokenCacheKey, resQyAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/xml"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
var xmlContentType = []string{"application/xml; charset=utf-8"}
|
|
||||||
var plainContentType = []string{"text/plain; charset=utf-8"}
|
|
||||||
|
|
||||||
//Render render from bytes
|
|
||||||
func (ctx *Context) Render(bytes []byte) {
|
|
||||||
//debug
|
|
||||||
//fmt.Println("response msg = ", string(bytes))
|
|
||||||
ctx.Writer.WriteHeader(200)
|
|
||||||
_, err := ctx.Writer.Write(bytes)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//String render from string
|
|
||||||
func (ctx *Context) String(str string) {
|
|
||||||
writeContextType(ctx.Writer, plainContentType)
|
|
||||||
ctx.Render([]byte(str))
|
|
||||||
}
|
|
||||||
|
|
||||||
//XML render to xml
|
|
||||||
func (ctx *Context) XML(obj interface{}) {
|
|
||||||
writeContextType(ctx.Writer, xmlContentType)
|
|
||||||
bytes, err := xml.Marshal(obj)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ctx.Render(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeContextType(w http.ResponseWriter, value []string) {
|
|
||||||
header := w.Header()
|
|
||||||
if val := header["Content-Type"]; len(val) == 0 {
|
|
||||||
header["Content-Type"] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
6
credential/access_token.go
Normal file
6
credential/access_token.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
//AccessTokenHandle AccessToken 接口
|
||||||
|
type AccessTokenHandle interface {
|
||||||
|
GetAccessToken() (accessToken string, err error)
|
||||||
|
}
|
||||||
99
credential/default_access_token.go
Normal file
99
credential/default_access_token.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
//AccessTokenURL 获取access_token的接口
|
||||||
|
accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
||||||
|
//CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
||||||
|
CacheKeyOfficialAccountPrefix = "gowechat_officialaccount_"
|
||||||
|
//CacheKeyMiniProgramPrefix 小程序cache key前缀
|
||||||
|
CacheKeyMiniProgramPrefix = "gowechat_miniprogram_"
|
||||||
|
)
|
||||||
|
|
||||||
|
//DefaultAccessToken 默认AccessToken 获取
|
||||||
|
type DefaultAccessToken struct {
|
||||||
|
appID string
|
||||||
|
appSecret string
|
||||||
|
cacheKeyPrefix string
|
||||||
|
cache cache.Cache
|
||||||
|
accessTokenLock *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewDefaultAccessToken new DefaultAccessToken
|
||||||
|
func NewDefaultAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenHandle {
|
||||||
|
if cache == nil {
|
||||||
|
panic("cache is ineed")
|
||||||
|
}
|
||||||
|
return &DefaultAccessToken{
|
||||||
|
appID: appID,
|
||||||
|
appSecret: appSecret,
|
||||||
|
cache: cache,
|
||||||
|
cacheKeyPrefix: cacheKeyPrefix,
|
||||||
|
accessTokenLock: new(sync.Mutex),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResAccessToken struct
|
||||||
|
type ResAccessToken struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
ExpiresIn int64 `json:"expires_in"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAccessToken 获取access_token,先从cache中获取,没有则从服务端获取
|
||||||
|
func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||||
|
//加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||||
|
ak.accessTokenLock.Lock()
|
||||||
|
defer ak.accessTokenLock.Unlock()
|
||||||
|
|
||||||
|
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
||||||
|
val := ak.cache.Get(accessTokenCacheKey)
|
||||||
|
if val != nil {
|
||||||
|
accessToken = val.(string)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//cache失效,从微信服务器获取
|
||||||
|
var resAccessToken ResAccessToken
|
||||||
|
resAccessToken, err = GetTokenFromServer(ak.appID, ak.appSecret)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expires := resAccessToken.ExpiresIn - 1500
|
||||||
|
err = ak.cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
accessToken = resAccessToken.AccessToken
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTokenFromServer 强制从微信服务器获取token
|
||||||
|
func GetTokenFromServer(appID, appSecret string) (resAccessToken ResAccessToken, err error) {
|
||||||
|
url := fmt.Sprintf("%s?grant_type=client_credential&appid=%s&secret=%s", accessTokenURL, appID, appSecret)
|
||||||
|
var body []byte
|
||||||
|
body, err = util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, &resAccessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if resAccessToken.ErrMsg != "" {
|
||||||
|
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrCode, resAccessToken.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
18
credential/default_access_token_test.go
Normal file
18
credential/default_access_token_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/h2non/gock.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetTicketFromServer(t *testing.T) {
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New(getTicketURL).Reply(200).JSON(&ResTicket{Ticket: "mock-ticket", ExpiresIn: 10})
|
||||||
|
ticket, err := GetTicketFromServer("arg-ak")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, int64(0), ticket.ErrCode)
|
||||||
|
assert.Equal(t, "mock-ticket", ticket.Ticket, "they should be equal")
|
||||||
|
assert.Equal(t, int64(10), ticket.ExpiresIn, "they should be equal")
|
||||||
|
}
|
||||||
83
credential/default_js_ticket.go
Normal file
83
credential/default_js_ticket.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
//获取ticket的url
|
||||||
|
const getTicketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"
|
||||||
|
|
||||||
|
//DefaultJsTicket 默认获取js ticket方法
|
||||||
|
type DefaultJsTicket struct {
|
||||||
|
appID string
|
||||||
|
cacheKeyPrefix string
|
||||||
|
cache cache.Cache
|
||||||
|
//jsAPITicket 读写锁 同一个AppID一个
|
||||||
|
jsAPITicketLock *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewDefaultJsTicket new
|
||||||
|
func NewDefaultJsTicket(appID string, cacheKeyPrefix string, cache cache.Cache) JsTicketHandle {
|
||||||
|
return &DefaultJsTicket{
|
||||||
|
appID: appID,
|
||||||
|
cache: cache,
|
||||||
|
cacheKeyPrefix: cacheKeyPrefix,
|
||||||
|
jsAPITicketLock: new(sync.Mutex),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResTicket 请求jsapi_tikcet返回结果
|
||||||
|
type ResTicket struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
Ticket string `json:"ticket"`
|
||||||
|
ExpiresIn int64 `json:"expires_in"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTicket 获取jsapi_ticket
|
||||||
|
func (js *DefaultJsTicket) GetTicket(accessToken string) (ticketStr string, err error) {
|
||||||
|
js.jsAPITicketLock.Lock()
|
||||||
|
defer js.jsAPITicketLock.Unlock()
|
||||||
|
|
||||||
|
//先从cache中取
|
||||||
|
jsAPITicketCacheKey := fmt.Sprintf("%s_jsapi_ticket_%s", js.cacheKeyPrefix, js.appID)
|
||||||
|
val := js.cache.Get(jsAPITicketCacheKey)
|
||||||
|
if val != nil {
|
||||||
|
ticketStr = val.(string)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ticket ResTicket
|
||||||
|
ticket, err = GetTicketFromServer(accessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expires := ticket.ExpiresIn - 1500
|
||||||
|
err = js.cache.Set(jsAPITicketCacheKey, ticket.Ticket, time.Duration(expires)*time.Second)
|
||||||
|
ticketStr = ticket.Ticket
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTicketFromServer 从服务器中获取ticket
|
||||||
|
func GetTicketFromServer(accessToken string) (ticket ResTicket, err error) {
|
||||||
|
var response []byte
|
||||||
|
url := fmt.Sprintf(getTicketURL, accessToken)
|
||||||
|
response, err = util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(response, &ticket)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ticket.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("getTicket Error : errcode=%d , errmsg=%s", ticket.ErrCode, ticket.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
7
credential/js_ticket.go
Normal file
7
credential/js_ticket.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
//JsTicketHandle js ticket获取
|
||||||
|
type JsTicketHandle interface {
|
||||||
|
//GetTicket 获取ticket
|
||||||
|
GetTicket(accessToken string) (ticket string, err error)
|
||||||
|
}
|
||||||
23
doc.go
23
doc.go
@@ -3,29 +3,6 @@ Package wechat provide wechat sdk for go
|
|||||||
|
|
||||||
使用Golang开发的微信SDK,简单、易用。
|
使用Golang开发的微信SDK,简单、易用。
|
||||||
|
|
||||||
以下是一个处理消息接收以及回复的例子:
|
|
||||||
|
|
||||||
//配置微信参数
|
|
||||||
config := &wechat.Config{
|
|
||||||
AppID: "xxxx",
|
|
||||||
AppSecret: "xxxx",
|
|
||||||
Token: "xxxx",
|
|
||||||
EncodingAESKey: "xxxx",
|
|
||||||
}
|
|
||||||
wc := wechat.NewWechat(config)
|
|
||||||
|
|
||||||
// 传入request和responseWriter
|
|
||||||
server := wc.GetServer(request, responseWriter)
|
|
||||||
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
|
||||||
|
|
||||||
//回复消息:演示回复用户发送的消息
|
|
||||||
text := message.NewText(msg.Content)
|
|
||||||
return &message.Reply{message.MsgText, text}
|
|
||||||
})
|
|
||||||
|
|
||||||
server.Serve()
|
|
||||||
server.Send()
|
|
||||||
|
|
||||||
|
|
||||||
更多信息:https://github.com/silenceper/wechat
|
更多信息:https://github.com/silenceper/wechat
|
||||||
|
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
"github.com/astaxie/beego/context"
|
|
||||||
"github.com/silenceper/wechat"
|
|
||||||
"github.com/silenceper/wechat/message"
|
|
||||||
)
|
|
||||||
|
|
||||||
func hello(ctx *context.Context) {
|
|
||||||
//配置微信参数
|
|
||||||
config := &wechat.Config{
|
|
||||||
AppID: "your app id",
|
|
||||||
AppSecret: "your app secret",
|
|
||||||
Token: "your token",
|
|
||||||
EncodingAESKey: "your encoding aes key",
|
|
||||||
}
|
|
||||||
wc := wechat.NewWechat(config)
|
|
||||||
|
|
||||||
// 传入request和responseWriter
|
|
||||||
server := wc.GetServer(ctx.Request, ctx.ResponseWriter)
|
|
||||||
//设置接收消息的处理方法
|
|
||||||
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
|
||||||
|
|
||||||
//回复消息:演示回复用户发送的消息
|
|
||||||
text := message.NewText(msg.Content)
|
|
||||||
return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
|
||||||
})
|
|
||||||
|
|
||||||
//处理消息接收以及回复
|
|
||||||
err := server.Serve()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//发送回复的消息
|
|
||||||
server.Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
beego.Any("/", hello)
|
|
||||||
beego.Run(":8001")
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/silenceper/wechat"
|
|
||||||
"github.com/silenceper/wechat/message"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
router := gin.Default()
|
|
||||||
|
|
||||||
router.Any("/", hello)
|
|
||||||
router.Run(":8001")
|
|
||||||
}
|
|
||||||
|
|
||||||
func hello(c *gin.Context) {
|
|
||||||
|
|
||||||
//配置微信参数
|
|
||||||
config := &wechat.Config{
|
|
||||||
AppID: "your app id",
|
|
||||||
AppSecret: "your app secret",
|
|
||||||
Token: "your token",
|
|
||||||
EncodingAESKey: "your encoding aes key",
|
|
||||||
}
|
|
||||||
wc := wechat.NewWechat(config)
|
|
||||||
|
|
||||||
// 传入request和responseWriter
|
|
||||||
server := wc.GetServer(c.Request, c.Writer)
|
|
||||||
//设置接收消息的处理方法
|
|
||||||
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
|
||||||
|
|
||||||
//回复消息:演示回复用户发送的消息
|
|
||||||
text := message.NewText(msg.Content)
|
|
||||||
return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
|
||||||
})
|
|
||||||
|
|
||||||
//处理消息接收以及回复
|
|
||||||
err := server.Serve()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//发送回复的消息
|
|
||||||
server.Send()
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat"
|
|
||||||
"github.com/silenceper/wechat/message"
|
|
||||||
)
|
|
||||||
|
|
||||||
func hello(rw http.ResponseWriter, req *http.Request) {
|
|
||||||
|
|
||||||
//配置微信参数
|
|
||||||
config := &wechat.Config{
|
|
||||||
AppID: "your app id",
|
|
||||||
AppSecret: "your app secret",
|
|
||||||
Token: "your token",
|
|
||||||
EncodingAESKey: "your encoding aes key",
|
|
||||||
}
|
|
||||||
wc := wechat.NewWechat(config)
|
|
||||||
|
|
||||||
// 传入request和responseWriter
|
|
||||||
server := wc.GetServer(req, rw)
|
|
||||||
//设置接收消息的处理方法
|
|
||||||
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
|
||||||
|
|
||||||
//回复消息:演示回复用户发送的消息
|
|
||||||
text := message.NewText(msg.Content)
|
|
||||||
return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
|
||||||
})
|
|
||||||
|
|
||||||
//处理消息接收以及回复
|
|
||||||
err := server.Serve()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//发送回复的消息
|
|
||||||
server.Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
http.HandleFunc("/", hello)
|
|
||||||
err := http.ListenAndServe(":8001", nil)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("start server error , err=%v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
go.mod
27
go.mod
@@ -1,21 +1,14 @@
|
|||||||
module github.com/silenceper/wechat
|
module github.com/silenceper/wechat/v2
|
||||||
|
|
||||||
go 1.13
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/astaxie/beego v1.7.1
|
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
|
||||||
github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a
|
github.com/fatih/structs v1.1.0
|
||||||
github.com/gin-gonic/gin v1.1.4
|
github.com/gomodule/redigo v1.8.1
|
||||||
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b // indirect
|
github.com/sirupsen/logrus v1.6.0
|
||||||
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible
|
github.com/spf13/cast v1.3.1
|
||||||
github.com/kr/pretty v0.1.0
|
github.com/stretchr/testify v1.5.1
|
||||||
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
|
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
|
||||||
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c // indirect
|
gopkg.in/h2non/gock.v1 v1.0.15
|
||||||
github.com/stretchr/testify v1.4.0 // indirect
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
|
|
||||||
golang.org/x/net v0.0.0-20191125084936-ffdde1057850 // indirect
|
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
|
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
|
||||||
gopkg.in/go-playground/validator.v8 v8.18.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
65
go.sum
65
go.sum
@@ -1,44 +1,43 @@
|
|||||||
github.com/astaxie/beego v1.7.1 h1:TuqX4F9e3ujVEycudgWrwUj11WMppLZyunJKIBoxTFw=
|
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0=
|
||||||
github.com/astaxie/beego v1.7.1/go.mod h1:0R4++1tUqERR0WYFWdfkcrsyoVBCG4DgpDGokT3yb+U=
|
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
|
||||||
github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a h1:k5TuEkqEYCRs8+66WdOkswWOj+L/YbP5ruainvn94wg=
|
|
||||||
github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
|
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/gin-gonic/gin v1.1.4 h1:XLaCFbU39SSGRQrEeP7Z7mM3lvRqC4vE5tEaVdLDdSE=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/gin-gonic/gin v1.1.4/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b h1:fE/yi9pibxGEc0gSJuEShcsBXE2d5FW3OudsjE9tKzQ=
|
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||||
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||||
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible h1:QJ4V3LjaRe/6NKoaaj2QzQZcezt5gNXdPv0axxS4VNA=
|
github.com/gomodule/redigo v1.8.1 h1:Abmo0bI7Xf0IhdIPc7HZQzZcShdnmxeoVuDDtIQp8N8=
|
||||||
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
github.com/gomodule/redigo v1.8.1/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
|
||||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
|
||||||
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE=
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
||||||
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739/go.mod h1:zUx1mhth20V3VKgL5jbd1BSQcW4Fy6Qs4PZvQwRFwzM=
|
|
||||||
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c h1:YHHK/dEmr2Jo1cWD1VMB2waEeHJhHFp3CEylwWy/VcY=
|
|
||||||
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||||
|
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||||
|
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
|
||||||
|
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20191125084936-ffdde1057850 h1:Vq85/r8R9IdcUHmZ0/nQlUg1v15rzvQ2sHdnZAj/x7s=
|
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
|
||||||
golang.org/x/net v0.0.0-20191125084936-ffdde1057850/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
||||||
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
|
||||||
gopkg.in/go-playground/validator.v8 v8.18.1 h1:F8SLY5Vqesjs1nI1EL4qmF1PQZ1sitsmq0rPYXLyfGU=
|
|
||||||
gopkg.in/go-playground/validator.v8 v8.18.1/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
|
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|||||||
109
js/js.go
109
js/js.go
@@ -1,109 +0,0 @@
|
|||||||
package js
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
const getTicketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"
|
|
||||||
|
|
||||||
// Js struct
|
|
||||||
type Js struct {
|
|
||||||
*context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config 返回给用户jssdk配置信息
|
|
||||||
type Config struct {
|
|
||||||
AppID string `json:"app_id"`
|
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
NonceStr string `json:"nonce_str"`
|
|
||||||
Signature string `json:"signature"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// resTicket 请求jsapi_tikcet返回结果
|
|
||||||
type resTicket struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
Ticket string `json:"ticket"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewJs init
|
|
||||||
func NewJs(context *context.Context) *Js {
|
|
||||||
js := new(Js)
|
|
||||||
js.Context = context
|
|
||||||
return js
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetConfig 获取jssdk需要的配置参数
|
|
||||||
//uri 为当前网页地址
|
|
||||||
func (js *Js) GetConfig(uri string) (config *Config, err error) {
|
|
||||||
config = new(Config)
|
|
||||||
var ticketStr string
|
|
||||||
ticketStr, err = js.GetTicket()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
nonceStr := util.RandomStr(16)
|
|
||||||
timestamp := util.GetCurrTs()
|
|
||||||
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri)
|
|
||||||
sigStr := util.Signature(str)
|
|
||||||
|
|
||||||
config.AppID = js.AppID
|
|
||||||
config.NonceStr = nonceStr
|
|
||||||
config.Timestamp = timestamp
|
|
||||||
config.Signature = sigStr
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetTicket 获取jsapi_ticket
|
|
||||||
func (js *Js) GetTicket() (ticketStr string, err error) {
|
|
||||||
js.GetJsAPITicketLock().Lock()
|
|
||||||
defer js.GetJsAPITicketLock().Unlock()
|
|
||||||
|
|
||||||
//先从cache中取
|
|
||||||
jsAPITicketCacheKey := fmt.Sprintf("jsapi_ticket_%s", js.AppID)
|
|
||||||
val := js.Cache.Get(jsAPITicketCacheKey)
|
|
||||||
if val != nil {
|
|
||||||
ticketStr = val.(string)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var ticket resTicket
|
|
||||||
ticket, err = js.getTicketFromServer()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ticketStr = ticket.Ticket
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//getTicketFromServer 强制从服务器中获取ticket
|
|
||||||
func (js *Js) getTicketFromServer() (ticket resTicket, err error) {
|
|
||||||
var accessToken string
|
|
||||||
accessToken, err = js.GetAccessToken()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var response []byte
|
|
||||||
url := fmt.Sprintf(getTicketURL, accessToken)
|
|
||||||
response, err = util.HTTPGet(url)
|
|
||||||
err = json.Unmarshal(response, &ticket)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ticket.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("getTicket Error : errcode=%d , errmsg=%s", ticket.ErrCode, ticket.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
jsAPITicketCacheKey := fmt.Sprintf("jsapi_ticket_%s", js.AppID)
|
|
||||||
expires := ticket.ExpiresIn - 1500
|
|
||||||
err = js.Cache.Set(jsAPITicketCacheKey, ticket.Ticket, time.Duration(expires)*time.Second)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
143
menu/button.go
143
menu/button.go
@@ -1,143 +0,0 @@
|
|||||||
package menu
|
|
||||||
|
|
||||||
//Button 菜单按钮
|
|
||||||
type Button struct {
|
|
||||||
Type string `json:"type,omitempty"`
|
|
||||||
Name string `json:"name,omitempty"`
|
|
||||||
Key string `json:"key,omitempty"`
|
|
||||||
URL string `json:"url,omitempty"`
|
|
||||||
MediaID string `json:"media_id,omitempty"`
|
|
||||||
AppID string `json:"appid,omitempty"`
|
|
||||||
PagePath string `json:"pagepath,omitempty"`
|
|
||||||
SubButtons []*Button `json:"sub_button,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetSubButton 设置二级菜单
|
|
||||||
func (btn *Button) SetSubButton(name string, subButtons []*Button) {
|
|
||||||
btn.Name = name
|
|
||||||
btn.SubButtons = subButtons
|
|
||||||
btn.Type = ""
|
|
||||||
btn.Key = ""
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetClickButton btn 为click类型
|
|
||||||
func (btn *Button) SetClickButton(name, key string) {
|
|
||||||
btn.Type = "click"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetViewButton view类型
|
|
||||||
func (btn *Button) SetViewButton(name, url string) {
|
|
||||||
btn.Type = "view"
|
|
||||||
btn.Name = name
|
|
||||||
btn.URL = url
|
|
||||||
btn.Key = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetScanCodePushButton 扫码推事件
|
|
||||||
func (btn *Button) SetScanCodePushButton(name, key string) {
|
|
||||||
btn.Type = "scancode_push"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetScanCodeWaitMsgButton 设置 扫码推事件且弹出"消息接收中"提示框
|
|
||||||
func (btn *Button) SetScanCodeWaitMsgButton(name, key string) {
|
|
||||||
btn.Type = "scancode_waitmsg"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetPicSysPhotoButton 设置弹出系统拍照发图按钮
|
|
||||||
func (btn *Button) SetPicSysPhotoButton(name, key string) {
|
|
||||||
btn.Type = "pic_sysphoto"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetPicPhotoOrAlbumButton 设置弹出拍照或者相册发图类型按钮
|
|
||||||
func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) {
|
|
||||||
btn.Type = "pic_photo_or_album"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPicWeixinButton 设置弹出微信相册发图器类型按钮
|
|
||||||
func (btn *Button) SetPicWeixinButton(name, key string) {
|
|
||||||
btn.Type = "pic_weixin"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLocationSelectButton 设置 弹出地理位置选择器 类型按钮
|
|
||||||
func (btn *Button) SetLocationSelectButton(name, key string) {
|
|
||||||
btn.Type = "location_select"
|
|
||||||
btn.Name = name
|
|
||||||
btn.Key = key
|
|
||||||
|
|
||||||
btn.URL = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetMediaIDButton 设置 下发消息(除文本消息) 类型按钮
|
|
||||||
func (btn *Button) SetMediaIDButton(name, mediaID string) {
|
|
||||||
btn.Type = "media_id"
|
|
||||||
btn.Name = name
|
|
||||||
btn.MediaID = mediaID
|
|
||||||
|
|
||||||
btn.Key = ""
|
|
||||||
btn.URL = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetViewLimitedButton 设置 跳转图文消息URL 类型按钮
|
|
||||||
func (btn *Button) SetViewLimitedButton(name, mediaID string) {
|
|
||||||
btn.Type = "view_limited"
|
|
||||||
btn.Name = name
|
|
||||||
btn.MediaID = mediaID
|
|
||||||
|
|
||||||
btn.Key = ""
|
|
||||||
btn.URL = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//SetMiniprogramButton 设置 跳转小程序 类型按钮 (公众号后台必须已经关联小程序)
|
|
||||||
func (btn *Button) SetMiniprogramButton(name, url, appID, pagePath string) {
|
|
||||||
btn.Type = "miniprogram"
|
|
||||||
btn.Name = name
|
|
||||||
btn.URL = url
|
|
||||||
btn.AppID = appID
|
|
||||||
btn.PagePath = pagePath
|
|
||||||
|
|
||||||
btn.Key = ""
|
|
||||||
btn.MediaID = ""
|
|
||||||
btn.SubButtons = nil
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
package message
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
templateSendURL = "https://api.weixin.qq.com/cgi-bin/message/template/send"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Template 模板消息
|
|
||||||
type Template struct {
|
|
||||||
*context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewTemplate 实例化
|
|
||||||
func NewTemplate(context *context.Context) *Template {
|
|
||||||
tpl := new(Template)
|
|
||||||
tpl.Context = context
|
|
||||||
return tpl
|
|
||||||
}
|
|
||||||
|
|
||||||
//Message 发送的模板消息内容
|
|
||||||
type Message struct {
|
|
||||||
ToUser string `json:"touser"` // 必须, 接受者OpenID
|
|
||||||
TemplateID string `json:"template_id"` // 必须, 模版ID
|
|
||||||
URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
|
|
||||||
Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置
|
|
||||||
Data map[string]*DataItem `json:"data"` // 必须, 模板数据
|
|
||||||
|
|
||||||
MiniProgram struct {
|
|
||||||
AppID string `json:"appid"` //所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
|
|
||||||
PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
|
|
||||||
} `json:"miniprogram"` //可选,跳转至小程序地址
|
|
||||||
}
|
|
||||||
|
|
||||||
//DataItem 模版内某个 .DATA 的值
|
|
||||||
type DataItem struct {
|
|
||||||
Value string `json:"value"`
|
|
||||||
Color string `json:"color,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type resTemplateSend struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
MsgID int64 `json:"msgid"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//Send 发送模板消息
|
|
||||||
func (tpl *Template) Send(msg *Message) (msgID int64, err error) {
|
|
||||||
var accessToken string
|
|
||||||
accessToken, err = tpl.GetAccessToken()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
|
||||||
response, err := util.PostJSON(uri, msg)
|
|
||||||
|
|
||||||
var result resTemplateSend
|
|
||||||
err = json.Unmarshal(response, &result)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if result.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
msgID = result.MsgID
|
|
||||||
return
|
|
||||||
}
|
|
||||||
8
minigame/README.md
Normal file
8
minigame/README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# 微信小游戏
|
||||||
|
|
||||||
|
[官方文档](https://developers.weixin.qq.com/minigame/dev/api-backend/)
|
||||||
|
|
||||||
|
## 快速入门
|
||||||
|
```go
|
||||||
|
|
||||||
|
```
|
||||||
20
miniprogram/README.md
Normal file
20
miniprogram/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# 微信小程序
|
||||||
|
|
||||||
|
[官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/)
|
||||||
|
|
||||||
|
|
||||||
|
## 包说明
|
||||||
|
- analysis 数据分析相关API
|
||||||
|
|
||||||
|
## 快速入门
|
||||||
|
```go
|
||||||
|
wc := wechat.NewWechat()
|
||||||
|
memory := cache.NewMemory()
|
||||||
|
cfg := &miniConfig.Config{
|
||||||
|
AppID: "xxx",
|
||||||
|
AppSecret: "xxx",
|
||||||
|
Cache: memory,
|
||||||
|
}
|
||||||
|
miniprogram := wc.GetMiniProgram(cfg)
|
||||||
|
miniprogram.GetAnalysis().GetAnalysisDailyRetain()
|
||||||
|
```
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package miniprogram
|
package analysis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -30,10 +32,20 @@ const (
|
|||||||
getAnalysisVisitPageURL = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token=%s"
|
getAnalysisVisitPageURL = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Analysis analyis 数据分析
|
||||||
|
type Analysis struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewAnalysis new
|
||||||
|
func NewAnalysis(ctx *context.Context) *Analysis {
|
||||||
|
return &Analysis{ctx}
|
||||||
|
}
|
||||||
|
|
||||||
// fetchData 拉取统计数据
|
// fetchData 拉取统计数据
|
||||||
func (wxa *MiniProgram) fetchData(urlStr string, body interface{}) (response []byte, err error) {
|
func (analysis *Analysis) fetchData(urlStr string, body interface{}) (response []byte, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = wxa.GetAccessToken()
|
accessToken, err = analysis.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -42,8 +54,8 @@ func (wxa *MiniProgram) fetchData(urlStr string, body interface{}) (response []b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalysisRetainItem 留存项结构
|
// RetainItem 留存项结构
|
||||||
type AnalysisRetainItem struct {
|
type RetainItem struct {
|
||||||
Key int `json:"key"` // 标识,0开始表示当天,1表示1甜后,以此类推
|
Key int `json:"key"` // 标识,0开始表示当天,1表示1甜后,以此类推
|
||||||
Value int `json:"value"` // key对应日期的新增用户数/活跃用户数(key=0时)或留存用户数(k>0时)
|
Value int `json:"value"` // key对应日期的新增用户数/活跃用户数(key=0时)或留存用户数(k>0时)
|
||||||
}
|
}
|
||||||
@@ -51,18 +63,18 @@ type AnalysisRetainItem struct {
|
|||||||
// ResAnalysisRetain 小程序留存数据返回
|
// ResAnalysisRetain 小程序留存数据返回
|
||||||
type ResAnalysisRetain struct {
|
type ResAnalysisRetain struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
RefDate string `json:"ref_date"` // 日期
|
RefDate string `json:"ref_date"` // 日期
|
||||||
VisitUVNew []AnalysisRetainItem `json:"visit_uv_new"` // 新增用户留存
|
VisitUVNew []RetainItem `json:"visit_uv_new"` // 新增用户留存
|
||||||
VisitUV []AnalysisRetainItem `json:"visit_uv"` // 活跃用户留存
|
VisitUV []RetainItem `json:"visit_uv"` // 活跃用户留存
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAnalysisRetain 获取用户访问小程序留存数据(日、月、周)
|
// getAnalysisRetain 获取用户访问小程序留存数据(日、月、周)
|
||||||
func (wxa *MiniProgram) getAnalysisRetain(urlStr string, beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
func (analysis *Analysis) getAnalysisRetain(urlStr string, beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(urlStr, body)
|
response, err := analysis.fetchData(urlStr, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -78,18 +90,18 @@ func (wxa *MiniProgram) getAnalysisRetain(urlStr string, beginDate, endDate stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisDailyRetain 获取用户访问小程序日留存
|
// GetAnalysisDailyRetain 获取用户访问小程序日留存
|
||||||
func (wxa *MiniProgram) GetAnalysisDailyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
func (analysis *Analysis) GetAnalysisDailyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
||||||
return wxa.getAnalysisRetain(getAnalysisDailyRetainURL, beginDate, endDate)
|
return analysis.getAnalysisRetain(getAnalysisDailyRetainURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisMonthlyRetain 获取用户访问小程序月留存
|
// GetAnalysisMonthlyRetain 获取用户访问小程序月留存
|
||||||
func (wxa *MiniProgram) GetAnalysisMonthlyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
func (analysis *Analysis) GetAnalysisMonthlyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
||||||
return wxa.getAnalysisRetain(getAnalysisMonthlyRetainURL, beginDate, endDate)
|
return analysis.getAnalysisRetain(getAnalysisMonthlyRetainURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisWeeklyRetain 获取用户访问小程序周留存
|
// GetAnalysisWeeklyRetain 获取用户访问小程序周留存
|
||||||
func (wxa *MiniProgram) GetAnalysisWeeklyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
func (analysis *Analysis) GetAnalysisWeeklyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error) {
|
||||||
return wxa.getAnalysisRetain(getAnalysisWeeklyRetainURL, beginDate, endDate)
|
return analysis.getAnalysisRetain(getAnalysisWeeklyRetainURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResAnalysisDailySummary 小程序访问数据概况
|
// ResAnalysisDailySummary 小程序访问数据概况
|
||||||
@@ -104,16 +116,16 @@ type ResAnalysisDailySummary struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisDailySummary 获取用户访问小程序数据概况
|
// GetAnalysisDailySummary 获取用户访问小程序数据概况
|
||||||
func (wxa *MiniProgram) GetAnalysisDailySummary(beginDate, endDate string) (result ResAnalysisDailySummary, err error) {
|
func (analysis *Analysis) GetAnalysisDailySummary(beginDate, endDate string) (result ResAnalysisDailySummary, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(getAnalysisDailySummaryURL, body)
|
response, err := analysis.fetchData(getAnalysisDailySummaryURL, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(string(response))
|
|
||||||
err = json.Unmarshal(response, &result)
|
err = json.Unmarshal(response, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -141,12 +153,12 @@ type ResAnalysisVisitTrend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getAnalysisRetain 获取小程序访问数据趋势(日、月、周)
|
// getAnalysisRetain 获取小程序访问数据趋势(日、月、周)
|
||||||
func (wxa *MiniProgram) getAnalysisVisitTrend(urlStr string, beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
func (analysis *Analysis) getAnalysisVisitTrend(urlStr string, beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(urlStr, body)
|
response, err := analysis.fetchData(urlStr, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -162,18 +174,18 @@ func (wxa *MiniProgram) getAnalysisVisitTrend(urlStr string, beginDate, endDate
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisDailyVisitTrend 获取用户访问小程序数据日趋势
|
// GetAnalysisDailyVisitTrend 获取用户访问小程序数据日趋势
|
||||||
func (wxa *MiniProgram) GetAnalysisDailyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
func (analysis *Analysis) GetAnalysisDailyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
||||||
return wxa.getAnalysisVisitTrend(getAnalysisDailyVisitTrendURL, beginDate, endDate)
|
return analysis.getAnalysisVisitTrend(getAnalysisDailyVisitTrendURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisMonthlyVisitTrend 获取用户访问小程序数据月趋势
|
// GetAnalysisMonthlyVisitTrend 获取用户访问小程序数据月趋势
|
||||||
func (wxa *MiniProgram) GetAnalysisMonthlyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
func (analysis *Analysis) GetAnalysisMonthlyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
||||||
return wxa.getAnalysisVisitTrend(getAnalysisMonthlyVisitTrendURL, beginDate, endDate)
|
return analysis.getAnalysisVisitTrend(getAnalysisMonthlyVisitTrendURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisWeeklyVisitTrend 获取用户访问小程序数据周趋势
|
// GetAnalysisWeeklyVisitTrend 获取用户访问小程序数据周趋势
|
||||||
func (wxa *MiniProgram) GetAnalysisWeeklyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
func (analysis *Analysis) GetAnalysisWeeklyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error) {
|
||||||
return wxa.getAnalysisVisitTrend(getAnalysisWeeklyVisitTrendURL, beginDate, endDate)
|
return analysis.getAnalysisVisitTrend(getAnalysisWeeklyVisitTrendURL, beginDate, endDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserPortraitItem 用户画像项目
|
// UserPortraitItem 用户画像项目
|
||||||
@@ -203,12 +215,12 @@ type ResAnalysisUserPortrait struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisUserPortrait 获取小程序新增或活跃用户的画像分布数据
|
// GetAnalysisUserPortrait 获取小程序新增或活跃用户的画像分布数据
|
||||||
func (wxa *MiniProgram) GetAnalysisUserPortrait(beginDate, endDate string) (result ResAnalysisUserPortrait, err error) {
|
func (analysis *Analysis) GetAnalysisUserPortrait(beginDate, endDate string) (result ResAnalysisUserPortrait, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(getAnalysisUserPortraitURL, body)
|
response, err := analysis.fetchData(getAnalysisUserPortraitURL, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -244,12 +256,12 @@ type ResAnalysisVisitDistribution struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisVisitDistribution 获取用户小程序访问分布数据
|
// GetAnalysisVisitDistribution 获取用户小程序访问分布数据
|
||||||
func (wxa *MiniProgram) GetAnalysisVisitDistribution(beginDate, endDate string) (result ResAnalysisVisitDistribution, err error) {
|
func (analysis *Analysis) GetAnalysisVisitDistribution(beginDate, endDate string) (result ResAnalysisVisitDistribution, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(getAnalysisVisitDistributionURL, body)
|
response, err := analysis.fetchData(getAnalysisVisitDistributionURL, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -284,12 +296,12 @@ type ResAnalysisVisitPage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnalysisVisitPage 获取小程序页面访问数据
|
// GetAnalysisVisitPage 获取小程序页面访问数据
|
||||||
func (wxa *MiniProgram) GetAnalysisVisitPage(beginDate, endDate string) (result ResAnalysisVisitPage, err error) {
|
func (analysis *Analysis) GetAnalysisVisitPage(beginDate, endDate string) (result ResAnalysisVisitPage, err error) {
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"begin_date": beginDate,
|
"begin_date": beginDate,
|
||||||
"end_date": endDate,
|
"end_date": endDate,
|
||||||
}
|
}
|
||||||
response, err := wxa.fetchData(getAnalysisVisitPageURL, body)
|
response, err := analysis.fetchData(getAnalysisVisitPageURL, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,27 @@
|
|||||||
package miniprogram
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Auth 登录/用户信息
|
||||||
|
type Auth struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewAuth new auth
|
||||||
|
func NewAuth(ctx *context.Context) *Auth {
|
||||||
|
return &Auth{ctx}
|
||||||
|
}
|
||||||
|
|
||||||
// ResCode2Session 登录凭证校验的返回结果
|
// ResCode2Session 登录凭证校验的返回结果
|
||||||
type ResCode2Session struct {
|
type ResCode2Session struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
@@ -20,9 +31,9 @@ type ResCode2Session struct {
|
|||||||
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code2Session 登录凭证校验
|
//Code2Session 登录凭证校验。
|
||||||
func (wxa *MiniProgram) Code2Session(jsCode string) (result ResCode2Session, err error) {
|
func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
|
||||||
urlStr := fmt.Sprintf(code2SessionURL, wxa.AppID, wxa.AppSecret, jsCode)
|
urlStr := fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)
|
||||||
var response []byte
|
var response []byte
|
||||||
response, err = util.HTTPGet(urlStr)
|
response, err = util.HTTPGet(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -38,3 +49,8 @@ func (wxa *MiniProgram) Code2Session(jsCode string) (result ResCode2Session, err
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetPaidUnionID 用户支付完成后,获取该用户的 UnionId,无需用户授权
|
||||||
|
func (auth *Auth) GetPaidUnionID() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
13
miniprogram/config/config.go
Normal file
13
miniprogram/config/config.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
// Package config 小程序config配置
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config config for 小程序
|
||||||
|
type Config struct {
|
||||||
|
AppID string `json:"app_id"` // appid
|
||||||
|
AppSecret string `json:"app_secret"` // appsecret
|
||||||
|
Cache cache.Cache
|
||||||
|
}
|
||||||
12
miniprogram/context/context.go
Normal file
12
miniprogram/context/context.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Context struct
|
||||||
|
type Context struct {
|
||||||
|
*config.Config
|
||||||
|
credential.AccessTokenHandle
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package miniprogram
|
package encryptor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
@@ -6,8 +6,22 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Encryptor struct
|
||||||
|
type Encryptor struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewEncryptor 实例
|
||||||
|
func NewEncryptor(context *context.Context) *Encryptor {
|
||||||
|
basic := new(Encryptor)
|
||||||
|
basic.Context = context
|
||||||
|
return basic
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrAppIDNotMatch appid不匹配
|
// ErrAppIDNotMatch appid不匹配
|
||||||
ErrAppIDNotMatch = errors.New("app id not match")
|
ErrAppIDNotMatch = errors.New("app id not match")
|
||||||
@@ -19,25 +33,17 @@ var (
|
|||||||
ErrInvalidPKCS7Padding = errors.New("invalid padding on input")
|
ErrInvalidPKCS7Padding = errors.New("invalid padding on input")
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserInfo 用户信息
|
// PlainData 用户信息/手机号信息
|
||||||
type UserInfo struct {
|
type PlainData struct {
|
||||||
OpenID string `json:"openId"`
|
OpenID string `json:"openId"`
|
||||||
UnionID string `json:"unionId"`
|
UnionID string `json:"unionId"`
|
||||||
NickName string `json:"nickName"`
|
NickName string `json:"nickName"`
|
||||||
Gender int `json:"gender"`
|
Gender int `json:"gender"`
|
||||||
City string `json:"city"`
|
City string `json:"city"`
|
||||||
Province string `json:"province"`
|
Province string `json:"province"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
AvatarURL string `json:"avatarUrl"`
|
AvatarURL string `json:"avatarUrl"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Watermark struct {
|
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
AppID string `json:"appid"`
|
|
||||||
} `json:"watermark"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// PhoneInfo 用户手机号
|
|
||||||
type PhoneInfo struct {
|
|
||||||
PhoneNumber string `json:"phoneNumber"`
|
PhoneNumber string `json:"phoneNumber"`
|
||||||
PurePhoneNumber string `json:"purePhoneNumber"`
|
PurePhoneNumber string `json:"purePhoneNumber"`
|
||||||
CountryCode string `json:"countryCode"`
|
CountryCode string `json:"countryCode"`
|
||||||
@@ -96,35 +102,18 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt 解密数据
|
// Decrypt 解密数据
|
||||||
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
|
func (encryptor *Encryptor) Decrypt(sessionKey, encryptedData, iv string) (*PlainData, error) {
|
||||||
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var userInfo UserInfo
|
var plainData PlainData
|
||||||
err = json.Unmarshal(cipherText, &userInfo)
|
err = json.Unmarshal(cipherText, &plainData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if userInfo.Watermark.AppID != wxa.AppID {
|
if plainData.Watermark.AppID != encryptor.AppID {
|
||||||
return nil, ErrAppIDNotMatch
|
return nil, ErrAppIDNotMatch
|
||||||
}
|
}
|
||||||
return &userInfo, nil
|
return &plainData, nil
|
||||||
}
|
|
||||||
|
|
||||||
// DecryptPhone 解密数据(手机)
|
|
||||||
func (wxa *MiniProgram) DecryptPhone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) {
|
|
||||||
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var phoneInfo PhoneInfo
|
|
||||||
err = json.Unmarshal(cipherText, &phoneInfo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if phoneInfo.Watermark.AppID != wxa.AppID {
|
|
||||||
return nil, ErrAppIDNotMatch
|
|
||||||
}
|
|
||||||
return &phoneInfo, nil
|
|
||||||
}
|
}
|
||||||
57
miniprogram/message/consts.go
Normal file
57
miniprogram/message/consts.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import "encoding/xml"
|
||||||
|
|
||||||
|
// MsgType 基本消息类型
|
||||||
|
type MsgType string
|
||||||
|
|
||||||
|
// EventType 事件类型
|
||||||
|
type EventType string
|
||||||
|
|
||||||
|
// InfoType 第三方平台授权事件类型
|
||||||
|
type InfoType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
//MsgTypeText 文本消息
|
||||||
|
MsgTypeText MsgType = "text"
|
||||||
|
//MsgTypeImage 图片消息
|
||||||
|
MsgTypeImage = "image"
|
||||||
|
//MsgTypeLink 图文链接
|
||||||
|
MsgTypeLink = "link"
|
||||||
|
//MsgTypeMiniProgramPage 小程序卡片
|
||||||
|
MsgTypeMiniProgramPage = "miniprogrampage"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommonToken 消息中通用的结构
|
||||||
|
type CommonToken struct {
|
||||||
|
XMLName xml.Name `xml:"xml"`
|
||||||
|
ToUserName string `xml:"ToUserName"`
|
||||||
|
FromUserName string `xml:"FromUserName"`
|
||||||
|
CreateTime int64 `xml:"CreateTime"`
|
||||||
|
MsgType MsgType `xml:"MsgType"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MiniProgramMixMessage 小程序回调的消息结构
|
||||||
|
type MiniProgramMixMessage struct {
|
||||||
|
CommonToken
|
||||||
|
|
||||||
|
MsgID int64 `xml:"MsgId"`
|
||||||
|
|
||||||
|
// 文本消息
|
||||||
|
Content string `xml:"Content"`
|
||||||
|
|
||||||
|
// 图片消息
|
||||||
|
PicURL string `xml:"PicUrl"`
|
||||||
|
MediaID string `xml:"MediaId"`
|
||||||
|
|
||||||
|
// 小程序卡片消息
|
||||||
|
Title string `xml:"Title"`
|
||||||
|
AppID string `xml:"AppId"`
|
||||||
|
PagePath string `xml:"PagePath"`
|
||||||
|
ThumbURL string `xml:"ThumbUrl"`
|
||||||
|
ThumbMediaID string `xml:"ThumbMediaId"`
|
||||||
|
|
||||||
|
// 进入会话事件
|
||||||
|
Event string `xml:"Event"`
|
||||||
|
SessionFrom string `xml:"SessionFrom"`
|
||||||
|
}
|
||||||
124
miniprogram/message/customer_message.go
Normal file
124
miniprogram/message/customer_message.go
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Manager 消息管理者,可以发送消息
|
||||||
|
type Manager struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCustomerMessageManager 实例化消息管理者
|
||||||
|
func NewCustomerMessageManager(context *context.Context) *Manager {
|
||||||
|
return &Manager{
|
||||||
|
context,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//MediaText 文本消息的文字
|
||||||
|
type MediaText struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//MediaResource 消息使用的临时素材id
|
||||||
|
type MediaResource struct {
|
||||||
|
MediaID string `json:"media_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//MediaMiniprogrampage 小程序卡片
|
||||||
|
type MediaMiniprogrampage struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Appid string `json:"appid"`
|
||||||
|
Pagepath string `json:"pagepath"`
|
||||||
|
ThumbMediaID string `json:"thumb_media_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MediaLink 发送图文链接
|
||||||
|
type MediaLink struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
ThumbURL string `json:"thumb_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//CustomerMessage 客服消息
|
||||||
|
type CustomerMessage struct {
|
||||||
|
ToUser string `json:"touser"` //接受者OpenID
|
||||||
|
Msgtype MsgType `json:"msgtype"` //客服消息类型
|
||||||
|
Text *MediaText `json:"text,omitempty"` //可选
|
||||||
|
Image *MediaResource `json:"image,omitempty"` //可选
|
||||||
|
Link *MediaLink `json:"link,omitempty"` //可选
|
||||||
|
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCustomerTextMessage 文本消息结构体构造方法
|
||||||
|
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
|
||||||
|
return &CustomerMessage{
|
||||||
|
ToUser: toUser,
|
||||||
|
Msgtype: MsgTypeText,
|
||||||
|
Text: &MediaText{
|
||||||
|
Content: text,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCustomerImgMessage 图片消息的构造方法
|
||||||
|
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
|
||||||
|
return &CustomerMessage{
|
||||||
|
ToUser: toUser,
|
||||||
|
Msgtype: MsgTypeImage,
|
||||||
|
Image: &MediaResource{
|
||||||
|
MediaID: mediaID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCustomerLinkMessage 图文链接消息的构造方法
|
||||||
|
func NewCustomerLinkMessage(toUser, title, description, url, thumbURL string) *CustomerMessage {
|
||||||
|
return &CustomerMessage{
|
||||||
|
ToUser: toUser,
|
||||||
|
Msgtype: MsgTypeLink,
|
||||||
|
Link: &MediaLink{
|
||||||
|
Title: title,
|
||||||
|
Description: description,
|
||||||
|
URL: url,
|
||||||
|
ThumbURL: thumbURL,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
|
||||||
|
func NewCustomerMiniprogrampageMessage(toUser, title, pagepath, thumbMediaID string) *CustomerMessage {
|
||||||
|
return &CustomerMessage{
|
||||||
|
ToUser: toUser,
|
||||||
|
Msgtype: MsgTypeMiniProgramPage,
|
||||||
|
Miniprogrampage: &MediaMiniprogrampage{
|
||||||
|
Title: title,
|
||||||
|
Pagepath: pagepath,
|
||||||
|
ThumbMediaID: thumbMediaID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send 发送客服消息
|
||||||
|
func (manager *Manager) Send(msg *CustomerMessage) error {
|
||||||
|
accessToken, err := manager.Context.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
|
||||||
|
response, err := util.PostJSON(uri, msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.DecodeWithCommonError(response, "SendCustomerMessage")
|
||||||
|
}
|
||||||
@@ -1,17 +1,74 @@
|
|||||||
package miniprogram
|
package miniprogram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/analysis"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/message"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/tcb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MiniProgram struct extends context
|
//MiniProgram 微信小程序相关API
|
||||||
type MiniProgram struct {
|
type MiniProgram struct {
|
||||||
*context.Context
|
ctx *context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMiniProgram 实例化小程序接口
|
//NewMiniProgram 实例化小程序API
|
||||||
func NewMiniProgram(context *context.Context) *MiniProgram {
|
func NewMiniProgram(cfg *config.Config) *MiniProgram {
|
||||||
miniProgram := new(MiniProgram)
|
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
|
||||||
miniProgram.Context = context
|
ctx := &context.Context{
|
||||||
return miniProgram
|
Config: cfg,
|
||||||
|
AccessTokenHandle: defaultAkHandle,
|
||||||
|
}
|
||||||
|
return &MiniProgram{ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetAccessTokenHandle 自定义access_token获取方式
|
||||||
|
func (miniProgram *MiniProgram) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
|
||||||
|
miniProgram.ctx.AccessTokenHandle = accessTokenHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext get Context
|
||||||
|
func (miniProgram *MiniProgram) GetContext() *context.Context {
|
||||||
|
return miniProgram.ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEncryptor 小程序加解密
|
||||||
|
func (miniProgram *MiniProgram) GetEncryptor() *encryptor.Encryptor {
|
||||||
|
return encryptor.NewEncryptor(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAuth 登录/用户信息相关接口
|
||||||
|
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {
|
||||||
|
return auth.NewAuth(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAnalysis 数据分析
|
||||||
|
func (miniProgram *MiniProgram) GetAnalysis() *analysis.Analysis {
|
||||||
|
return analysis.NewAnalysis(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetQRCode 小程序码相关API
|
||||||
|
func (miniProgram *MiniProgram) GetQRCode() *qrcode.QRCode {
|
||||||
|
return qrcode.NewQRCode(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTcb 小程序云开发API
|
||||||
|
func (miniProgram *MiniProgram) GetTcb() *tcb.Tcb {
|
||||||
|
return tcb.NewTcb(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetSubscribe 小程序订阅消息
|
||||||
|
func (miniProgram *MiniProgram) GetSubscribe() *subscribe.Subscribe {
|
||||||
|
return subscribe.NewSubscribe(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCustomerMessage 客服消息接口
|
||||||
|
func (miniProgram *MiniProgram) GetCustomerMessage() *message.Manager {
|
||||||
|
return message.NewCustomerMessageManager(miniProgram.ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package miniprogram
|
package qrcode
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,6 +15,25 @@ const (
|
|||||||
getWXACodeUnlimitURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s"
|
getWXACodeUnlimitURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//QRCode struct
|
||||||
|
type QRCode struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewQRCode 实例
|
||||||
|
func NewQRCode(context *context.Context) *QRCode {
|
||||||
|
qrCode := new(QRCode)
|
||||||
|
qrCode.Context = context
|
||||||
|
return qrCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// Color QRCode color
|
||||||
|
type Color struct {
|
||||||
|
R string `json:"r"`
|
||||||
|
G string `json:"g"`
|
||||||
|
B string `json:"b"`
|
||||||
|
}
|
||||||
|
|
||||||
// QRCoder 小程序码参数
|
// QRCoder 小程序码参数
|
||||||
type QRCoder struct {
|
type QRCoder struct {
|
||||||
// page 必须是已经发布的小程序存在的页面,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
|
// page 必须是已经发布的小程序存在的页面,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
|
||||||
@@ -32,17 +52,10 @@ type QRCoder struct {
|
|||||||
IsHyaline bool `json:"is_hyaline,omitempty"`
|
IsHyaline bool `json:"is_hyaline,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color QRCode color
|
|
||||||
type Color struct {
|
|
||||||
R string `json:"r"`
|
|
||||||
G string `json:"g"`
|
|
||||||
B string `json:"b"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// fetchCode 请求并返回二维码二进制数据
|
// fetchCode 请求并返回二维码二进制数据
|
||||||
func (wxa *MiniProgram) fetchCode(urlStr string, body interface{}) (response []byte, err error) {
|
func (qrCode *QRCode) fetchCode(urlStr string, body interface{}) (response []byte, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = wxa.GetAccessToken()
|
accessToken, err = qrCode.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -61,31 +74,29 @@ func (wxa *MiniProgram) fetchCode(urlStr string, body interface{}) (response []b
|
|||||||
err = fmt.Errorf("fetchCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
err = fmt.Errorf("fetchCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else if contentType == "image/jpeg" {
|
}
|
||||||
|
if contentType == "image/jpeg" {
|
||||||
// 返回文件
|
// 返回文件
|
||||||
return response, nil
|
return response, nil
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType)
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWXAQRCode 获取小程序二维码,适用于需要的码数量较少的业务场景
|
// CreateWXAQRCode 获取小程序二维码,适用于需要的码数量较少的业务场景
|
||||||
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/createWXAQRCode.html
|
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/createWXAQRCode.html
|
||||||
func (wxa *MiniProgram) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error) {
|
func (qrCode *QRCode) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error) {
|
||||||
return wxa.fetchCode(createWXAQRCodeURL, coderParams)
|
return qrCode.fetchCode(createWXAQRCodeURL, coderParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWXACode 获取小程序码,适用于需要的码数量较少的业务场景
|
// GetWXACode 获取小程序码,适用于需要的码数量较少的业务场景
|
||||||
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACode.html
|
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACode.html
|
||||||
func (wxa *MiniProgram) GetWXACode(coderParams QRCoder) (response []byte, err error) {
|
func (qrCode *QRCode) GetWXACode(coderParams QRCoder) (response []byte, err error) {
|
||||||
return wxa.fetchCode(getWXACodeURL, coderParams)
|
return qrCode.fetchCode(getWXACodeURL, coderParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWXACodeUnlimit 获取小程序码,适用于需要的码数量极多的业务场景
|
// GetWXACodeUnlimit 获取小程序码,适用于需要的码数量极多的业务场景
|
||||||
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACodeUnlimit.html
|
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACodeUnlimit.html
|
||||||
func (wxa *MiniProgram) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error) {
|
func (qrCode *QRCode) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error) {
|
||||||
return wxa.fetchCode(getWXACodeUnlimitURL, coderParams)
|
return qrCode.fetchCode(getWXACodeUnlimitURL, coderParams)
|
||||||
}
|
}
|
||||||
93
miniprogram/subscribe/subscribe.go
Normal file
93
miniprogram/subscribe/subscribe.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package subscribe
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
//发送订阅消息
|
||||||
|
//https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
|
||||||
|
subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
|
||||||
|
|
||||||
|
// 获取当前帐号下的个人模板列表
|
||||||
|
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
||||||
|
getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Subscribe 订阅消息
|
||||||
|
type Subscribe struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSubscribe 实例化
|
||||||
|
func NewSubscribe(ctx *context.Context) *Subscribe {
|
||||||
|
return &Subscribe{Context: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message 订阅消息请求参数
|
||||||
|
type Message struct {
|
||||||
|
ToUser string `json:"touser"` //必选,接收者(用户)的 openid
|
||||||
|
TemplateID string `json:"template_id"` //必选,所需下发的订阅模板id
|
||||||
|
Page string `json:"page"` //可选,点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
|
||||||
|
Data map[string]*DataItem `json:"data"` //必选, 模板内容
|
||||||
|
MiniprogramState string `json:"miniprogram_state"` //可选,跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
||||||
|
Lang string `json:"lang"` //入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
|
||||||
|
}
|
||||||
|
|
||||||
|
//DataItem 模版内某个 .DATA 的值
|
||||||
|
type DataItem struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//TemplateItem template item
|
||||||
|
type TemplateItem struct {
|
||||||
|
PriTmplID string `json:"priTmplId"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Example string `json:"example"`
|
||||||
|
Type int64 `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//TemplateList template list
|
||||||
|
type TemplateList struct {
|
||||||
|
util.CommonError
|
||||||
|
Data []TemplateItem `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send 发送订阅消息
|
||||||
|
func (s *Subscribe) Send(msg *Message) (err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = s.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
|
||||||
|
response, err := util.PostJSON(uri, msg)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(response, "Send")
|
||||||
|
}
|
||||||
|
|
||||||
|
//ListTemplates 获取当前帐号下的个人模板列表
|
||||||
|
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
||||||
|
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
|
||||||
|
accessToken, err := s.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getTemplateURL, accessToken)
|
||||||
|
response, err := util.HTTPGet(uri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
templateList := TemplateList{}
|
||||||
|
err = util.DecodeWithError(response, &templateList, "ListTemplates")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &templateList, nil
|
||||||
|
}
|
||||||
@@ -29,4 +29,4 @@ if err != nil {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
更多使用方法参考[GODOC](https://godoc.org/github.com/silenceper/wechat/tcb)
|
更多使用方法参考[PKG.DEV](https://pkg.go.dev/github.com/silenceper/wechat/v2/miniprogram/tcb)
|
||||||
@@ -3,7 +3,7 @@ package tcb
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -3,7 +3,7 @@ package tcb
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -3,7 +3,7 @@ package tcb
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -130,5 +130,5 @@ func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFi
|
|||||||
}
|
}
|
||||||
batchDeleteFileRes := &BatchDeleteFileRes{}
|
batchDeleteFileRes := &BatchDeleteFileRes{}
|
||||||
err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
|
err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
|
||||||
return batchDeleteFileRes, nil
|
return batchDeleteFileRes, err
|
||||||
}
|
}
|
||||||
15
miniprogram/tcb/tcb.go
Normal file
15
miniprogram/tcb/tcb.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package tcb
|
||||||
|
|
||||||
|
import "github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
|
||||||
|
//Tcb Tencent Cloud Base
|
||||||
|
type Tcb struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewTcb new Tencent Cloud Base
|
||||||
|
func NewTcb(context *context.Context) *Tcb {
|
||||||
|
return &Tcb{
|
||||||
|
context,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package oauth
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
qyRedirectOauthURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&agentid=%s&state=%s#wechat_redirect"
|
|
||||||
qyUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s"
|
|
||||||
qyUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail"
|
|
||||||
)
|
|
||||||
|
|
||||||
//GetQyRedirectURL 获取企业微信跳转的url地址
|
|
||||||
func (oauth *Oauth) GetQyRedirectURL(redirectURI, agentid, scope, state string) (string, error) {
|
|
||||||
//url encode
|
|
||||||
urlStr := url.QueryEscape(redirectURI)
|
|
||||||
return fmt.Sprintf(qyRedirectOauthURL, oauth.AppID, urlStr, scope, agentid, state), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//QyUserInfo 用户授权获取到用户信息
|
|
||||||
type QyUserInfo struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
UserID string `json:"UserId"`
|
|
||||||
DeviceID string `json:"DeviceId"`
|
|
||||||
UserTicket string `json:"user_ticket"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetQyUserInfoByCode 根据code获取企业user_info
|
|
||||||
func (oauth *Oauth) GetQyUserInfoByCode(code string) (result QyUserInfo, err error) {
|
|
||||||
qyAccessToken, e := oauth.GetQyAccessToken()
|
|
||||||
if e != nil {
|
|
||||||
err = e
|
|
||||||
return
|
|
||||||
}
|
|
||||||
urlStr := fmt.Sprintf(qyUserInfoURL, qyAccessToken, code)
|
|
||||||
var response []byte
|
|
||||||
response, err = util.HTTPGet(urlStr)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(response, &result)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if result.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("GetQyUserInfoByCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//QyUserDetail 到用户详情
|
|
||||||
type QyUserDetail struct {
|
|
||||||
util.CommonError
|
|
||||||
|
|
||||||
UserID string `json:"UserId"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Mobile string `json:"mobile"`
|
|
||||||
Gender string `json:"gender"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
Avatar string `json:"avatar"`
|
|
||||||
QrCode string `json:"qr_code"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//GetQyUserDetailUserTicket 根据user_ticket获取到用户详情
|
|
||||||
func (oauth *Oauth) GetQyUserDetailUserTicket(userTicket string) (result QyUserDetail, err error) {
|
|
||||||
var qyAccessToken string
|
|
||||||
qyAccessToken, err = oauth.GetQyAccessToken()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", qyUserDetailURL, qyAccessToken)
|
|
||||||
var response []byte
|
|
||||||
response, err = util.PostJSON(uri, map[string]string{
|
|
||||||
"user_ticket": userTicket,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(response, &result)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if result.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("GetQyUserDetailUserTicket Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
5
officialaccount/README.md
Normal file
5
officialaccount/README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# 微信公众号
|
||||||
|
|
||||||
|
[官方文档](https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html)
|
||||||
|
|
||||||
|
## 快速入门
|
||||||
84
officialaccount/basic/basic.go
Normal file
84
officialaccount/basic/basic.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package basic
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
//清理接口调用次数
|
||||||
|
clearQuotaURL = "https://api.weixin.qq.com/cgi-bin/clear_quota"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Basic struct
|
||||||
|
type Basic struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewBasic 实例
|
||||||
|
func NewBasic(context *context.Context) *Basic {
|
||||||
|
basic := new(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
|
||||||
|
}
|
||||||
|
|
||||||
|
//ClearQuota 清理接口调用次数
|
||||||
|
func (basic *Basic) ClearQuota() error {
|
||||||
|
ak, err := basic.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", clearQuotaURL, ak)
|
||||||
|
data, err := util.PostJSON(url, map[string]string{
|
||||||
|
"appid": basic.AppID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(data, "ClearQuota")
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package qr
|
package basic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -6,8 +6,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
"github.com/silenceper/wechat/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -23,18 +22,6 @@ const (
|
|||||||
actionLimitStr = "QR_LIMIT_STR_SCENE"
|
actionLimitStr = "QR_LIMIT_STR_SCENE"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QR 二维码
|
|
||||||
type QR struct {
|
|
||||||
*context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
//NewQR 二维码实例
|
|
||||||
func NewQR(context *context.Context) *QR {
|
|
||||||
q := new(QR)
|
|
||||||
q.Context = context
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request 临时二维码
|
// Request 临时二维码
|
||||||
type Request struct {
|
type Request struct {
|
||||||
ExpireSeconds int64 `json:"expire_seconds,omitempty"`
|
ExpireSeconds int64 `json:"expire_seconds,omitempty"`
|
||||||
@@ -56,8 +43,8 @@ type Ticket struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetQRTicket 获取二维码 Ticket
|
// GetQRTicket 获取二维码 Ticket
|
||||||
func (q *QR) GetQRTicket(tq *Request) (t *Ticket, err error) {
|
func (basic *Basic) GetQRTicket(tq *Request) (t *Ticket, err error) {
|
||||||
accessToken, err := q.GetAccessToken()
|
accessToken, err := basic.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -75,6 +62,11 @@ func (q *QR) GetQRTicket(tq *Request) (t *Ticket, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if t.ErrMsg != "" {
|
||||||
|
err = fmt.Errorf("get qr_ticket error : errcode=%v , errormsg=%v", t.ErrCode, t.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
52
officialaccount/basic/short_url.go
Normal file
52
officialaccount/basic/short_url.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package basic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// 将一条长链接转成短链接
|
||||||
|
// https://developers.weixin.qq.com/doc/offiaccount/Account_Management/URL_Shortener.html
|
||||||
|
long2shortURL = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=%s"
|
||||||
|
long2shortAction = "long2short"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
reqLong2ShortURL struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
LongURL string `json:"long_url"`
|
||||||
|
}
|
||||||
|
resplong2ShortURL struct {
|
||||||
|
ShortURL string `json:"short_url"`
|
||||||
|
util.CommonError
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Long2ShortURL 将一条长链接转成短链接
|
||||||
|
func (basic *Basic) Long2ShortURL(longURL string) (shortURL string, err error) {
|
||||||
|
var (
|
||||||
|
req = &reqLong2ShortURL{
|
||||||
|
Action: long2shortAction,
|
||||||
|
LongURL: longURL,
|
||||||
|
}
|
||||||
|
resp = new(resplong2ShortURL)
|
||||||
|
ac, uri string
|
||||||
|
responseBytes []byte
|
||||||
|
)
|
||||||
|
ac, err = basic.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri = fmt.Sprintf(long2shortURL, ac)
|
||||||
|
responseBytes, err = util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = util.DecodeWithError(responseBytes, resp, long2shortAction); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
shortURL = resp.ShortURL
|
||||||
|
return
|
||||||
|
}
|
||||||
354
officialaccount/broadcast/broadcast.go
Normal file
354
officialaccount/broadcast/broadcast.go
Normal file
@@ -0,0 +1,354 @@
|
|||||||
|
package broadcast
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sendURLByTag = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"
|
||||||
|
sendURLByOpenID = "https://api.weixin.qq.com/cgi-bin/message/mass/send"
|
||||||
|
deleteSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/delete"
|
||||||
|
previewSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"
|
||||||
|
massStatusSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/get"
|
||||||
|
getSpeedSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/get"
|
||||||
|
setSpeedSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/set"
|
||||||
|
)
|
||||||
|
|
||||||
|
//MsgType 发送消息类型
|
||||||
|
type MsgType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
//MsgTypeNews 图文消息
|
||||||
|
MsgTypeNews MsgType = "mpnews"
|
||||||
|
//MsgTypeText 文本
|
||||||
|
MsgTypeText MsgType = "text"
|
||||||
|
//MsgTypeVoice 语音/音频
|
||||||
|
MsgTypeVoice MsgType = "voice"
|
||||||
|
//MsgTypeImage 图片
|
||||||
|
MsgTypeImage MsgType = "image"
|
||||||
|
//MsgTypeVideo 视频
|
||||||
|
MsgTypeVideo MsgType = "mpvideo"
|
||||||
|
//MsgTypeWxCard 卡券
|
||||||
|
MsgTypeWxCard MsgType = "wxcard"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Broadcast 群发消息
|
||||||
|
type Broadcast struct {
|
||||||
|
*context.Context
|
||||||
|
preview bool
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewBroadcast new
|
||||||
|
func NewBroadcast(ctx *context.Context) *Broadcast {
|
||||||
|
return &Broadcast{ctx, false}
|
||||||
|
}
|
||||||
|
|
||||||
|
//User 发送的用户
|
||||||
|
type User struct {
|
||||||
|
TagID int64
|
||||||
|
OpenID []string
|
||||||
|
}
|
||||||
|
|
||||||
|
//Result 群发返回结果
|
||||||
|
type Result struct {
|
||||||
|
util.CommonError
|
||||||
|
MsgID int64 `json:"msg_id"`
|
||||||
|
MsgDataID int64 `json:"msg_data_id"`
|
||||||
|
MsgStatus string `json:"msg_status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//SpeedResult 群发速度返回结果
|
||||||
|
type SpeedResult struct {
|
||||||
|
util.CommonError
|
||||||
|
Speed int64 `json:"speed"`
|
||||||
|
RealSpeed int64 `json:"realspeed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//sendRequest 发送请求的数据
|
||||||
|
type sendRequest struct {
|
||||||
|
//根据tag获全部发送
|
||||||
|
Filter map[string]interface{} `json:"filter,omitempty"`
|
||||||
|
//根据OpenID发送
|
||||||
|
ToUser interface{} `json:"touser,omitempty"`
|
||||||
|
//发送文本
|
||||||
|
Text map[string]interface{} `json:"text,omitempty"`
|
||||||
|
//发送图文消息
|
||||||
|
Mpnews map[string]interface{} `json:"mpnews,omitempty"`
|
||||||
|
//发送语音
|
||||||
|
Voice map[string]interface{} `json:"voice,omitempty"`
|
||||||
|
//发送图片
|
||||||
|
Images *Image `json:"images,omitempty"`
|
||||||
|
//发送卡券
|
||||||
|
WxCard map[string]interface{} `json:"wxcard,omitempty"`
|
||||||
|
MsgType MsgType `json:"msgtype"`
|
||||||
|
SendIgnoreReprint int32 `json:"send_ignore_reprint,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//Image 发送图片
|
||||||
|
type Image struct {
|
||||||
|
MediaIDs []string `json:"media_ids"`
|
||||||
|
Recommend string `json:"recommend"`
|
||||||
|
NeedOpenComment int32 `json:"need_open_comment"`
|
||||||
|
OnlyFansCanComment int32 `json:"only_fans_can_comment"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendText 群发文本
|
||||||
|
//user 为nil,表示全员发送
|
||||||
|
//&User{TagID:2} 根据tag发送
|
||||||
|
//&User{OpenID:[]string("xxx","xxx")} 根据openid发送
|
||||||
|
func (broadcast *Broadcast) SendText(user *User, content string) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeText,
|
||||||
|
}
|
||||||
|
req.Text = map[string]interface{}{
|
||||||
|
"content": content,
|
||||||
|
}
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendText")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendNews 发送图文
|
||||||
|
func (broadcast *Broadcast) SendNews(user *User, mediaID string, ignoreReprint bool) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeNews,
|
||||||
|
}
|
||||||
|
if ignoreReprint {
|
||||||
|
req.SendIgnoreReprint = 1
|
||||||
|
}
|
||||||
|
req.Mpnews = map[string]interface{}{
|
||||||
|
"media_id": mediaID,
|
||||||
|
}
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendNews")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendVoice 发送语音
|
||||||
|
func (broadcast *Broadcast) SendVoice(user *User, mediaID string) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeVoice,
|
||||||
|
}
|
||||||
|
req.Voice = map[string]interface{}{
|
||||||
|
"media_id": mediaID,
|
||||||
|
}
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendVoice")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendImage 发送图片
|
||||||
|
func (broadcast *Broadcast) SendImage(user *User, images *Image) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeImage,
|
||||||
|
}
|
||||||
|
req.Images = images
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendImage")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendVideo 发送视频
|
||||||
|
func (broadcast *Broadcast) SendVideo(user *User, mediaID string, title, description string) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeVideo,
|
||||||
|
}
|
||||||
|
req.Voice = map[string]interface{}{
|
||||||
|
"media_id": mediaID,
|
||||||
|
"title": title,
|
||||||
|
"description": description,
|
||||||
|
}
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendVideo")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendWxCard 发送卡券
|
||||||
|
func (broadcast *Broadcast) SendWxCard(user *User, cardID string) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := &sendRequest{
|
||||||
|
ToUser: nil,
|
||||||
|
MsgType: MsgTypeWxCard,
|
||||||
|
}
|
||||||
|
req.WxCard = map[string]interface{}{
|
||||||
|
"card_id": cardID,
|
||||||
|
}
|
||||||
|
req, sendURL := broadcast.chooseTagOrOpenID(user, req)
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", sendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "SendWxCard")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Delete 删除群发消息
|
||||||
|
func (broadcast *Broadcast) Delete(msgID int64, articleIDx int64) error {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{
|
||||||
|
"msg_id": msgID,
|
||||||
|
"article_idx": articleIDx,
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", deleteSendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(data, "Delete")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preview 预览
|
||||||
|
func (broadcast *Broadcast) Preview() *Broadcast {
|
||||||
|
broadcast.preview = true
|
||||||
|
return broadcast
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMassStatus 获取群发状态
|
||||||
|
func (broadcast *Broadcast) GetMassStatus(msgID string) (*Result, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{
|
||||||
|
"msg_id": msgID,
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", massStatusSendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &Result{}
|
||||||
|
err = util.DecodeWithError(data, res, "GetMassStatus")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSpeed 获取群发速度
|
||||||
|
func (broadcast *Broadcast) GetSpeed() (*SpeedResult, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", getSpeedSendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &SpeedResult{}
|
||||||
|
err = util.DecodeWithError(data, res, "GetSpeed")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSpeed 设置群发速度
|
||||||
|
func (broadcast *Broadcast) SetSpeed(speed int) (*SpeedResult, error) {
|
||||||
|
ak, err := broadcast.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req := map[string]interface{}{
|
||||||
|
"speed": speed,
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", setSpeedSendURL, ak)
|
||||||
|
data, err := util.PostJSON(url, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := &SpeedResult{}
|
||||||
|
err = util.DecodeWithError(data, res, "SetSpeed")
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (broadcast *Broadcast) chooseTagOrOpenID(user *User, req *sendRequest) (ret *sendRequest, url string) {
|
||||||
|
sendURL := ""
|
||||||
|
if user == nil {
|
||||||
|
req.Filter = map[string]interface{}{
|
||||||
|
"is_to_all": true,
|
||||||
|
}
|
||||||
|
sendURL = sendURLByTag
|
||||||
|
} else {
|
||||||
|
if broadcast.preview {
|
||||||
|
// 预览
|
||||||
|
req.ToUser = user.OpenID
|
||||||
|
sendURL = previewSendURL
|
||||||
|
} else {
|
||||||
|
if user.TagID != 0 {
|
||||||
|
req.Filter = map[string]interface{}{
|
||||||
|
"is_to_all": false,
|
||||||
|
"tag_id": user.TagID,
|
||||||
|
}
|
||||||
|
sendURL = sendURLByTag
|
||||||
|
}
|
||||||
|
if len(user.OpenID) != 0 {
|
||||||
|
req.ToUser = user.OpenID
|
||||||
|
sendURL = sendURLByOpenID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return req, sendURL
|
||||||
|
}
|
||||||
14
officialaccount/config/config.go
Normal file
14
officialaccount/config/config.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config config for 微信公众号
|
||||||
|
type Config struct {
|
||||||
|
AppID string `json:"app_id"` //appid
|
||||||
|
AppSecret string `json:"app_secret"` //appsecret
|
||||||
|
Token string `json:"token"` //token
|
||||||
|
EncodingAESKey string `json:"encoding_aes_key"` //EncodingAESKey
|
||||||
|
Cache cache.Cache
|
||||||
|
}
|
||||||
12
officialaccount/context/context.go
Normal file
12
officialaccount/context/context.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Context struct
|
||||||
|
type Context struct {
|
||||||
|
*config.Config
|
||||||
|
credential.AccessTokenHandle
|
||||||
|
}
|
||||||
271
officialaccount/datacube/broadcast.go
Normal file
271
officialaccount/datacube/broadcast.go
Normal file
@@ -0,0 +1,271 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getArticleSummary = "https://api.weixin.qq.com/datacube/getarticlesummary"
|
||||||
|
getArticleTotal = "https://api.weixin.qq.com/datacube/getarticletotal"
|
||||||
|
getUserRead = "https://api.weixin.qq.com/datacube/getuserread"
|
||||||
|
getUserReadHour = "https://api.weixin.qq.com/datacube/getuserreadhour"
|
||||||
|
getUserShare = "https://api.weixin.qq.com/datacube/getusershare"
|
||||||
|
getUserShareHour = "https://api.weixin.qq.com/datacube/getusersharehour"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ResArticleSummary 获取图文群发每日数据响应
|
||||||
|
type ResArticleSummary struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
MsgID string `json:"msgid"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
IntPageReadUser int `json:"int_page_read_user"`
|
||||||
|
IntPageReadCount int `json:"int_page_read_count"`
|
||||||
|
OriPageReadUser int `json:"ori_page_read_user"`
|
||||||
|
OriPageReadCount int `json:"ori_page_read_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
AddToFavUser int `json:"add_to_fav_user"`
|
||||||
|
AddToFavCount int `json:"add_to_fav_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResArticleTotal 获取图文群发总数据响应
|
||||||
|
type ResArticleTotal struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
MsgID string `json:"msgid"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Details []ArticleTotalDetails `json:"details"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ArticleTotalDetails 获取图文群发总数据响应文字详情
|
||||||
|
type ArticleTotalDetails struct {
|
||||||
|
StatDate string `json:"stat_date"`
|
||||||
|
TargetUser int `json:"target_user"`
|
||||||
|
IntPageReadUser int `json:"int_page_read_user"`
|
||||||
|
IntPageReadCount int `json:"int_page_read_count"`
|
||||||
|
OriPageReadUser int `json:"ori_page_read_user"`
|
||||||
|
OriPageReadCount int `json:"ori_page_read_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
AddToFavUser int `json:"add_to_fav_user"`
|
||||||
|
AddToFavCount int `json:"add_to_fav_count"`
|
||||||
|
IntPageFromSessionReadUser int `json:"int_page_from_session_read_user"`
|
||||||
|
IntPageFromSessionReadCount int `json:"int_page_from_session_read_count"`
|
||||||
|
IntPageFromHistMsgReadUser int `json:"int_page_from_hist_msg_read_user"`
|
||||||
|
IntPageFromHistMsgReadCount int `json:"int_page_from_hist_msg_read_count"`
|
||||||
|
IntPageFromFeedReadUser int `json:"int_page_from_feed_read_user"`
|
||||||
|
IntPageFromFeedReadCount int `json:"int_page_from_feed_read_count"`
|
||||||
|
IntPageFromFriendsReadUser int `json:"int_page_from_friends_read_user"`
|
||||||
|
IntPageFromFriendsReadCount int `json:"int_page_from_friends_read_count"`
|
||||||
|
IntPageFromOtherReadUser int `json:"int_page_from_other_read_user"`
|
||||||
|
IntPageFromOtherReadCount int `json:"int_page_from_other_read_count"`
|
||||||
|
FeedShareFromSessionUser int `json:"feed_share_from_session_user"`
|
||||||
|
FeedShareFromSessionCnt int `json:"feed_share_from_session_cnt"`
|
||||||
|
FeedShareFromFeedUser int `json:"feed_share_from_feed_user"`
|
||||||
|
FeedShareFromFeedCnt int `json:"feed_share_from_feed_cnt"`
|
||||||
|
FeedShareFromOtherUser int `json:"feed_share_from_other_user"`
|
||||||
|
FeedShareFromOtherCnt int `json:"feed_share_from_other_cnt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUserRead 获取图文统计数据响应
|
||||||
|
type ResUserRead struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
UserSource int `json:"user_source"`
|
||||||
|
IntPageReadUser int `json:"int_page_read_user"`
|
||||||
|
IntPageReadCount int `json:"int_page_read_count"`
|
||||||
|
OriPageReadUser int `json:"ori_page_read_user"`
|
||||||
|
OriPageReadCount int `json:"ori_page_read_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
AddToFavUser int `json:"add_to_fav_user"`
|
||||||
|
AddToFavCount int `json:"add_to_fav_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUserReadHour 获取图文统计分时数据
|
||||||
|
type ResUserReadHour struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
RefHour int `json:"ref_hour"`
|
||||||
|
UserSource int `json:"user_source"`
|
||||||
|
IntPageReadUser int `json:"int_page_read_user"`
|
||||||
|
IntPageReadCount int `json:"int_page_read_count"`
|
||||||
|
OriPageReadUser int `json:"ori_page_read_user"`
|
||||||
|
OriPageReadCount int `json:"ori_page_read_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
AddToFavUser int `json:"add_to_fav_user"`
|
||||||
|
AddToFavCount int `json:"add_to_fav_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUserShare 获取图文分享转发数据
|
||||||
|
type ResUserShare struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
ShareScene int `json:"share_scene"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUserShareHour 获取图文分享转发分时数据
|
||||||
|
type ResUserShareHour struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
RefHour int `json:"ref_hour"`
|
||||||
|
ShareScene int `json:"share_scene"`
|
||||||
|
ShareCount int `json:"share_count"`
|
||||||
|
ShareUser int `json:"share_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetArticleSummary 获取图文群发每日数据
|
||||||
|
func (cube *DataCube) GetArticleSummary(s string, e string) (resArticleSummary ResArticleSummary, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getArticleSummary, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resArticleSummary, "GetArticleSummary")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetArticleTotal 获取图文群发总数据
|
||||||
|
func (cube *DataCube) GetArticleTotal(s string, e string) (resArticleTotal ResArticleTotal, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getArticleTotal, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resArticleTotal, "GetArticleTotal")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserRead 获取图文统计数据
|
||||||
|
func (cube *DataCube) GetUserRead(s string, e string) (resUserRead ResUserRead, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserRead, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserRead, "GetUserRead")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserReadHour 获取图文统计分时数据
|
||||||
|
func (cube *DataCube) GetUserReadHour(s string, e string) (resUserReadHour ResUserReadHour, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserReadHour, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserReadHour, "GetUserReadHour")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserShare 获取图文分享转发数据
|
||||||
|
func (cube *DataCube) GetUserShare(s string, e string) (resUserShare ResUserShare, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserShare, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserShare, "GetUserShare")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserShareHour 获取图文分享转发分时数据
|
||||||
|
func (cube *DataCube) GetUserShareHour(s string, e string) (resUserShareHour ResUserShareHour, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserShareHour, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserShareHour, "GetUserShareHour")
|
||||||
|
return
|
||||||
|
}
|
||||||
22
officialaccount/datacube/datacube.go
Normal file
22
officialaccount/datacube/datacube.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type reqDate struct {
|
||||||
|
BeginDate string `json:"begin_date"`
|
||||||
|
EndDate string `json:"end_date"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//DataCube 数据统计
|
||||||
|
type DataCube struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewCube 数据统计
|
||||||
|
func NewCube(context *context.Context) *DataCube {
|
||||||
|
dataCube := new(DataCube)
|
||||||
|
dataCube.Context = context
|
||||||
|
return dataCube
|
||||||
|
}
|
||||||
83
officialaccount/datacube/interface.go
Normal file
83
officialaccount/datacube/interface.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getInterfaceSummary = "https://api.weixin.qq.com/datacube/getinterfacesummary"
|
||||||
|
getInterfaceSummaryHour = "https://api.weixin.qq.com/datacube/getinterfacesummaryhour"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ResInterfaceSummary 接口分析数据响应
|
||||||
|
type ResInterfaceSummary struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
CallbackCount int `json:"callback_count"`
|
||||||
|
FailCount int `json:"fail_count"`
|
||||||
|
TotalTimeCost int `json:"total_time_cost"`
|
||||||
|
MaxTimeCost int `json:"max_time_cost"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResInterfaceSummaryHour 接口分析分时数据响应
|
||||||
|
type ResInterfaceSummaryHour struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
RefHour int `json:"ref_hour"`
|
||||||
|
CallbackCount int `json:"callback_count"`
|
||||||
|
FailCount int `json:"fail_count"`
|
||||||
|
TotalTimeCost int `json:"total_time_cost"`
|
||||||
|
MaxTimeCost int `json:"max_time_cost"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetInterfaceSummary 获取接口分析数据
|
||||||
|
func (cube *DataCube) GetInterfaceSummary(s string, e string) (resInterfaceSummary ResInterfaceSummary, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getInterfaceSummary, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resInterfaceSummary, "GetInterfaceSummary")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetInterfaceSummaryHour 获取接口分析分时数据
|
||||||
|
func (cube *DataCube) GetInterfaceSummaryHour(s string, e string) (resInterfaceSummaryHour ResInterfaceSummaryHour, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getInterfaceSummaryHour, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resInterfaceSummaryHour, "GetInterfaceSummaryHour")
|
||||||
|
return
|
||||||
|
}
|
||||||
253
officialaccount/datacube/message.go
Normal file
253
officialaccount/datacube/message.go
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getUpstreamMsg = "https://api.weixin.qq.com/datacube/getupstreammsg"
|
||||||
|
getUpstreamMsgHour = "https://api.weixin.qq.com/datacube/getupstreammsghour"
|
||||||
|
getUpstreamMsgWeek = "https://api.weixin.qq.com/datacube/getupstreammsgweek"
|
||||||
|
getUpstreamMsgMonth = "https://api.weixin.qq.com/datacube/getupstreammsgmonth"
|
||||||
|
getUpstreamMsgDist = "https://api.weixin.qq.com/datacube/getupstreammsgdist"
|
||||||
|
getUpstreamMsgDistWeek = "https://api.weixin.qq.com/datacube/getupstreammsgdistweek"
|
||||||
|
getUpstreamMsgDistMonth = "https://api.weixin.qq.com/datacube/getupstreammsgdistmonth"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ResUpstreamMsg 获取消息发送概况数据响应
|
||||||
|
type ResUpstreamMsg struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
MsgType int `json:"msg_type"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
MsgCount int `json:"msg_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgHour 获取消息分送分时数据响应
|
||||||
|
type ResUpstreamMsgHour struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
RefHour int `json:"ref_hour"`
|
||||||
|
MsgType int `json:"msg_type"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
MsgCount int `json:"msg_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgWeek 获取消息发送周数据响应
|
||||||
|
type ResUpstreamMsgWeek struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
MsgType int `json:"msg_type"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
MsgCount int `json:"msg_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgMonth 获取消息发送月数据响应
|
||||||
|
type ResUpstreamMsgMonth struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
MsgType int `json:"msg_type"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
MsgCount int `json:"msg_count"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgDist 获取消息发送分布数据响应
|
||||||
|
type ResUpstreamMsgDist struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
CountInterval int `json:"count_interval"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgDistWeek 获取消息发送分布周数据响应
|
||||||
|
type ResUpstreamMsgDistWeek struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
CountInterval int `json:"count_interval"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUpstreamMsgDistMonth 获取消息发送分布月数据响应
|
||||||
|
type ResUpstreamMsgDistMonth struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
CountInterval int `json:"count_interval"`
|
||||||
|
MsgUser int `json:"msg_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsg 获取消息发送概况数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsg(s string, e string) (resUpstreamMsg ResUpstreamMsg, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsg, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsg, "GetUpstreamMsg")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgHour 获取消息分送分时数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgHour(s string, e string) (resUpstreamMsgHour ResUpstreamMsgHour, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgHour, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgHour, "GetUpstreamMsgHour")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgWeek 获取消息发送周数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgWeek(s string, e string) (resUpstreamMsgWeek ResUpstreamMsgWeek, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgWeek, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgWeek, "GetUpstreamMsgWeek")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgMonth 获取消息发送月数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgMonth(s string, e string) (resUpstreamMsgMonth ResUpstreamMsgMonth, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgMonth, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgMonth, "GetUpstreamMsgMonth")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgDist 获取消息发送分布数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgDist(s string, e string) (resUpstreamMsgDist ResUpstreamMsgDist, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgDist, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgDist, "GetUpstreamMsgDist")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgDistWeek 获取消息发送分布周数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgDistWeek(s string, e string) (resUpstreamMsgDistWeek ResUpstreamMsgDistWeek, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgDistWeek, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgDistWeek, "GetUpstreamMsgDistWeek")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUpstreamMsgDistMonth 获取消息发送分布月数据
|
||||||
|
func (cube *DataCube) GetUpstreamMsgDistMonth(s string, e string) (resUpstreamMsgDistMonth ResUpstreamMsgDistMonth, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUpstreamMsgDistMonth, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUpstreamMsgDistMonth, "GetUpstreamMsgDistMonth")
|
||||||
|
return
|
||||||
|
}
|
||||||
272
officialaccount/datacube/publisher.go
Normal file
272
officialaccount/datacube/publisher.go
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
//AdSlot 广告位类型
|
||||||
|
type AdSlot string
|
||||||
|
|
||||||
|
const (
|
||||||
|
//SlotIDBizBottom 公众号底部广告
|
||||||
|
SlotIDBizBottom AdSlot = "SLOT_ID_BIZ_BOTTOM"
|
||||||
|
//SlotIDBizMidContext 公众号文中广告
|
||||||
|
SlotIDBizMidContext AdSlot = "SLOT_ID_BIZ_MID_CONTEXT"
|
||||||
|
//SlotIDBizVideoEnd 公众号视频后贴
|
||||||
|
SlotIDBizVideoEnd AdSlot = "SLOT_ID_BIZ_VIDEO_END"
|
||||||
|
//SlotIDBizSponsor 公众号互选广告
|
||||||
|
SlotIDBizSponsor AdSlot = "SLOT_ID_BIZ_SPONSOR"
|
||||||
|
//SlotIDBizCps 公众号返佣商品
|
||||||
|
SlotIDBizCps AdSlot = "SLOT_ID_BIZ_CPS"
|
||||||
|
//SlotIDWeappBanner 小程序banner
|
||||||
|
SlotIDWeappBanner AdSlot = "SLOT_ID_WEAPP_BANNER"
|
||||||
|
//SlotIDWeappRewardVideo 小程序激励视频
|
||||||
|
SlotIDWeappRewardVideo AdSlot = "SLOT_ID_WEAPP_REWARD_VIDEO"
|
||||||
|
//SlotIDWeappInterstitial 小程序插屏广告
|
||||||
|
SlotIDWeappInterstitial AdSlot = "SLOT_ID_WEAPP_INTERSTITIAL"
|
||||||
|
//SlotIDWeappVideoFeeds 小程序视频广告
|
||||||
|
SlotIDWeappVideoFeeds AdSlot = "SLOT_ID_WEAPP_VIDEO_FEEDS"
|
||||||
|
//SlotIDWeappVideoBegin 小程序视频前贴
|
||||||
|
SlotIDWeappVideoBegin AdSlot = "SLOT_ID_WEAPP_VIDEO_BEGIN"
|
||||||
|
//SlotIDWeappBox 小程序格子广告
|
||||||
|
SlotIDWeappBox AdSlot = "SLOT_ID_WEAPP_BOX"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
publisherURL = "https://api.weixin.qq.com/publisher/stat"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
actionPublisherAdPosGeneral = "publisher_adpos_general"
|
||||||
|
actionPublisherCpsGeneral = "publisher_cps_general"
|
||||||
|
actionPublisherSettlement = "publisher_settlement"
|
||||||
|
)
|
||||||
|
|
||||||
|
//BaseResp 错误信息
|
||||||
|
type BaseResp struct {
|
||||||
|
ErrMsg string `json:"err_msg"`
|
||||||
|
Ret int `json:"ret"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResPublisherAdPos 公众号分广告位数据响应
|
||||||
|
type ResPublisherAdPos struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
BaseResp BaseResp `json:"base_resp"`
|
||||||
|
List []ResAdPosList `json:"list"`
|
||||||
|
Summary ResAdPosSummary `json:"summary"`
|
||||||
|
TotalNum int `json:"total_num"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResAdPosList 公众号分广告位列表
|
||||||
|
type ResAdPosList struct {
|
||||||
|
SlotID int64 `json:"slot_id"`
|
||||||
|
AdSlot string `json:"ad_slot"`
|
||||||
|
Date string `json:"date"`
|
||||||
|
ReqSuccCount int `json:"req_succ_count"`
|
||||||
|
ExposureCount int `json:"exposure_count"`
|
||||||
|
ExposureRate float64 `json:"exposure_rate"`
|
||||||
|
ClickCount int `json:"click_count"`
|
||||||
|
ClickRate float64 `json:"click_rate"`
|
||||||
|
Income int `json:"income"`
|
||||||
|
Ecpm float64 `json:"ecpm"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResAdPosSummary 公众号分广告位概览
|
||||||
|
type ResAdPosSummary struct {
|
||||||
|
ReqSuccCount int `json:"req_succ_count"`
|
||||||
|
ExposureCount int `json:"exposure_count"`
|
||||||
|
ExposureRate float64 `json:"exposure_rate"`
|
||||||
|
ClickCount int `json:"click_count"`
|
||||||
|
ClickRate float64 `json:"click_rate"`
|
||||||
|
Income int `json:"income"`
|
||||||
|
Ecpm float64 `json:"ecpm"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResPublisherCps 公众号返佣商品数据响应
|
||||||
|
type ResPublisherCps struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
BaseResp BaseResp `json:"base_resp"`
|
||||||
|
List []ResCpsList `json:"list"`
|
||||||
|
Summary ResCpsSummary `json:"summary"`
|
||||||
|
TotalNum int `json:"total_num"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResCpsList 公众号返佣商品列表
|
||||||
|
type ResCpsList struct {
|
||||||
|
Date string `json:"date"`
|
||||||
|
ExposureCount int `json:"exposure_count"`
|
||||||
|
ClickCount int `json:"click_count"`
|
||||||
|
ClickRate float64 `json:"click_rate"`
|
||||||
|
OrderCount int `json:"order_count"`
|
||||||
|
OrderRate float64 `json:"order_rate"`
|
||||||
|
TotalFee int `json:"total_fee"`
|
||||||
|
TotalCommission int `json:"total_commission"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResCpsSummary 公众号返佣概览
|
||||||
|
type ResCpsSummary struct {
|
||||||
|
ExposureCount int `json:"exposure_count"`
|
||||||
|
ClickCount int `json:"click_count"`
|
||||||
|
ClickRate float64 `json:"click_rate"`
|
||||||
|
OrderCount int `json:"order_count"`
|
||||||
|
OrderRate float64 `json:"order_rate"`
|
||||||
|
TotalFee int `json:"total_fee"`
|
||||||
|
TotalCommission int `json:"total_commission"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResPublisherSettlement 公众号结算收入数据及结算主体信息响应
|
||||||
|
type ResPublisherSettlement struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
BaseResp BaseResp `json:"base_resp"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
PenaltyAll int `json:"penalty_all"`
|
||||||
|
RevenueAll int64 `json:"revenue_all"`
|
||||||
|
SettledRevenueAll int64 `json:"settled_revenue_all"`
|
||||||
|
SettlementList []SettlementList `json:"settlement_list"`
|
||||||
|
TotalNum int `json:"total_num"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//SettlementList 结算单列表
|
||||||
|
type SettlementList struct {
|
||||||
|
Date string `json:"date"`
|
||||||
|
Zone string `json:"zone"`
|
||||||
|
Month string `json:"month"`
|
||||||
|
Order int `json:"order"`
|
||||||
|
SettStatus int `json:"sett_status"`
|
||||||
|
SettledRevenue int `json:"settled_revenue"`
|
||||||
|
SettNo string `json:"sett_no"`
|
||||||
|
MailSendCnt string `json:"mail_send_cnt"`
|
||||||
|
SlotRevenue []SlotRevenue `json:"slot_revenue"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//SlotRevenue 产生收入的广告
|
||||||
|
type SlotRevenue struct {
|
||||||
|
SlotID string `json:"slot_id"`
|
||||||
|
SlotSettledRevenue int `json:"slot_settled_revenue"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ParamsPublisher 拉取数据参数
|
||||||
|
type ParamsPublisher struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
StartDate string `json:"start_date"`
|
||||||
|
EndDate string `json:"end_date"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
PageSize int `json:"page_size"`
|
||||||
|
AdSlot AdSlot `json:"ad_slot"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchData 拉取统计数据
|
||||||
|
func (cube *DataCube) fetchData(params ParamsPublisher) (response []byte, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
v := url.Values{}
|
||||||
|
v.Add("action", params.Action)
|
||||||
|
v.Add("access_token", accessToken)
|
||||||
|
v.Add("page", strconv.Itoa(params.Page))
|
||||||
|
v.Add("page_size", strconv.Itoa(params.PageSize))
|
||||||
|
v.Add("start_date", params.StartDate)
|
||||||
|
v.Add("end_date", params.EndDate)
|
||||||
|
if params.AdSlot != "" {
|
||||||
|
v.Add("ad_slot", string(params.AdSlot))
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?%s", publisherURL, v.Encode())
|
||||||
|
|
||||||
|
response, err = util.HTTPGet(uri)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetPublisherAdPosGeneral 获取公众号分广告位数据
|
||||||
|
func (cube *DataCube) GetPublisherAdPosGeneral(startDate, endDate string, page, pageSize int, adSlot AdSlot) (resPublisherAdPos ResPublisherAdPos, err error) {
|
||||||
|
params := ParamsPublisher{
|
||||||
|
Action: actionPublisherAdPosGeneral,
|
||||||
|
StartDate: startDate,
|
||||||
|
EndDate: endDate,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
AdSlot: adSlot,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := cube.fetchData(params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resPublisherAdPos, "GetPublisherAdPosGeneral")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resPublisherAdPos.BaseResp.Ret != 0 {
|
||||||
|
err = fmt.Errorf("GetPublisherAdPosGeneral Error , errcode=%d , errmsg=%s", resPublisherAdPos.BaseResp.Ret, resPublisherAdPos.BaseResp.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetPublisherCpsGeneral 获取公众号返佣商品数据
|
||||||
|
func (cube *DataCube) GetPublisherCpsGeneral(startDate, endDate string, page, pageSize int) (resPublisherCps ResPublisherCps, err error) {
|
||||||
|
params := ParamsPublisher{
|
||||||
|
Action: actionPublisherCpsGeneral,
|
||||||
|
StartDate: startDate,
|
||||||
|
EndDate: endDate,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := cube.fetchData(params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resPublisherCps, "GetPublisherCpsGeneral")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resPublisherCps.BaseResp.Ret != 0 {
|
||||||
|
err = fmt.Errorf("GetPublisherCpsGeneral Error , errcode=%d , errmsg=%s", resPublisherCps.BaseResp.Ret, resPublisherCps.BaseResp.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetPublisherSettlement 获取公众号结算收入数据及结算主体信息
|
||||||
|
func (cube *DataCube) GetPublisherSettlement(startDate, endDate string, page, pageSize int) (resPublisherSettlement ResPublisherSettlement, err error) {
|
||||||
|
params := ParamsPublisher{
|
||||||
|
Action: actionPublisherSettlement,
|
||||||
|
StartDate: startDate,
|
||||||
|
EndDate: endDate,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := cube.fetchData(params)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resPublisherSettlement, "GetPublisherSettlement")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resPublisherSettlement.BaseResp.Ret != 0 {
|
||||||
|
err = fmt.Errorf("GetPublisherSettlement Error , errcode=%d , errmsg=%s", resPublisherSettlement.BaseResp.Ret, resPublisherSettlement.BaseResp.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
78
officialaccount/datacube/user.go
Normal file
78
officialaccount/datacube/user.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package datacube
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getUserSummary = "https://api.weixin.qq.com/datacube/getusersummary"
|
||||||
|
getUserAccumulate = "https://api.weixin.qq.com/datacube/getusercumulate"
|
||||||
|
)
|
||||||
|
|
||||||
|
//ResUserSummary 获取用户增减数据响应
|
||||||
|
type ResUserSummary struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
UserSource int `json:"user_source"`
|
||||||
|
NewUser int `json:"new_user"`
|
||||||
|
CancelUser int `json:"cancel_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ResUserAccumulate 获取累计用户数据响应
|
||||||
|
type ResUserAccumulate struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
List []struct {
|
||||||
|
RefDate string `json:"ref_date"`
|
||||||
|
CumulateUser int `json:"cumulate_user"`
|
||||||
|
} `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserSummary 获取用户增减数据
|
||||||
|
func (cube *DataCube) GetUserSummary(s string, e string) (resUserSummary ResUserSummary, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserSummary, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserSummary, "GetUserSummary")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetUserAccumulate 获取累计用户数据
|
||||||
|
func (cube *DataCube) GetUserAccumulate(s string, e string) (resUserAccumulate ResUserAccumulate, err error) {
|
||||||
|
accessToken, err := cube.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getUserAccumulate, accessToken)
|
||||||
|
reqDate := &reqDate{
|
||||||
|
BeginDate: s,
|
||||||
|
EndDate: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := util.PostJSON(uri, reqDate)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &resUserAccumulate, "GetUserAccumulate")
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
|
//Package device 设备相关接口
|
||||||
package device
|
package device
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -41,7 +42,7 @@ type ReqDevice struct {
|
|||||||
// ble – 3
|
// ble – 3
|
||||||
// wifi -- 4
|
// wifi -- 4
|
||||||
// 一个设备可以支持多种连接类型,用符号"|"做分割,客户端优先选择靠前的连接方式(优先级按|关系的排序依次降低),举例:
|
// 一个设备可以支持多种连接类型,用符号"|"做分割,客户端优先选择靠前的连接方式(优先级按|关系的排序依次降低),举例:
|
||||||
// 1:表示设备仅支持andiod classic bluetooth 1|2:表示设备支持andiod 和ios 两种classic bluetooth,但是客户端优先选择andriod classic bluetooth 协议,如果andriod classic bluetooth协议连接失败,再选择ios classic bluetooth协议进行连接
|
// 1:表示设备仅支持andiod classic bluetooth 1|2:表示设备支持android 和ios 两种classic bluetooth,但是客户端优先选择android classic bluetooth 协议,如果android classic bluetooth协议连接失败,再选择ios classic bluetooth协议进行连接
|
||||||
// (注:安卓平台不同时支持BLE和classic类型)
|
// (注:安卓平台不同时支持BLE和classic类型)
|
||||||
ConnectProtocol string `json:"connect_protocol"`
|
ConnectProtocol string `json:"connect_protocol"`
|
||||||
//auth及通信的加密key,第三方需要将key烧制在设备上(128bit),格式采用16进制串的方式(长度为32字节),不需要0X前缀,如: 1234567890ABCDEF1234567890ABCDEF
|
//auth及通信的加密key,第三方需要将key烧制在设备上(128bit),格式采用16进制串的方式(长度为32字节),不需要0X前缀,如: 1234567890ABCDEF1234567890ABCDEF
|
||||||
@@ -56,7 +57,7 @@ type ReqDevice struct {
|
|||||||
// 0:不加密的version
|
// 0:不加密的version
|
||||||
// 1:version 1
|
// 1:version 1
|
||||||
AuthVer string `json:"auth_ver"`
|
AuthVer string `json:"auth_ver"`
|
||||||
// 表示mac地址在厂商广播manufature data里含有mac地址的偏移,取值如下:
|
// 表示mac地址在厂商广播manufacture data里含有mac地址的偏移,取值如下:
|
||||||
// -1:在尾部、
|
// -1:在尾部、
|
||||||
// -2:表示不包含mac地址 其他:非法偏移
|
// -2:表示不包含mac地址 其他:非法偏移
|
||||||
ManuMacPos string `json:"manu_mac_pos"`
|
ManuMacPos string `json:"manu_mac_pos"`
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReqBind 设备绑定解绑共通实体
|
// ReqBind 设备绑定解绑共通实体
|
||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
//ResCreateQRCode 获取二维码的返回实体
|
//ResCreateQRCode 获取二维码的返回实体
|
||||||
@@ -60,7 +60,7 @@ func (d *Device) VerifyQRCode(ticket string) (res ResVerifyQRCode, err error) {
|
|||||||
req := map[string]interface{}{
|
req := map[string]interface{}{
|
||||||
"ticket": ticket,
|
"ticket": ticket,
|
||||||
}
|
}
|
||||||
fmt.Println(req)
|
|
||||||
var response []byte
|
var response []byte
|
||||||
if response, err = util.PostJSON(uri, req); err != nil {
|
if response, err = util.PostJSON(uri, req); err != nil {
|
||||||
return
|
return
|
||||||
64
officialaccount/js/js.go
Normal file
64
officialaccount/js/js.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package js
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Js struct
|
||||||
|
type Js struct {
|
||||||
|
*context.Context
|
||||||
|
credential.JsTicketHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config 返回给用户jssdk配置信息
|
||||||
|
type Config struct {
|
||||||
|
AppID string `json:"app_id"`
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
NonceStr string `json:"nonce_str"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewJs init
|
||||||
|
func NewJs(context *context.Context) *Js {
|
||||||
|
js := new(Js)
|
||||||
|
js.Context = context
|
||||||
|
jsTicketHandle := credential.NewDefaultJsTicket(context.AppID, credential.CacheKeyOfficialAccountPrefix, context.Cache)
|
||||||
|
js.SetJsTicketHandle(jsTicketHandle)
|
||||||
|
return js
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetJsTicketHandle 自定义js ticket取值方式
|
||||||
|
func (js *Js) SetJsTicketHandle(ticketHandle credential.JsTicketHandle) {
|
||||||
|
js.JsTicketHandle = ticketHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetConfig 获取jssdk需要的配置参数
|
||||||
|
//uri 为当前网页地址
|
||||||
|
func (js *Js) GetConfig(uri string) (config *Config, err error) {
|
||||||
|
config = new(Config)
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = js.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ticketStr string
|
||||||
|
ticketStr, err = js.GetTicket(accessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nonceStr := util.RandomStr(16)
|
||||||
|
timestamp := util.GetCurrTS()
|
||||||
|
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri)
|
||||||
|
sigStr := util.Signature(str)
|
||||||
|
|
||||||
|
config.AppID = js.AppID
|
||||||
|
config.NonceStr = nonceStr
|
||||||
|
config.Timestamp = timestamp
|
||||||
|
config.Signature = sigStr
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -5,15 +5,32 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
|
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
|
||||||
addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material"
|
updateNewsURL = "https://api.weixin.qq.com/cgi-bin/material/update_news"
|
||||||
delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material"
|
addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material"
|
||||||
getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material"
|
delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material"
|
||||||
|
getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material"
|
||||||
|
getMaterialCountURL = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount"
|
||||||
|
batchGetMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"
|
||||||
|
)
|
||||||
|
|
||||||
|
//PermanentMaterialType 永久素材类型
|
||||||
|
type PermanentMaterialType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
//PermanentMaterialTypeImage 永久素材图片类型(image)
|
||||||
|
PermanentMaterialTypeImage PermanentMaterialType = "image"
|
||||||
|
//PermanentMaterialTypeVideo 永久素材视频类型(video)
|
||||||
|
PermanentMaterialTypeVideo PermanentMaterialType = "video"
|
||||||
|
//PermanentMaterialTypeVoice 永久素材语音类型 (voice)
|
||||||
|
PermanentMaterialTypeVoice PermanentMaterialType = "voice"
|
||||||
|
//PermanentMaterialTypeNews 永久素材图文类型(news)
|
||||||
|
PermanentMaterialTypeNews PermanentMaterialType = "news"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Material 素材管理
|
//Material 素材管理
|
||||||
@@ -55,6 +72,9 @@ func (material *Material) GetNews(id string) ([]*Article, error) {
|
|||||||
}
|
}
|
||||||
req.MediaID = id
|
req.MediaID = id
|
||||||
responseBytes, err := util.PostJSON(uri, req)
|
responseBytes, err := util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var res struct {
|
var res struct {
|
||||||
NewsItem []*Article `json:"news_item"`
|
NewsItem []*Article `json:"news_item"`
|
||||||
@@ -91,6 +111,9 @@ func (material *Material) AddNews(articles []*Article) (mediaID string, err erro
|
|||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", addNewsURL, accessToken)
|
uri := fmt.Sprintf("%s?access_token=%s", addNewsURL, accessToken)
|
||||||
responseBytes, err := util.PostJSON(uri, req)
|
responseBytes, err := util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
var res resArticles
|
var res resArticles
|
||||||
err = json.Unmarshal(responseBytes, &res)
|
err = json.Unmarshal(responseBytes, &res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,6 +123,32 @@ func (material *Material) AddNews(articles []*Article) (mediaID string, err erro
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//reqUpdateArticle 更新永久性图文素材请求信息
|
||||||
|
type reqUpdateArticle struct {
|
||||||
|
MediaID string `json:"media_id"`
|
||||||
|
Index int64 `json:"index"`
|
||||||
|
Articles *Article `json:"articles"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNews 更新永久图文素材
|
||||||
|
func (material *Material) UpdateNews(article *Article, mediaID string, index int64) (err error) {
|
||||||
|
req := &reqUpdateArticle{mediaID, index, article}
|
||||||
|
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = material.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", updateNewsURL, accessToken)
|
||||||
|
var response []byte
|
||||||
|
response, err = util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(response, "UpdateNews")
|
||||||
|
}
|
||||||
|
|
||||||
//resAddMaterial 永久性素材上传返回的结果
|
//resAddMaterial 永久性素材上传返回的结果
|
||||||
type resAddMaterial struct {
|
type resAddMaterial struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
@@ -112,6 +161,7 @@ type resAddMaterial struct {
|
|||||||
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
||||||
if mediaType == MediaTypeVideo {
|
if mediaType == MediaTypeVideo {
|
||||||
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = material.GetAccessToken()
|
accessToken, err = material.GetAccessToken()
|
||||||
@@ -216,3 +266,86 @@ func (material *Material) DeleteMaterial(mediaID string) error {
|
|||||||
|
|
||||||
return util.DecodeWithCommonError(response, "DeleteMaterial")
|
return util.DecodeWithCommonError(response, "DeleteMaterial")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ArticleList 永久素材列表
|
||||||
|
type ArticleList struct {
|
||||||
|
util.CommonError
|
||||||
|
TotalCount int64 `json:"total_count"`
|
||||||
|
ItemCount int64 `json:"item_count"`
|
||||||
|
Item []ArticleListItem `json:"item"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ArticleListItem 用于ArticleList的item节点
|
||||||
|
type ArticleListItem struct {
|
||||||
|
MediaID string `json:"media_id"`
|
||||||
|
Content ArticleListContent `json:"content"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
UpdateTime int64 `json:"update_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//ArticleListContent 用于ArticleListItem的content节点
|
||||||
|
type ArticleListContent struct {
|
||||||
|
NewsItem []Article `json:"news_item"`
|
||||||
|
UpdateTime int64 `json:"update_time"`
|
||||||
|
CreateTime int64 `json:"create_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//reqBatchGetMaterial BatchGetMaterial请求参数
|
||||||
|
type reqBatchGetMaterial struct {
|
||||||
|
Type PermanentMaterialType `json:"type"`
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Offset int64 `json:"offset"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchGetMaterial 批量获取永久素材
|
||||||
|
//reference:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html
|
||||||
|
func (material *Material) BatchGetMaterial(permanentMaterialType PermanentMaterialType, offset, count int64) (list ArticleList, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = material.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", batchGetMaterialURL, accessToken)
|
||||||
|
|
||||||
|
req := reqBatchGetMaterial{
|
||||||
|
Type: permanentMaterialType,
|
||||||
|
Offset: offset,
|
||||||
|
Count: count,
|
||||||
|
}
|
||||||
|
|
||||||
|
var response []byte
|
||||||
|
response, err = util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(response, &list, "BatchGetMaterial")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResMaterialCount 素材总数
|
||||||
|
type ResMaterialCount struct {
|
||||||
|
util.CommonError
|
||||||
|
VoiceCount int64 `json:"voice_count"` // 语音总数量
|
||||||
|
VideoCount int64 `json:"video_count"` // 视频总数量
|
||||||
|
ImageCount int64 `json:"image_count"` // 图片总数量
|
||||||
|
NewsCount int64 `json:"news_count"` // 图文总数量
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMaterialCount 获取素材总数.
|
||||||
|
func (material *Material) GetMaterialCount() (res ResMaterialCount, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = material.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", getMaterialCountURL, accessToken)
|
||||||
|
var response []byte
|
||||||
|
response, err = util.HTTPGet(uri)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = util.DecodeWithError(response, &res, "GetMaterialCount")
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
//MediaType 媒体文件类型
|
//MediaType 媒体文件类型
|
||||||
@@ -14,11 +14,11 @@ const (
|
|||||||
//MediaTypeImage 媒体文件:图片
|
//MediaTypeImage 媒体文件:图片
|
||||||
MediaTypeImage MediaType = "image"
|
MediaTypeImage MediaType = "image"
|
||||||
//MediaTypeVoice 媒体文件:声音
|
//MediaTypeVoice 媒体文件:声音
|
||||||
MediaTypeVoice = "voice"
|
MediaTypeVoice MediaType = "voice"
|
||||||
//MediaTypeVideo 媒体文件:视频
|
//MediaTypeVideo 媒体文件:视频
|
||||||
MediaTypeVideo = "video"
|
MediaTypeVideo MediaType = "video"
|
||||||
//MediaTypeThumb 媒体文件:缩略图
|
//MediaTypeThumb 媒体文件:缩略图
|
||||||
MediaTypeThumb = "thumb"
|
MediaTypeThumb MediaType = "thumb"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -106,5 +106,4 @@ func (material *Material) ImageUpload(filename string) (url string, err error) {
|
|||||||
}
|
}
|
||||||
url = image.URL
|
url = image.URL
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
215
officialaccount/menu/button.go
Normal file
215
officialaccount/menu/button.go
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
package menu
|
||||||
|
|
||||||
|
//Button 菜单按钮
|
||||||
|
type Button struct {
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Key string `json:"key,omitempty"`
|
||||||
|
URL string `json:"url,omitempty"`
|
||||||
|
MediaID string `json:"media_id,omitempty"`
|
||||||
|
AppID string `json:"appid,omitempty"`
|
||||||
|
PagePath string `json:"pagepath,omitempty"`
|
||||||
|
SubButtons []*Button `json:"sub_button,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetSubButton 设置二级菜单
|
||||||
|
func (btn *Button) SetSubButton(name string, subButtons []*Button) *Button {
|
||||||
|
btn.Name = name
|
||||||
|
btn.SubButtons = subButtons
|
||||||
|
btn.Type = ""
|
||||||
|
btn.Key = ""
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetClickButton btn 为click类型
|
||||||
|
func (btn *Button) SetClickButton(name, key string) *Button {
|
||||||
|
btn.Type = "click"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetViewButton view类型
|
||||||
|
func (btn *Button) SetViewButton(name, url string) *Button {
|
||||||
|
btn.Type = "view"
|
||||||
|
btn.Name = name
|
||||||
|
btn.URL = url
|
||||||
|
btn.Key = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetScanCodePushButton 扫码推事件
|
||||||
|
func (btn *Button) SetScanCodePushButton(name, key string) *Button {
|
||||||
|
btn.Type = "scancode_push"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetScanCodeWaitMsgButton 设置 扫码推事件且弹出"消息接收中"提示框
|
||||||
|
func (btn *Button) SetScanCodeWaitMsgButton(name, key string) *Button {
|
||||||
|
btn.Type = "scancode_waitmsg"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetPicSysPhotoButton 设置弹出系统拍照发图按钮
|
||||||
|
func (btn *Button) SetPicSysPhotoButton(name, key string) *Button {
|
||||||
|
btn.Type = "pic_sysphoto"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetPicPhotoOrAlbumButton 设置弹出拍照或者相册发图类型按钮
|
||||||
|
func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) *Button {
|
||||||
|
btn.Type = "pic_photo_or_album"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetPicWeixinButton 设置弹出微信相册发图器类型按钮
|
||||||
|
func (btn *Button) SetPicWeixinButton(name, key string) *Button {
|
||||||
|
btn.Type = "pic_weixin"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetLocationSelectButton 设置 弹出地理位置选择器 类型按钮
|
||||||
|
func (btn *Button) SetLocationSelectButton(name, key string) *Button {
|
||||||
|
btn.Type = "location_select"
|
||||||
|
btn.Name = name
|
||||||
|
btn.Key = key
|
||||||
|
|
||||||
|
btn.URL = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetMediaIDButton 设置 下发消息(除文本消息) 类型按钮
|
||||||
|
func (btn *Button) SetMediaIDButton(name, mediaID string) *Button {
|
||||||
|
btn.Type = "media_id"
|
||||||
|
btn.Name = name
|
||||||
|
btn.MediaID = mediaID
|
||||||
|
|
||||||
|
btn.Key = ""
|
||||||
|
btn.URL = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetViewLimitedButton 设置 跳转图文消息URL 类型按钮
|
||||||
|
func (btn *Button) SetViewLimitedButton(name, mediaID string) *Button {
|
||||||
|
btn.Type = "view_limited"
|
||||||
|
btn.Name = name
|
||||||
|
btn.MediaID = mediaID
|
||||||
|
|
||||||
|
btn.Key = ""
|
||||||
|
btn.URL = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetMiniprogramButton 设置 跳转小程序 类型按钮 (公众号后台必须已经关联小程序)
|
||||||
|
func (btn *Button) SetMiniprogramButton(name, url, appID, pagePath string) *Button {
|
||||||
|
btn.Type = "miniprogram"
|
||||||
|
btn.Name = name
|
||||||
|
btn.URL = url
|
||||||
|
btn.AppID = appID
|
||||||
|
btn.PagePath = pagePath
|
||||||
|
|
||||||
|
btn.Key = ""
|
||||||
|
btn.MediaID = ""
|
||||||
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewSubButton 二级菜单
|
||||||
|
func NewSubButton(name string, subButtons []*Button) *Button {
|
||||||
|
return (&Button{}).SetSubButton(name, subButtons)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewClickButton btn 为click类型
|
||||||
|
func NewClickButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetClickButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewViewButton view类型
|
||||||
|
func NewViewButton(name, url string) *Button {
|
||||||
|
return (&Button{}).SetViewButton(name, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewScanCodePushButton 扫码推事件
|
||||||
|
func NewScanCodePushButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetScanCodePushButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewScanCodeWaitMsgButton 扫码推事件且弹出"消息接收中"提示框
|
||||||
|
func NewScanCodeWaitMsgButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetScanCodeWaitMsgButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewPicSysPhotoButton 弹出系统拍照发图按钮
|
||||||
|
func NewPicSysPhotoButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetPicSysPhotoButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewPicPhotoOrAlbumButton 弹出拍照或者相册发图类型按钮
|
||||||
|
func NewPicPhotoOrAlbumButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetPicPhotoOrAlbumButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewPicWeixinButton 弹出微信相册发图器类型按钮
|
||||||
|
func NewPicWeixinButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetPicWeixinButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewLocationSelectButton 弹出地理位置选择器 类型按钮
|
||||||
|
func NewLocationSelectButton(name, key string) *Button {
|
||||||
|
return (&Button{}).SetLocationSelectButton(name, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewMediaIDButton 下发消息(除文本消息) 类型按钮
|
||||||
|
func NewMediaIDButton(name, mediaID string) *Button {
|
||||||
|
return (&Button{}).SetMediaIDButton(name, mediaID)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewViewLimitedButton 跳转图文消息URL 类型按钮
|
||||||
|
func NewViewLimitedButton(name, mediaID string) *Button {
|
||||||
|
return (&Button{}).SetViewLimitedButton(name, mediaID)
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewMiniprogramButton 跳转小程序 类型按钮 (公众号后台必须已经关联小程序)
|
||||||
|
func NewMiniprogramButton(name, url, appID, pagePath string) *Button {
|
||||||
|
return (&Button{}).SetMiniprogramButton(name, url, appID, pagePath)
|
||||||
|
}
|
||||||
28
officialaccount/menu/button_test.go
Normal file
28
officialaccount/menu/button_test.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package menu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewButtonFun(t *testing.T) {
|
||||||
|
buttons := []*Button{
|
||||||
|
NewSubButton("1", []*Button{
|
||||||
|
NewViewButton("1.1", "https://baidu.com"),
|
||||||
|
NewViewButton("1.2", "https://baidu.com"),
|
||||||
|
NewViewButton("1.3", "https://baidu.com"),
|
||||||
|
}),
|
||||||
|
NewSubButton("2", []*Button{
|
||||||
|
NewViewButton("2.1", "https://baidu.com"),
|
||||||
|
NewViewButton("2.2", "https://baidu.com"),
|
||||||
|
NewViewButton("2.3", "https://baidu.com"),
|
||||||
|
}),
|
||||||
|
NewViewButton("3", "https://baidu.com"),
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(buttons)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, `[{"name":"1","sub_button":[{"type":"view","name":"1.1","url":"https://baidu.com"},{"type":"view","name":"1.2","url":"https://baidu.com"},{"type":"view","name":"1.3","url":"https://baidu.com"}]},{"name":"2","sub_button":[{"type":"view","name":"2.1","url":"https://baidu.com"},{"type":"view","name":"2.2","url":"https://baidu.com"},{"type":"view","name":"2.3","url":"https://baidu.com"}]},{"type":"view","name":"3","url":"https://baidu.com"}]`, string(data))
|
||||||
|
}
|
||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -102,12 +102,12 @@ type ButtonNew struct {
|
|||||||
|
|
||||||
//MatchRule 个性化菜单规则
|
//MatchRule 个性化菜单规则
|
||||||
type MatchRule struct {
|
type MatchRule struct {
|
||||||
GroupID int32 `json:"group_id,omitempty"`
|
GroupID string `json:"group_id,omitempty"`
|
||||||
Sex int32 `json:"sex,omitempty"`
|
Sex string `json:"sex,omitempty"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
Province string `json:"province,omitempty"`
|
Province string `json:"province,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
ClientPlatformType int32 `json:"client_platform_type,omitempty"`
|
ClientPlatformType string `json:"client_platform_type,omitempty"`
|
||||||
Language string `json:"language,omitempty"`
|
Language string `json:"language,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +138,23 @@ func (menu *Menu) SetMenu(buttons []*Button) error {
|
|||||||
return util.DecodeWithCommonError(response, "SetMenu")
|
return util.DecodeWithCommonError(response, "SetMenu")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//SetMenuByJSON 设置按钮
|
||||||
|
func (menu *Menu) SetMenuByJSON(jsonInfo string) error {
|
||||||
|
accessToken, err := menu.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", menuCreateURL, accessToken)
|
||||||
|
|
||||||
|
response, err := util.HTTPPost(uri, jsonInfo)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.DecodeWithCommonError(response, "SetMenuByJSON")
|
||||||
|
}
|
||||||
|
|
||||||
//GetMenu 获取菜单配置
|
//GetMenu 获取菜单配置
|
||||||
func (menu *Menu) GetMenu() (resMenu ResMenu, err error) {
|
func (menu *Menu) GetMenu() (resMenu ResMenu, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
@@ -198,6 +215,22 @@ func (menu *Menu) AddConditional(buttons []*Button, matchRule *MatchRule) error
|
|||||||
return util.DecodeWithCommonError(response, "AddConditional")
|
return util.DecodeWithCommonError(response, "AddConditional")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//AddConditionalByJSON 添加个性化菜单
|
||||||
|
func (menu *Menu) AddConditionalByJSON(jsonInfo string) error {
|
||||||
|
accessToken, err := menu.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", menuAddConditionalURL, accessToken)
|
||||||
|
response, err := util.HTTPPost(uri, jsonInfo)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.DecodeWithCommonError(response, "AddConditional")
|
||||||
|
}
|
||||||
|
|
||||||
//DeleteConditional 删除个性化菜单
|
//DeleteConditional 删除个性化菜单
|
||||||
func (menu *Menu) DeleteConditional(menuID int64) error {
|
func (menu *Menu) DeleteConditional(menuID int64) error {
|
||||||
accessToken, err := menu.GetAccessToken()
|
accessToken, err := menu.GetAccessToken()
|
||||||
@@ -3,8 +3,9 @@ package message
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/silenceper/wechat/context"
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -72,6 +73,20 @@ func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
|
||||||
|
func NewCustomerMiniprogrampageMessage(toUser, title, appID, pagePath, thumbMediaID string) *CustomerMessage {
|
||||||
|
return &CustomerMessage{
|
||||||
|
ToUser: toUser,
|
||||||
|
Msgtype: MsgTypeMiniprogrampage,
|
||||||
|
Miniprogrampage: &MediaMiniprogrampage{
|
||||||
|
Title: title,
|
||||||
|
AppID: appID,
|
||||||
|
Pagepath: pagePath,
|
||||||
|
ThumbMediaID: thumbMediaID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//MediaText 文本消息的文字
|
//MediaText 文本消息的文字
|
||||||
type MediaText struct {
|
type MediaText struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
@@ -133,7 +148,7 @@ type MediaWxcard struct {
|
|||||||
//MediaMiniprogrampage 小程序消息
|
//MediaMiniprogrampage 小程序消息
|
||||||
type MediaMiniprogrampage struct {
|
type MediaMiniprogrampage struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Appid string `json:"appid"`
|
AppID string `json:"appid"`
|
||||||
Pagepath string `json:"pagepath"`
|
Pagepath string `json:"pagepath"`
|
||||||
ThumbMediaID string `json:"thumb_media_id"`
|
ThumbMediaID string `json:"thumb_media_id"`
|
||||||
}
|
}
|
||||||
@@ -146,6 +161,9 @@ func (manager *Manager) Send(msg *CustomerMessage) error {
|
|||||||
}
|
}
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
|
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
|
||||||
response, err := util.PostJSON(uri, msg)
|
response, err := util.PostJSON(uri, msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var result util.CommonError
|
var result util.CommonError
|
||||||
err = json.Unmarshal(response, &result)
|
err = json.Unmarshal(response, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -3,7 +3,7 @@ package message
|
|||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/device"
|
"github.com/silenceper/wechat/v2/officialaccount/device"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgType 基本消息类型
|
// MsgType 基本消息类型
|
||||||
@@ -24,6 +24,8 @@ const (
|
|||||||
MsgTypeVoice = "voice"
|
MsgTypeVoice = "voice"
|
||||||
//MsgTypeVideo 表示视频消息
|
//MsgTypeVideo 表示视频消息
|
||||||
MsgTypeVideo = "video"
|
MsgTypeVideo = "video"
|
||||||
|
//MsgTypeMiniprogrampage 表示小程序卡片消息
|
||||||
|
MsgTypeMiniprogrampage = "miniprogrampage"
|
||||||
//MsgTypeShortVideo 表示短视频消息[限接收]
|
//MsgTypeShortVideo 表示短视频消息[限接收]
|
||||||
MsgTypeShortVideo = "shortvideo"
|
MsgTypeShortVideo = "shortvideo"
|
||||||
//MsgTypeLocation 表示坐标消息[限接收]
|
//MsgTypeLocation 表示坐标消息[限接收]
|
||||||
@@ -72,6 +74,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
//微信开放平台需要用到
|
||||||
|
|
||||||
// InfoTypeVerifyTicket 返回ticket
|
// InfoTypeVerifyTicket 返回ticket
|
||||||
InfoTypeVerifyTicket InfoType = "component_verify_ticket"
|
InfoTypeVerifyTicket InfoType = "component_verify_ticket"
|
||||||
// InfoTypeAuthorized 授权
|
// InfoTypeAuthorized 授权
|
||||||
@@ -87,20 +91,21 @@ type MixMessage struct {
|
|||||||
CommonToken
|
CommonToken
|
||||||
|
|
||||||
//基本消息
|
//基本消息
|
||||||
MsgID int64 `xml:"MsgId"`
|
MsgID int64 `xml:"MsgId"` //其他消息推送过来是MsgId
|
||||||
Content string `xml:"Content"`
|
TemplateMsgID int64 `xml:"MsgID"` //模板消息推送成功的消息是MsgID
|
||||||
Recognition string `xml:"Recognition"`
|
Content string `xml:"Content"`
|
||||||
PicURL string `xml:"PicUrl"`
|
Recognition string `xml:"Recognition"`
|
||||||
MediaID string `xml:"MediaId"`
|
PicURL string `xml:"PicUrl"`
|
||||||
Format string `xml:"Format"`
|
MediaID string `xml:"MediaId"`
|
||||||
ThumbMediaID string `xml:"ThumbMediaId"`
|
Format string `xml:"Format"`
|
||||||
LocationX float64 `xml:"Location_X"`
|
ThumbMediaID string `xml:"ThumbMediaId"`
|
||||||
LocationY float64 `xml:"Location_Y"`
|
LocationX float64 `xml:"Location_X"`
|
||||||
Scale float64 `xml:"Scale"`
|
LocationY float64 `xml:"Location_Y"`
|
||||||
Label string `xml:"Label"`
|
Scale float64 `xml:"Scale"`
|
||||||
Title string `xml:"Title"`
|
Label string `xml:"Label"`
|
||||||
Description string `xml:"Description"`
|
Title string `xml:"Title"`
|
||||||
URL string `xml:"Url"`
|
Description string `xml:"Description"`
|
||||||
|
URL string `xml:"Url"`
|
||||||
|
|
||||||
//事件相关
|
//事件相关
|
||||||
Event EventType `xml:"Event"`
|
Event EventType `xml:"Event"`
|
||||||
@@ -220,3 +225,8 @@ func (msg *CommonToken) SetCreateTime(createTime int64) {
|
|||||||
func (msg *CommonToken) SetMsgType(msgType MsgType) {
|
func (msg *CommonToken) SetMsgType(msgType MsgType) {
|
||||||
msg.MsgType = msgType
|
msg.MsgType = msgType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetOpenID get the FromUserName value
|
||||||
|
func (msg *CommonToken) GetOpenID() string {
|
||||||
|
return string(msg.FromUserName)
|
||||||
|
}
|
||||||
115
officialaccount/message/template.go
Normal file
115
officialaccount/message/template.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
templateSendURL = "https://api.weixin.qq.com/cgi-bin/message/template/send"
|
||||||
|
templateListURL = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Template 模板消息
|
||||||
|
type Template struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewTemplate 实例化
|
||||||
|
func NewTemplate(context *context.Context) *Template {
|
||||||
|
tpl := new(Template)
|
||||||
|
tpl.Context = context
|
||||||
|
return tpl
|
||||||
|
}
|
||||||
|
|
||||||
|
//TemplateMessage 发送的模板消息内容
|
||||||
|
type TemplateMessage struct {
|
||||||
|
ToUser string `json:"touser"` // 必须, 接受者OpenID
|
||||||
|
TemplateID string `json:"template_id"` // 必须, 模版ID
|
||||||
|
URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
|
||||||
|
Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置
|
||||||
|
Data map[string]*TemplateDataItem `json:"data"` // 必须, 模板数据
|
||||||
|
|
||||||
|
MiniProgram struct {
|
||||||
|
AppID string `json:"appid"` //所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
|
||||||
|
PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
|
||||||
|
} `json:"miniprogram"` //可选,跳转至小程序地址
|
||||||
|
}
|
||||||
|
|
||||||
|
//TemplateDataItem 模版内某个 .DATA 的值
|
||||||
|
type TemplateDataItem struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
Color string `json:"color,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type resTemplateSend struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
MsgID int64 `json:"msgid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send 发送模板消息
|
||||||
|
func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = tpl.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
||||||
|
response, err := util.PostJSON(uri, msg)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var result resTemplateSend
|
||||||
|
err = json.Unmarshal(response, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msgID = result.MsgID
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TemplateItem 模板消息.
|
||||||
|
type TemplateItem struct {
|
||||||
|
TemplateID string `json:"template_id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
PrimaryIndustry string `json:"primary_industry"`
|
||||||
|
DeputyIndustry string `json:"deputy_industry"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Example string `json:"example"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type resTemplateList struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
TemplateList []*TemplateItem `json:"template_list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//List 获取模板列表
|
||||||
|
func (tpl *Template) List() (templateList []*TemplateItem, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = tpl.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
|
||||||
|
var response []byte
|
||||||
|
response, err = util.HTTPGet(uri)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var res resTemplateList
|
||||||
|
err = util.DecodeWithError(response, &res, "ListTemplate")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
templateList = res.TemplateList
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -13,11 +13,11 @@ type TransInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewTransferCustomer 实例化
|
//NewTransferCustomer 实例化
|
||||||
func NewTransferCustomer(KfAccount string) *TransferCustomer {
|
func NewTransferCustomer(kfAccount string) *TransferCustomer {
|
||||||
tc := new(TransferCustomer)
|
tc := new(TransferCustomer)
|
||||||
if KfAccount != "" {
|
if kfAccount != "" {
|
||||||
transInfo := new(TransInfo)
|
transInfo := new(TransInfo)
|
||||||
transInfo.KfAccount = KfAccount
|
transInfo.KfAccount = kfAccount
|
||||||
tc.TransInfo = transInfo
|
tc.TransInfo = transInfo
|
||||||
}
|
}
|
||||||
return tc
|
return tc
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -15,7 +15,7 @@ const (
|
|||||||
webAppRedirectOauthURL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect"
|
webAppRedirectOauthURL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect"
|
||||||
accessTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
|
accessTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
|
||||||
refreshAccessTokenURL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
|
refreshAccessTokenURL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
|
||||||
userInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN"
|
userInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s"
|
||||||
checkAccessTokenURL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s"
|
checkAccessTokenURL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ func (oauth *Oauth) Redirect(writer http.ResponseWriter, req *http.Request, redi
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
http.Redirect(writer, req, location, 302)
|
http.Redirect(writer, req, location, http.StatusFound)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,8 +144,11 @@ type UserInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//GetUserInfo 如果scope为 snsapi_userinfo 则可以通过此方法获取到用户基本信息
|
//GetUserInfo 如果scope为 snsapi_userinfo 则可以通过此方法获取到用户基本信息
|
||||||
func (oauth *Oauth) GetUserInfo(accessToken, openID string) (result UserInfo, err error) {
|
func (oauth *Oauth) GetUserInfo(accessToken, openID, lang string) (result UserInfo, err error) {
|
||||||
urlStr := fmt.Sprintf(userInfoURL, accessToken, openID)
|
if lang == "" {
|
||||||
|
lang = "zh_CN"
|
||||||
|
}
|
||||||
|
urlStr := fmt.Sprintf(userInfoURL, accessToken, openID, lang)
|
||||||
var response []byte
|
var response []byte
|
||||||
response, err = util.HTTPGet(urlStr)
|
response, err = util.HTTPGet(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
115
officialaccount/officialaccount.go
Normal file
115
officialaccount/officialaccount.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package officialaccount
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/datacube"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/basic"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/broadcast"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/config"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/device"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/js"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/material"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/menu"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/message"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/oauth"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/server"
|
||||||
|
"github.com/silenceper/wechat/v2/officialaccount/user"
|
||||||
|
)
|
||||||
|
|
||||||
|
//OfficialAccount 微信公众号相关API
|
||||||
|
type OfficialAccount struct {
|
||||||
|
ctx *context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewOfficialAccount 实例化公众号API
|
||||||
|
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
|
||||||
|
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
|
||||||
|
ctx := &context.Context{
|
||||||
|
Config: cfg,
|
||||||
|
AccessTokenHandle: defaultAkHandle,
|
||||||
|
}
|
||||||
|
return &OfficialAccount{ctx: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
//SetAccessTokenHandle 自定义access_token获取方式
|
||||||
|
func (officialAccount *OfficialAccount) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
|
||||||
|
officialAccount.ctx.AccessTokenHandle = accessTokenHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext get Context
|
||||||
|
func (officialAccount *OfficialAccount) GetContext() *context.Context {
|
||||||
|
return officialAccount.ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBasic qr/url 相关配置
|
||||||
|
func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
|
||||||
|
return basic.NewBasic(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMenu 菜单管理接口
|
||||||
|
func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
|
||||||
|
return menu.NewMenu(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetServer 消息管理:接收事件,被动回复消息管理
|
||||||
|
func (officialAccount *OfficialAccount) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
|
||||||
|
srv := server.NewServer(officialAccount.ctx)
|
||||||
|
srv.Request = req
|
||||||
|
srv.Writer = writer
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAccessToken 获取access_token
|
||||||
|
func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
|
||||||
|
return officialAccount.ctx.GetAccessToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOauth oauth2网页授权
|
||||||
|
func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
|
||||||
|
return oauth.NewOauth(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMaterial 素材管理
|
||||||
|
func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
|
||||||
|
return material.NewMaterial(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetJs js-sdk配置
|
||||||
|
func (officialAccount *OfficialAccount) GetJs() *js.Js {
|
||||||
|
return js.NewJs(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUser 用户管理接口
|
||||||
|
func (officialAccount *OfficialAccount) GetUser() *user.User {
|
||||||
|
return user.NewUser(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTemplate 模板消息接口
|
||||||
|
func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
|
||||||
|
return message.NewTemplate(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCustomerMessageManager 客服消息接口
|
||||||
|
func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
|
||||||
|
return message.NewMessageManager(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDevice 获取智能设备的实例
|
||||||
|
func (officialAccount *OfficialAccount) GetDevice() *device.Device {
|
||||||
|
return device.NewDevice(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetBroadcast 群发消息
|
||||||
|
//TODO 待完善
|
||||||
|
func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
|
||||||
|
return broadcast.NewBroadcast(officialAccount.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetDataCube 数据统计
|
||||||
|
func (officialAccount *OfficialAccount) GetDataCube() *datacube.DataCube {
|
||||||
|
return datacube.NewCube(officialAccount.ctx)
|
||||||
|
}
|
||||||
@@ -5,29 +5,34 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/message"
|
"github.com/silenceper/wechat/v2/officialaccount/message"
|
||||||
"github.com/silenceper/wechat/util"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Server struct
|
//Server struct
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*context.Context
|
*context.Context
|
||||||
|
Writer http.ResponseWriter
|
||||||
|
Request *http.Request
|
||||||
|
|
||||||
debug bool
|
skipValidate bool
|
||||||
|
|
||||||
openID string
|
openID string
|
||||||
|
|
||||||
messageHandler func(message.MixMessage) *message.Reply
|
messageHandler func(message.MixMessage) *message.Reply
|
||||||
|
|
||||||
requestRawXMLMsg []byte
|
RequestRawXMLMsg []byte
|
||||||
requestMsg message.MixMessage
|
RequestMsg message.MixMessage
|
||||||
responseRawXMLMsg []byte
|
ResponseRawXMLMsg []byte
|
||||||
responseMsg interface{}
|
ResponseMsg interface{}
|
||||||
|
|
||||||
isSafeMode bool
|
isSafeMode bool
|
||||||
random []byte
|
random []byte
|
||||||
@@ -42,14 +47,15 @@ func NewServer(context *context.Context) *Server {
|
|||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDebug set debug field
|
// SkipValidate set skip validate
|
||||||
func (srv *Server) SetDebug(debug bool) {
|
func (srv *Server) SkipValidate(skip bool) {
|
||||||
srv.debug = debug
|
srv.skipValidate = skip
|
||||||
}
|
}
|
||||||
|
|
||||||
//Serve 处理微信的请求消息
|
//Serve 处理微信的请求消息
|
||||||
func (srv *Server) Serve() error {
|
func (srv *Server) Serve() error {
|
||||||
if !srv.Validate() {
|
if !srv.Validate() {
|
||||||
|
log.Error("Validate Signature Failed.")
|
||||||
return fmt.Errorf("请求校验失败")
|
return fmt.Errorf("请求校验失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,22 +70,21 @@ func (srv *Server) Serve() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//debug
|
//debug print request msg
|
||||||
if srv.debug {
|
log.Debugf("request msg =%s", string(srv.RequestRawXMLMsg))
|
||||||
fmt.Println("request msg = ", string(srv.requestRawXMLMsg))
|
|
||||||
}
|
|
||||||
|
|
||||||
return srv.buildResponse(response)
|
return srv.buildResponse(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Validate 校验请求是否合法
|
//Validate 校验请求是否合法
|
||||||
func (srv *Server) Validate() bool {
|
func (srv *Server) Validate() bool {
|
||||||
if srv.debug {
|
if srv.skipValidate {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
timestamp := srv.Query("timestamp")
|
timestamp := srv.Query("timestamp")
|
||||||
nonce := srv.Query("nonce")
|
nonce := srv.Query("nonce")
|
||||||
signature := srv.Query("signature")
|
signature := srv.Query("signature")
|
||||||
|
log.Debugf("validate signature, timestamp=%s, nonce=%s", timestamp, nonce)
|
||||||
return signature == util.Signature(srv.Token, timestamp, nonce)
|
return signature == util.Signature(srv.Token, timestamp, nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +109,7 @@ func (srv *Server) handleRequest() (reply *message.Reply, err error) {
|
|||||||
if !success {
|
if !success {
|
||||||
err = errors.New("消息类型转换失败")
|
err = errors.New("消息类型转换失败")
|
||||||
}
|
}
|
||||||
srv.requestMsg = mixMessage
|
srv.RequestMsg = mixMessage
|
||||||
reply = srv.messageHandler(mixMessage)
|
reply = srv.messageHandler(mixMessage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -150,7 +155,7 @@ func (srv *Server) getMessage() (interface{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.requestRawXMLMsg = rawXMLMsgBytes
|
srv.RequestRawXMLMsg = rawXMLMsgBytes
|
||||||
|
|
||||||
return srv.parseRequestMessage(rawXMLMsgBytes)
|
return srv.parseRequestMessage(rawXMLMsgBytes)
|
||||||
}
|
}
|
||||||
@@ -194,35 +199,36 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) {
|
|||||||
value := reflect.ValueOf(msgData)
|
value := reflect.ValueOf(msgData)
|
||||||
//msgData must be a ptr
|
//msgData must be a ptr
|
||||||
kind := value.Kind().String()
|
kind := value.Kind().String()
|
||||||
if "ptr" != kind {
|
if kind != "ptr" {
|
||||||
return message.ErrUnsupportReply
|
return message.ErrUnsupportReply
|
||||||
}
|
}
|
||||||
|
|
||||||
params := make([]reflect.Value, 1)
|
params := make([]reflect.Value, 1)
|
||||||
params[0] = reflect.ValueOf(srv.requestMsg.FromUserName)
|
params[0] = reflect.ValueOf(srv.RequestMsg.FromUserName)
|
||||||
value.MethodByName("SetToUserName").Call(params)
|
value.MethodByName("SetToUserName").Call(params)
|
||||||
|
|
||||||
params[0] = reflect.ValueOf(srv.requestMsg.ToUserName)
|
params[0] = reflect.ValueOf(srv.RequestMsg.ToUserName)
|
||||||
value.MethodByName("SetFromUserName").Call(params)
|
value.MethodByName("SetFromUserName").Call(params)
|
||||||
|
|
||||||
params[0] = reflect.ValueOf(msgType)
|
params[0] = reflect.ValueOf(msgType)
|
||||||
value.MethodByName("SetMsgType").Call(params)
|
value.MethodByName("SetMsgType").Call(params)
|
||||||
|
|
||||||
params[0] = reflect.ValueOf(util.GetCurrTs())
|
params[0] = reflect.ValueOf(util.GetCurrTS())
|
||||||
value.MethodByName("SetCreateTime").Call(params)
|
value.MethodByName("SetCreateTime").Call(params)
|
||||||
|
|
||||||
srv.responseMsg = msgData
|
srv.ResponseMsg = msgData
|
||||||
srv.responseRawXMLMsg, err = xml.Marshal(msgData)
|
srv.ResponseRawXMLMsg, err = xml.Marshal(msgData)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Send 将自定义的消息发送
|
//Send 将自定义的消息发送
|
||||||
func (srv *Server) Send() (err error) {
|
func (srv *Server) Send() (err error) {
|
||||||
replyMsg := srv.responseMsg
|
replyMsg := srv.ResponseMsg
|
||||||
|
log.Debugf("response msg =%+v", replyMsg)
|
||||||
if srv.isSafeMode {
|
if srv.isSafeMode {
|
||||||
//安全模式下对消息进行加密
|
//安全模式下对消息进行加密
|
||||||
var encryptedMsg []byte
|
var encryptedMsg []byte
|
||||||
encryptedMsg, err = util.EncryptMsg(srv.random, srv.responseRawXMLMsg, srv.AppID, srv.EncodingAESKey)
|
encryptedMsg, err = util.EncryptMsg(srv.random, srv.ResponseRawXMLMsg, srv.AppID, srv.EncodingAESKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
58
officialaccount/server/util.go
Normal file
58
officialaccount/server/util.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var xmlContentType = []string{"application/xml; charset=utf-8"}
|
||||||
|
var plainContentType = []string{"text/plain; charset=utf-8"}
|
||||||
|
|
||||||
|
func writeContextType(w http.ResponseWriter, value []string) {
|
||||||
|
header := w.Header()
|
||||||
|
if val := header["Content-Type"]; len(val) == 0 {
|
||||||
|
header["Content-Type"] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Render render from bytes
|
||||||
|
func (srv *Server) Render(bytes []byte) {
|
||||||
|
//debug
|
||||||
|
//fmt.Println("response msg = ", string(bytes))
|
||||||
|
srv.Writer.WriteHeader(200)
|
||||||
|
_, err := srv.Writer.Write(bytes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//String render from string
|
||||||
|
func (srv *Server) String(str string) {
|
||||||
|
writeContextType(srv.Writer, plainContentType)
|
||||||
|
srv.Render([]byte(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
//XML render to xml
|
||||||
|
func (srv *Server) XML(obj interface{}) {
|
||||||
|
writeContextType(srv.Writer, xmlContentType)
|
||||||
|
bytes, err := xml.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
srv.Render(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query returns the keyed url query value if it exists
|
||||||
|
func (srv *Server) Query(key string) string {
|
||||||
|
value, _ := srv.GetQuery(key)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQuery is like Query(), it returns the keyed url query value
|
||||||
|
func (srv *Server) GetQuery(key string) (string, bool) {
|
||||||
|
req := srv.Request
|
||||||
|
if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
|
||||||
|
return values[0], true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
87
officialaccount/user/migrate.go
Normal file
87
officialaccount/user/migrate.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
//Package user migrate 用于微信公众号账号迁移,获取openID变化
|
||||||
|
//参考文档:https://kf.qq.com/faq/1901177NrqMr190117nqYJze.html
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
changeOpenIDURL = "https://api.weixin.qq.com/cgi-bin/changeopenid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChangeOpenIDResult OpenID迁移变化
|
||||||
|
type ChangeOpenIDResult struct {
|
||||||
|
OriOpenID string `json:"ori_openid"`
|
||||||
|
NewOpenID string `json:"new_openid"`
|
||||||
|
ErrMsg string `json:"err_msg,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangeOpenIDResultList OpenID迁移变化列表
|
||||||
|
type ChangeOpenIDResultList struct {
|
||||||
|
util.CommonError
|
||||||
|
List []ChangeOpenIDResult `json:"result_list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListChangeOpenIDs 返回指定OpenID变化列表
|
||||||
|
// fromAppID 为老账号AppID
|
||||||
|
// openIDs 为老账号的openID,openIDs限100个以内
|
||||||
|
// AccessToken 为新账号的AccessToken
|
||||||
|
func (user *User) ListChangeOpenIDs(fromAppID string, openIDs ...string) (list *ChangeOpenIDResultList, err error) {
|
||||||
|
list = &ChangeOpenIDResultList{}
|
||||||
|
//list.List = make([]ChangeOpenIDResult, 0)
|
||||||
|
if len(openIDs) > 100 {
|
||||||
|
err = errors.New("openIDs length must be lt 100")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if fromAppID == "" {
|
||||||
|
err = errors.New("fromAppID is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", changeOpenIDURL, accessToken)
|
||||||
|
var resp []byte
|
||||||
|
var req struct {
|
||||||
|
FromAppID string `json:"from_appid"`
|
||||||
|
OpenidList []string `json:"openid_list"`
|
||||||
|
}
|
||||||
|
req.FromAppID = fromAppID
|
||||||
|
req.OpenidList = append(req.OpenidList, openIDs...)
|
||||||
|
resp, err = util.PostJSON(uri, req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DecodeWithError(resp, list, "ListChangeOpenIDs")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAllChangeOpenIDs 返回所有用户OpenID列表
|
||||||
|
// fromAppID 为老账号AppID
|
||||||
|
// openIDs 为老账号的openID
|
||||||
|
// AccessToken 为新账号的AccessToken
|
||||||
|
func (user *User) ListAllChangeOpenIDs(fromAppID string, openIDs ...string) (list []ChangeOpenIDResult, err error) {
|
||||||
|
list = make([]ChangeOpenIDResult, 0)
|
||||||
|
chunks := util.SliceChunk(openIDs, 100)
|
||||||
|
for _, chunk := range chunks {
|
||||||
|
result, err := user.ListChangeOpenIDs(fromAppID, chunk...)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
list = append(list, result.List...)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
241
officialaccount/user/tag.go
Normal file
241
officialaccount/user/tag.go
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
tagCreateURL = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=%s"
|
||||||
|
tagGetURL = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s"
|
||||||
|
tagUpdateURL = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=%s"
|
||||||
|
tagDeleteURL = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=%s"
|
||||||
|
tagUserListURL = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=%s"
|
||||||
|
tagBatchtaggingURL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=%s"
|
||||||
|
tagBatchuntaggingURL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=%s"
|
||||||
|
tagUserTidListURL = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=%s"
|
||||||
|
)
|
||||||
|
|
||||||
|
//TagInfo 标签信息
|
||||||
|
type TagInfo struct {
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TagOpenIDList 标签用户列表
|
||||||
|
type TagOpenIDList struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
Data struct {
|
||||||
|
OpenIDs []string `json:"openid"`
|
||||||
|
} `json:"data"`
|
||||||
|
NextOpenID string `json:"next_openid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//CreateTag 创建标签
|
||||||
|
func (user *User) CreateTag(tagName string) (tagInfo *TagInfo, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf(tagCreateURL, accessToken)
|
||||||
|
var response []byte
|
||||||
|
var request struct {
|
||||||
|
Tag struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"tag"`
|
||||||
|
}
|
||||||
|
request.Tag.Name = tagName
|
||||||
|
response, err = util.PostJSON(uri, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var result struct {
|
||||||
|
util.CommonError
|
||||||
|
Tag *TagInfo `json:"tag"`
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(response, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("CreateTag Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return result.Tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//DeleteTag 删除标签
|
||||||
|
func (user *User) DeleteTag(tagID int32) (err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagDeleteURL, accessToken)
|
||||||
|
var request struct {
|
||||||
|
Tag struct {
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
} `json:"tag"`
|
||||||
|
}
|
||||||
|
request.Tag.ID = tagID
|
||||||
|
resp, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(resp, "DeleteTag")
|
||||||
|
}
|
||||||
|
|
||||||
|
//UpdateTag 编辑标签
|
||||||
|
func (user *User) UpdateTag(tagID int32, tagName string) (err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagUpdateURL, accessToken)
|
||||||
|
var request struct {
|
||||||
|
Tag struct {
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"tag"`
|
||||||
|
}
|
||||||
|
request.Tag.ID = tagID
|
||||||
|
request.Tag.Name = tagName
|
||||||
|
resp, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(resp, "UpdateTag")
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetTag 获取公众号已创建的标签
|
||||||
|
func (user *User) GetTag() (tags []*TagInfo, err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagGetURL, accessToken)
|
||||||
|
response, err := util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var result struct {
|
||||||
|
util.CommonError
|
||||||
|
Tags []*TagInfo `json:"tags"`
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(response, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return result.Tags, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//OpenIDListByTag 获取标签下粉丝列表
|
||||||
|
func (user *User) OpenIDListByTag(tagID int32, nextOpenID ...string) (userList *TagOpenIDList, err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagUserListURL, accessToken)
|
||||||
|
var request = struct {
|
||||||
|
ID int32 `json:"tagid"`
|
||||||
|
OpenID string `json:"next_openid"`
|
||||||
|
}{
|
||||||
|
ID: tagID,
|
||||||
|
}
|
||||||
|
if len(nextOpenID) > 0 {
|
||||||
|
request.OpenID = nextOpenID[0]
|
||||||
|
}
|
||||||
|
response, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
userList = new(TagOpenIDList)
|
||||||
|
err = json.Unmarshal(response, &userList)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//BatchTag 批量为用户打标签
|
||||||
|
func (user *User) BatchTag(openIDList []string, tagID int32) (err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(openIDList) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var request = struct {
|
||||||
|
OpenIDList []string `json:"openid_list"`
|
||||||
|
TagID int32 `json:"tagid"`
|
||||||
|
}{
|
||||||
|
OpenIDList: openIDList,
|
||||||
|
TagID: tagID,
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagBatchtaggingURL, accessToken)
|
||||||
|
resp, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(resp, "BatchTag")
|
||||||
|
}
|
||||||
|
|
||||||
|
//BatchUntag 批量为用户取消标签
|
||||||
|
func (user *User) BatchUntag(openIDList []string, tagID int32) (err error) {
|
||||||
|
if len(openIDList) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagBatchuntaggingURL, accessToken)
|
||||||
|
var request = struct {
|
||||||
|
OpenIDList []string `json:"openid_list"`
|
||||||
|
TagID int32 `json:"tagid"`
|
||||||
|
}{
|
||||||
|
OpenIDList: openIDList,
|
||||||
|
TagID: tagID,
|
||||||
|
}
|
||||||
|
resp, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(resp, "BatchUntag")
|
||||||
|
}
|
||||||
|
|
||||||
|
//UserTidList 获取用户身上的标签列表
|
||||||
|
func (user *User) UserTidList(openID string) (tagIDList []int32, err error) {
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(tagUserTidListURL, accessToken)
|
||||||
|
var request = struct {
|
||||||
|
OpenID string `json:"openid"`
|
||||||
|
TagID int32 `json:"tagid"`
|
||||||
|
}{
|
||||||
|
OpenID: openID,
|
||||||
|
}
|
||||||
|
resp, err := util.PostJSON(url, &request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var result struct {
|
||||||
|
util.CommonError
|
||||||
|
TagIDList []int32 `json:"tagid_list"`
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(resp, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("UserTidList Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return result.TagIDList, nil
|
||||||
|
}
|
||||||
@@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -44,7 +44,7 @@ type Info struct {
|
|||||||
UnionID string `json:"unionid"`
|
UnionID string `json:"unionid"`
|
||||||
Remark string `json:"remark"`
|
Remark string `json:"remark"`
|
||||||
GroupID int32 `json:"groupid"`
|
GroupID int32 `json:"groupid"`
|
||||||
TagidList []int32 `json:"tagid_list"`
|
TagIDList []int32 `json:"tagid_list"`
|
||||||
SubscribeScene string `json:"subscribe_scene"`
|
SubscribeScene string `json:"subscribe_scene"`
|
||||||
QrScene int `json:"qr_scene"`
|
QrScene int `json:"qr_scene"`
|
||||||
QrSceneStr string `json:"qr_scene_str"`
|
QrSceneStr string `json:"qr_scene_str"`
|
||||||
@@ -52,6 +52,8 @@ type Info struct {
|
|||||||
|
|
||||||
// OpenidList 用户列表
|
// OpenidList 用户列表
|
||||||
type OpenidList struct {
|
type OpenidList struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
Total int `json:"total"`
|
Total int `json:"total"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
Data struct {
|
Data struct {
|
||||||
@@ -124,19 +126,20 @@ func (user *User) ListUserOpenIDs(nextOpenid ...string) (*OpenidList, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userlist := new(OpenidList)
|
userlist := OpenidList{}
|
||||||
err = json.Unmarshal(response, userlist)
|
|
||||||
|
err = util.DecodeWithError(response, &userlist, "ListUserOpenIDs")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return userlist, nil
|
return &userlist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListAllUserOpenIDs 返回所有用户OpenID列表
|
// ListAllUserOpenIDs 返回所有用户OpenID列表
|
||||||
func (user *User) ListAllUserOpenIDs() ([]string, error) {
|
func (user *User) ListAllUserOpenIDs() ([]string, error) {
|
||||||
nextOpenid := ""
|
nextOpenid := ""
|
||||||
openids := []string{}
|
openids := make([]string, 0)
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
ul, err := user.ListUserOpenIDs(nextOpenid)
|
ul, err := user.ListUserOpenIDs(nextOpenid)
|
||||||
63
openplatform/README.md
Normal file
63
openplatform/README.md
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# 微信开放平台
|
||||||
|
|
||||||
|
|
||||||
|
[官方文档](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Third_party_platform_appid.html)
|
||||||
|
|
||||||
|
## 快速入门
|
||||||
|
|
||||||
|
### 服务端处理
|
||||||
|
```go
|
||||||
|
wc := wechat.NewWechat()
|
||||||
|
memory := cache.NewMemory()
|
||||||
|
cfg := &openplatform.Config{
|
||||||
|
AppID: "xxx",
|
||||||
|
AppSecret: "xxx",
|
||||||
|
Token: "xxx",
|
||||||
|
EncodingAESKey: "xxx",
|
||||||
|
Cache: memory,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
openPlatform := wc.GetOpenPlatform(cfg)
|
||||||
|
// 传入request和responseWriter
|
||||||
|
server := openPlatform.GetServer(req, rw)
|
||||||
|
//设置接收消息的处理方法
|
||||||
|
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
||||||
|
if msg.InfoType == message.InfoTypeVerifyTicket {
|
||||||
|
componentVerifyTicket, err := openPlatform.SetComponentAccessToken(msg.ComponentVerifyTicket)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
//debug
|
||||||
|
fmt.Println(componentVerifyTicket)
|
||||||
|
rw.Write([]byte("success"))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
//handle other message
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
//处理消息接收以及回复
|
||||||
|
err := server.Serve()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//发送回复的消息
|
||||||
|
server.Send()
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
### 待授权处理消息
|
||||||
|
```go
|
||||||
|
|
||||||
|
//授权的第三方公众号的appID
|
||||||
|
appID := "xxx"
|
||||||
|
openPlatform := wc.GetOpenPlatform(cfg)
|
||||||
|
openPlatform.GetOfficialAccount(appID)
|
||||||
|
|
||||||
|
```
|
||||||
34
openplatform/account/account.go
Normal file
34
openplatform/account/account.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package account
|
||||||
|
|
||||||
|
import "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
|
||||||
|
//Account 开放平台张哈管理
|
||||||
|
//TODO 实现方法
|
||||||
|
type Account struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewAccount new
|
||||||
|
func NewAccount(ctx *context.Context) *Account {
|
||||||
|
return &Account{ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create 创建开放平台帐号并绑定公众号/小程序
|
||||||
|
func (account *Account) Create(appID string) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Bind 将公众号/小程序绑定到开放平台帐号下
|
||||||
|
func (account *Account) Bind(appID string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Unbind 将公众号/小程序从开放平台帐号下解绑
|
||||||
|
func (account *Account) Unbind(appID string, openAppID string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get 获取公众号/小程序所绑定的开放平台帐号
|
||||||
|
func (account *Account) Get(appID string) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
14
openplatform/config/config.go
Normal file
14
openplatform/config/config.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Config config for 微信开放平台
|
||||||
|
type Config struct {
|
||||||
|
AppID string `json:"app_id"` //appid
|
||||||
|
AppSecret string `json:"app_secret"` //appsecret
|
||||||
|
Token string `json:"token"` //token
|
||||||
|
EncodingAESKey string `json:"encoding_aes_key"` //EncodingAESKey
|
||||||
|
Cache cache.Cache
|
||||||
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
|
//Package context 开放平台相关context
|
||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,7 +16,12 @@ const (
|
|||||||
queryAuthURL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s"
|
queryAuthURL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s"
|
||||||
refreshTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s"
|
refreshTokenURL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s"
|
||||||
getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s"
|
getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s"
|
||||||
getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
|
componentLoginURL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%d&biz_appid=%s"
|
||||||
|
bindComponentURL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&auth_type=%d&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&biz_appid=%s#wechat_redirect"
|
||||||
|
//TODO 获取授权方选项信息
|
||||||
|
//getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
|
||||||
|
//TODO 获取已授权的账号信息
|
||||||
|
//getuthorizerListURL = "POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ComponentAccessToken 第三方平台
|
// ComponentAccessToken 第三方平台
|
||||||
@@ -52,7 +59,9 @@ func (ctx *Context) SetComponentAccessToken(verifyTicket string) (*ComponentAcce
|
|||||||
|
|
||||||
accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
|
accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
|
||||||
expires := at.ExpiresIn - 1500
|
expires := at.ExpiresIn - 1500
|
||||||
ctx.Cache.Set(accessTokenCacheKey, at.AccessToken, time.Duration(expires)*time.Second)
|
if err := ctx.Cache.Set(accessTokenCacheKey, at.AccessToken, time.Duration(expires)*time.Second); err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return at, nil
|
return at, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +90,24 @@ func (ctx *Context) GetPreCode() (string, error) {
|
|||||||
return ret.PreCode, nil
|
return ret.PreCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetComponentLoginPage 获取第三方公众号授权链接(扫码授权)
|
||||||
|
func (ctx *Context) GetComponentLoginPage(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||||
|
code, err := ctx.GetPreCode()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(componentLoginURL, ctx.AppID, code, url.QueryEscape(redirectURI), authType, bizAppID), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBindComponentURL 获取第三方公众号授权链接(链接跳转,适用移动端)
|
||||||
|
func (ctx *Context) GetBindComponentURL(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||||
|
code, err := ctx.GetPreCode()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(bindComponentURL, authType, ctx.AppID, code, url.QueryEscape(redirectURI), bizAppID), nil
|
||||||
|
}
|
||||||
|
|
||||||
// ID 微信返回接口中各种类型字段
|
// ID 微信返回接口中各种类型字段
|
||||||
type ID struct {
|
type ID struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
@@ -123,13 +150,17 @@ func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ret struct {
|
var ret struct {
|
||||||
|
util.CommonError
|
||||||
Info *AuthBaseInfo `json:"authorization_info"`
|
Info *AuthBaseInfo `json:"authorization_info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(body, &ret); err != nil {
|
if err := json.Unmarshal(body, &ret); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if ret.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("QueryAuthCode error : errcode=%v , errmsg=%v", ret.ErrCode, ret.ErrMsg)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return ret.Info, nil
|
return ret.Info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,8 +188,9 @@ func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessT
|
|||||||
}
|
}
|
||||||
|
|
||||||
authrTokenKey := "authorizer_access_token_" + appid
|
authrTokenKey := "authorizer_access_token_" + appid
|
||||||
ctx.Cache.Set(authrTokenKey, ret.AccessToken, time.Minute*80)
|
if err := ctx.Cache.Set(authrTokenKey, ret.AccessToken, time.Minute*80); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
10
openplatform/context/context.go
Normal file
10
openplatform/context/context.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Context struct
|
||||||
|
type Context struct {
|
||||||
|
*config.Config
|
||||||
|
}
|
||||||
52
openplatform/miniprogram/basic/basic.go
Normal file
52
openplatform/miniprogram/basic/basic.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package basic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getAccountBasicInfoURL = "https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Basic 基础信息设置
|
||||||
|
type Basic struct {
|
||||||
|
*openContext.Context
|
||||||
|
appID string
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewBasic new
|
||||||
|
func NewBasic(opContext *openContext.Context, appID string) *Basic {
|
||||||
|
return &Basic{Context: opContext, appID: appID}
|
||||||
|
}
|
||||||
|
|
||||||
|
//AccountBasicInfo 基础信息
|
||||||
|
type AccountBasicInfo struct {
|
||||||
|
util.CommonError
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetAccountBasicInfo 获取小程序基础信息
|
||||||
|
//reference:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Mini_Program_Information_Settings.html
|
||||||
|
func (basic *Basic) GetAccountBasicInfo() (*AccountBasicInfo, error) {
|
||||||
|
ak, err := basic.GetAuthrAccessToken(basic.AppID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf("%s?access_token=%s", getAccountBasicInfoURL, ak)
|
||||||
|
data, err := util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := &AccountBasicInfo{}
|
||||||
|
if err := util.DecodeWithError(data, result, "account/getaccountbasicinfo"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//modify_domain设置服务器域名
|
||||||
|
//TODO
|
||||||
|
//func (encryptor *Basic) modifyDomain() {
|
||||||
|
//}
|
||||||
69
openplatform/miniprogram/component/component.go
Normal file
69
openplatform/miniprogram/component/component.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package component
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
fastregisterweappURL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Component 快速创建小程序
|
||||||
|
type Component struct {
|
||||||
|
*openContext.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewComponent new
|
||||||
|
func NewComponent(opContext *openContext.Context) *Component {
|
||||||
|
return &Component{opContext}
|
||||||
|
}
|
||||||
|
|
||||||
|
//RegisterMiniProgramParam 快速注册小程序参数
|
||||||
|
type RegisterMiniProgramParam struct {
|
||||||
|
Name string `json:"name"` //企业名
|
||||||
|
Code string `json:"code"` //企业代码
|
||||||
|
CodeType string `json:"code_type"` //企业代码类型 1:统一社会信用代码(18 位) 2:组织机构代码(9 位 xxxxxxxx-x) 3:营业执照注册号(15 位)
|
||||||
|
LegalPersonaWechat string `json:"legal_persona_wechat"` //法人微信号
|
||||||
|
LegalPersonaName string `json:"legal_persona_name"` //法人姓名(绑定银行卡)
|
||||||
|
ComponentPhone string `json:"component_phone"` //第三方联系电话(方便法人与第三方联系)
|
||||||
|
}
|
||||||
|
|
||||||
|
//RegisterMiniProgram 快速创建小程
|
||||||
|
//reference: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Fast_Registration_Interface_document.html
|
||||||
|
func (component *Component) RegisterMiniProgram(param *RegisterMiniProgramParam) error {
|
||||||
|
componentAK, err := component.GetComponentAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(fastregisterweappURL+"?action=create&component_access_token=%s", componentAK)
|
||||||
|
data, err := util.PostJSON(url, param)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(data, "component/fastregisterweapp?action=create")
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetRegistrationStatusParam 查询任务创建状态
|
||||||
|
type GetRegistrationStatusParam struct {
|
||||||
|
Name string `json:"name"` //企业名
|
||||||
|
LegalPersonaWechat string `json:"legal_persona_wechat"` //法人微信号
|
||||||
|
LegalPersonaName string `json:"legal_persona_name"` //法人姓名(绑定银行卡)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetRegistrationStatus 查询创建任务状态.
|
||||||
|
func (component *Component) GetRegistrationStatus(param *GetRegistrationStatusParam) error {
|
||||||
|
componentAK, err := component.GetComponentAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(fastregisterweappURL+"?action=search&component_access_token=%s", componentAK)
|
||||||
|
data, err := util.PostJSON(url, param)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(data, "component/fastregisterweapp?action=search")
|
||||||
|
}
|
||||||
32
openplatform/miniprogram/miniprogram.go
Normal file
32
openplatform/miniprogram/miniprogram.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package miniprogram
|
||||||
|
|
||||||
|
import (
|
||||||
|
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
|
||||||
|
)
|
||||||
|
|
||||||
|
//MiniProgram 代小程序实现业务
|
||||||
|
type MiniProgram struct {
|
||||||
|
AppID string
|
||||||
|
openContext *openContext.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewMiniProgram 实例化
|
||||||
|
func NewMiniProgram(opCtx *openContext.Context, appID string) *MiniProgram {
|
||||||
|
return &MiniProgram{
|
||||||
|
openContext: opCtx,
|
||||||
|
AppID: appID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetComponent get component
|
||||||
|
//快速注册小程序相关
|
||||||
|
func (miniProgram *MiniProgram) GetComponent() *component.Component {
|
||||||
|
return component.NewComponent(miniProgram.openContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetBasic 基础信息设置
|
||||||
|
func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
|
||||||
|
return basic.NewBasic(miniProgram.openContext, miniProgram.AppID)
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user