Skip to content

Commit

Permalink
Adaptation of new go-redis components (#546)
Browse files Browse the repository at this point in the history
* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session)

* [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html)

* upgrade json error

* upgrade json error

* [feature] Wallet Transfer returns the pointer object

* feat:Adaptation of new go-redis components

* improve code

* feat:upgrade golangci-lint-action version

* fix

* test ci

* fix

* test ci

* fix

* test

* improve code

* feat:GetPhoneNumber return ptr

Co-authored-by: houseme <[email protected]>
  • Loading branch information
houseme and housemecn authored Apr 14, 2022
1 parent 1b5c5fb commit 8e81a41
Show file tree
Hide file tree
Showing 30 changed files with 232 additions and 153 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
## 问题及现象

<!-- 描述你的问题现象,报错**贴截图**粘贴或者贴具体信息,提供**必要的代码段**
13 changes: 10 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ jobs:
golangci:
strategy:
matrix:
go-version: [1.15.x]
go-version: [1.15.x,1.16.x]
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3.1.0
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
Expand All @@ -33,12 +34,18 @@ jobs:
image: memcached
ports:
- 11211:11211

# strategy set
strategy:
matrix:
go: ["1.14","1.15", "1.16", "1.17", "1.18"]

steps:
- uses: actions/checkout@v2
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ${{ matrix.go }}
id: go
- name: Test
run: go test -v -race ./...
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# WeChat SDK for Go

