From 9a901a3c3da52cd8c46e37b4be7ce7c46ae8dbd1 Mon Sep 17 00:00:00 2001 From: sunjiayu <767978963@qq.com> Date: Sun, 15 Sep 2024 10:05:02 +0800 Subject: [PATCH] add migrate interface --- main.go | 26 ++++++++++++++++---------- service/platform/platform.go | 19 +++++++++++++++++++ service/wx_payment/wx_payment.go | 10 +++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 0194a8e..7bbed51 100644 --- a/main.go +++ b/main.go @@ -92,16 +92,6 @@ func run() error { if err != nil { return err } - - wxPaymentServer, err := wx_payment_service.WxPaymentServiceInitialize(&ctx) - if err != nil { - grpclog.Fatal("WxPaymentServiceInitialize failed error:", err) - return err - } - err = wx_payment_pb.RegisterWxPaymentServiceHandlerServer(ctx, mux, wxPaymentServer) - if err != nil { - return err - } // Generated routes end // Custom routes begin @@ -230,6 +220,22 @@ func run() error { grpclog.Fatalf("PlatformService RedisSRem HandlePath failed error:%+v", err) return err } + if err := mux.HandlePath("POST", "/platform/migrate", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { + platformServer.Migrate(&ctx, w, r) + }); err != nil { + grpclog.Fatalf("PlatformService RedisSRem HandlePath failed error:%+v", err) + return err + } + // 微信支付 + wxPaymentServer, err := wx_payment_service.WxPaymentServiceInitialize(&ctx, platformServer) + if err != nil { + grpclog.Fatal("WxPaymentServiceInitialize failed error:", err) + return err + } + err = wx_payment_pb.RegisterWxPaymentServiceHandlerServer(ctx, mux, wxPaymentServer) + if err != nil { + return err + } // 转发数据接口 forwardServer, err := platform.ForwardServiceInitialize(&ctx) diff --git a/service/platform/platform.go b/service/platform/platform.go index 5a59539..eab6e8d 100644 --- a/service/platform/platform.go +++ b/service/platform/platform.go @@ -315,3 +315,22 @@ func (server PlatformService) RedisSRem(ctx *context.Context, w http.ResponseWri w.Header().Set("Content-Type", "application/json") w.Write([]byte(resp)) } + +func (server PlatformService) Migrate(ctx *context.Context, w http.ResponseWriter, r *http.Request) { + grpclog.Infof("Migrate triggered") + key := "mikiai_whitelist_user" + whitelist := server.redisClient.SMembers(*ctx, key) + if whitelist == nil { + grpclog.Errorf("error exec smembers cmd") + return + } + openids := whitelist.Val() + for _, openid := range openids { + input := WhitelistUserData{OpenID: &openid} + server.WhitelistMySqlInsert(ctx, &input) + } + resp := fmt.Sprintf("{\"res\":%v}", 1) + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(resp)) +} diff --git a/service/wx_payment/wx_payment.go b/service/wx_payment/wx_payment.go index 7176b5a..3e5a16a 100644 --- a/service/wx_payment/wx_payment.go +++ b/service/wx_payment/wx_payment.go @@ -26,10 +26,11 @@ type WxPaymentServiceImpl struct { WxSerialNo string `yaml:"wx_serial_no"` RedisClient *redis.Client WxClient *core.Client + Platform *platform.PlatformService wx_payment.UnimplementedWxPaymentServiceServer } -func WxPaymentServiceInitialize(ctx *context.Context) (*WxPaymentServiceImpl, error) { +func WxPaymentServiceInitialize(ctx *context.Context, platform *platform.PlatformService) (*WxPaymentServiceImpl, error) { // load keys content, err := os.ReadFile(*authFile) if err != nil { @@ -75,6 +76,7 @@ func WxPaymentServiceInitialize(ctx *context.Context) (*WxPaymentServiceImpl, er }) server.WxClient = wxClient + server.Platform = platform return &server, nil } @@ -130,6 +132,12 @@ func (server WxPaymentServiceImpl) Jsapi(ctx context.Context, req *wx_payment.Js grpclog.Errorf("error exec smembers cmd") return &resp, nil } + // dbQueryData := platform.WhitelistUserData{OpenID: &openid} + // _, err := server.Platform.WhitelistMySqlQuery(&ctx, &dbQueryData) + // if err != nil { + // grpclog.Errorf("whitelist query openid: %v fail err:%v", dbQueryData.OpenID, err) + // return &resp, nil + // } if isMember.Val() { grpclog.Infof("openid:%v is in whitelist, order_type=3", openid) // Edit order db