double.huang 4 лет назад
Родитель
Сommit
8a630036c1

+ 2 - 2
etc/i2bill-api.yaml

@@ -12,8 +12,8 @@ Transform:
       - 47.103.219.158:30019
     Key: transform.rpc
 Weixin:
-  Appid:
-  Secret:
+  Appid: wxf8e794856efe5590
+  Secret: 9a75377da9afde4c666e1c3eb00d2598
 JwtAuth:
   AccessSecret: 6hy789iu87
   AccessExpire: 604800

+ 1 - 1
go.mod

@@ -4,7 +4,7 @@ go 1.16
 
 require (
 	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
+	git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615090349-f7bddc376275
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/satori/go.uuid v1.2.0
 	gopkg.in/yaml.v2 v2.4.0

+ 0 - 3
go.sum

@@ -1,10 +1,7 @@
 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=
 git.i2edu.net/i2/i2-bill-erp v0.0.0-20210615090349-f7bddc376275/go.mod h1:oWdflHBfbc2hoh4cFsg8HaheBVGHcSTw38BnbGMrk7c=
 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=

+ 2 - 0
i2bill.api

@@ -71,6 +71,8 @@ type response {
 }
 
 service i2bill-api {
+	@handler Hello // TODO
+	get /api/hello
 	@handler LoginByWeixin // TODO
 	post /api/auth/loginByWeixin(authLoginBody) returns(authResponse)
 }

+ 21 - 0
internal/handler/hellohandler.go

@@ -0,0 +1,21 @@
+package handler
+
+import (
+	"net/http"
+
+	"git.i2edu.net/i2/go-zero/rest/httpx"
+	"git.i2edu.net/i2/i2-bill-api/internal/logic"
+	"git.i2edu.net/i2/i2-bill-api/internal/svc"
+)
+
+func HelloHandler(ctx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		l := logic.NewHelloLogic(r.Context(), ctx)
+		err := l.Hello()
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.Ok(w)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

@@ -12,6 +12,11 @@ import (
 func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
 	engine.AddRoutes(
 		[]rest.Route{
+			{
+				Method:  http.MethodGet,
+				Path:    "/api/hello",
+				Handler: HelloHandler(serverCtx),
+			},
 			{
 				Method:  http.MethodPost,
 				Path:    "/api/auth/loginByWeixin",

+ 28 - 0
internal/logic/hellologic.go

@@ -0,0 +1,28 @@
+package logic
+
+import (
+	"context"
+
+	"git.i2edu.net/i2/go-zero/core/logx"
+	"git.i2edu.net/i2/i2-bill-api/internal/svc"
+)
+
+type HelloLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewHelloLogic(ctx context.Context, svcCtx *svc.ServiceContext) HelloLogic {
+	return HelloLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *HelloLogic) Hello() error {
+	// todo: add your logic here and delete this line
+	l.svcCtx.Wechat.GetAccessToken()
+	return nil
+}

+ 4 - 4
internal/svc/servicecontext.go

@@ -1,6 +1,7 @@
 package svc
 
 import (
+	"fmt"
 	"path"
 	"time"
 
@@ -31,10 +32,7 @@ type Wechat struct {
 func (wc *Wechat) GetAccessToken() (string, error) {
 	token := ""
 	err := wc.RdCli.GetCache(cacheWechatTokenPrefix, &token)
-	if err != nil {
-		return "", err
-	}
-	if token == "" {
+	if err == model.ErrRdsNotFound {
 		req := utils.Get("https://api.weixin.qq.com/cgi-bin/token")
 		req.Param("grant_type", "client_credential")
 		req.Param("secret", wc.c.Weixin.Secret)
@@ -44,9 +42,11 @@ func (wc *Wechat) GetAccessToken() (string, error) {
 			ExpiresIn   int    `json:"expires_in"`
 		}{}
 		err = req.ToJSON(&res)
+		fmt.Println("---66", err)
 		if err != nil {
 			return "", err
 		}
+		fmt.Println(res)
 		token = res.AccessToken
 		err := wc.RdCli.SetWithExpire(cacheWechatTokenPrefix, res.AccessToken, time.Duration((res.ExpiresIn-60*2))*time.Second)
 		if err != nil {

+ 5 - 1
model/usermodel.go

@@ -172,7 +172,11 @@ func (m *defaultUserModel) SetCache(key string, v interface{}) error {
 }
 
 func (m *defaultUserModel) GetCache(key string, v interface{}) error {
-	return m.CachedConn.GetCache(key, v)
+	err := m.CachedConn.GetCache(key, v)
+	if err == sql.ErrNoRows {
+		err = ErrRdsNotFound
+	}
+	return err
 }
 
 func (m *defaultUserModel) SetWithExpire(key string, v interface{}, expire time.Duration) error {

+ 7 - 1
model/vars.go

@@ -1,5 +1,11 @@
 package model
 
-import "git.i2edu.net/i2/go-zero/core/stores/sqlx"
+import (
+	"errors"
+
+	"git.i2edu.net/i2/go-zero/core/stores/sqlx"
+)
 
 var ErrNotFound = sqlx.ErrNotFound
+
+var ErrRdsNotFound = errors.New("rds: no rows in result set")