mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
Compare commits
10 Commits
v2.0.3-bet
...
v2.0.5-rc.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8fb058740 | ||
|
|
d5a67eaf29 | ||
|
|
e5f0d5eab7 | ||
|
|
2eae660002 | ||
|
|
c0da806e03 | ||
|
|
185baa5d12 | ||
|
|
bf42c188cb | ||
|
|
53b0f26688 | ||
|
|
430277c947 | ||
|
|
71e3ddaab3 |
28
.github/workflows/go.yml
vendored
28
.github/workflows/go.yml
vendored
@@ -7,34 +7,38 @@ on:
|
|||||||
branches: [ master,release-* ]
|
branches: [ master,release-* ]
|
||||||
|
|
||||||
jobs:
|
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:
|
build:
|
||||||
name: Build
|
name: Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
redis:
|
redis:
|
||||||
image: redis
|
image: redis
|
||||||
ports:
|
ports:
|
||||||
- 6379:6379
|
- 6379:6379
|
||||||
options: --entrypoint redis-server
|
options: --entrypoint redis-server
|
||||||
memcached:
|
memcached:
|
||||||
image: memcached
|
image: memcached
|
||||||
ports:
|
ports:
|
||||||
- 11211:11211
|
- 11211:11211
|
||||||
steps:
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
- name: Set up Go 1.x
|
- name: Set up Go 1.x
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: ^1.13
|
go-version: ^1.13
|
||||||
id: go
|
id: go
|
||||||
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- name: golangci-lint
|
|
||||||
uses: golangci/golangci-lint-action@v1
|
|
||||||
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.26
|
|
||||||
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test -v -race ./...
|
run: go test -v -race ./...
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ linters:
|
|||||||
- errcheck
|
- errcheck
|
||||||
- funlen
|
- funlen
|
||||||
- goconst
|
- goconst
|
||||||
- gocritic
|
# - gocritic
|
||||||
- gocyclo
|
- gocyclo
|
||||||
- gofmt
|
- gofmt
|
||||||
- goimports
|
- goimports
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
```
|
```
|
||||||
import github.com/silenceper/wechat/v2
|
import "github.com/silenceper/wechat/v2"
|
||||||
```
|
```
|
||||||
|
|
||||||
以下是一个微信公众号处理消息接收以及回复的例子:
|
以下是一个微信公众号处理消息接收以及回复的例子:
|
||||||
@@ -58,7 +58,7 @@ server.Send()
|
|||||||
- miniprogram: 小程序API
|
- miniprogram: 小程序API
|
||||||
- minigame:小游戏API
|
- minigame:小游戏API
|
||||||
- pay:微信支付API
|
- pay:微信支付API
|
||||||
- opernplatform:开放平台API
|
- openplatform:开放平台API
|
||||||
- work:企业微信
|
- work:企业微信
|
||||||
- aispeech:智能对话
|
- aispeech:智能对话
|
||||||
|
|
||||||
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
//Package config 小程序config配置
|
// Package config 小程序config配置
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/silenceper/wechat/v2/cache"
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Config config for 小程序
|
// Config config for 小程序
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string `json:"app_id"` //appid
|
AppID string `json:"app_id"` // appid
|
||||||
AppSecret string `json:"app_secret"` //appsecret
|
AppSecret string `json:"app_secret"` // appsecret
|
||||||
Cache cache.Cache
|
Cache cache.Cache
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,11 @@ func (basic *Basic) 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sendURLByTag = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"
|
sendURLByTag = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"
|
||||||
sendURLByOpenID = "https://api.weixin.qq.com/cgi-bin/message/mass/send"
|
sendURLByOpenID = "https://api.weixin.qq.com/cgi-bin/message/mass/send"
|
||||||
deleteSendURL = "https://api.weixin.qq.com/cgi-bin/message/mass/delete"
|
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 发送消息类型
|
//MsgType 发送消息类型
|
||||||
@@ -34,11 +38,12 @@ const (
|
|||||||
//Broadcast 群发消息
|
//Broadcast 群发消息
|
||||||
type Broadcast struct {
|
type Broadcast struct {
|
||||||
*context.Context
|
*context.Context
|
||||||
|
preview bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewBroadcast new
|
//NewBroadcast new
|
||||||
func NewBroadcast(ctx *context.Context) *Broadcast {
|
func NewBroadcast(ctx *context.Context) *Broadcast {
|
||||||
return &Broadcast{ctx}
|
return &Broadcast{ctx, false}
|
||||||
}
|
}
|
||||||
|
|
||||||
//User 发送的用户
|
//User 发送的用户
|
||||||
@@ -50,8 +55,16 @@ type User struct {
|
|||||||
//Result 群发返回结果
|
//Result 群发返回结果
|
||||||
type Result struct {
|
type Result struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
MsgID int64 `json:"msg_id"`
|
MsgID int64 `json:"msg_id"`
|
||||||
MsgDataID int64 `json:"msg_data_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 发送请求的数据
|
//sendRequest 发送请求的数据
|
||||||
@@ -250,7 +263,66 @@ func (broadcast *Broadcast) Delete(msgID int64, articleIDx int64) error {
|
|||||||
return util.DecodeWithCommonError(data, "Delete")
|
return util.DecodeWithCommonError(data, "Delete")
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO 发送预览,群发消息状态,发送速度
|
// 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) {
|
func (broadcast *Broadcast) chooseTagOrOpenID(user *User, req *sendRequest) (ret *sendRequest, url string) {
|
||||||
sendURL := ""
|
sendURL := ""
|
||||||
@@ -260,16 +332,22 @@ func (broadcast *Broadcast) chooseTagOrOpenID(user *User, req *sendRequest) (ret
|
|||||||
}
|
}
|
||||||
sendURL = sendURLByTag
|
sendURL = sendURLByTag
|
||||||
} else {
|
} else {
|
||||||
if user.TagID != 0 {
|
if broadcast.preview {
|
||||||
req.Filter = map[string]interface{}{
|
// 预览
|
||||||
"is_to_all": false,
|
|
||||||
"tag_id": user.TagID,
|
|
||||||
}
|
|
||||||
sendURL = sendURLByTag
|
|
||||||
}
|
|
||||||
if len(user.OpenID) != 0 {
|
|
||||||
req.ToUser = user.OpenID
|
req.ToUser = user.OpenID
|
||||||
sendURL = sendURLByOpenID
|
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
|
return req, sendURL
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"github.com/silenceper/wechat/v2/cache"
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Config config for 微信公众号
|
// Config config for 微信公众号
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string `json:"app_id"` //appid
|
AppID string `json:"app_id"` //appid
|
||||||
AppSecret string `json:"app_secret"` //appsecret
|
AppSecret string `json:"app_secret"` //appsecret
|
||||||
|
|||||||
@@ -13,47 +13,51 @@ type Button struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//SetSubButton 设置二级菜单
|
//SetSubButton 设置二级菜单
|
||||||
func (btn *Button) SetSubButton(name string, subButtons []*Button) {
|
func (btn *Button) SetSubButton(name string, subButtons []*Button) *Button {
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.SubButtons = subButtons
|
btn.SubButtons = subButtons
|
||||||
btn.Type = ""
|
btn.Type = ""
|
||||||
btn.Key = ""
|
btn.Key = ""
|
||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetClickButton btn 为click类型
|
//SetClickButton btn 为click类型
|
||||||
func (btn *Button) SetClickButton(name, key string) {
|
func (btn *Button) SetClickButton(name, key string) *Button {
|
||||||
btn.Type = "click"
|
btn.Type = "click"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetViewButton view类型
|
//SetViewButton view类型
|
||||||
func (btn *Button) SetViewButton(name, url string) {
|
func (btn *Button) SetViewButton(name, url string) *Button {
|
||||||
btn.Type = "view"
|
btn.Type = "view"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.URL = url
|
btn.URL = url
|
||||||
btn.Key = ""
|
btn.Key = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetScanCodePushButton 扫码推事件
|
//SetScanCodePushButton 扫码推事件
|
||||||
func (btn *Button) SetScanCodePushButton(name, key string) {
|
func (btn *Button) SetScanCodePushButton(name, key string) *Button {
|
||||||
btn.Type = "scancode_push"
|
btn.Type = "scancode_push"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetScanCodeWaitMsgButton 设置 扫码推事件且弹出"消息接收中"提示框
|
//SetScanCodeWaitMsgButton 设置 扫码推事件且弹出"消息接收中"提示框
|
||||||
func (btn *Button) SetScanCodeWaitMsgButton(name, key string) {
|
func (btn *Button) SetScanCodeWaitMsgButton(name, key string) *Button {
|
||||||
btn.Type = "scancode_waitmsg"
|
btn.Type = "scancode_waitmsg"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
@@ -61,10 +65,11 @@ func (btn *Button) SetScanCodeWaitMsgButton(name, key string) {
|
|||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetPicSysPhotoButton 设置弹出系统拍照发图按钮
|
//SetPicSysPhotoButton 设置弹出系统拍照发图按钮
|
||||||
func (btn *Button) SetPicSysPhotoButton(name, key string) {
|
func (btn *Button) SetPicSysPhotoButton(name, key string) *Button {
|
||||||
btn.Type = "pic_sysphoto"
|
btn.Type = "pic_sysphoto"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
@@ -72,10 +77,11 @@ func (btn *Button) SetPicSysPhotoButton(name, key string) {
|
|||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetPicPhotoOrAlbumButton 设置弹出拍照或者相册发图类型按钮
|
//SetPicPhotoOrAlbumButton 设置弹出拍照或者相册发图类型按钮
|
||||||
func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) {
|
func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) *Button {
|
||||||
btn.Type = "pic_photo_or_album"
|
btn.Type = "pic_photo_or_album"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
@@ -83,10 +89,11 @@ func (btn *Button) SetPicPhotoOrAlbumButton(name, key string) {
|
|||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPicWeixinButton 设置弹出微信相册发图器类型按钮
|
//SetPicWeixinButton 设置弹出微信相册发图器类型按钮
|
||||||
func (btn *Button) SetPicWeixinButton(name, key string) {
|
func (btn *Button) SetPicWeixinButton(name, key string) *Button {
|
||||||
btn.Type = "pic_weixin"
|
btn.Type = "pic_weixin"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
@@ -94,10 +101,11 @@ func (btn *Button) SetPicWeixinButton(name, key string) {
|
|||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLocationSelectButton 设置 弹出地理位置选择器 类型按钮
|
//SetLocationSelectButton 设置 弹出地理位置选择器 类型按钮
|
||||||
func (btn *Button) SetLocationSelectButton(name, key string) {
|
func (btn *Button) SetLocationSelectButton(name, key string) *Button {
|
||||||
btn.Type = "location_select"
|
btn.Type = "location_select"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.Key = key
|
btn.Key = key
|
||||||
@@ -105,10 +113,11 @@ func (btn *Button) SetLocationSelectButton(name, key string) {
|
|||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetMediaIDButton 设置 下发消息(除文本消息) 类型按钮
|
//SetMediaIDButton 设置 下发消息(除文本消息) 类型按钮
|
||||||
func (btn *Button) SetMediaIDButton(name, mediaID string) {
|
func (btn *Button) SetMediaIDButton(name, mediaID string) *Button {
|
||||||
btn.Type = "media_id"
|
btn.Type = "media_id"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.MediaID = mediaID
|
btn.MediaID = mediaID
|
||||||
@@ -116,10 +125,11 @@ func (btn *Button) SetMediaIDButton(name, mediaID string) {
|
|||||||
btn.Key = ""
|
btn.Key = ""
|
||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetViewLimitedButton 设置 跳转图文消息URL 类型按钮
|
//SetViewLimitedButton 设置 跳转图文消息URL 类型按钮
|
||||||
func (btn *Button) SetViewLimitedButton(name, mediaID string) {
|
func (btn *Button) SetViewLimitedButton(name, mediaID string) *Button {
|
||||||
btn.Type = "view_limited"
|
btn.Type = "view_limited"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.MediaID = mediaID
|
btn.MediaID = mediaID
|
||||||
@@ -127,10 +137,11 @@ func (btn *Button) SetViewLimitedButton(name, mediaID string) {
|
|||||||
btn.Key = ""
|
btn.Key = ""
|
||||||
btn.URL = ""
|
btn.URL = ""
|
||||||
btn.SubButtons = nil
|
btn.SubButtons = nil
|
||||||
|
return btn
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetMiniprogramButton 设置 跳转小程序 类型按钮 (公众号后台必须已经关联小程序)
|
//SetMiniprogramButton 设置 跳转小程序 类型按钮 (公众号后台必须已经关联小程序)
|
||||||
func (btn *Button) SetMiniprogramButton(name, url, appID, pagePath string) {
|
func (btn *Button) SetMiniprogramButton(name, url, appID, pagePath string) *Button {
|
||||||
btn.Type = "miniprogram"
|
btn.Type = "miniprogram"
|
||||||
btn.Name = name
|
btn.Name = name
|
||||||
btn.URL = url
|
btn.URL = url
|
||||||
@@ -140,4 +151,65 @@ func (btn *Button) SetMiniprogramButton(name, url, appID, pagePath string) {
|
|||||||
btn.Key = ""
|
btn.Key = ""
|
||||||
btn.MediaID = ""
|
btn.MediaID = ""
|
||||||
btn.SubButtons = nil
|
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))
|
||||||
|
}
|
||||||
@@ -147,12 +147,12 @@ func (menu *Menu) SetMenuByJSON(jsonInfo string) error {
|
|||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", menuCreateURL, accessToken)
|
uri := fmt.Sprintf("%s?access_token=%s", menuCreateURL, accessToken)
|
||||||
|
|
||||||
response, err := util.PostJSON(uri, jsonInfo)
|
response, err := util.HTTPPost(uri, jsonInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.DecodeWithCommonError(response, "SetMenu")
|
return util.DecodeWithCommonError(response, "SetMenuByJSON")
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetMenu 获取菜单配置
|
//GetMenu 获取菜单配置
|
||||||
@@ -223,7 +223,7 @@ func (menu *Menu) AddConditionalByJSON(jsonInfo string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s", menuAddConditionalURL, accessToken)
|
uri := fmt.Sprintf("%s?access_token=%s", menuAddConditionalURL, accessToken)
|
||||||
response, err := util.PostJSON(uri, jsonInfo)
|
response, err := util.HTTPPost(uri, jsonInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ type Js struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewJs init
|
//NewJs init
|
||||||
func NewJs(context *context.Context) *Js {
|
func NewJs(context *context.Context, appID string) *Js {
|
||||||
js := new(Js)
|
js := new(Js)
|
||||||
js.Context = context
|
js.Context = context
|
||||||
jsTicketHandle := credential.NewDefaultJsTicket(context.AppID, credential.CacheKeyOfficialAccountPrefix, context.Cache)
|
jsTicketHandle := credential.NewDefaultJsTicket(appID, credential.CacheKeyOfficialAccountPrefix, context.Cache)
|
||||||
js.SetJsTicketHandle(jsTicketHandle)
|
js.SetJsTicketHandle(jsTicketHandle)
|
||||||
return js
|
return js
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func (officialAccount *OfficialAccount) PlatformOauth() *oauth.Oauth {
|
|||||||
|
|
||||||
// PlatformJs 平台代获取js-sdk配置
|
// PlatformJs 平台代获取js-sdk配置
|
||||||
func (officialAccount *OfficialAccount) PlatformJs() *js.Js {
|
func (officialAccount *OfficialAccount) PlatformJs() *js.Js {
|
||||||
return js.NewJs(officialAccount.GetContext())
|
return js.NewJs(officialAccount.GetContext(), officialAccount.appID)
|
||||||
}
|
}
|
||||||
|
|
||||||
//DefaultAuthrAccessToken 默认获取授权ak的方法
|
//DefaultAuthrAccessToken 默认获取授权ak的方法
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
//Config config for pay
|
// Config config for pay
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string `json:"app_id"`
|
AppID string `json:"app_id"`
|
||||||
MchID string `json:"mch_id"`
|
MchID string `json:"mch_id"`
|
||||||
|
|||||||
77
pay/notify/refund.go
Normal file
77
pay/notify/refund.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package notify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// reference: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10
|
||||||
|
|
||||||
|
// RefundedResult 退款回调
|
||||||
|
type RefundedResult struct {
|
||||||
|
ReturnCode *string `xml:"return_code"`
|
||||||
|
ReturnMsg *string `xml:"return_msg"`
|
||||||
|
|
||||||
|
AppID *string `xml:"appid"`
|
||||||
|
MchID *string `xml:"mch_id"`
|
||||||
|
NonceStr *string `xml:"nonce_str"`
|
||||||
|
ReqInfo *string `xml:"req_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RefundedReqInfo 退款结果(明文)
|
||||||
|
type RefundedReqInfo struct {
|
||||||
|
TransactionID *string `xml:"transaction_id"`
|
||||||
|
OutTradeNO *string `xml:"out_trade_no"`
|
||||||
|
RefundID *string `xml:"refund_id"`
|
||||||
|
OutRefundNO *string `xml:"out_refund_no"`
|
||||||
|
TotalFee *int `xml:"total_fee"`
|
||||||
|
SettlementTotalFee *int `xml:"settlement_total_fee"`
|
||||||
|
RefundFee *int `xml:"refund_fee"`
|
||||||
|
SettlementRefundFee *int `xml:"settlement_refund_fee"`
|
||||||
|
RefundStatus *string `xml:"refund_status"`
|
||||||
|
SuccessTime *string `xml:"success_time"`
|
||||||
|
RefundRecvAccount *string `xml:"refund_recv_account"`
|
||||||
|
RefundAccount *string `xml:"refund_account"`
|
||||||
|
RefundRequestSource *string `xml:"refund_request_source"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RefundedResp 消息通知返回
|
||||||
|
type RefundedResp struct {
|
||||||
|
ReturnCode string `xml:"return_code"`
|
||||||
|
ReturnMsg string `xml:"return_msg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecryptReqInfo 对退款结果进行解密
|
||||||
|
func (notify *Notify) DecryptReqInfo(result *RefundedResult) (*RefundedReqInfo, error) {
|
||||||
|
var err error
|
||||||
|
if result == nil || result.ReqInfo == nil {
|
||||||
|
return nil, errors.New("empty refunded_result or req_info")
|
||||||
|
}
|
||||||
|
|
||||||
|
base64Decode, err := base64.StdEncoding.DecodeString(*result.ReqInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := md5.New()
|
||||||
|
if _, err = hash.Write([]byte(notify.Key)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
md5APIKey := hex.EncodeToString(hash.Sum(nil))
|
||||||
|
|
||||||
|
data, err := util.AesECBDecrypt(base64Decode, []byte(md5APIKey))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := &RefundedReqInfo{}
|
||||||
|
if err = xml.Unmarshal(data, res); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
26
pay/notify/refund_test.go
Normal file
26
pay/notify/refund_test.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package notify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/pay/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNotify_DecryptReqInfo(t *testing.T) {
|
||||||
|
// data_source: https://studygolang.com/articles/11811
|
||||||
|
notify := &Notify{Config: &config.Config{Key: "ziR0QKsTUfMOuochC9RfCdmfHECorQAP"}}
|
||||||
|
info := "YYwp8C48th0wnQzTqeI+41pflB26v+smFj9z6h9RPBgxTyZyxc+4YNEz7QEgZNWj/6rIb2MfyWMZmCc41CfjKSssoSZPXxOhUayb6KvNSZ1p6frOX1PDWzhyruXK7ouNND+gDsG4yZ0XXzsL4/pYNwLLba/71QrnkJ/BHcByk4EXnglju5DLup9pJQSnTxjomI9Rxu57m9jg5lLQFxMWXyeASZJNvof0ulnHlWJswS4OxKOkmW7VEyKyLGV6npoOm03Qsx2wkRxLsSa9gPpg4hdaReeUqh1FMbm7aWjyrVYT/MEZWg98p4GomEIYvz34XfDncTezX4bf/ZiSLXt79aE1/YTZrYfymXeCrGjlbe0rg/T2ezJHAC870u2vsVbY1/KcE2A443N+DEnAziXlBQ1AeWq3Rqk/O6/TMM0lomzgctAOiAMg+bh5+Gu1ubA9O3E+vehULydD5qx2o6i3+qA9ORbH415NyRrQdeFq5vmCiRikp5xYptWiGZA0tkoaLKMPQ4ndE5gWHqiBbGPfULZWokI+QjjhhBmwgbd6J0VqpRorwOuzC/BHdkP72DCdNcm7IDUpggnzBIy0+seWIkcHEryKjge3YDHpJeQCqrAH0CgxXHDt1xtbQbST1VqFyuhPhUjDXMXrknrGPN/oE1t0rLRq+78cI+k8xe5E6seeUXQsEe8r3358mpcDYSmXWSXVZxK6er9EF98APqHwcndyEJD2YyCh/mMVhERuX+7kjlRXSiNUWa/Cv/XAKFQuvUYA5ea2eYWtPRHa4DpyuF1SNsaqVKfgqKXZrJHfAgslVpSVqUpX4zkKszHF4kwMZO3M7J1P94Mxa7Tm9mTOJePOoHPXeEB+m9rX6pSfoi3mJDQ5inJ+Vc4gOkg/Wd/lqiy6TTyP/dHDN6/v+AuJx5AXBo/2NDD3fWhHjkqEKIuARr2ClZt9ZRQO4HkXdZo7CN06sGCHk48Tg8PmxnxKcMZm7Aoquv5yMIM2gWSWIRJhwJ8cUpafIHc+GesDlbF6Zbt+/KXkafJAQq2RklEN+WvZ/zFz113EPgWPjp16TwBoziq96MMekvWKY/vdhjol8VFtGH9F61Oy1Xwf6DJtPw=="
|
||||||
|
res, err := notify.DecryptReqInfo(&RefundedResult{ReqInfo: &info})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := xml.Marshal(res)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Log(string(bytes))
|
||||||
|
}
|
||||||
@@ -63,6 +63,7 @@ type PreOrder struct {
|
|||||||
TradeType string `xml:"trade_type,omitempty"`
|
TradeType string `xml:"trade_type,omitempty"`
|
||||||
PrePayID string `xml:"prepay_id,omitempty"`
|
PrePayID string `xml:"prepay_id,omitempty"`
|
||||||
CodeURL string `xml:"code_url,omitempty"`
|
CodeURL string `xml:"code_url,omitempty"`
|
||||||
|
MWebURL string `xml:"mweb_url,omitempty"`
|
||||||
ErrCode string `xml:"err_code,omitempty"`
|
ErrCode string `xml:"err_code,omitempty"`
|
||||||
ErrCodeDes string `xml:"err_code_des,omitempty"`
|
ErrCodeDes string `xml:"err_code_des,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ type Params struct {
|
|||||||
RefundFee string
|
RefundFee string
|
||||||
RefundDesc string
|
RefundDesc string
|
||||||
RootCa string //ca证书
|
RootCa string //ca证书
|
||||||
|
NotifyURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
//request 接口请求参数
|
//request 接口请求参数
|
||||||
@@ -43,7 +44,7 @@ type request struct {
|
|||||||
TotalFee string `xml:"total_fee"`
|
TotalFee string `xml:"total_fee"`
|
||||||
RefundFee string `xml:"refund_fee"`
|
RefundFee string `xml:"refund_fee"`
|
||||||
RefundDesc string `xml:"refund_desc,omitempty"`
|
RefundDesc string `xml:"refund_desc,omitempty"`
|
||||||
//NotifyUrl string `xml:"notify_url,omitempty"`
|
NotifyURL string `xml:"notify_url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//Response 接口返回
|
//Response 接口返回
|
||||||
@@ -83,13 +84,16 @@ func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
|
|||||||
param["total_fee"] = p.TotalFee
|
param["total_fee"] = p.TotalFee
|
||||||
param["sign_type"] = util.SignTypeMD5
|
param["sign_type"] = util.SignTypeMD5
|
||||||
param["transaction_id"] = p.TransactionID
|
param["transaction_id"] = p.TransactionID
|
||||||
|
if p.NotifyURL != "" {
|
||||||
|
param["notify_url"] = p.NotifyURL
|
||||||
|
}
|
||||||
|
|
||||||
sign, err := util.ParamSign(param, refund.Key)
|
sign, err := util.ParamSign(param, refund.Key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
request := request{
|
req := request{
|
||||||
AppID: refund.AppID,
|
AppID: refund.AppID,
|
||||||
MchID: refund.MchID,
|
MchID: refund.MchID,
|
||||||
NonceStr: nonceStr,
|
NonceStr: nonceStr,
|
||||||
@@ -101,7 +105,7 @@ func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
|
|||||||
RefundFee: p.RefundFee,
|
RefundFee: p.RefundFee,
|
||||||
RefundDesc: p.RefundDesc,
|
RefundDesc: p.RefundDesc,
|
||||||
}
|
}
|
||||||
rawRet, err := util.PostXMLWithTLS(refundGateway, request, p.RootCa, refund.MchID)
|
rawRet, err := util.PostXMLWithTLS(refundGateway, req, p.RootCa, refund.MchID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
109
util/crypto.go
109
util/crypto.go
@@ -1,6 +1,7 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
@@ -20,7 +21,7 @@ const (
|
|||||||
SignTypeHMACSHA256 = `HMAC-SHA256`
|
SignTypeHMACSHA256 = `HMAC-SHA256`
|
||||||
)
|
)
|
||||||
|
|
||||||
//EncryptMsg 加密消息
|
// EncryptMsg 加密消息
|
||||||
func EncryptMsg(random, rawXMLMsg []byte, appID, aesKey string) (encrtptMsg []byte, err error) {
|
func EncryptMsg(random, rawXMLMsg []byte, appID, aesKey string) (encrtptMsg []byte, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
@@ -38,7 +39,7 @@ func EncryptMsg(random, rawXMLMsg []byte, appID, aesKey string) (encrtptMsg []by
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//AESEncryptMsg ciphertext = AES_Encrypt[random(16B) + msg_len(4B) + rawXMLMsg + appId]
|
// AESEncryptMsg ciphertext = AES_Encrypt[random(16B) + msg_len(4B) + rawXMLMsg + appId]
|
||||||
//参考:github.com/chanxuehong/wechat.v2
|
//参考:github.com/chanxuehong/wechat.v2
|
||||||
func AESEncryptMsg(random, rawXMLMsg []byte, appID string, aesKey []byte) (ciphertext []byte) {
|
func AESEncryptMsg(random, rawXMLMsg []byte, appID string, aesKey []byte) (ciphertext []byte) {
|
||||||
const (
|
const (
|
||||||
@@ -76,7 +77,7 @@ func AESEncryptMsg(random, rawXMLMsg []byte, appID string, aesKey []byte) (ciphe
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//DecryptMsg 消息解密
|
// DecryptMsg 消息解密
|
||||||
func DecryptMsg(appID, encryptedMsg, aesKey string) (random, rawMsgXMLBytes []byte, err error) {
|
func DecryptMsg(appID, encryptedMsg, aesKey string) (random, rawMsgXMLBytes []byte, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
@@ -227,3 +228,105 @@ func ParamSign(p map[string]string, key string) (string, error) {
|
|||||||
|
|
||||||
return CalculateSign(str, signType, key)
|
return CalculateSign(str, signType, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ECB provides confidentiality by assigning a fixed ciphertext block to each plaintext block.
|
||||||
|
// See NIST SP 800-38A, pp 08-09
|
||||||
|
// reference: https://codereview.appspot.com/7860047/patch/23001/24001
|
||||||
|
type ecb struct {
|
||||||
|
b cipher.Block
|
||||||
|
blockSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newECB(b cipher.Block) *ecb {
|
||||||
|
return &ecb{
|
||||||
|
b: b,
|
||||||
|
blockSize: b.BlockSize(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ECBEncryptor -
|
||||||
|
type ECBEncryptor ecb
|
||||||
|
|
||||||
|
// NewECBEncryptor returns a BlockMode which encrypts in electronic code book mode, using the given Block.
|
||||||
|
func NewECBEncryptor(b cipher.Block) cipher.BlockMode {
|
||||||
|
return (*ECBEncryptor)(newECB(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockSize implement BlockMode.BlockSize
|
||||||
|
func (x *ECBEncryptor) BlockSize() int {
|
||||||
|
return x.blockSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptBlocks implement BlockMode.CryptBlocks
|
||||||
|
func (x *ECBEncryptor) CryptBlocks(dst, src []byte) {
|
||||||
|
if len(src)%x.blockSize != 0 {
|
||||||
|
panic("crypto/cipher: input not full blocks")
|
||||||
|
}
|
||||||
|
if len(dst) < len(src) {
|
||||||
|
panic("crypto/cipher: output smaller than input")
|
||||||
|
}
|
||||||
|
for len(src) > 0 {
|
||||||
|
x.b.Encrypt(dst, src[:x.blockSize])
|
||||||
|
src = src[x.blockSize:]
|
||||||
|
dst = dst[x.blockSize:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ECBDecryptor -
|
||||||
|
type ECBDecryptor ecb
|
||||||
|
|
||||||
|
// NewECBDecryptor returns a BlockMode which decrypts in electronic code book mode, using the given Block.
|
||||||
|
func NewECBDecryptor(b cipher.Block) cipher.BlockMode {
|
||||||
|
return (*ECBDecryptor)(newECB(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockSize implement BlockMode.BlockSize
|
||||||
|
func (x *ECBDecryptor) BlockSize() int {
|
||||||
|
return x.blockSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// CryptBlocks implement BlockMode.CryptBlocks
|
||||||
|
func (x *ECBDecryptor) CryptBlocks(dst, src []byte) {
|
||||||
|
if len(src)%x.blockSize != 0 {
|
||||||
|
panic("crypto/cipher: input not full blocks")
|
||||||
|
}
|
||||||
|
if len(dst) < len(src) {
|
||||||
|
panic("crypto/cipher: output smaller than input")
|
||||||
|
}
|
||||||
|
for len(src) > 0 {
|
||||||
|
x.b.Decrypt(dst, src[:x.blockSize])
|
||||||
|
src = src[x.blockSize:]
|
||||||
|
dst = dst[x.blockSize:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AesECBDecrypt will decrypt data with PKCS5Padding
|
||||||
|
func AesECBDecrypt(ciphertext []byte, aesKey []byte) ([]byte, error) {
|
||||||
|
if len(ciphertext) < aes.BlockSize {
|
||||||
|
return nil, errors.New("ciphertext too short")
|
||||||
|
}
|
||||||
|
// ECB mode always works in whole blocks.
|
||||||
|
if len(ciphertext)%aes.BlockSize != 0 {
|
||||||
|
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||||
|
}
|
||||||
|
block, err := aes.NewCipher(aesKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
NewECBDecryptor(block).CryptBlocks(ciphertext, ciphertext)
|
||||||
|
return PKCS5UnPadding(ciphertext), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PKCS5Padding -
|
||||||
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
||||||
|
padding := blockSize - len(ciphertext)%blockSize
|
||||||
|
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||||
|
return append(ciphertext, padText...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PKCS5UnPadding -
|
||||||
|
func PKCS5UnPadding(origData []byte) []byte {
|
||||||
|
length := len(origData)
|
||||||
|
unPadding := int(origData[length-1])
|
||||||
|
return origData[:(length - unPadding)]
|
||||||
|
}
|
||||||
|
|||||||
12
util/http.go
12
util/http.go
@@ -52,9 +52,9 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<"))
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">"))
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&"))
|
||||||
body := bytes.NewBuffer(jsonData)
|
body := bytes.NewBuffer(jsonData)
|
||||||
response, err := http.Post(uri, "application/json;charset=utf-8", body)
|
response, err := http.Post(uri, "application/json;charset=utf-8", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -75,9 +75,9 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<"))
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">"))
|
||||||
jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
|
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&"))
|
||||||
|
|
||||||
body := bytes.NewBuffer(jsonData)
|
body := bytes.NewBuffer(jsonData)
|
||||||
response, err := http.Post(uri, "application/json;charset=utf-8", body)
|
response, err := http.Post(uri, "application/json;charset=utf-8", body)
|
||||||
|
|||||||
Reference in New Issue
Block a user