1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 21:02:25 +08:00

Compare commits

..

9 Commits

Author SHA1 Message Date
silenceper
5d8fd1f5bd fix 恢复直接使用enc.SetEscapeHTML参数 (#412)
Co-authored-by: zhenlinwen <zhenlinwen@tencent.com>
2021-06-23 09:49:41 +08:00
HUCHAOQI
45caf61899 Silenceper release 2.0 (#408)
* feat(miniapp): 增加统一服务消息

* feat(miniapp): 增加获取微信运动数据接口

* refactor(werun): 更改werun的位置

* fix(lint): 更改stuct名称

Co-authored-by: hyperq <hyperq1g@gmail>
2021-06-10 10:28:01 +08:00
silenceper
b42f1e1a7f Update README.md (#407) 2021-06-08 11:52:03 +08:00
Lien Li
2f88fad0bd 支持解密 分享数据 得到 openGId (#406)
* 支持解密 分享数据 

https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.getShareInfo.html

* fix fmt
2021-06-07 10:26:54 +08:00
Lien Li
13e5b3938b Update encryptor.go (#405) 2021-06-07 10:23:49 +08:00
MengYX
828ba7875b fix: incorrect redigo version (#404) 2021-06-07 10:04:45 +08:00
lanbiaowan
7a513080a8 addNews return error (#401)
Co-authored-by: p_biaolan <biao.lan@qingteng.cn>
2021-06-07 10:03:56 +08:00
faith
0e8fc3f88b [付款] pay.go 增加 GetTransfer 方法 (#400) 2021-05-28 09:39:32 +08:00
silenceper
c95b844c83 Update README.md 2021-05-26 15:19:04 +08:00
9 changed files with 77 additions and 24 deletions

View File

@@ -8,7 +8,7 @@
## 文档 && 例子
[Wechat SDK 2.0 文档](http://silenceper.com/wechat)
[Wechat SDK 2.0 文档](https://silenceper.com/wechat)
[Wechat SDK 2.0 例子](https://github.com/gowechat/example)

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.14
require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/fatih/structs v1.1.0
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gomodule/redigo v1.8.4
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.8.1

11
go.sum
View File

@@ -1,14 +1,13 @@
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gomodule/redigo v1.8.4 h1:Z5JUg94HMTR1XpwBaSH4vq3+PNSIykBLxMdglbw10gg=
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -25,27 +24,27 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -45,6 +45,8 @@ type PlainData struct {
AvatarURL string `json:"avatarUrl"`
Language string `json:"language"`
PhoneNumber string `json:"phoneNumber"`
OpenGID string `json:"openGId"`
MsgTicket string `json:"msgTicket"`
PurePhoneNumber string `json:"purePhoneNumber"`
CountryCode string `json:"countryCode"`
Watermark struct {
@@ -74,8 +76,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
return data[:len(data)-n], nil
}
// getCipherText returns slice of the cipher text
func getCipherText(sessionKey, encryptedData, iv string) ([]byte, 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
@@ -103,7 +105,7 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
// Decrypt 解密数据
func (encryptor *Encryptor) Decrypt(sessionKey, encryptedData, iv string) (*PlainData, error) {
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
cipherText, err := GetCipherText(sessionKey, encryptedData, iv)
if err != nil {
return nil, err
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
"github.com/silenceper/wechat/v2/miniprogram/tcb"
"github.com/silenceper/wechat/v2/miniprogram/werun"
)
//MiniProgram 微信小程序相关API
@@ -72,3 +73,8 @@ func (miniProgram *MiniProgram) GetSubscribe() *subscribe.Subscribe {
func (miniProgram *MiniProgram) GetCustomerMessage() *message.Manager {
return message.NewCustomerMessageManager(miniProgram.ctx)
}
// GetWeRun 微信运动接口
func (miniProgram *MiniProgram) GetWeRun() *werun.WeRun {
return werun.NewWeRun(miniProgram.ctx)
}

View File

@@ -0,0 +1,40 @@
package werun
import (
"encoding/json"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
)
// WeRun 微信运动
type WeRun struct {
*context.Context
}
// Data 微信运动数据
type Data struct {
StepInfoList []struct {
Timestamp int `json:"timestamp"`
Step int `json:"step"`
} `json:"stepInfoList"`
}
// NewWeRun 实例化
func NewWeRun(ctx *context.Context) *WeRun {
return &WeRun{Context: ctx}
}
// GetWeRunData 解密数据
func (werun *WeRun) GetWeRunData(sessionKey, encryptedData, iv string) (*Data, error) {
cipherText, err := encryptor.GetCipherText(sessionKey, encryptedData, iv)
if err != nil {
return nil, err
}
var weRunData Data
err = json.Unmarshal(cipherText, &weRunData)
if err != nil {
return nil, err
}
return &weRunData, nil
}

View File

@@ -119,6 +119,9 @@ func (material *Material) AddNews(articles []*Article) (mediaID string, err erro
if err != nil {
return
}
if res.ErrCode != 0 {
return "", fmt.Errorf("errcode=%d,errmsg=%s", res.ErrCode, res.ErrMsg)
}
mediaID = res.MediaID
return
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/silenceper/wechat/v2/pay/notify"
"github.com/silenceper/wechat/v2/pay/order"
"github.com/silenceper/wechat/v2/pay/refund"
"github.com/silenceper/wechat/v2/pay/transfer"
)
//Pay 微信支付相关API
@@ -31,3 +32,8 @@ func (pay *Pay) GetNotify() *notify.Notify {
func (pay *Pay) GetRefund() *refund.Refund {
return refund.NewRefund(pay.cfg)
}
// GetTransfer 付款
func (pay *Pay) GetTransfer() *transfer.Transfer {
return transfer.NewTransfer(pay.cfg)
}

View File

@@ -48,15 +48,14 @@ func HTTPPost(uri string, data string) ([]byte, error) {
//PostJSON post json 数据请求
func PostJSON(uri string, obj interface{}) ([]byte, error) {
jsonData, err := json.Marshal(obj)
jsonBuf := new(bytes.Buffer)
enc := json.NewEncoder(jsonBuf)
enc.SetEscapeHTML(false)
err := enc.Encode(obj)
if err != nil {
return nil, err
}
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<"))
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">"))
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&"))
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", jsonBuf)
if err != nil {
return nil, err
}
@@ -70,17 +69,15 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
// PostJSONWithRespContentType post json数据请求且返回数据类型
func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
jsonData, err := json.Marshal(obj)
jsonBuf := new(bytes.Buffer)
enc := json.NewEncoder(jsonBuf)
enc.SetEscapeHTML(false)
err := enc.Encode(obj)
if err != nil {
return nil, "", err
}
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003c"), []byte("<"))
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u003e"), []byte(">"))
jsonData = bytes.ReplaceAll(jsonData, []byte("\\u0026"), []byte("&"))
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", jsonBuf)
if err != nil {
return nil, "", err
}