1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-05 13:12:26 +08:00

Compare commits

...

68 Commits

Author SHA1 Message Date
silenceper
9b06954b10 Merge pull request #202 from ckeyer/add_ThumbURL
add ThumbURL
2020-01-14 16:31:43 +08:00
silenceper
431f7d3a9f Update FUNDING.yml 2020-01-10 19:47:44 +08:00
silenceper
8e24b47a70 Update FUNDING.yml 2020-01-10 19:46:35 +08:00
silenceper
83bd282760 Update README.md
增加公众号
2020-01-10 19:43:49 +08:00
Chuanjian Wang
bf167d959c add ThumbURL 2019-12-25 11:23:45 +08:00
silenceper
08b69d9419 Merge pull request #199 from silenceper/develop
fix tcb example
2019-11-26 13:53:48 +08:00
silenceper
ab39ec00d4 fix tcb example 2019-11-26 13:53:08 +08:00
silenceper
cafb84d6da Merge pull request #198 from silenceper/develop
增加小程序云开发http api
2019-11-26 13:49:49 +08:00
silenceper
d46df74eee 1、fix golnt错误
2、将包管理改为go mod
2019-11-26 13:44:55 +08:00
silenceper
96678d2279 增加小程序-云开发http api 2019-11-26 13:09:36 +08:00
silenceper
6e5bb2553d Merge pull request #196 from cielu/master
调整map、验签增加 attach参数
2019-11-21 22:27:54 +08:00
ciel yu
1dbe3f60ea 验签增加 attach 参数 2019-11-20 17:49:09 +08:00
cielu
0f99e2e34a Merge pull request #2 from silenceper/master
update into new
2019-11-20 17:42:04 +08:00
silenceper
7ea817a7c6 1、使用go mod 管理依赖
2、将template模板消息,移入到message文件夹
2019-11-19 22:51:40 +08:00
silenceper
de140f1037 Merge branch 'master' into develop 2019-11-19 18:54:53 +08:00
silenceper
d91e82c183 Merge pull request #194 from silenceper/hotfix/add-cdata
序列化 xml 时添加 cdata 标签
2019-11-18 20:54:38 +08:00
silenceper
3111e12c00 Merge pull request #187 from cielu/master
支付,增加通知回调,签名验证
2019-11-18 20:49:00 +08:00
silenceper
9c8717afba Merge pull request #169 from ckeyer/get_news
增加获取永久素材接口
2019-11-18 20:48:32 +08:00
silenceper
d7f371cb65 序列化 xml 时添加 cdata 标签 2019-11-18 20:45:32 +08:00
silenceper
39e81b45bf Merge pull request #192 from larry-dev/master
增加了关于微信智能硬件的一些方式
2019-11-18 09:35:58 +08:00
larry.liu
733c53a044 格式化代码 2019-11-15 16:32:25 +08:00
larry.liu
5617c9512d 格式化代码样式 2019-11-15 15:56:38 +08:00
larry.liu
d806d1c968 Merge remote-tracking branch 'origin/master' 2019-11-15 15:34:32 +08:00
larry.liu
254ac9d7a6 修改对象类型错误 2019-11-15 15:34:17 +08:00
silenceper
19e3174107 Update .travis.yml 2019-11-15 15:01:03 +08:00
liu larry
0dbc292f44 Merge branch 'master' into master 2019-11-15 14:55:11 +08:00
larry.liu
33d00f45c5 增加设备回调消息实体 2019-11-15 14:46:50 +08:00
larry.liu
c4a361bbf6 完善微信智能硬件接口 2019-11-15 14:46:16 +08:00
larry.liu
f4e58b0712 删除多余代码 2019-11-15 08:05:53 +08:00
larry.liu
5b307df969 增加微信设备管理
1.设备授权方法
2.获取设备二维码方法
2019-11-14 19:30:33 +08:00
silenceper
b16d231a29 Merge pull request #191 from silenceper/revert-184-master
Revert "fix: 序列化 xml 时添加 cdata 标签"
2019-11-12 15:44:13 +08:00
silenceper
c30319c74c Revert "fix: 序列化 xml 时添加 cdata 标签" 2019-11-12 15:05:07 +08:00
silenceper
76c1832798 Merge pull request #184 from chencanxin/master
fix: 序列化 xml 时添加 cdata 标签
2019-11-08 13:42:06 +08:00
陈灿鑫
f6d07aa714 fix: 修复 CDATA.MarshalXML 注释 2019-11-08 11:17:11 +08:00
陈灿鑫
1475417a64 fix: 添加 CDATA.MarshalXML 注释 2019-11-08 11:05:56 +08:00
陈灿鑫
bf456aa77b fix: 添加 CDATA 注释 2019-11-08 10:24:47 +08:00
ciel yu
934ca61b3b 支付 增加 接收通知回调,通知签名验证 2019-11-07 01:42:38 +08:00
cielu
00ca733814 Merge pull request #1 from silenceper/master
同步原仓库
2019-11-07 01:25:40 +08:00
silenceper
2e796b21d3 Merge pull request #186 from huangzhiran/master
修复readme.md里错误的例子
2019-11-06 18:06:12 +08:00
huangzhiran
412c2f0ea9 修复readme.md里错误的例子 2019-11-06 17:52:17 +08:00
silenceper
8cb724ece0 Delete bug_report.md 2019-11-05 14:50:18 +08:00
silenceper
13facb6df8 Create ISSUE_TEMPLATE.md 2019-11-05 14:50:03 +08:00
silenceper
55615762eb Update issue templates 2019-11-05 14:48:27 +08:00
silenceper
1fc4cc70ec Create FUNDING.yml 2019-11-05 14:45:22 +08:00
陈灿鑫
e09031b58c fix: 序列化 xml 时添加 cdata 标签 2019-11-01 23:21:25 +08:00
silenceper
279ff79406 Merge pull request #181 from 4funs/master
修复上传永久视频素材, close #180
2019-10-29 14:27:34 +08:00
kaiiak
7bde39a634 修复上传永久视频素材 2019-10-28 00:53:56 +08:00
silenceper
1711aeb46d Merge pull request #170 from quxiaolong1504/dev
增加内容审核相关 message 字段
2019-10-21 13:57:54 +08:00
quxiaolong
87470f143d 增加内容审核相关 message 字段 2019-10-15 12:24:42 +08:00
Chuanjian Wang
900a54ee06 增加获取永久素材接口 2019-10-15 10:44:54 +08:00
silenceper
7170f6ef32 Merge pull request #168 from cielu/master
支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
2019-10-15 09:02:47 +08:00
ciel yu
0c9dd16f1f 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
统一下单 新增非必传参数
2019-10-14 19:35:08 +08:00
ciel yu
f68f9d6f5e 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
统一下单 新增非必传参数
2019-10-14 19:28:28 +08:00
ciel yu
09dabb232d 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
统一下单 新增非必传参数
2019-10-14 19:14:02 +08:00
ciel yu
3ea624f832 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
统一下单 新增非必传参数
2019-10-14 19:08:35 +08:00
ciel yu
546dce2396 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
统一下单 新增非必传参数
2019-10-14 18:59:42 +08:00
ciel yu
abd7f512ba 支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数 2019-10-14 18:33:50 +08:00
silenceper
b8239ef9a9 Merge pull request #167 from cielu/master
小程序数据解密,增加手机号解密,兼容之前的用户信息解密
2019-10-14 09:00:37 +08:00
ciel yu
0dffcde475 小程序数据解密,增加手机号解密,兼容之前的用户信息解密 2019-10-12 17:38:41 +08:00
ciel yu
4e6fd625da 小程序数据解密,增加手机号解密,兼容之前的用户信息解密 2019-10-12 16:46:59 +08:00
ciel yu
453089e83e 小程序数据解密,增加手机号解密,兼容之前的用户信息解密 2019-10-12 16:41:26 +08:00
ciel yu
bb97bddc08 Revert "小程序数据解密,增加手机号解密,兼容之前的用户信息解密"
This reverts commit 54e2c82f
2019-10-12 16:27:55 +08:00
cielu
54e2c82fff 小程序数据解密,增加手机号解密,兼容之前的用户信息解密 2019-10-12 16:03:39 +08:00
silenceper
fab09a0bbe Merge pull request #162 from WhisperRain/master
add module to send customer message
2019-10-02 22:22:29 +08:00
WhisperRain
f8ab592606 add(customer message)Add module to send customer message 2019-10-01 20:11:57 +08:00
silenceper
688bca7436 merge master 2019-07-18 16:12:50 +08:00
silenceper
58d6810432 Merge pull request #106 from aikangs/develop
增加批量获取用户信息的功能
2019-04-23 21:11:56 +08:00
song kang
b0f1f71f37 fix code format 2019-03-31 13:40:23 +08:00
30 changed files with 1594 additions and 190 deletions

