Selaa lähdekoodia

feat: 添加小程序token

double.huang 4 vuotta sitten
vanhempi
commit
e52fe08e24
6 muutettua tiedostoa jossa 85 lisäystä ja 8 poistoa
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 0 2
      internal/handler/static.go
  4. 1 1
      internal/logic/loginbyweixinlogic.go
  5. 58 4
      internal/svc/servicecontext.go
  6. 23 0
      model/usermodel.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module git.i2edu.net/i2/i2-bill-api
 go 1.16
 
 require (
-	git.i2edu.net/i2/go-zero v1.0.0
+	git.i2edu.net/i2/go-zero v1.0.1-0.20210616091154-7fac117e009f
 	git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615090349-f7bddc376275 // indirect
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/satori/go.uuid v1.2.0

+ 2 - 0
go.sum

@@ -1,6 +1,8 @@
 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 git.i2edu.net/i2/go-zero v1.0.0 h1:tB6YOQ4PwOkZwIrzXV+fv6rZajohZbdsBTLw+PHjo3E=
 git.i2edu.net/i2/go-zero v1.0.0/go.mod h1:a9idDtfMmMXrZIHyDg6XnYjWuCpKG0I6zv6Vo9Fpncc=
+git.i2edu.net/i2/go-zero v1.0.1-0.20210616091154-7fac117e009f h1:zD6rIG7+PhJwYXuQbHyw1pn/z5oUXzrUg9bo9F9+b5k=
+git.i2edu.net/i2/go-zero v1.0.1-0.20210616091154-7fac117e009f/go.mod h1:a9idDtfMmMXrZIHyDg6XnYjWuCpKG0I6zv6Vo9Fpncc=
 git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615054657-2a5f8ce2446f h1:hZU9S5PHoZ1u4qJ1qZMI+fK92uozj3ygWOHpnIJ/3tE=
 git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615054657-2a5f8ce2446f/go.mod h1:oWdflHBfbc2hoh4cFsg8HaheBVGHcSTw38BnbGMrk7c=
 git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615090349-f7bddc376275 h1:HsXmEM5HnDNUd44de6CZ1OeDdCt/Z6oQN601WNZ5Pyo=

+ 0 - 2
internal/handler/static.go

@@ -1,7 +1,6 @@
 package handler
 
 import (
-	"fmt"
 	"net/http"
 	"os"
 	"path"
@@ -96,7 +95,6 @@ func Static(urlPrefix string, fs ServeFileSystem) (r []rest.Route) {
 			Path:    path,
 			Handler: h,
 		})
-		fmt.Println(path)
 	}
 	return
 }

+ 1 - 1
internal/logic/loginbyweixinlogic.go

@@ -41,7 +41,7 @@ func (l *LoginByWeixinLogic) LoginByWeixin(req types.AuthLoginBody) (*types.Auth
 		return nil, err
 	}
 
-	//
+	// l.svcCtx.RdCli.GetCache()
 	resp, err := l.svcCtx.Transformer.GetUser(l.ctx, &transformclient.Request{
 		Id: "0909",
 	})

+ 58 - 4
internal/svc/servicecontext.go

@@ -1,24 +1,78 @@
 package svc
 
 import (
-	"git.i2edu.net/i2/i2-bill-api/internal/config"
-	"git.i2edu.net/i2/i2-bill-api/model"
-	"git.i2edu.net/i2/i2-bill-erp/transformclient"
+	"time"
 
 	"git.i2edu.net/i2/go-zero/core/stores/sqlx"
 	"git.i2edu.net/i2/go-zero/zrpc"
+	"git.i2edu.net/i2/i2-bill-api/internal/config"
+	"git.i2edu.net/i2/i2-bill-api/internal/utils"
+	"git.i2edu.net/i2/i2-bill-api/model"
+	"git.i2edu.net/i2/i2-bill-erp/transformclient"
 )
 
+const cacheWechatTokenPrefix = "cache:wechat:token:"
+
+// RdCli defined
+type RdCli interface {
+	SetCache(key string, v interface{}) error
+	GetCache(key string, v interface{}) error
+	SetWithExpire(key string, v interface{}, expire time.Duration) error
+}
+
+// Wechat defined
+type Wechat struct {
+	RdCli RdCli
+	c     config.Config
+}
+
+// GetAccessToken defined
+func (wc *Wechat) GetAccessToken() (string, error) {
+	token := ""
+	err := wc.RdCli.GetCache(cacheWechatTokenPrefix, &token)
+	if err != nil {
+		return "", err
+	}
+	if token == "" {
+		req := utils.Get("https://api.weixin.qq.com/cgi-bin/token")
+		req.Param("grant_type", "client_credential")
+		req.Param("secret", wc.c.Weixin.Secret)
+		req.Param("appid", wc.c.Weixin.Appid)
+		var res = struct {
+			AccessToken string `json:"access_token"`
+			ExpiresIn   int    `json:"expires_in"`
+		}{}
+		err = req.ToJSON(&res)
+		if err != nil {
+			return "", err
+		}
+		token = res.AccessToken
+		err := wc.RdCli.SetWithExpire(cacheWechatTokenPrefix, res.AccessToken, time.Duration((res.ExpiresIn-60*2))*time.Second)
+		if err != nil {
+			return "", err
+		}
+	}
+	return token, nil
+}
+
 type ServiceContext struct {
 	Config      config.Config
 	UserModel   model.UserModel
+	RdCli       RdCli
+	Wechat      *Wechat
 	Transformer transformclient.Transform
 }
 
 func NewServiceContext(c config.Config) *ServiceContext {
-	return &ServiceContext{
+	svc := &ServiceContext{
 		Config:      c,
 		Transformer: transformclient.NewTransform(zrpc.MustNewClient(c.Transform)),
 		UserModel:   model.NewUserModel(sqlx.NewMysql(c.DataSource), c.Cache),
 	}
+	svc.RdCli = svc.UserModel.CacheStorage()
+	svc.Wechat = &Wechat{
+		RdCli: svc.RdCli,
+		c:     c,
+	}
+	return svc
 }

+ 23 - 0
model/usermodel.go

@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	"fmt"
 	"strings"
+	"time"
 
 	"git.i2edu.net/i2/go-zero/core/stores/cache"
 	"git.i2edu.net/i2/go-zero/core/stores/sqlc"
@@ -24,6 +25,11 @@ var (
 )
 
 type (
+	Cache interface {
+		SetCache(key string, v interface{}) error
+		GetCache(key string, v interface{}) error
+		SetWithExpire(key string, v interface{}, expire time.Duration) error
+	}
 	UserModel interface {
 		Insert(data User) (sql.Result, error)
 		FindOne(id int64) (*User, error)
@@ -31,6 +37,7 @@ type (
 		FindOneByWeiXinOpenId(openid string) (*User, error)
 		Update(data User) error
 		Delete(id int64) error
+		CacheStorage() Cache
 	}
 
 	defaultUserModel struct {
@@ -159,3 +166,19 @@ func (m *defaultUserModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{
 	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRows, m.table)
 	return conn.QueryRow(v, query, primary)
 }
+
+func (m *defaultUserModel) SetCache(key string, v interface{}) error {
+	return m.CachedConn.SetCache(key, v)
+}
+
+func (m *defaultUserModel) GetCache(key string, v interface{}) error {
+	return m.CachedConn.GetCache(key, v)
+}
+
+func (m *defaultUserModel) SetWithExpire(key string, v interface{}, expire time.Duration) error {
+	return m.CachedConn.SetWithExpire(key, v, expire)
+}
+
+func (m *defaultUserModel) CacheStorage() Cache {
+	return m
+}