Skip to content

Commit 8e81a41

Browse files
housemehousemecn
andauthored
Adaptation of new go-redis components (#546)
* [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]>
1 parent 1b5c5fb commit 8e81a41

File tree

30 files changed

+232
-153
lines changed

30 files changed

+232
-153
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
## 问题及现象
2+
23
<!-- 描述你的问题现象,报错**贴截图**粘贴或者贴具体信息,提供**必要的代码段**

.github/workflows/go.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ jobs:
1010
golangci:
1111
strategy:
1212
matrix:
13-
go-version: [1.15.x]
13+
go-version: [1.15.x,1.16.x]
1414
name: golangci-lint
1515
runs-on: ubuntu-latest
1616
steps:
17+
- uses: actions/setup-go@v2
1718
- uses: actions/checkout@v2
1819
- name: golangci-lint
19-
uses: golangci/golangci-lint-action@v2
20+
uses: golangci/golangci-lint-action@v3.1.0
2021
with:
2122
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
2223
version: v1.31
@@ -33,12 +34,18 @@ jobs:
3334
image: memcached
3435
ports:
3536
- 11211:11211
37+
38+
# strategy set
39+
strategy:
40+
matrix:
41+
go: ["1.14","1.15", "1.16", "1.17", "1.18"]
42+
3643
steps:
3744
- uses: actions/checkout@v2
3845
- name: Set up Go 1.x
3946
uses: actions/setup-go@v2
4047
with:
41-
go-version: ^1.13
48+
go-version: ${{ matrix.go }}
4249
id: go
4350
- name: Test
4451
run: go test -v -race ./...

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
# WeChat SDK for Go
2+
23
![Go](https://github.com/silenceper/wechat/workflows/Go/badge.svg?branch=release-2.0)
34
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat)
45
[![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)
56
![version](https://img.shields.io/badge/version-v2-green)
67

78
使用Golang开发的微信SDK,简单、易用。
8-
>注意:当前版本为v2版本,v1版本已废弃
9-
9+
> 注意:当前版本为v2版本,v1版本已废弃
1010
1111
## 文档 && 例子
12+
1213
[API列表](https://github.com/silenceper/wechat/tree/v2/doc/api)
1314

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

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

18-
1919
## 快速开始
20+
2021
```
2122
import "github.com/silenceper/wechat/v2"
2223
```
@@ -58,6 +59,7 @@ server.Send()
5859
```
5960

6061
## 目录说明
62+
6163
- officialaccount: 微信公众号API
6264
- miniprogram: 小程序API
6365
- minigame:小游戏API
@@ -68,11 +70,13 @@ server.Send()
6870
- doc: api文档
6971

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

7578
## 公众号
79+
7680
![img](https://silenceper.oss-cn-beijing.aliyuncs.com/qrcode/search_study_program.png)
7781

7882
## License

cache/redis.go

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package cache
22

33
import (
4-
"encoding/json"
4+
"context"
55
"time"
66

7-
"github.com/gomodule/redigo/redis"
7+
"github.com/go-redis/redis/v8"
88
)
99

1010
// Redis .redis cache
1111
type Redis struct {
12-
conn *redis.Pool
12+
ctx context.Context
13+
conn redis.UniversalClient
1314
}
1415

1516
// RedisOpts redis 连接属性
@@ -23,90 +24,49 @@ type RedisOpts struct {
2324
}
2425

2526
// NewRedis 实例化
26-
func NewRedis(opts *RedisOpts, dialOpts ...redis.DialOption) *Redis {
27-
pool := &redis.Pool{
28-
MaxActive: opts.MaxActive,
29-
MaxIdle: opts.MaxIdle,
30-
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
31-
Dial: func() (redis.Conn, error) {
32-
dialOpts = append(dialOpts, []redis.DialOption{
33-
redis.DialDatabase(opts.Database),
34-
redis.DialPassword(opts.Password),
35-
}...)
36-
return redis.Dial("tcp", opts.Host, dialOpts...)
37-
},
38-
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
39-
if time.Since(t) < time.Minute {
40-
return nil
41-
}
42-
_, err := conn.Do("PING")
43-
return err
44-
},
45-
}
46-
return &Redis{pool}
47-
}
48-
49-
// SetRedisPool 设置redis连接池
50-
func (r *Redis) SetRedisPool(pool *redis.Pool) {
51-
r.conn = pool
27+
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
28+
conn := redis.NewUniversalClient(&redis.UniversalOptions{
29+
Addrs: []string{opts.Host},
30+
DB: opts.Database,
31+
Password: opts.Password,
32+
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
33+
MinIdleConns: opts.MaxIdle,
34+
})
35+
return &Redis{ctx: ctx, conn: conn}
5236
}
5337

5438
// SetConn 设置conn
55-
func (r *Redis) SetConn(conn *redis.Pool) {
39+
func (r *Redis) SetConn(conn redis.UniversalClient) {
5640
r.conn = conn
5741
}
5842

43+
// SetRedisCtx 设置redis ctx 参数
44+
func (r *Redis) SetRedisCtx(ctx context.Context) {
45+
r.ctx = ctx
46+
}
47+
5948
// Get 获取一个值
6049
func (r *Redis) Get(key string) interface{} {
61-
conn := r.conn.Get()
62-
defer conn.Close()
63-
64-
var data []byte
65-
var err error
66-
if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
67-
return nil
68-
}
69-
var reply interface{}
70-
if err = json.Unmarshal(data, &reply); err != nil {
50+
result, err := r.conn.Do(r.ctx, "GET", key).Result()
51+
if err != nil {
7152
return nil
7253
}
73-
74-
return reply
54+
return result
7555
}
7656

7757
// Set 设置一个值
78-
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) (err error) {
79-
conn := r.conn.Get()
80-
defer conn.Close()
81-
82-
var data []byte
83-
if data, err = json.Marshal(val); err != nil {
84-
return
85-
}
86-
87-
_, err = conn.Do("SETEX", key, int64(timeout/time.Second), data)
88-
89-
return
58+
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
59+
return r.conn.SetEX(r.ctx, key, val, timeout).Err()
9060
}
9161

9262
// IsExist 判断key是否存在
9363
func (r *Redis) IsExist(key string) bool {
94-
conn := r.conn.Get()
95-
defer conn.Close()
64+
result, _ := r.conn.Exists(r.ctx, key).Result()
9665

97-
a, _ := conn.Do("EXISTS", key)
98-
i := a.(int64)
99-
return i > 0
66+
return result > 0
10067
}
10168

10269
// Delete 删除
10370
func (r *Redis) Delete(key string) error {
104-
conn := r.conn.Get()
105-
defer conn.Close()
106-
107-
if _, err := conn.Do("DEL", key); err != nil {
108-
return err
109-
}
110-
111-
return nil
71+
return r.conn.Del(r.ctx, key).Err()
11272
}

cache/redis_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
package cache
22

33
import (
4+
"context"
45
"testing"
56
"time"
67
)
78

89
func TestRedis(t *testing.T) {
9-
opts := &RedisOpts{
10-
Host: "127.0.0.1:6379",
11-
}
12-
redis := NewRedis(opts)
10+
var (
11+
timeoutDuration = time.Second
12+
ctx = context.Background()
13+
opts = &RedisOpts{
14+
Host: "127.0.0.1:6379",
15+
}
16+
redis = NewRedis(ctx, opts)
17+
err error
18+
val = "silenceper"
19+
key = "username"
20+
)
1321
redis.SetConn(redis.conn)
14-
var err error
15-
timeoutDuration := 1 * time.Second
22+
redis.SetRedisCtx(ctx)
1623

17-
if err = redis.Set("username", "silenceper", timeoutDuration); err != nil {
24+
if err = redis.Set(key, val, timeoutDuration); err != nil {
1825
t.Error("set Error", err)
1926
}
2027

21-
if !redis.IsExist("username") {
28+
if !redis.IsExist(key) {
2229
t.Error("IsExist Error")
2330
}
2431

25-
name := redis.Get("username").(string)
26-
if name != "silenceper" {
32+
name := redis.Get(key).(string)
33+
if name != val {
2734
t.Error("get Error")
2835
}
2936

30-
if err = redis.Delete("username"); err != nil {
37+
if err = redis.Delete(key); err != nil {
3138
t.Errorf("delete Error , err=%v", err)
3239
}
3340
}

doc/api/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# API 文档
2+
23
已完成以及未完成API列表汇总
34

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

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

11-
1211
- [微信公众号](./officialaccount.md)
1312
- [小程序](./miniprogram.md)
1413
- [小游戏](./minigame.md)

doc/api/aispeech.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 智能对话
2+
23
TODO

doc/api/minigame.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 小游戏
2+
23
TODO

doc/api/miniprogram.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 小程序
2+
23
TODO

doc/api/wxpay.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 微信支付
2+
23
TODO

0 commit comments

Comments
 (0)