From 2423b58855fe1708a7d2b6290da8c70ec679d7b4 Mon Sep 17 00:00:00 2001 From: silenceper Date: Sat, 23 May 2020 11:28:02 +0800 Subject: [PATCH] rebase release-2.0 --- .github/workflows/go.yml | 41 ++++++++++++++++++++++++++++++ README.md | 8 +++++- go.mod | 2 ++ go.sum | 17 +++++++++++++ officialaccount/officialaccount.go | 2 +- officialaccount/server/server.go | 20 ++++++++------- wechat.go | 20 +++++++++++++-- 7 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..de24808 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,41 @@ +name: Go + +on: + push: + branches: [ master,release-* ] + pull_request: + branches: [ master,release-* ] + +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.13 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Get dependencies + run: | + go get -v -t -d ./... + if [ -f Gopkg.toml ]; then + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + dep ensure + fi + + - name: Lint Go Code + run: | + export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14 + go get -u golang.org/x/lint/golint + go vet ./... + golint -set_exit_status $(go list ./...) + + - name: Test + run: go test -v -race ./... diff --git a/README.md b/README.md index 2e567f9..ef55760 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # WeChat SDK for Go -[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=release-1.3)](https://travis-ci.org/silenceper/wechat) +[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=release-2.0)](https://travis-ci.org/silenceper/wechat) [![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) @@ -44,6 +44,8 @@ server.Send() ``` +## 文档 +[Wechat SDK 2.0 文档](http://silenceper.com/wechat) ## 目录说明 @@ -55,6 +57,10 @@ server.Send() - work:企业微信 - aispeech:智能对话 +## 如何贡献 +- 提交issue,描述需要贡献的内容 +- 完成更改后,提交PR + ## License diff --git a/go.mod b/go.mod index 28bf554..bf49a13 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,7 @@ go 1.14 require ( github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b github.com/gomodule/redigo v1.8.1 + github.com/sirupsen/logrus v1.6.0 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 + golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect ) diff --git a/go.sum b/go.sum index b786a4f..351f9a8 100644 --- a/go.sum +++ b/go.sum @@ -2,20 +2,37 @@ github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pO github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= 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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gomodule/redigo v1.8.1 h1:Abmo0bI7Xf0IhdIPc7HZQzZcShdnmxeoVuDDtIQp8N8= github.com/gomodule/redigo v1.8.1/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/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= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw= golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= 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/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/officialaccount/officialaccount.go b/officialaccount/officialaccount.go index 66d4ca1..d471a8f 100644 --- a/officialaccount/officialaccount.go +++ b/officialaccount/officialaccount.go @@ -25,7 +25,7 @@ type OfficialAccount struct { //NewOfficialAccount 实例化公众号API func NewOfficialAccount(cfg *config.Config) *OfficialAccount { if cfg.Cache == nil { - panic("cache未设置") + panic("cache 未设置") } ctx := &context.Context{ Config: cfg, diff --git a/officialaccount/server/server.go b/officialaccount/server/server.go index a3ac17e..922ec30 100644 --- a/officialaccount/server/server.go +++ b/officialaccount/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/silenceper/wechat/v2/officialaccount/context" "github.com/silenceper/wechat/v2/officialaccount/message" + log "github.com/sirupsen/logrus" "github.com/silenceper/wechat/v2/util" ) @@ -22,7 +23,7 @@ type Server struct { Writer http.ResponseWriter Request *http.Request - debug bool + skipValidate bool openID string @@ -46,14 +47,15 @@ func NewServer(context *context.Context) *Server { return srv } -// SetDebug set debug field -func (srv *Server) SetDebug(debug bool) { - srv.debug = debug +// SeSkipValidate set skip validate +func (srv *Server) SkipValidate(skip bool) { + srv.skipValidate = skip } //Serve 处理微信的请求消息 func (srv *Server) Serve() error { if !srv.Validate() { + log.Error("Validate Signature Failed.") return fmt.Errorf("请求校验失败") } @@ -68,22 +70,21 @@ func (srv *Server) Serve() error { return err } - //debug - if srv.debug { - fmt.Println("request msg = ", string(srv.requestRawXMLMsg)) - } + //debug print request msg + log.Debugf("request msg =%s", string(srv.requestRawXMLMsg)) return srv.buildResponse(response) } //Validate 校验请求是否合法 func (srv *Server) Validate() bool { - if srv.debug { + if srv.skipValidate { return true } timestamp := srv.Query("timestamp") nonce := srv.Query("nonce") signature := srv.Query("signature") + log.Debugf("validate signature, timestamp=%s, nonce=%s", timestamp, nonce) return signature == util.Signature(srv.Token, timestamp, nonce) } @@ -223,6 +224,7 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) { //Send 将自定义的消息发送 func (srv *Server) Send() (err error) { replyMsg := srv.responseMsg + log.Debugf("response msg =%+v", replyMsg) if srv.isSafeMode { //安全模式下对消息进行加密 var encryptedMsg []byte diff --git a/wechat.go b/wechat.go index 15cf609..bb29def 100644 --- a/wechat.go +++ b/wechat.go @@ -1,15 +1,31 @@ package wechat import ( + "os" + "github.com/silenceper/wechat/v2/miniprogram" miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" - payConfig "github.com/silenceper/wechat/v2/pay/config" - "github.com/silenceper/wechat/v2/officialaccount" offConfig "github.com/silenceper/wechat/v2/officialaccount/config" "github.com/silenceper/wechat/v2/pay" + payConfig "github.com/silenceper/wechat/v2/pay/config" + log "github.com/sirupsen/logrus" ) +func init() { + + // Log as JSON instead of the default ASCII formatter. + log.SetFormatter(&log.TextFormatter{}) + + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example + log.SetOutput(os.Stdout) + + // Only log the warning severity or above. + log.SetLevel(log.DebugLevel) + +} + // Wechat struct type Wechat struct { }