![Go](https://github.com/silenceper/wechat/workflows/Go/badge.svg?branch=release-2.0)
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat)
[![pkg](https://img.shields.io/badge/dev-reference-007d9c?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/silenceper/wechat/v2?tab=doc)
![version](https://img.shields.io/badge/version-v2-green)

使用Golang开发的微信SDK,简单、易用。
>注意:当前版本为v2版本,v1版本已废弃
> 注意:当前版本为v2版本,v1版本已废弃
## 文档 && 例子

[API列表](https://github.com/silenceper/wechat/tree/v2/doc/api)

[Wechat SDK 2.0 文档](https://silenceper.com/wechat)

[Wechat SDK 2.0 例子](https://github.com/gowechat/example)


## 快速开始

```
import "github.com/silenceper/wechat/v2"
```
Expand Down Expand Up @@ -58,6 +59,7 @@ server.Send()
```

## 目录说明

- officialaccount: 微信公众号API
- miniprogram: 小程序API
- minigame:小游戏API
Expand All @@ -68,11 +70,13 @@ server.Send()
- doc: api文档

## 贡献

-[API列表](https://github.com/silenceper/wechat/tree/v2/doc/api)中查看哪些API未实现
- 提交issue,描述需要贡献的内容
- 完成更改后,提交PR

## 公众号

![img](https://silenceper.oss-cn-beijing.aliyuncs.com/qrcode/search_study_program.png)

## License
Expand Down
94 changes: 27 additions & 67 deletions cache/redis.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cache

import (
"encoding/json"
"context"
"time"

"github.com/gomodule/redigo/redis"
"github.com/go-redis/redis/v8"
)

// Redis .redis cache
type Redis struct {
conn *redis.Pool
ctx context.Context
conn redis.UniversalClient
}

// RedisOpts redis 连接属性
Expand All @@ -23,90 +24,49 @@ type RedisOpts struct {
}

// NewRedis 实例化
func NewRedis(opts *RedisOpts, dialOpts ...redis.DialOption) *Redis {
pool := &redis.Pool{
MaxActive: opts.MaxActive,
MaxIdle: opts.MaxIdle,
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
Dial: func() (redis.Conn, error) {
dialOpts = append(dialOpts, []redis.DialOption{
redis.DialDatabase(opts.Database),
redis.DialPassword(opts.Password),
}...)
return redis.Dial("tcp", opts.Host, dialOpts...)
},
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := conn.Do("PING")
return err
},
}
return &Redis{pool}
}

// SetRedisPool 设置redis连接池
func (r *Redis) SetRedisPool(pool *redis.Pool) {
r.conn = pool
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
conn := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{opts.Host},
DB: opts.Database,
Password: opts.Password,
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
MinIdleConns: opts.MaxIdle,
})
return &Redis{ctx: ctx, conn: conn}
}

// SetConn 设置conn
func (r *Redis) SetConn(conn *redis.Pool) {
func (r *Redis) SetConn(conn redis.UniversalClient) {
r.conn = conn
}

// SetRedisCtx 设置redis ctx 参数
func (r *Redis) SetRedisCtx(ctx context.Context) {
r.ctx = ctx
}

// Get 获取一个值
func (r *Redis) Get(key string) interface{} {
conn := r.conn.Get()
defer conn.Close()

var data []byte
var err error
if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
return nil
}
var reply interface{}
if err = json.Unmarshal(data, &reply); err != nil {
result, err := r.conn.Do(r.ctx, "GET", key).Result()
if err != nil {
return nil
}

return reply
return result
}

// Set 设置一个值
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) (err error) {
conn := r.conn.Get()
defer conn.Close()

var data []byte
if data, err = json.Marshal(val); err != nil {
return
}

_, err = conn.Do("SETEX", key, int64(timeout/time.Second), data)

return
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
return r.conn.SetEX(r.ctx, key, val, timeout).Err()
}

// IsExist 判断key是否存在
func (r *Redis) IsExist(key string) bool {
conn := r.conn.Get()
defer conn.Close()
result, _ := r.conn.Exists(r.ctx, key).Result()

a, _ := conn.Do("EXISTS", key)
i := a.(int64)
return i > 0
return result > 0
}

// Delete 删除
func (r *Redis) Delete(key string) error {
conn := r.conn.Get()
defer conn.Close()

if _, err := conn.Do("DEL", key); err != nil {
return err
}

return nil
return r.conn.Del(r.ctx, key).Err()
}
29 changes: 18 additions & 11 deletions cache/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package cache

import (
"context"
"testing"
"time"
)

func TestRedis(t *testing.T) {
opts := &RedisOpts{
Host: "127.0.0.1:6379",
}
redis := NewRedis(opts)
var (
timeoutDuration = time.Second
ctx = context.Background()
opts = &RedisOpts{
Host: "127.0.0.1:6379",
}
redis = NewRedis(ctx, opts)
err error
val = "silenceper"
key = "username"
)
redis.SetConn(redis.conn)
var err error
timeoutDuration := 1 * time.Second
redis.SetRedisCtx(ctx)

if err = redis.Set("username", "silenceper", timeoutDuration); err != nil {
if err = redis.Set(key, val, timeoutDuration); err != nil {
t.Error("set Error", err)
}

if !redis.IsExist("username") {
if !redis.IsExist(key) {
t.Error("IsExist Error")
}

name := redis.Get("username").(string)
if name != "silenceper" {
name := redis.Get(key).(string)
if name != val {
t.Error("get Error")
}

if err = redis.Delete("username"); err != nil {
if err = redis.Delete(key); err != nil {
t.Errorf("delete Error , err=%v", err)
}
}
3 changes: 1 addition & 2 deletions doc/api/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# API 文档

已完成以及未完成API列表汇总

如果有兴趣参与贡献,可以在具体的API表格后面标识自己为贡献者以及完成时间,例如:


| 名称 | 请求方式 | URL | 是否已实现 | 使用方法 |贡献者|完成时间|
| :---------------------: | -------- | :------------------------- | ---------- | -------- |-------- |-------- |
| 获取公众号类目 | GET | /wxaapi/newtmpl/getcategory | NO | |silenceper| 2021-12-20|


- [微信公众号](./officialaccount.md)
- [小程序](./miniprogram.md)
- [小游戏](./minigame.md)
Expand Down
1 change: 1 addition & 0 deletions doc/api/aispeech.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 智能对话

TODO
1 change: 1 addition & 0 deletions doc/api/minigame.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 小游戏

TODO
1 change: 1 addition & 0 deletions doc/api/miniprogram.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 小程序

TODO
1 change: 1 addition & 0 deletions doc/api/wxpay.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 微信支付

TODO
16 changes: 6 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ module github.com/silenceper/wechat/v2
go 1.14

require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d
github.com/fatih/structs v1.1.0
github.com/gomodule/redigo v1.8.5
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/go-redis/redis/v8 v8.11.6-0.20220405070650-99c79f7041fc
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/h2non/gock.v1 v1.0.15
github.com/spf13/cast v1.4.1
github.com/stretchr/testify v1.7.1
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
gopkg.in/h2non/gock.v1 v1.1.2
)
Loading

0 comments on commit 8e81a41

Please sign in to comment.