12
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: https://silenceper.com/img/wechat-pay.jpeg

2
.github/ISSUE_TEMPLATE.md vendored Normal file
View File

@@ -0,0 +1,2 @@
## 问题及现象
<!-- 描述你的问题现象,报错**贴截图**粘贴或者贴具体信息,提供**必要的代码段**

3
.gitignore vendored
View File

@@ -24,5 +24,6 @@ _testmain.go
*.prof
.DS_Store
.vscode/
vendor/*/
vendor
.idea/
examples/tcb/*

View File

@@ -1,6 +1,7 @@
language: go
go:
- 1.13.x
- 1.12.x
- 1.11.x
- 1.10.x

View File

@@ -3,7 +3,6 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat)
[![GoDoc](http://godoc.org/github.com/silenceper/wechat?status.svg)](http://godoc.org/github.com/silenceper/wechat)
使用Golang开发的微信SDK简单、易用。
## 快速开始
@@ -46,6 +45,10 @@ server.Send()
- Beego: [./examples/beego/beego.go](./examples/beego/beego.go)
- Gin Framework: [./examples/gin/gin.go](./examples/gin/gin.go)
## 交流群:
![关注公众号入群交流](https://silenceper.oss-cn-beijing.aliyuncs.com/qrcode/qr_code_study_program_258.jpg)
>关注公众号并回复“入群”
## 基本配置
```go
@@ -97,6 +100,7 @@ Cache主要用来保存全局access_token以及js-sdk中的ticket
- 获取js-sdk配置
- [素材管理](#素材管理)
- [小程序开发](#小程序开发)
- [小程序-云开发](./tcb)
## 消息管理
@@ -283,8 +287,8 @@ type Reply struct {
#### 回复图片消息
```go
//mediaID 可通过素材管理-上上传多媒体文件获得
image :=message.NewVideo("mediaID")
return &message.Reply{message.MsgTypeVideo, image}
image :=message.NewImage("mediaID")
return &message.Reply{message.MsgTypeImage, image}
```
#### 回复视频消息
```go

116
device/authorize.go Normal file
View File

@@ -0,0 +1,116 @@
package device
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/util"
)
const (
// DeviceAdd 添加设备标识
DeviceAdd = iota
// DeviceUpgrade 更新设备标识
DeviceUpgrade
)
type reqDeviceAuthorize struct {
// 设备id的个数
DeviceNum string `json:"device_num"`
// 设备id的列表json的array格式其size必须等于device_num
DeviceList []ReqDevice `json:"device_list"`
// 请求操作的类型限定取值为0设备授权缺省值为0 1设备更新更新已授权设备的各属性值
OpType string `json:"op_type,omitempty"`
// 设备的产品编号(由微信硬件平台分配)。可在公众号设备功能管理页面查询。
//当 op_type 为0product_id 为1不要填写 product_id 字段(会引起不必要错误);
//当 op_typy 为0product_id 不为1必须填写 product_id 字段;
//当 op_type 为 1 时,不要填写 product_id 字段。
ProductID string `json:"product_id,omitempty"`
}
//ReqDevice 设备授权实体
type ReqDevice struct {
// 设备的 device id
ID string `json:"id"`
// 设备的mac地址 格式采用16进制串的方式长度为12字节
// 不需要0X前缀 1234567890AB
Mac string `json:"mac"`
// 支持以下四种连接协议:
// android classic bluetooth 1
// ios classic bluetooth 2
// ble 3
// wifi -- 4
// 一个设备可以支持多种连接类型,用符号"|"做分割,客户端优先选择靠前的连接方式(优先级按|关系的排序依次降低),举例:
// 1表示设备仅支持andiod classic bluetooth 1|2表示设备支持andiod 和ios 两种classic bluetooth但是客户端优先选择andriod classic bluetooth 协议如果andriod classic bluetooth协议连接失败再选择ios classic bluetooth协议进行连接
// 安卓平台不同时支持BLE和classic类型
ConnectProtocol string `json:"connect_protocol"`
//auth及通信的加密key第三方需要将key烧制在设备上128bit格式采用16进制串的方式长度为32字节不需要0X前缀 1234567890ABCDEF1234567890ABCDEF
AuthKey string `json:"auth_key"`
// 断开策略,目前支持: 1退出公众号页面时即断开连接 2退出公众号之后保持连接不断开
CloseStrategy string `json:"close_strategy"`
//连接策略32位整型按bit位置位目前仅第1bit和第3bit位有效bit置0为无效1为有效第2bit已被废弃且bit位可以按或置位如1|4=5各bit置位含义说明如下
//1第1bit置位在公众号对话页面不停的尝试连接设备
//4第3bit置位处于非公众号页面如主界面等微信自动连接。当用户切换微信到前台时可能尝试去连接设备连上后一定时间会断开
ConnStrategy string `json:"conn_strategy"`
// auth version设备和微信进行auth时会根据该版本号来确认auth buf和auth key的格式各version对应的auth buf及key的具体格式可以参看“客户端蓝牙外设协议”该字段目前支持取值
// 0不加密的version
// 1version 1
AuthVer string `json:"auth_ver"`
// 表示mac地址在厂商广播manufature data里含有mac地址的偏移取值如下
// -1在尾部、
// -2表示不包含mac地址 其他:非法偏移
ManuMacPos string `json:"manu_mac_pos"`
// 表示mac地址在厂商serial number里含有mac地址的偏移取值如下
// -1表示在尾部
// -2表示不包含mac地址 其他:非法偏移
SerMacPost string `json:"ser_mac_post"`
// 精简协议类型取值如下计步设备精简协议1 若该字段填1connect_protocol 必须包括3。非精简协议设备切勿填写该字段
BleSimpleProtocol string `json:"ble_simple_protocol,omitempty"`
}
//ResBaseInfo 授权回调实体
type ResBaseInfo struct {
BaseInfo struct {
DeviceType string `json:"device_type"`
DeviceID string `json:"device_id"`
} `json:"base_info"`
}
// 授权回调根信息
type resDeviceAuthorize struct {
util.CommonError
Resp []ResBaseInfo `json:"resp"`
}
// DeviceAuthorize 设备授权
func (d *Device) DeviceAuthorize(devices []ReqDevice, opType int, product string) (res []ResBaseInfo, err error) {
var accessToken string
accessToken, err = d.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriAuthorize, accessToken)
req := reqDeviceAuthorize{
DeviceNum: fmt.Sprintf("%d", len(devices)),
DeviceList: devices,
OpType: fmt.Sprintf("%d", opType),
ProductID: product,
}
var response []byte
response, err = util.PostJSON(uri, req)
if err != nil {
return nil, err
}
var result resDeviceAuthorize
err = json.Unmarshal(response, &result)
if err != nil {
return
}
if result.ErrCode != 0 {
err = fmt.Errorf("DeviceAuthorize Error , errcode=%d , errmsg=%s", result.ErrCode, result.ErrMsg)
return
}
res = result.Resp
return
}

106
device/bind.go Normal file
View File

@@ -0,0 +1,106 @@
package device
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/util"
)
// ReqBind 设备绑定解绑共通实体
type ReqBind struct {
Ticket string `json:"ticket,omitempty"`
DeviceID string `json:"device_id"`
OpenID string `json:"openid"`
}
type resBind struct {
BaseResp util.CommonError `json:"base_resp"`
}
// Bind 设备绑定
func (d *Device) Bind(req ReqBind) (err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriBind, accessToken)
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
var result resBind
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.BaseResp.ErrCode != 0 {
err = fmt.Errorf("DeviceBind Error , errcode=%d , errmsg=%s", result.BaseResp.ErrCode, result.BaseResp.ErrMsg)
return
}
return
}
// Unbind 设备解绑
func (d *Device) Unbind(req ReqBind) (err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriUnbind, accessToken)
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
var result resBind
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.BaseResp.ErrCode != 0 {
err = fmt.Errorf("DeviceBind Error , errcode=%d , errmsg=%s", result.BaseResp.ErrCode, result.BaseResp.ErrMsg)
return
}
return
}
// CompelBind 强制绑定用户和设备
func (d *Device) CompelBind(req ReqBind) (err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriCompelBind, accessToken)
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
var result resBind
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.BaseResp.ErrCode != 0 {
err = fmt.Errorf("DeviceBind Error , errcode=%d , errmsg=%s", result.BaseResp.ErrCode, result.BaseResp.ErrMsg)
return
}
return
}
// CompelUnbind 强制解绑用户和设备
func (d *Device) CompelUnbind(req ReqBind) (err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriCompelUnbind, accessToken)
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
var result resBind
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.BaseResp.ErrCode != 0 {
err = fmt.Errorf("DeviceBind Error , errcode=%d , errmsg=%s", result.BaseResp.ErrCode, result.BaseResp.ErrMsg)
return
}
return
}

60
device/device.go Normal file
View File

@@ -0,0 +1,60 @@
package device
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/util"
)
const (
uriAuthorize = "https://api.weixin.qq.com/device/authorize_device"
uriQRCode = "https://api.weixin.qq.com/device/create_qrcode"
uriVerifyQRCode = "https://api.weixin.qq.com/device/verify_qrcode"
uriBind = "https://api.weixin.qq.com/device/bind"
uriUnbind = "https://api.weixin.qq.com/device/unbind"
uriCompelBind = "https://api.weixin.qq.com/device/compel_bind"
uriCompelUnbind = "https://api.weixin.qq.com/device/compel_unbind"
uriState = "https://api.weixin.qq.com/device/get_stat"
)
//Device struct
type Device struct {
*context.Context
}
//NewDevice 实例
func NewDevice(context *context.Context) *Device {
device := new(Device)
device.Context = context
return device
}
// ResDeviceState 设备状态响应实体
type ResDeviceState struct {
util.CommonError
Status int `json:"status"`
StatusInfo string `json:"status_info"`
}
// State 设备状态查询
func (d *Device) State(device string) (res ResDeviceState, err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s&device_id=%s", uriState, accessToken, device)
var response []byte
if response, err = util.HTTPGet(uri); err != nil {
return
}
if err = json.Unmarshal(response, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = fmt.Errorf("DeviceState Error , errcode=%d , errmsg=%s", res.ErrCode, res.ErrMsg)
return
}
return
}

9
device/message.go Normal file
View File

@@ -0,0 +1,9 @@
package device
//MsgDevice 设备消息响应
type MsgDevice struct {
DeviceType string
DeviceID string
SessionID string
OpenID string
}

76
device/qrcode.go Normal file
View File

@@ -0,0 +1,76 @@
package device
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/util"
)
//ResCreateQRCode 获取二维码的返回实体
type ResCreateQRCode struct {
util.CommonError
DeviceNum int `json:"device_num"`
CodeList []struct {
DeviceID string `json:"device_id"`
Ticket string `json:"ticket"`
} `json:"code_list"`
}
// CreateQRCode 获取设备二维码
func (d *Device) CreateQRCode(devices []string) (res ResCreateQRCode, err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriQRCode, accessToken)
req := map[string]interface{}{
"device_num": len(devices),
"device_id_list": devices,
}
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
if err = json.Unmarshal(response, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = fmt.Errorf("DeviceCreateQRCode Error , errcode=%d , errmsg=%s", res.ErrCode, res.ErrMsg)
return
}
return
}
//ResVerifyQRCode 验证授权结果实体
type ResVerifyQRCode struct {
util.CommonError
DeviceType string `json:"device_type"`
DeviceID string `json:"device_id"`
Mac string `json:"mac"`
}
// VerifyQRCode 验证设备二维码
func (d *Device) VerifyQRCode(ticket string) (res ResVerifyQRCode, err error) {
var accessToken string
if accessToken, err = d.GetAccessToken(); err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", uriVerifyQRCode, accessToken)
req := map[string]interface{}{
"ticket": ticket,
}
fmt.Println(req)
var response []byte
if response, err = util.PostJSON(uri, req); err != nil {
return
}
if err = json.Unmarshal(response, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = fmt.Errorf("DeviceCreateQRCode Error , errcode=%d , errmsg=%s", res.ErrCode, res.ErrMsg)
return
}
return
}

21
go.mod Normal file
View File

@@ -0,0 +1,21 @@
module github.com/silenceper/wechat
go 1.13
require (
github.com/astaxie/beego v1.7.1
github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a
github.com/gin-gonic/gin v1.1.4
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b // indirect
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible
github.com/kr/pretty v0.1.0
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c // indirect
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
)

44
go.sum Normal file
View File

@@ -0,0 +1,44 @@
github.com/astaxie/beego v1.7.1 h1:TuqX4F9e3ujVEycudgWrwUj11WMppLZyunJKIBoxTFw=
github.com/astaxie/beego v1.7.1/go.mod h1:0R4++1tUqERR0WYFWdfkcrsyoVBCG4DgpDGokT3yb+U=
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-gonic/gin v1.1.4 h1:XLaCFbU39SSGRQrEeP7Z7mM3lvRqC4vE5tEaVdLDdSE=
github.com/gin-gonic/gin v1.1.4/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b h1:fE/yi9pibxGEc0gSJuEShcsBXE2d5FW3OudsjE9tKzQ=
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible h1:QJ4V3LjaRe/6NKoaaj2QzQZcezt5gNXdPv0axxS4VNA=
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE=
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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
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/net v0.0.0-20191125084936-ffdde1057850/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
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/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@@ -13,6 +13,7 @@ const (
addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material"
delMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/del_material"
getMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/get_material"
)
//Material 素材管理
@@ -31,11 +32,39 @@ func NewMaterial(context *context.Context) *Material {
type Article struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
ThumbURL string `json:"thumb_url"`
Author string `json:"author"`
Digest string `json:"digest"`
ShowCoverPic int `json:"show_cover_pic"`
Content string `json:"content"`
ContentSourceURL string `json:"content_source_url"`
URL string `json:"url"`
DownURL string `json:"down_url"`
}
// GetNews 获取/下载永久素材
func (material *Material) GetNews(id string) ([]*Article, error) {
accessToken, err := material.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", getMaterialURL, accessToken)
var req struct {
MediaID string `json:"media_id"`
}
req.MediaID = id
responseBytes, err := util.PostJSON(uri, req)
var res struct {
NewsItem []*Article `json:"news_item"`
}
err = json.Unmarshal(responseBytes, &res)
if err != nil {
return nil, err
}
return res.NewsItem, nil
}
//reqArticles 永久性图文素材请求信息
@@ -138,11 +167,11 @@ func (material *Material) AddVideo(filename, title, introduction string) (mediaI
fields := []util.MultipartFormField{
{
IsFile: true,
Fieldname: "video",
Fieldname: "media",
Filename: filename,
},
{
IsFile: true,
IsFile: false,
Fieldname: "description",
Value: fieldValue,
},

160
message/customer_message.go Normal file
View File

@@ -0,0 +1,160 @@
package message
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/util"
)
const (
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
)
//Manager 消息管理者,可以发送消息
type Manager struct {
*context.Context
}
//NewMessageManager 实例化消息管理者
func NewMessageManager(context *context.Context) *Manager {
return &Manager{
context,
}
}
//CustomerMessage 客服消息
type CustomerMessage struct {
ToUser string `json:"touser"` //接受者OpenID
Msgtype MsgType `json:"msgtype"` //客服消息类型
Text *MediaText `json:"text,omitempty"` //可选
Image *MediaResource `json:"image,omitempty"` //可选
Voice *MediaResource `json:"voice,omitempty"` //可选
Video *MediaVideo `json:"video,omitempty"` //可选
Music *MediaMusic `json:"music,omitempty"` //可选
News *MediaNews `json:"news,omitempty"` //可选
Mpnews *MediaResource `json:"mpnews,omitempty"` //可选
Wxcard *MediaWxcard `json:"wxcard,omitempty"` //可选
Msgmenu *MediaMsgmenu `json:"msgmenu,omitempty"` //可选
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
}
//NewCustomerTextMessage 文本消息结构体构造方法
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeText,
Text: &MediaText{
text,
},
}
}
//NewCustomerImgMessage 图片消息的构造方法
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeImage,
Image: &MediaResource{
mediaID,
},
}
}
//NewCustomerVoiceMessage 语音消息的构造方法
func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeVoice,
Voice: &MediaResource{
mediaID,
},
}
}
//MediaText 文本消息的文字
type MediaText struct {
Content string `json:"content"`
}
//MediaResource 消息使用的永久素材id
type MediaResource struct {
MediaID string `json:"media_id"`
}
//MediaVideo 视频消息包含的内容
type MediaVideo struct {
MediaID string `json:"media_id"`
ThumbMediaID string `json:"thumb_media_id"`
Title string `json:"title"`
Description string `json:"description"`
}
//MediaMusic 音乐消息包括的内容
type MediaMusic struct {
Title string `json:"title"`
Description string `json:"description"`
Musicurl string `json:"musicurl"`
Hqmusicurl string `json:"hqmusicurl"`
ThumbMediaID string `json:"thumb_media_id"`
}
//MediaNews 图文消息的内容
type MediaNews struct {
Articles []MediaArticles `json:"articles"`
}
//MediaArticles 图文消息的内容的文章列表中的单独一条
type MediaArticles struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Picurl string `json:"picurl"`
}
//MediaMsgmenu 菜单消息的内容
type MediaMsgmenu struct {
HeadContent string `json:"head_content"`
List []MsgmenuItem `json:"list"`
TailContent string `json:"tail_content"`
}
//MsgmenuItem 菜单消息的菜单按钮
type MsgmenuItem struct {
ID string `json:"id"`
Content string `json:"content"`
}
//MediaWxcard 卡券的id
type MediaWxcard struct {
CardID string `json:"card_id"`
}
//MediaMiniprogrampage 小程序消息
type MediaMiniprogrampage struct {
Title string `json:"title"`
Appid string `json:"appid"`
Pagepath string `json:"pagepath"`
ThumbMediaID string `json:"thumb_media_id"`
}
//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)
var result util.CommonError
err = json.Unmarshal(response, &result)
if err != nil {
return err
}
if result.ErrCode != 0 {
err = fmt.Errorf("customer msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
return err
}
return nil
}

View File

@@ -1,6 +1,10 @@
package message
import "encoding/xml"
import (
"encoding/xml"
"github.com/silenceper/wechat/device"
)
// MsgType 基本消息类型
type MsgType string
@@ -63,6 +67,8 @@ const (
EventLocationSelect = "location_select"
//EventTemplateSendJobFinish 发送模板消息推送通知
EventTemplateSendJobFinish = "TEMPLATESENDJOBFINISH"
//EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
EventWxaMediaCheck = "wxa_media_check"
)
const (
@@ -144,6 +150,15 @@ type MixMessage struct {
OuterStr string `xml:"OuterStr"`
IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
UnionID string `xml:"UnionId"`
// 内容审核相关
IsRisky bool `xml:"isrisky"`
ExtraInfoJSON string `xml:"extra_info_json"`
TraceID string `xml:"trace_id"`
StatusCode int `xml:"status_code"`
//设备相关
device.MsgDevice
}
//EventPic 发图事件推送
@@ -167,22 +182,32 @@ type ResponseEncryptedXMLMsg struct {
Nonce string `xml:"Nonce" json:"Nonce"`
}
// CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
type CDATA string
// MarshalXML 实现自己的序列化方法
func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(struct {
string `xml:",cdata"`
}{string(c)}, start)
}
// CommonToken 消息中通用的结构
type CommonToken struct {
XMLName xml.Name `xml:"xml"`
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
ToUserName CDATA `xml:"ToUserName"`
FromUserName CDATA `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType MsgType `xml:"MsgType"`
}
//SetToUserName set ToUserName
func (msg *CommonToken) SetToUserName(toUserName string) {
func (msg *CommonToken) SetToUserName(toUserName CDATA) {
msg.ToUserName = toUserName
}
//SetFromUserName set FromUserName
func (msg *CommonToken) SetFromUserName(fromUserName string) {
func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
msg.FromUserName = fromUserName
}

View File

@@ -1,4 +1,4 @@
package template
package message
import (
"encoding/json"

View File

@@ -3,12 +3,12 @@ package message
//Text 文本消息
type Text struct {
CommonToken
Content string `xml:"Content"`
Content CDATA `xml:"Content"`
}
//NewText 初始化文本消息
func NewText(content string) *Text {
text := new(Text)
text.Content = content
text.Content = CDATA(content)
return text
}

View File

@@ -36,6 +36,17 @@ type UserInfo struct {
} `json:"watermark"`
}
// PhoneInfo 用户手机号
type PhoneInfo struct {
PhoneNumber string `json:"phoneNumber"`
PurePhoneNumber string `json:"purePhoneNumber"`
CountryCode string `json:"countryCode"`
Watermark struct {
Timestamp int64 `json:"timestamp"`
AppID string `json:"appid"`
} `json:"watermark"`
}
// pkcs7Unpad returns slice of the original data without padding
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 {
@@ -57,8 +68,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
return data[:len(data)-n], nil
}
// Decrypt 解密数据
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
// getCipherText returns slice of the cipher text
func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
if err != nil {
return nil, err
@@ -81,6 +92,15 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo
if err != nil {
return nil, err
}
return cipherText, nil
}
// Decrypt 解密数据
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
if err != nil {
return nil, err
}
var userInfo UserInfo
err = json.Unmarshal(cipherText, &userInfo)
if err != nil {
@@ -91,3 +111,20 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo
}
return &userInfo, 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
}

87
pay/notify_result.go Normal file
View File

@@ -0,0 +1,87 @@
package pay
import (
"fmt"
"github.com/silenceper/wechat/util"
"sort"
)
// Base 公用参数
type Base struct {
AppID string `xml:"appid"`
MchID string `xml:"mch_id"`
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
}
// NotifyResult 下单回调
type NotifyResult struct {
Base
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
ResultCode string `xml:"result_code"`
OpenID string `xml:"openid"`
IsSubscribe string `xml:"is_subscribe"`
TradeType string `xml:"trade_type"`
BankType string `xml:"bank_type"`
TotalFee int `xml:"total_fee"`
FeeType string `xml:"fee_type"`
CashFee int `xml:"cash_fee"`
CashFeeType string `xml:"cash_fee_type"`
TransactionID string `xml:"transaction_id"`
OutTradeNo string `xml:"out_trade_no"`
Attach string `xml:"attach"`
TimeEnd string `xml:"time_end"`
}
// NotifyResp 消息通知返回
type NotifyResp struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
}
// VerifySign 验签
func (pcf *Pay) VerifySign(notifyRes NotifyResult) bool {
// 封装map 请求过来的 map
resMap := make(map[string]interface{})
// base
resMap["appid"] = notifyRes.AppID
resMap["mch_id"] = notifyRes.MchID
resMap["nonce_str"] = notifyRes.NonceStr
// NotifyResult
resMap["return_code"] = notifyRes.ReturnCode
resMap["result_code"] = notifyRes.ResultCode
resMap["openid"] = notifyRes.OpenID
resMap["is_subscribe"] = notifyRes.IsSubscribe
resMap["trade_type"] = notifyRes.TradeType
resMap["bank_type"] = notifyRes.BankType
resMap["total_fee"] = notifyRes.TotalFee
resMap["fee_type"] = notifyRes.FeeType
resMap["cash_fee"] = notifyRes.CashFee
resMap["transaction_id"] = notifyRes.TransactionID
resMap["out_trade_no"] = notifyRes.OutTradeNo
resMap["attach"] = notifyRes.Attach
resMap["time_end"] = notifyRes.TimeEnd
// 支付key
sortedKeys := make([]string, 0, len(resMap))
for k := range resMap {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// STEP2, 对key=value的键值对用&连接起来,略过空值
var signStrings string
for _, k := range sortedKeys {
value := fmt.Sprintf("%v", resMap[k])
if value != "" {
signStrings = signStrings + k + "=" + value + "&"
}
}
// STEP3, 在键值对的最后加上key=API_KEY
signStrings = signStrings + "key=" + pcf.PayKey
// STEP4, 进行MD5签名并且将所有字符转为大写.
sign := util.MD5Sum(signStrings)
if sign != notifyRes.Sign {
return false
}
return true
}

View File

@@ -2,10 +2,17 @@ package pay
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"encoding/xml"
"errors"
"hash"
"sort"
"strconv"
"strings"
"time"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/util"
@@ -27,15 +34,21 @@ type Params struct {
OutTradeNo string
OpenID string
TradeType string
SignType string
Detail string
Attach string
GoodsTag string
NotifyURL string
}
// Config 是传出用于 jsdk 用的参数
// Config 是传出用于 js sdk 用的参数
type Config struct {
Timestamp int64
NonceStr string
PrePayID string
SignType string
Sign string
Timestamp string `json:"timestamp"`
NonceStr string `json:"nonceStr"`
PrePayID string `json:"prePayId"`
SignType string `json:"signType"`
Package string `json:"package"`
PaySign string `json:"paySign"`
}
// PreOrder 是 unifie order 接口的返回
@@ -54,7 +67,7 @@ type PreOrder struct {
ErrCodeDes string `xml:"err_code_des,omitempty"`
}
//payRequest 接口请求参数
// payRequest 接口请求参数
type payRequest struct {
AppID string `xml:"appid"`
MchID string `xml:"mch_id"`
@@ -64,20 +77,20 @@ type payRequest struct {
SignType string `xml:"sign_type,omitempty"`
Body string `xml:"body"`
Detail string `xml:"detail,omitempty"`
Attach string `xml:"attach,omitempty"` //附加数据
OutTradeNo string `xml:"out_trade_no"` //商户订单号
FeeType string `xml:"fee_type,omitempty"` //标价币种
TotalFee string `xml:"total_fee"` //标价金额
SpbillCreateIP string `xml:"spbill_create_ip"` //终端IP
TimeStart string `xml:"time_start,omitempty"` //交易起始时间
TimeExpire string `xml:"time_expire,omitempty"` //交易结束时间
GoodsTag string `xml:"goods_tag,omitempty"` //订单优惠标记
NotifyURL string `xml:"notify_url"` //通知地址
TradeType string `xml:"trade_type"` //交易类型
ProductID string `xml:"product_id,omitempty"` //商品ID
Attach string `xml:"attach,omitempty"` // 附加数据
OutTradeNo string `xml:"out_trade_no"` // 商户订单号
FeeType string `xml:"fee_type,omitempty"` // 标价币种
TotalFee string `xml:"total_fee"` // 标价金额
SpbillCreateIP string `xml:"spbill_create_ip"` // 终端IP
TimeStart string `xml:"time_start,omitempty"` // 交易起始时间
TimeExpire string `xml:"time_expire,omitempty"` // 交易结束时间
GoodsTag string `xml:"goods_tag,omitempty"` // 订单优惠标记
NotifyURL string `xml:"notify_url"` // 通知地址
TradeType string `xml:"trade_type"` // 交易类型
ProductID string `xml:"product_id,omitempty"` // 商品ID
LimitPay string `xml:"limit_pay,omitempty"` //
OpenID string `xml:"openid,omitempty"` //用户标识
SceneInfo string `xml:"scene_info,omitempty"` //场景信息
OpenID string `xml:"openid,omitempty"` // 用户标识
SceneInfo string `xml:"scene_info,omitempty"` // 场景信息
}
// NewPay return an instance of Pay package
@@ -86,20 +99,72 @@ func NewPay(ctx *context.Context) *Pay {
return &pay
}
// BridgeConfig get js bridge config
func (pcf *Pay) BridgeConfig(p *Params) (cfg Config, err error) {
var (
buffer strings.Builder
h hash.Hash
timestamp = strconv.FormatInt(time.Now().Unix(), 10)
)
order, err := pcf.PrePayOrder(p)
if err != nil {
return
}
buffer.WriteString("appId=")
buffer.WriteString(order.AppID)
buffer.WriteString("&nonceStr=")
buffer.WriteString(order.NonceStr)
buffer.WriteString("&package=")
buffer.WriteString("prepay_id=" + order.PrePayID)
buffer.WriteString("&signType=")
buffer.WriteString(p.SignType)
buffer.WriteString("&timeStamp=")
buffer.WriteString(timestamp)
buffer.WriteString("&key=")
buffer.WriteString(pcf.PayKey)
if p.SignType == "MD5" {
h = md5.New()
} else {
h = hmac.New(sha256.New, []byte(pcf.PayKey))
}
h.Write([]byte(buffer.String()))
// 签名
cfg.PaySign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
cfg.NonceStr = order.NonceStr
cfg.Timestamp = timestamp
cfg.PrePayID = order.PrePayID
cfg.SignType = p.SignType
cfg.Package = "prepay_id=" + order.PrePayID
return
}
// PrePayOrder return data for invoke wechat payment
func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
nonceStr := util.RandomStr(32)
notifyURL := pcf.PayNotifyURL
// 签名类型
if p.SignType == "" {
p.SignType = "MD5"
}
// 通知地址
if p.NotifyURL != "" {
notifyURL = p.NotifyURL
}
param := make(map[string]interface{})
param["appid"] = pcf.AppID
param["body"] = p.Body
param["mch_id"] = pcf.PayMchID
param["nonce_str"] = nonceStr
param["notify_url"] = pcf.PayNotifyURL
param["out_trade_no"] = p.OutTradeNo
param["spbill_create_ip"] = p.CreateIP
param["total_fee"] = p.TotalFee
param["trade_type"] = p.TradeType
param["openid"] = p.OpenID
param["sign_type"] = p.SignType
param["detail"] = p.Detail
param["attach"] = p.Attach
param["goods_tag"] = p.GoodsTag
param["notify_url"] = notifyURL
bizKey := "&key=" + pcf.PayKey
str := orderParam(param, bizKey)
@@ -113,9 +178,13 @@ func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
OutTradeNo: p.OutTradeNo,
TotalFee: p.TotalFee,
SpbillCreateIP: p.CreateIP,
NotifyURL: pcf.PayNotifyURL,
NotifyURL: notifyURL,
TradeType: p.TradeType,
OpenID: p.OpenID,
SignType: p.SignType,
Detail: p.Detail,
Attach: p.Attach,
GoodsTag: p.GoodsTag,
}
rawRet, err := util.PostXML(payGateway, request)
if err != nil {
@@ -126,7 +195,7 @@ func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
return
}
if payOrder.ReturnCode == "SUCCESS" {
//pay success
// pay success
if payOrder.ResultCode == "SUCCESS" {
err = nil
return

View File

@@ -65,7 +65,9 @@ func (srv *Server) Serve() error {
}
//debug
//fmt.Println("request msg = ", string(srv.requestRawXMLMsg))
if srv.debug {
fmt.Println("request msg = ", string(srv.requestRawXMLMsg))
}
return srv.buildResponse(response)
}

32
tcb/README.md Normal file
View File

@@ -0,0 +1,32 @@
# 小程序-云开发 SDK
Tencent Cloud Base [文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/)
## 使用说明
**初始化配置**
```golang
//使用memcache保存access_token也可选择redis或自定义cache
memCache=cache.NewMemcache("127.0.0.1:11211")
//配置小程序参数
config := &wechat.Config{
AppID: "your app id",
AppSecret: "your app secret",
Cache: memCache,
}
wc := wechat.NewWechat(config)
wcTcb := wc.GetTcb()
```
### 举例
#### 触发云函数
```golang
res, err := wcTcb.InvokeCloudFunction("test-xxxx", "add", `{"a":1,"b":2}`)
if err != nil {
panic(err)
}
```
更多使用方法参考[GODOC](https://godoc.org/github.com/silenceper/wechat/tcb)

35
tcb/cloudfunction.go Normal file
View File

@@ -0,0 +1,35 @@
package tcb
import (
"fmt"
"github.com/silenceper/wechat/util"
)
const (
//触发云函数
invokeCloudFunctionURL = "https://api.weixin.qq.com/tcb/invokecloudfunction"
)
//InvokeCloudFunctionRes 云函数调用返回结果
type InvokeCloudFunctionRes struct {
util.CommonError
RespData string `json:"resp_data"` //云函数返回的buffer
}
//InvokeCloudFunction 云函数调用
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html
func (tcb *Tcb) InvokeCloudFunction(env, name, args string) (*InvokeCloudFunctionRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s&env=%s&name=%s", invokeCloudFunctionURL, accessToken, env, name)
response, err := util.HTTPPost(uri, args)
if err != nil {
return nil, err
}
invokeCloudFunctionRes := &InvokeCloudFunctionRes{}
err = util.DecodeWithError(response, invokeCloudFunctionRes, "InvokeCloudFunction")
return invokeCloudFunctionRes, err
}

418
tcb/database.go Normal file
View File

@@ -0,0 +1,418 @@
package tcb
import (
"fmt"
"github.com/silenceper/wechat/util"
)
const (
//数据库导入
databaseMigrateImportURL = "https://api.weixin.qq.com/tcb/databasemigrateimport"
//数据库导出
databaseMigrateExportURL = "https://api.weixin.qq.com/tcb/databasemigrateexport"
//数据库迁移状态查询
databaseMigrateQueryInfoURL = "https://api.weixin.qq.com/tcb/databasemigratequeryinfo"
//变更数据库索引
updateIndexURL = "https://api.weixin.qq.com/tcb/updateindex"
//新增集合
databaseCollectionAddURL = "https://api.weixin.qq.com/tcb/databasecollectionadd"
//删除集合
databaseCollectionDeleteURL = "https://api.weixin.qq.com/tcb/databasecollectiondelete"
//获取特定云环境下集合信息
databaseCollectionGetURL = "https://api.weixin.qq.com/tcb/databasecollectionget"
//数据库插入记录
databaseAddURL = "https://api.weixin.qq.com/tcb/databaseadd"
//数据库删除记录
databaseDeleteURL = "https://api.weixin.qq.com/tcb/databasedelete"
//数据库更新记录
databaseUpdateURL = "https://api.weixin.qq.com/tcb/databaseupdate"
//数据库查询记录
databaseQueryURL = "https://api.weixin.qq.com/tcb/databasequery"
//统计集合记录数或统计查询语句对应的结果记录数
databaseCountURL = "https://api.weixin.qq.com/tcb/databasecount"
//ConflictModeInster 冲突处理模式 插入
ConflictModeInster ConflictMode = 1
//ConflictModeUpsert 冲突处理模式 更新
ConflictModeUpsert ConflictMode = 2
//FileTypeJSON 的合法值 json
FileTypeJSON FileType = 1
//FileTypeCsv 的合法值 csv
FileTypeCsv FileType = 2
)
//ConflictMode 冲突处理模式
type ConflictMode int
//FileType 文件上传和导出的允许文件类型
type FileType int
//ValidDirections 合法的direction值
var ValidDirections = []string{"1", "-1", "2dsphere"}
//DatabaseMigrateExportReq 数据库出 请求参数
type DatabaseMigrateExportReq struct {
Env string `json:"env,omitempty"` //云环境ID
FilePath string `json:"file_path,omitempty"` //导出文件路径(导入文件需先上传到同环境的存储中,可使用开发者工具或 HTTP API的上传文件 API上传
FileType FileType `json:"file_type,omitempty"` //导出文件类型,文件格式参考数据库导入指引中的文件格式部分 1:json 2:csv
Query string `json:"query,omitempty"` //导出条件
}
//DatabaseMigrateExportRes 数据库导出 返回结果
type DatabaseMigrateExportRes struct {
util.CommonError
JobID int64 `json:"job_id"` //导出任务ID可使用数据库迁移进度查询 API 查询导入进度及结果
}
//DatabaseMigrateImportReq 数据库导入 请求参数
type DatabaseMigrateImportReq struct {
Env string `json:"env,omitempty"` //云环境ID
CollectionName string `json:"collection_name,omitempty"` //集合名称
FilePath string `json:"file_path,omitempty"` //导出文件路径(文件会导出到同环境的云存储中,可使用获取下载链接 API 获取下载链接)
FileType FileType `json:"file_type,omitempty"` //导入文件类型,文件格式参考数据库导入指引中的文件格式部分 1:json 2:csv
StopOnError bool `json:"stop_on_error,omitempty"` //是否在遇到错误时停止导入
ConflictMode ConflictMode `json:"conflict_mode,omitempty"` //冲突处理模式 1:inster 2:UPSERT
}
//DatabaseMigrateImportRes 数据库导入 返回结果
type DatabaseMigrateImportRes struct {
util.CommonError
JobID int64 `json:"job_id"` //导入任务ID可使用数据库迁移进度查询 API 查询导入进度及结果
}
//DatabaseMigrateQueryInfoRes 数据库迁移状态查询
type DatabaseMigrateQueryInfoRes struct {
util.CommonError
Status string `json:"status"` //导出状态
RecordSuccess int64 `json:"record_success"` //导出成功记录数
RecordFail int64 `json:"record_fail"` //导出失败记录数
ErrMsg string `json:"err_msg"` //导出错误信息
FileURL string `json:"file_url"` //导出文件下载地址
}
//UpdateIndexReq 变更数据库索引 请求参数
type UpdateIndexReq struct {
Env string `json:"env,omitempty"` //云环境ID
CollectionName string `json:"collection_name,omitempty"` //集合名称
CreateIndexes []CreateIndex `json:"create_indexes,omitempty"` //新增索引
DropIndexes []DropIndex `json:"drop_indexes,omitempty"` //删除索引
}
//CreateIndex 新增索引
type CreateIndex struct {
Name string `json:"name,omitempty"` //索引名
Unique bool `json:"unique,omitempty"` //是否唯一
Keys []CreateIndexKey `json:"keys,omitempty"` //索引字段
}
//CreateIndexKey create index key
type CreateIndexKey struct {
Name string `json:"name,omitempty"` //字段名
Direction string `json:"direction,omitempty"` //字段排序
}
//DropIndex 删除索引
type DropIndex struct {
Name string `json:"name,omitempty"`
}
//DatabaseCollectionReq 新增/删除集合请求参数
type DatabaseCollectionReq struct {
Env string `json:"env,omitempty"` //云环境ID
CollectionName string `json:"collection_name,omitempty"` //集合名称
}
//DatabaseCollectionGetReq 获取特定云环境下集合信息请求
type DatabaseCollectionGetReq struct {
Env string `json:"env,omitempty"` //云环境ID
Limit int64 `json:"limit,omitempty"` //获取数量限制
Offset int64 `json:"offset,omitempty"` //偏移量
}
//DatabaseCollectionGetRes 获取特定云环境下集合信息结果
type DatabaseCollectionGetRes struct {
util.CommonError
Pager struct {
Limit int64 `json:"limit"` //单次查询限制
Offset int64 `json:"offset"` //偏移量
Total int64 `json:"total"` //符合查询条件的记录总数
} `json:"pager"`
Collections []struct {
Name string `json:"name"` //集合名
Count int64 `json:"count"` //表中文档数量
Size int64 `json:"size"` //表的大小(即表中文档总大小),单位:字节
IndexCount int64 `json:"index_count"` //索引数量
IndexSize int64 `json:"index_size"` //索引占用大小,单位:字节
} `json:"collections"`
}
//DatabaseReq 数据库插入/删除/更新/查询/统计记录请求参数
type DatabaseReq struct {
Env string `json:"env,omitempty"` //云环境ID
Query string `json:"query,omitempty"` //数据库操作语句
}
//DatabaseAddRes 数据库插入记录返回结果
type DatabaseAddRes struct {
util.CommonError
IDList []string `json:"id_list"` //插入成功的数据集合主键_id。
}
//DatabaseDeleteRes 数据库删除记录返回结果
type DatabaseDeleteRes struct {
util.CommonError
Deleted int64 `json:"deleted"` //删除记录数量
}
//DatabaseUpdateRes 数据库更新记录返回结果
type DatabaseUpdateRes struct {
util.CommonError
Matched int64 `json:"matched"` //更新条件匹配到的结果数
Modified int64 `json:"modified"` //修改的记录数注意使用set操作新插入的数据不计入修改数目
ID string `json:"id"`
}
//DatabaseQueryRes 数据库查询记录 返回结果
type DatabaseQueryRes struct {
util.CommonError
Pager struct {
Limit int64 `json:"limit"` //单次查询限制
Offset int64 `json:"offset"` //偏移量
Total int64 `json:"total"` //符合查询条件的记录总数
} `json:"pager"`
Data []string `json:"data"`
}
//DatabaseCountRes 统计集合记录数或统计查询语句对应的结果记录数 返回结果
type DatabaseCountRes struct {
util.CommonError
Count int64 `json:"count"` //记录数量
}
//DatabaseMigrateImport 数据库导入
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateImport.html
func (tcb *Tcb) DatabaseMigrateImport(req *DatabaseMigrateImportReq) (*DatabaseMigrateImportRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateImportURL, accessToken)
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
databaseMigrateImportRes := &DatabaseMigrateImportRes{}
err = util.DecodeWithError(response, databaseMigrateImportRes, "DatabaseMigrateImport")
return databaseMigrateImportRes, err
}
//DatabaseMigrateExport 数据库导出
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateExport.html
func (tcb *Tcb) DatabaseMigrateExport(req *DatabaseMigrateExportReq) (*DatabaseMigrateExportRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateExportURL, accessToken)
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
databaseMigrateExportRes := &DatabaseMigrateExportRes{}
err = util.DecodeWithError(response, databaseMigrateExportRes, "DatabaseMigrateExport")
return databaseMigrateExportRes, err
}
//DatabaseMigrateQueryInfo 数据库迁移状态查询
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateQueryInfo.html
func (tcb *Tcb) DatabaseMigrateQueryInfo(env string, jobID int64) (*DatabaseMigrateQueryInfoRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateQueryInfoURL, accessToken)
response, err := util.PostJSON(uri, map[string]interface{}{
"env": env,
"job_id": jobID,
})
if err != nil {
return nil, err
}
databaseMigrateQueryInfoRes := &DatabaseMigrateQueryInfoRes{}
err = util.DecodeWithError(response, databaseMigrateQueryInfoRes, "DatabaseMigrateQueryInfo")
return databaseMigrateQueryInfoRes, err
}
//UpdateIndex 变更数据库索引
//https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/updateIndex.html
func (tcb *Tcb) UpdateIndex(req *UpdateIndexReq) error {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", updateIndexURL, accessToken)
response, err := util.PostJSON(uri, req)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "UpdateIndex")
}
//DatabaseCollectionAdd 新增集合
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionAdd.html
func (tcb *Tcb) DatabaseCollectionAdd(env, collectionName string) error {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionAddURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseCollectionReq{
Env: env,
CollectionName: collectionName,
})
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "DatabaseCollectionAdd")
}
//DatabaseCollectionDelete 删除集合
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionDelete.html
func (tcb *Tcb) DatabaseCollectionDelete(env, collectionName string) error {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionDeleteURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseCollectionReq{
Env: env,
CollectionName: collectionName,
})
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "DatabaseCollectionDelete")
}
//DatabaseCollectionGet 获取特定云环境下集合信息
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionGet.html
func (tcb *Tcb) DatabaseCollectionGet(env string, limit, offset int64) (*DatabaseCollectionGetRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionGetURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseCollectionGetReq{
Env: env,
Limit: limit,
Offset: offset,
})
if err != nil {
return nil, err
}
databaseCollectionGetRes := &DatabaseCollectionGetRes{}
err = util.DecodeWithError(response, databaseCollectionGetRes, "DatabaseCollectionGet")
return databaseCollectionGetRes, err
}
//DatabaseAdd 数据库插入记录
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseAdd.html
func (tcb *Tcb) DatabaseAdd(env, query string) (*DatabaseAddRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseAddURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseReq{
Env: env,
Query: query,
})
if err != nil {
return nil, err
}
databaseAddRes := &DatabaseAddRes{}
err = util.DecodeWithError(response, databaseAddRes, "DatabaseAdd")
return databaseAddRes, err
}
//DatabaseDelete 数据库插入记录
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseDelete.html
func (tcb *Tcb) DatabaseDelete(env, query string) (*DatabaseDeleteRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseDeleteURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseReq{
Env: env,
Query: query,
})
if err != nil {
return nil, err
}
databaseDeleteRes := &DatabaseDeleteRes{}
err = util.DecodeWithError(response, databaseDeleteRes, "DatabaseDelete")
return databaseDeleteRes, err
}
//DatabaseUpdate 数据库插入记录
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseUpdate.html
func (tcb *Tcb) DatabaseUpdate(env, query string) (*DatabaseUpdateRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseUpdateURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseReq{
Env: env,
Query: query,
})
if err != nil {
return nil, err
}
databaseUpdateRes := &DatabaseUpdateRes{}
err = util.DecodeWithError(response, databaseUpdateRes, "DatabaseUpdate")
return databaseUpdateRes, err
}
//DatabaseQuery 数据库查询记录
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseQuery.html
func (tcb *Tcb) DatabaseQuery(env, query string) (*DatabaseQueryRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseQueryURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseReq{
Env: env,
Query: query,
})
if err != nil {
return nil, err
}
databaseQueryRes := &DatabaseQueryRes{}
err = util.DecodeWithError(response, databaseQueryRes, "DatabaseQuery")
return databaseQueryRes, err
}
//DatabaseCount 统计集合记录数或统计查询语句对应的结果记录数
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCount.html
func (tcb *Tcb) DatabaseCount(env, query string) (*DatabaseCountRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", databaseCountURL, accessToken)
response, err := util.PostJSON(uri, &DatabaseReq{
Env: env,
Query: query,
})
if err != nil {
return nil, err
}
databaseCountRes := &DatabaseCountRes{}
err = util.DecodeWithError(response, databaseCountRes, "DatabaseCount")
return databaseCountRes, err
}

134
tcb/file.go Normal file
View File

@@ -0,0 +1,134 @@
package tcb
import (
"fmt"
"github.com/silenceper/wechat/util"
)
const (
//获取文件上传链接
uploadFilePathURL = "https://api.weixin.qq.com/tcb/uploadfile"
//获取文件下载链接
batchDownloadFileURL = "https://api.weixin.qq.com/tcb/batchdownloadfile"
//删除文件链接
batchDeleteFileURL = "https://api.weixin.qq.com/tcb/batchdeletefile"
)
//UploadFileReq 上传文件请求值
type UploadFileReq struct {
Env string `json:"env,omitempty"`
Path string `json:"path,omitempty"`
}
//UploadFileRes 上传文件返回结果
type UploadFileRes struct {
util.CommonError
URL string `json:"url"` //上传url
Token string `json:"token"` //token
Authorization string `json:"authorization"` //authorization
FileID string `json:"file_id"` //文件ID
CosFileID string `json:"cos_file_id"` //cos文件ID
}
//BatchDownloadFileReq 上传文件请求值
type BatchDownloadFileReq struct {
Env string `json:"env,omitempty"`
FileList []*DownloadFile `json:"file_list,omitempty"`
}
//DownloadFile 文件信息
type DownloadFile struct {
FileID string `json:"fileid"` //文件ID
MaxAge int64 `json:"max_age"` //下载链接有效期
}
//BatchDownloadFileRes 上传文件返回结果
type BatchDownloadFileRes struct {
util.CommonError
FileList []struct {
FileID string `json:"file_id"` //文件ID
DownloadURL string `json:"download_url"` //下载链接
Status int64 `json:"status"` //状态码
ErrMsg string `json:"errmsg"` //该文件错误信息
} `json:"file_list"`
}
//BatchDeleteFileReq 批量删除文件请求参数
type BatchDeleteFileReq struct {
Env string `json:"env,omitempty"`
FileIDList []string `json:"fileid_list,omitempty"`
}
//BatchDeleteFileRes 批量删除文件返回结果
type BatchDeleteFileRes struct {
util.CommonError
DeleteList []struct {
FileID string `json:"fileid"`
Status int64 `json:"status"`
ErrMsg string `json:"errmsg"`
} `json:"delete_list"`
}
//UploadFile 上传文件
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html
func (tcb *Tcb) UploadFile(env, path string) (*UploadFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", uploadFilePathURL, accessToken)
req := &UploadFileReq{
Env: env,
Path: path,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
uploadFileRes := &UploadFileRes{}
err = util.DecodeWithError(response, uploadFileRes, "UploadFile")
return uploadFileRes, err
}
//BatchDownloadFile 获取文件下载链接
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html
func (tcb *Tcb) BatchDownloadFile(env string, fileList []*DownloadFile) (*BatchDownloadFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", batchDownloadFileURL, accessToken)
req := &BatchDownloadFileReq{
Env: env,
FileList: fileList,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
batchDownloadFileRes := &BatchDownloadFileRes{}
err = util.DecodeWithError(response, batchDownloadFileRes, "BatchDownloadFile")
return batchDownloadFileRes, err
}
//BatchDeleteFile 批量删除文件
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html
func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", batchDeleteFileURL, accessToken)
req := &BatchDeleteFileReq{
Env: env,
FileIDList: fileIDList,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
batchDeleteFileRes := &BatchDeleteFileRes{}
err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
return batchDeleteFileRes, nil
}

16
tcb/tcb.go Normal file
View File

@@ -0,0 +1,16 @@
package tcb
import "github.com/silenceper/wechat/context"
//Tcb Tencent Cloud Base
type Tcb struct{
*context.Context
}
//NewTcb new Tencent Cloud Base
func NewTcb(context *context.Context)*Tcb{
return &Tcb{
context,
}
}

View File

@@ -3,6 +3,7 @@ package util
import (
"encoding/json"
"fmt"
"reflect"
)
// CommonError 微信返回的通用错误json
@@ -23,3 +24,28 @@ func DecodeWithCommonError(response []byte, apiName string) (err error) {
}
return nil
}
// DecodeWithError 将返回值按照解析
func DecodeWithError(response []byte, obj interface{}, apiName string) error {
err := json.Unmarshal(response, obj)
if err != nil {
return fmt.Errorf("json Unmarshal Error, err=%v", err)
}
responseObj := reflect.ValueOf(obj)
if !responseObj.IsValid() {
return fmt.Errorf("obj is invalid")
}
commonError := responseObj.Elem().FieldByName("CommonError")
if !commonError.IsValid() || commonError.Kind() != reflect.Struct {
return fmt.Errorf("commonError is invalid or not struct")
}
errCode := commonError.FieldByName("ErrCode")
errMsg := commonError.FieldByName("ErrMsg")
if !errCode.IsValid() || !errMsg.IsValid() {
return fmt.Errorf("errcode or errmsg is invalid")
}
if errCode.Int() != 0 {
return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, errCode.Int(), errMsg.String())
}
return nil
}

View File

@@ -7,13 +7,14 @@ import (
"encoding/pem"
"encoding/xml"
"fmt"
"golang.org/x/crypto/pkcs12"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"golang.org/x/crypto/pkcs12"
)
//HTTPGet get 请求
@@ -30,17 +31,30 @@ func HTTPGet(uri string) ([]byte, error) {
return ioutil.ReadAll(response.Body)
}
//HTTPPost post 请求
func HTTPPost(uri string, data string) ([]byte, error) {
body := bytes.NewBuffer([]byte(data))
response, err := http.Post(uri, "", body)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
}
return ioutil.ReadAll(response.Body)
}
//PostJSON post json 数据请求
func PostJSON(uri string, obj interface{}) ([]byte, error) {
jsonData, err := json.Marshal(obj)
if err != nil {
return nil, err
}
jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
body := bytes.NewBuffer(jsonData)
response, err := http.Post(uri, "application/json;charset=utf-8", body)
if err != nil {

144
vendor/vendor.json vendored
View File

@@ -1,144 +0,0 @@
{
"comment": "",
"ignore": "test",
"package": [
{
"checksumSHA1": "ZZ4FL7s5f8QK4RysjZObSBYGOLY=",
"path": "github.com/astaxie/beego",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "LwEiQ/Hyb7Ul28TSlwowN9cpWDY=",
"path": "github.com/astaxie/beego/config",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "s+gj1rES9SvvCIyF8W2tzlziSPE=",
"path": "github.com/astaxie/beego/context",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "PDNn16w89zWODshT9zlPzSmWZFA=",
"path": "github.com/astaxie/beego/grace",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "Iz/p1UTvFNe5HFeohX7cvKEOQW0=",
"path": "github.com/astaxie/beego/logs",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "R797q1pCbp086SraUETxX1rsJYw=",
"path": "github.com/astaxie/beego/session",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "rxxln8GHFToVnaEJz4JMv0WbaKc=",
"path": "github.com/astaxie/beego/toolbox",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "fRJk3RZPPz6ovbautfsfxAk+CrI=",
"path": "github.com/astaxie/beego/utils",
"revision": "2d87d4feafeea0a133d217a82e6e02df0348fed5",
"revisionTime": "2016-09-22T15:18:45Z"
},
{
"checksumSHA1": "fNAC4qgZDqF3kxq74/yyk3PWdy8=",
"path": "github.com/bradfitz/gomemcache/memcache",
"revision": "fb1f79c6b65acda83063cbc69f6bba1522558bfc",
"revisionTime": "2016-01-17T19:21:50Z"
},
{
"checksumSHA1": "RsNwOto8G8aXIiRrlFn4dtU9q/g=",
"path": "github.com/gin-gonic/gin",
"revision": "e2212d40c62a98b388a5eb48ecbdcf88534688ba",
"revisionTime": "2016-12-04T22:13:08Z"
},
{
"checksumSHA1": "UsILDoIB2S7ra+w2fMdb85mX3HM=",
"path": "github.com/gin-gonic/gin/binding",
"revision": "e2212d40c62a98b388a5eb48ecbdcf88534688ba",
"revisionTime": "2016-12-04T22:13:08Z"
},
{
"checksumSHA1": "PHv9FNb7YavJWtAHcY6ZgXmkmHs=",
"path": "github.com/gin-gonic/gin/render",
"revision": "e2212d40c62a98b388a5eb48ecbdcf88534688ba",
"revisionTime": "2016-12-04T22:13:08Z"
},
{
"checksumSHA1": "kBeNcaKk56FguvPSUCEaH6AxpRc=",
"path": "github.com/golang/protobuf/proto",
"revision": "8ee79997227bf9b34611aee7946ae64735e6fd93",
"revisionTime": "2016-11-17T03:31:26Z"
},
{
"checksumSHA1": "w3QCCIYHgZzIXQ+xTl7oLfFrXHs=",
"path": "github.com/gomodule/redigo/internal",
"revision": "2cd21d9966bf7ff9ae091419744f0b3fb0fecace",
"revisionTime": "2018-06-27T14:45:07Z"
},
{
"checksumSHA1": "To/N5YA/FD0Rrs6r2OOmHXgxYwI=",
"path": "github.com/gomodule/redigo/redis",
"revision": "2cd21d9966bf7ff9ae091419744f0b3fb0fecace",
"revisionTime": "2018-06-27T14:45:07Z"
},
{
"checksumSHA1": "b0T0Hzd+zYk+OCDTFMps+jwa/nY=",
"path": "github.com/manucorporat/sse",
"revision": "ee05b128a739a0fb76c7ebd3ae4810c1de808d6d",
"revisionTime": "2016-01-26T18:01:36Z"
},
{
"checksumSHA1": "xZuhljnmBysJPta/lMyYmJdujCg=",
"path": "github.com/mattn/go-isatty",
"revision": "30a891c33c7cde7b02a981314b4228ec99380cca",
"revisionTime": "2016-11-23T14:36:37Z"
},
{
"checksumSHA1": "PJY7uCr3UnX4/Mf/RoWnbieSZ8o=",
"path": "golang.org/x/crypto/pkcs12",
"revision": "9f005a07e0d31d45e6656d241bb5c0f2efd4bc94",
"revisionTime": "2017-09-21T17:41:56Z"
},
{
"checksumSHA1": "iVJcif9M9uefvvqHCNR9VQrjc/s=",
"path": "golang.org/x/crypto/pkcs12/internal/rc2",
"revision": "9f005a07e0d31d45e6656d241bb5c0f2efd4bc94",
"revisionTime": "2017-09-21T17:41:56Z"
},
{
"checksumSHA1": "pancewZW3HwGvpDwfH5Imrbadc4=",
"path": "golang.org/x/net/context",
"revision": ""
},
{
"checksumSHA1": "8fD/im5Kwvy3JgmxulDTambmE8w=",
"path": "golang.org/x/sys/unix",
"revision": "a646d33e2ee3172a661fc09bca23bb4889a41bc8",
"revisionTime": "2016-07-15T05:43:45Z"
},
{
"checksumSHA1": "39V1idWER42Lmcmg2Uy40wMzOlo=",
"path": "gopkg.in/go-playground/validator.v8",
"revision": "5f57d2222ad794d0dffb07e664ea05e2ee07d60c",
"revisionTime": "2016-07-18T13:41:25Z"
},
{
"checksumSHA1": "12GqsW8PiRPnezDDy0v4brZrndM=",
"path": "gopkg.in/yaml.v2",
"revision": "a5b47d31c556af34a302ce5d659e6fea44d90de0",
"revisionTime": "2016-09-28T15:37:09Z"
}
],
"rootPath": "github.com/silenceper/wechat"
}

View File

@@ -6,15 +6,17 @@ import (
"github.com/silenceper/wechat/cache"
"github.com/silenceper/wechat/context"
"github.com/silenceper/wechat/device"
"github.com/silenceper/wechat/js"
"github.com/silenceper/wechat/material"
"github.com/silenceper/wechat/menu"
"github.com/silenceper/wechat/message"
"github.com/silenceper/wechat/miniprogram"
"github.com/silenceper/wechat/oauth"
"github.com/silenceper/wechat/pay"
"github.com/silenceper/wechat/qr"
"github.com/silenceper/wechat/server"
"github.com/silenceper/wechat/template"
"github.com/silenceper/wechat/tcb"
"github.com/silenceper/wechat/user"
)
@@ -93,8 +95,8 @@ func (wc *Wechat) GetUser() *user.User {
}
// GetTemplate 模板消息接口
func (wc *Wechat) GetTemplate() *template.Template {
return template.NewTemplate(wc.Context)
func (wc *Wechat) GetTemplate() *message.Template {
return message.NewTemplate(wc.Context)
}
// GetPay 返回支付消息的实例
@@ -111,3 +113,13 @@ func (wc *Wechat) GetQR() *qr.QR {
func (wc *Wechat) GetMiniProgram() *miniprogram.MiniProgram {
return miniprogram.NewMiniProgram(wc.Context)
}
// GetDevice 获取智能设备的实例
func (wc *Wechat) GetDevice() *device.Device {
return device.NewDevice(wc.Context)
}
// GetTcb 获取小程序-云开发的实例
func (wc *Wechat) GetTcb() *tcb.Tcb {
return tcb.NewTcb(wc.Context)
}