mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
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
|
||||||
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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"`
|
||||||
|
|||||||
@@ -20,7 +20,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 +38,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 +76,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 {
|
||||||
|
|||||||
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