/
miniprogram.go
120 lines (99 loc) · 3.44 KB
/
miniprogram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package wechat
import (
"context"
"fmt"
"time"
"github.com/google/wire"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/miniprogram"
"github.com/silenceper/wechat/v2/miniprogram/auth"
mpConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/silenceper/wechat/v2/miniprogram/security"
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/trace"
oteltrace "go.opentelemetry.io/otel/trace"
"github.com/xh-polaris/platform-sts/biz/infrastructure/config"
)
type RedisPlus struct {
*redis.Redis
}
func (r *RedisPlus) Get(key string) interface{} {
panic("implement me")
}
func (r *RedisPlus) Set(key string, val interface{}, timeout time.Duration) error {
panic("implement me")
}
func (r *RedisPlus) IsExist(key string) bool {
panic("implement me")
}
func (r *RedisPlus) Delete(key string) error {
panic("implement me")
}
func (r *RedisPlus) GetContext(ctx context.Context, key string) interface{} {
data, err := r.Redis.GetCtx(ctx, key)
if err != nil || data == "" {
return nil
}
return data
}
func (r *RedisPlus) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
str := fmt.Sprintf("%v", val)
err := r.Redis.SetexCtx(ctx, key, str, int(timeout.Seconds()))
return err
}
func (r *RedisPlus) IsExistContext(ctx context.Context, key string) bool {
data, _ := r.Redis.ExistsCtx(ctx, key)
return data
}
func (r *RedisPlus) DeleteContext(ctx context.Context, key string) error {
_, err := r.Redis.DelCtx(ctx, key)
return err
}
func NewRedisPlus(conf redis.RedisConf) *RedisPlus {
return &RedisPlus{
Redis: redis.MustNewRedis(conf),
}
}
type MiniProgramSDK struct {
sdk *miniprogram.MiniProgram
}
func (s *MiniProgramSDK) MsgCheck(ctx context.Context, in *security.MsgCheckRequest) (res security.MsgCheckResponse, err error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "mp/security/MsgCheck", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
return s.sdk.GetSecurity().MsgCheck(in)
}
func (s *MiniProgramSDK) Code2Session(ctx context.Context, jsCode string) (result auth.ResCode2Session, err error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "mp/auth/Code2Session", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
return s.sdk.GetAuth().Code2SessionContext(ctx, jsCode)
}
func (s *MiniProgramSDK) Send(ctx context.Context, in *subscribe.Message) (err error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "mp/subscribe/Send", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
return s.sdk.GetSubscribe().Send(in)
}
type MiniProgramMap map[string]*MiniProgramSDK
func NewWechatApplicationMap(config *config.Config) MiniProgramMap {
m := make(map[string]*MiniProgramSDK)
wx := wechat.NewWechat()
for _, conf := range config.WechatApplicationConfigs {
if conf.Type == "miniprogram" {
m[conf.AppID] = &MiniProgramSDK{sdk: wx.GetMiniProgram(&mpConfig.Config{
AppID: conf.AppID,
AppSecret: conf.AppSecret,
Cache: NewRedisPlus(*config.WeChatRedis),
})}
}
}
return m
}
var WechatSet = wire.NewSet(
NewWechatApplicationMap,
)