From b0ffa8c62f6c0f05543c7ae16257cf37953fad6d Mon Sep 17 00:00:00 2001 From: it512 Date: Mon, 16 Dec 2024 22:05:41 +0800 Subject: [PATCH] fix --- wechat/cmd/wechat-proxy/main.go | 4 ++-- wechat/rpc/gql/rpc.go | 4 ++-- wxlogin/cmd/wxlogin/main.go | 11 +++++++---- wxlogin/go.mod | 2 +- wxlogin/go.sum | 2 ++ wxlogin/gql/gql.go | 4 ++-- wxlogin/gql/graph/login.resolvers.go | 2 +- wxlogin/jwt.go | 21 ++++++++++++++++++++- wxlogin/wx/wx.go | 6 +++--- 9 files changed, 40 insertions(+), 16 deletions(-) diff --git a/wechat/cmd/wechat-proxy/main.go b/wechat/cmd/wechat-proxy/main.go index 1941e7c..133e431 100644 --- a/wechat/cmd/wechat-proxy/main.go +++ b/wechat/cmd/wechat-proxy/main.go @@ -38,7 +38,7 @@ func GetEnv(env string) string { func main() { appId := GetEnv(WECHAT_APPID) - appSecret :=GetEnv(WECHAT_APPSECRET) + appSecret := GetEnv(WECHAT_APPSECRET) addr := GetEnv(SERVER_ADDR) /* mchId := os.Getenv(WECHAT_MCHID) @@ -63,7 +63,7 @@ func main() { mux := chi.NewMux() mux.Use(middleware.Logger, middleware.Recoverer) - mux.Mount("/rpc", gql.New(auth)) + mux.Mount("/gqlrpc", gql.New(auth)) //mux.Mount("/wxnotify", bc.WxBCNotify()) svr := web.NewHttpServer(ctx, addr, mux) diff --git a/wechat/rpc/gql/rpc.go b/wechat/rpc/gql/rpc.go index 93fbc97..0fe10ad 100644 --- a/wechat/rpc/gql/rpc.go +++ b/wechat/rpc/gql/rpc.go @@ -19,8 +19,8 @@ func New(a *sns.Auth) chi.Router { srv := rpc.New(es) r := chi.NewRouter() - r.Handle("/", playground.ApolloSandboxHandler("rpc", "/rpc/gql")) - r.Handle("/gql", srv) + r.Handle("/", playground.ApolloSandboxHandler("rpc", "/gqlrpc/query")) + r.Handle("/query", srv) return r } diff --git a/wxlogin/cmd/wxlogin/main.go b/wxlogin/cmd/wxlogin/main.go index 52b9009..c35f984 100644 --- a/wxlogin/cmd/wxlogin/main.go +++ b/wxlogin/cmd/wxlogin/main.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "context" "log" "net/http" "os" @@ -10,9 +10,11 @@ import ( "github.com/Khan/genqlient/graphql" "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/twiglab/crm/wxlogin" "github.com/twiglab/crm/wxlogin/gql" + "github.com/twiglab/crm/wxlogin/web" "github.com/twiglab/crm/wxlogin/wx" ) @@ -48,12 +50,13 @@ func main() { root := chi.NewMux() + root.Use(middleware.Logger, middleware.Recoverer) + root.Mount("/gql", gql.GQLRouter(cli)) root.Mount("/jwt", wxlogin.JWTVerify(secret)) - if err := http.ListenAndServe(addr, root); err != nil { + svr := web.NewHttpServer(context.Background(), addr, root) + if err := web.RunServer(context.Background(), svr); err != nil { log.Fatal(err) } - - fmt.Println(mb) } diff --git a/wxlogin/go.mod b/wxlogin/go.mod index 75280d2..6370f69 100644 --- a/wxlogin/go.mod +++ b/wxlogin/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/twiglab/crm/member v0.0.0-20241116061244-2ba4efae26f1 - github.com/twiglab/crm/wechat v0.0.0-20241116061244-2ba4efae26f1 + github.com/twiglab/crm/wechat v0.0.0-20241216131727-886793cf0a25 github.com/vektah/gqlparser/v2 v2.5.20 ) diff --git a/wxlogin/go.sum b/wxlogin/go.sum index 92458fa..8c4443c 100644 --- a/wxlogin/go.sum +++ b/wxlogin/go.sum @@ -44,6 +44,8 @@ github.com/twiglab/crm/member v0.0.0-20241116061244-2ba4efae26f1 h1:JnPUqp1Jgrxr github.com/twiglab/crm/member v0.0.0-20241116061244-2ba4efae26f1/go.mod h1:dV6ntWKUnXXLbCzYkNU2d1jjD9FKUOcZon1SJ+X20xo= github.com/twiglab/crm/wechat v0.0.0-20241116061244-2ba4efae26f1 h1:wjngLNRqau18Vqn6ybCn4Ws2U1fwehXrQJ0sEWLDsh8= github.com/twiglab/crm/wechat v0.0.0-20241116061244-2ba4efae26f1/go.mod h1:gXsub5c9PNnxisPDWoDkAwKrt99MeUYgAb0ehGZRyTk= +github.com/twiglab/crm/wechat v0.0.0-20241216131727-886793cf0a25 h1:+nB/i8TimBVTxif2trD0S52Znpk3vb7HIZ3ozVtfvrA= +github.com/twiglab/crm/wechat v0.0.0-20241216131727-886793cf0a25/go.mod h1:lEvPdYOttpqGXb+ywgx4iLZlUdh9W8LGwSknlYEYDYs= github.com/vektah/gqlparser/v2 v2.5.20 h1:kPaWbhBntxoZPaNdBaIPT1Kh0i1b/onb5kXgEdP5JCo= github.com/vektah/gqlparser/v2 v2.5.20/go.mod h1:xMl+ta8a5M1Yo1A1Iwt/k7gSpscwSnHZdw7tfhEGfTM= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= diff --git a/wxlogin/gql/gql.go b/wxlogin/gql/gql.go index 04936b6..0b855e8 100644 --- a/wxlogin/gql/gql.go +++ b/wxlogin/gql/gql.go @@ -17,8 +17,8 @@ func GQLRouter(acli *wxlogin.AuthClient) chi.Router { )) r := chi.NewRouter() - r.Handle("/", playground.ApolloSandboxHandler("pg", "/gql")) - r.Handle("/gql", srv) + r.Handle("/", playground.ApolloSandboxHandler("pg", "/gql/query")) + r.Handle("/query", srv) return r } diff --git a/wxlogin/gql/graph/login.resolvers.go b/wxlogin/gql/graph/login.resolvers.go index 9f38950..7464ba5 100644 --- a/wxlogin/gql/graph/login.resolvers.go +++ b/wxlogin/gql/graph/login.resolvers.go @@ -12,7 +12,7 @@ import ( // Login is the resolver for the Login field. func (r *queryResolver) Login(ctx context.Context, input model.JsCodeReq) (*model.LoginUserResp, error) { - x, err := r.AuthCli.Login2(ctx, input.JsCode) + x, err := r.AuthCli.Login3(ctx, input.JsCode) if err != nil { return nil, err } diff --git a/wxlogin/jwt.go b/wxlogin/jwt.go index 25add6c..39ca10f 100644 --- a/wxlogin/jwt.go +++ b/wxlogin/jwt.go @@ -21,6 +21,7 @@ type AuthJWTConfig struct { type X struct { Jwt string Code string + OpenID string Claims *jwt.Claims } @@ -33,6 +34,24 @@ type AuthClient struct { // AuthJWTConfig AuthJWTConfig } +func (x *AuthClient) Login3(ctx context.Context, jsCode string) (*X, error) { + codes, err := x.WxCli.AuthUser(ctx, jsCode) + if err != nil { + return nil, err + } + + Code := uuid.NewString() + + claims := NewClaims(Code) + + jwt, err := signed(claims, x.Secret) + if err != nil { + return nil, err + } + + return &X{Jwt: jwt, Code: Code, OpenID: codes.OpenID, Claims: claims}, nil +} + func (x *AuthClient) Login2(ctx context.Context, jsCode string) (*X, error) { codes, err := x.WxCli.AuthUser(ctx, jsCode) if err != nil { @@ -51,7 +70,7 @@ func (x *AuthClient) Login2(ctx context.Context, jsCode string) (*X, error) { return nil, err } - return &X{Jwt: jwt, Code: member.Code, Claims: claims}, nil + return &X{Jwt: jwt, Code: member.Code, OpenID: codes.OpenID, Claims: claims}, nil } func NewClaims(code string) *jwt.Claims { diff --git a/wxlogin/wx/wx.go b/wxlogin/wx/wx.go index a99122c..1a1ba44 100644 --- a/wxlogin/wx/wx.go +++ b/wxlogin/wx/wx.go @@ -4,8 +4,8 @@ import ( "context" "github.com/Khan/genqlient/graphql" - "github.com/twiglab/crm/wechat/pkg/data" - "github.com/twiglab/crm/wechat/pkg/low" + "github.com/twiglab/crm/wechat/pkg/sns" + "github.com/twiglab/crm/wechat/pkg/sns/low" ) type WxCli struct { @@ -24,7 +24,7 @@ type Codes struct { // @return *Codes // @return error func (c *WxCli) AuthUser(ctx context.Context, jsCode string) (*Codes, error) { - resp, err := low.AuthUser(ctx, c.Client, data.JsCodeReq{JsCode: jsCode}) + resp, err := low.AuthUser(ctx, c.Client, sns.JsCodeReq{JsCode: jsCode}) if err != nil { return nil, err }