From e92287bb626865b781298fa631333379984c35ae Mon Sep 17 00:00:00 2001 From: xiabin Date: Mon, 24 Feb 2025 11:37:19 +0800 Subject: [PATCH] 1 --- .../internal/logic/get_app_list_logic.go | 39 +++-- app/auth_service/auth/auth_service.pb.go | 164 ++++++++++++++---- app/auth_service/auth/auth_service_grpc.pb.go | 46 ++++- app/auth_service/auth_service.proto | 10 ++ app/auth_service/authservice/auth_service.go | 22 ++- .../internal/logic/code2_session_logic.go | 4 + .../logic/refresh_access_token_logic.go | 44 +++++ .../internal/server/auth_service_server.go | 6 + .../internal/svc/service_context.go | 11 +- .../internal/logic/get_ecpm_logic.go | 28 ++- go.sum | 3 +- 11 files changed, 319 insertions(+), 58 deletions(-) create mode 100644 app/auth_service/internal/logic/refresh_access_token_logic.go diff --git a/app/admin_service/internal/logic/get_app_list_logic.go b/app/admin_service/internal/logic/get_app_list_logic.go index af5ecbf..4a22b09 100644 --- a/app/admin_service/internal/logic/get_app_list_logic.go +++ b/app/admin_service/internal/logic/get_app_list_logic.go @@ -4,6 +4,9 @@ import ( "context" "encoding/json" "fmt" + "strconv" + + "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/admin_service/admin_service" "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/admin_service/internal/svc" @@ -45,8 +48,8 @@ type MergedAppInfo struct { Type string `json:"type,omitempty"` } -// 获取app列表 -func (l *GetAppListLogic) GetAppList() ([]*MergedAppInfo, error) { +// GetAppList 获取app列表 +func (l *GetAppListLogic) GetAppList(_ *admin_service.GetAppListRequest) (res *admin_service.GetAppListResponse, err error) { // 从ETCD获取ecpm配置 ecpmResp, err := l.svcCtx.EtcdClient.Get(l.ctx, "/youtu/ecpm/config") if err != nil { @@ -87,9 +90,9 @@ func (l *GetAppListLogic) GetAppList() ([]*MergedAppInfo, error) { // 合并app数据 for _, app := range appDatas { - if entry, exists := merged[app.AppID]; exists { - entry.AppSecret = app.AppSecret - entry.Type = app.Type + if info, exists := merged[app.AppID]; exists { + info.AppSecret = app.AppSecret + info.Type = app.Type } else { merged[app.AppID] = &MergedAppInfo{ AppID: app.AppID, @@ -99,11 +102,27 @@ func (l *GetAppListLogic) GetAppList() ([]*MergedAppInfo, error) { } } - // 转换为切片 - result := make([]*MergedAppInfo, 0, len(merged)) - for _, v := range merged { - result = append(result, v) + // 构建返回结果 + res = &admin_service.GetAppListResponse{ + AppList: make([]*admin_service.AppInfo, 0, len(merged)), } - return result, nil + // 将合并后的数据转换为响应格式 + for _, info := range merged { + appInfo := &admin_service.AppInfo{ + AppId: info.AppID, + Secret: info.AppSecret, + Ecpm: float32(info.ECPM), + Ipu: uint32(info.IPU), + } + + // 转换type为int32 + if typeInt, err := strconv.Atoi(info.Type); err == nil { + appInfo.Type = int32(typeInt) + } + + res.AppList = append(res.AppList, appInfo) + } + + return res, nil } diff --git a/app/auth_service/auth/auth_service.pb.go b/app/auth_service/auth/auth_service.pb.go index dd8fa77..2ff6c73 100644 --- a/app/auth_service/auth/auth_service.pb.go +++ b/app/auth_service/auth/auth_service.pb.go @@ -333,6 +333,94 @@ func (x *GetAccessTokenResponse) GetAccessToken() string { return "" } +type RefreshAccessTokenRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + AppId string `protobuf:"bytes,1,opt,name=AppId,proto3" json:"AppId,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RefreshAccessTokenRequest) Reset() { + *x = RefreshAccessTokenRequest{} + mi := &file_auth_service_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RefreshAccessTokenRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RefreshAccessTokenRequest) ProtoMessage() {} + +func (x *RefreshAccessTokenRequest) ProtoReflect() protoreflect.Message { + mi := &file_auth_service_proto_msgTypes[6] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RefreshAccessTokenRequest.ProtoReflect.Descriptor instead. +func (*RefreshAccessTokenRequest) Descriptor() ([]byte, []int) { + return file_auth_service_proto_rawDescGZIP(), []int{6} +} + +func (x *RefreshAccessTokenRequest) GetAppId() string { + if x != nil { + return x.AppId + } + return "" +} + +type RefreshAccessTokenResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + AccessToken string `protobuf:"bytes,1,opt,name=AccessToken,proto3" json:"AccessToken,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RefreshAccessTokenResponse) Reset() { + *x = RefreshAccessTokenResponse{} + mi := &file_auth_service_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RefreshAccessTokenResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RefreshAccessTokenResponse) ProtoMessage() {} + +func (x *RefreshAccessTokenResponse) ProtoReflect() protoreflect.Message { + mi := &file_auth_service_proto_msgTypes[7] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RefreshAccessTokenResponse.ProtoReflect.Descriptor instead. +func (*RefreshAccessTokenResponse) Descriptor() ([]byte, []int) { + return file_auth_service_proto_rawDescGZIP(), []int{7} +} + +func (x *RefreshAccessTokenResponse) GetAccessToken() string { + if x != nil { + return x.AccessToken + } + return "" +} + var File_auth_service_proto protoreflect.FileDescriptor var file_auth_service_proto_rawDesc = string([]byte{ @@ -364,23 +452,37 @@ var file_auth_service_proto_rawDesc = string([]byte{ 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, - 0x65, 0x6e, 0x32, 0xf9, 0x01, 0x0a, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, 0x2e, 0x61, 0x75, - 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, - 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x55, 0x0a, 0x0c, 0x43, 0x6f, - 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x2e, 0x61, 0x75, 0x74, - 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, - 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x22, 0x2e, + 0x65, 0x6e, 0x22, 0x31, 0x0a, 0x19, 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x41, 0x63, 0x63, + 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x14, 0x0a, 0x05, 0x41, 0x70, 0x70, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, + 0x41, 0x70, 0x70, 0x49, 0x64, 0x22, 0x3e, 0x0a, 0x1a, 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, + 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, + 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, + 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0xe2, 0x02, 0x0a, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x35, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x15, + 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x55, 0x0a, + 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x21, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x43, 0x6f, 0x64, - 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x5b, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, - 0x6b, 0x65, 0x6e, 0x12, 0x23, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, - 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x24, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, - 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, - 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, - 0x5a, 0x06, 0x2e, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x22, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, + 0x43, 0x6f, 0x64, 0x65, 0x32, 0x53, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x5b, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, + 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x23, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, + 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x24, 0x2e, 0x61, 0x75, + 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x63, + 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x67, 0x0a, 0x12, 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x41, 0x63, 0x63, 0x65, + 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x27, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x41, 0x63, + 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x28, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, + 0x52, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x54, 0x6f, 0x6b, + 0x65, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, + 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, }) var ( @@ -395,24 +497,28 @@ func file_auth_service_proto_rawDescGZIP() []byte { return file_auth_service_proto_rawDescData } -var file_auth_service_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_auth_service_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_auth_service_proto_goTypes = []any{ - (*Request)(nil), // 0: auth_service.Request - (*Response)(nil), // 1: auth_service.Response - (*Code2SessionRequest)(nil), // 2: auth_service.Code2SessionRequest - (*Code2SessionResponse)(nil), // 3: auth_service.Code2SessionResponse - (*GetAccessTokenRequest)(nil), // 4: auth_service.GetAccessTokenRequest - (*GetAccessTokenResponse)(nil), // 5: auth_service.GetAccessTokenResponse + (*Request)(nil), // 0: auth_service.Request + (*Response)(nil), // 1: auth_service.Response + (*Code2SessionRequest)(nil), // 2: auth_service.Code2SessionRequest + (*Code2SessionResponse)(nil), // 3: auth_service.Code2SessionResponse + (*GetAccessTokenRequest)(nil), // 4: auth_service.GetAccessTokenRequest + (*GetAccessTokenResponse)(nil), // 5: auth_service.GetAccessTokenResponse + (*RefreshAccessTokenRequest)(nil), // 6: auth_service.RefreshAccessTokenRequest + (*RefreshAccessTokenResponse)(nil), // 7: auth_service.RefreshAccessTokenResponse } var file_auth_service_proto_depIdxs = []int32{ 0, // 0: auth_service.auth_service.Ping:input_type -> auth_service.Request 2, // 1: auth_service.auth_service.Code2Session:input_type -> auth_service.Code2SessionRequest 4, // 2: auth_service.auth_service.GetAccessToken:input_type -> auth_service.GetAccessTokenRequest - 1, // 3: auth_service.auth_service.Ping:output_type -> auth_service.Response - 3, // 4: auth_service.auth_service.Code2Session:output_type -> auth_service.Code2SessionResponse - 5, // 5: auth_service.auth_service.GetAccessToken:output_type -> auth_service.GetAccessTokenResponse - 3, // [3:6] is the sub-list for method output_type - 0, // [0:3] is the sub-list for method input_type + 6, // 3: auth_service.auth_service.RefreshAccessToken:input_type -> auth_service.RefreshAccessTokenRequest + 1, // 4: auth_service.auth_service.Ping:output_type -> auth_service.Response + 3, // 5: auth_service.auth_service.Code2Session:output_type -> auth_service.Code2SessionResponse + 5, // 6: auth_service.auth_service.GetAccessToken:output_type -> auth_service.GetAccessTokenResponse + 7, // 7: auth_service.auth_service.RefreshAccessToken:output_type -> auth_service.RefreshAccessTokenResponse + 4, // [4:8] is the sub-list for method output_type + 0, // [0:4] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -429,7 +535,7 @@ func file_auth_service_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_auth_service_proto_rawDesc), len(file_auth_service_proto_rawDesc)), NumEnums: 0, - NumMessages: 6, + NumMessages: 8, NumExtensions: 0, NumServices: 1, }, diff --git a/app/auth_service/auth/auth_service_grpc.pb.go b/app/auth_service/auth/auth_service_grpc.pb.go index d448f91..26e041c 100644 --- a/app/auth_service/auth/auth_service_grpc.pb.go +++ b/app/auth_service/auth/auth_service_grpc.pb.go @@ -19,9 +19,10 @@ import ( const _ = grpc.SupportPackageIsVersion9 const ( - AuthService_Ping_FullMethodName = "/auth_service.auth_service/Ping" - AuthService_Code2Session_FullMethodName = "/auth_service.auth_service/Code2Session" - AuthService_GetAccessToken_FullMethodName = "/auth_service.auth_service/GetAccessToken" + AuthService_Ping_FullMethodName = "/auth_service.auth_service/Ping" + AuthService_Code2Session_FullMethodName = "/auth_service.auth_service/Code2Session" + AuthService_GetAccessToken_FullMethodName = "/auth_service.auth_service/GetAccessToken" + AuthService_RefreshAccessToken_FullMethodName = "/auth_service.auth_service/RefreshAccessToken" ) // AuthServiceClient is the client API for AuthService service. @@ -31,6 +32,8 @@ type AuthServiceClient interface { Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) Code2Session(ctx context.Context, in *Code2SessionRequest, opts ...grpc.CallOption) (*Code2SessionResponse, error) GetAccessToken(ctx context.Context, in *GetAccessTokenRequest, opts ...grpc.CallOption) (*GetAccessTokenResponse, error) + // 刷新AccessToken + RefreshAccessToken(ctx context.Context, in *RefreshAccessTokenRequest, opts ...grpc.CallOption) (*RefreshAccessTokenResponse, error) } type authServiceClient struct { @@ -71,6 +74,16 @@ func (c *authServiceClient) GetAccessToken(ctx context.Context, in *GetAccessTok return out, nil } +func (c *authServiceClient) RefreshAccessToken(ctx context.Context, in *RefreshAccessTokenRequest, opts ...grpc.CallOption) (*RefreshAccessTokenResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(RefreshAccessTokenResponse) + err := c.cc.Invoke(ctx, AuthService_RefreshAccessToken_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + // AuthServiceServer is the server API for AuthService service. // All implementations must embed UnimplementedAuthServiceServer // for forward compatibility. @@ -78,6 +91,8 @@ type AuthServiceServer interface { Ping(context.Context, *Request) (*Response, error) Code2Session(context.Context, *Code2SessionRequest) (*Code2SessionResponse, error) GetAccessToken(context.Context, *GetAccessTokenRequest) (*GetAccessTokenResponse, error) + // 刷新AccessToken + RefreshAccessToken(context.Context, *RefreshAccessTokenRequest) (*RefreshAccessTokenResponse, error) mustEmbedUnimplementedAuthServiceServer() } @@ -97,6 +112,9 @@ func (UnimplementedAuthServiceServer) Code2Session(context.Context, *Code2Sessio func (UnimplementedAuthServiceServer) GetAccessToken(context.Context, *GetAccessTokenRequest) (*GetAccessTokenResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method GetAccessToken not implemented") } +func (UnimplementedAuthServiceServer) RefreshAccessToken(context.Context, *RefreshAccessTokenRequest) (*RefreshAccessTokenResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method RefreshAccessToken not implemented") +} func (UnimplementedAuthServiceServer) mustEmbedUnimplementedAuthServiceServer() {} func (UnimplementedAuthServiceServer) testEmbeddedByValue() {} @@ -172,6 +190,24 @@ func _AuthService_GetAccessToken_Handler(srv interface{}, ctx context.Context, d return interceptor(ctx, in, info, handler) } +func _AuthService_RefreshAccessToken_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RefreshAccessTokenRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AuthServiceServer).RefreshAccessToken(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: AuthService_RefreshAccessToken_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AuthServiceServer).RefreshAccessToken(ctx, req.(*RefreshAccessTokenRequest)) + } + return interceptor(ctx, in, info, handler) +} + // AuthService_ServiceDesc is the grpc.ServiceDesc for AuthService service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -191,6 +227,10 @@ var AuthService_ServiceDesc = grpc.ServiceDesc{ MethodName: "GetAccessToken", Handler: _AuthService_GetAccessToken_Handler, }, + { + MethodName: "RefreshAccessToken", + Handler: _AuthService_RefreshAccessToken_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "auth_service.proto", diff --git a/app/auth_service/auth_service.proto b/app/auth_service/auth_service.proto index 6c95ae1..6ed2040 100644 --- a/app/auth_service/auth_service.proto +++ b/app/auth_service/auth_service.proto @@ -33,6 +33,13 @@ message GetAccessTokenResponse{ string AccessToken = 1; } +message RefreshAccessTokenRequest{ + string AppId = 1; +} + +message RefreshAccessTokenResponse{ + string AccessToken = 1; +} service auth_service { @@ -41,4 +48,7 @@ service auth_service { rpc Code2Session(Code2SessionRequest) returns(Code2SessionResponse); rpc GetAccessToken(GetAccessTokenRequest) returns(GetAccessTokenResponse); + + // 刷新AccessToken + rpc RefreshAccessToken(RefreshAccessTokenRequest) returns(RefreshAccessTokenResponse); } diff --git a/app/auth_service/authservice/auth_service.go b/app/auth_service/authservice/auth_service.go index be00814..78d4441 100644 --- a/app/auth_service/authservice/auth_service.go +++ b/app/auth_service/authservice/auth_service.go @@ -14,17 +14,21 @@ import ( ) type ( - Code2SessionRequest = auth.Code2SessionRequest - Code2SessionResponse = auth.Code2SessionResponse - GetAccessTokenRequest = auth.GetAccessTokenRequest - GetAccessTokenResponse = auth.GetAccessTokenResponse - Request = auth.Request - Response = auth.Response + Code2SessionRequest = auth.Code2SessionRequest + Code2SessionResponse = auth.Code2SessionResponse + GetAccessTokenRequest = auth.GetAccessTokenRequest + GetAccessTokenResponse = auth.GetAccessTokenResponse + RefreshAccessTokenRequest = auth.RefreshAccessTokenRequest + RefreshAccessTokenResponse = auth.RefreshAccessTokenResponse + Request = auth.Request + Response = auth.Response AuthService interface { Ping(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) Code2Session(ctx context.Context, in *Code2SessionRequest, opts ...grpc.CallOption) (*Code2SessionResponse, error) GetAccessToken(ctx context.Context, in *GetAccessTokenRequest, opts ...grpc.CallOption) (*GetAccessTokenResponse, error) + // 刷新AccessToken + RefreshAccessToken(ctx context.Context, in *RefreshAccessTokenRequest, opts ...grpc.CallOption) (*RefreshAccessTokenResponse, error) } defaultAuthService struct { @@ -52,3 +56,9 @@ func (m *defaultAuthService) GetAccessToken(ctx context.Context, in *GetAccessTo client := auth.NewAuthServiceClient(m.cli.Conn()) return client.GetAccessToken(ctx, in, opts...) } + +// 刷新AccessToken +func (m *defaultAuthService) RefreshAccessToken(ctx context.Context, in *RefreshAccessTokenRequest, opts ...grpc.CallOption) (*RefreshAccessTokenResponse, error) { + client := auth.NewAuthServiceClient(m.cli.Conn()) + return client.RefreshAccessToken(ctx, in, opts...) +} diff --git a/app/auth_service/internal/logic/code2_session_logic.go b/app/auth_service/internal/logic/code2_session_logic.go index d828c71..08037a3 100644 --- a/app/auth_service/internal/logic/code2_session_logic.go +++ b/app/auth_service/internal/logic/code2_session_logic.go @@ -56,6 +56,10 @@ func (l *Code2SessionLogic) Code2Session(in *auth.Code2SessionRequest) (*auth.Co return nil, err } auModel.UserID = &userId.UserId + err = au.WithContext(l.ctx).Save(auModel) + if err != nil { + return nil, err + } } else { userModel, err := l.svcCtx.UserManagerClient.FindById(l.ctx, &user.UserId{UserId: *auModel.UserID}) if err != nil { diff --git a/app/auth_service/internal/logic/refresh_access_token_logic.go b/app/auth_service/internal/logic/refresh_access_token_logic.go new file mode 100644 index 0000000..6fbf89a --- /dev/null +++ b/app/auth_service/internal/logic/refresh_access_token_logic.go @@ -0,0 +1,44 @@ +package logic + +import ( + "context" + "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/auth_service/auth" + "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/auth_service/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type RefreshAccessTokenLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewRefreshAccessTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshAccessTokenLogic { + return &RefreshAccessTokenLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// RefreshAccessToken 刷新AccessToken +func (l *RefreshAccessTokenLogic) RefreshAccessToken(in *auth.RefreshAccessTokenRequest) (*auth.RefreshAccessTokenResponse, error) { + //从redis中删除access_token + err := l.svcCtx.DeleteDWCache("douyin_openapi_access_token_" + in.AppId) + if err != nil { + return nil, err + } + + cli, err := l.svcCtx.Cli.Get(in.AppId) + if err != nil { + return nil, err + } + + accessToken, err := cli.GetAccessToken(l.ctx) + if err != nil { + return nil, err + } + + return &auth.RefreshAccessTokenResponse{AccessToken: accessToken}, nil +} diff --git a/app/auth_service/internal/server/auth_service_server.go b/app/auth_service/internal/server/auth_service_server.go index 90e5108..25f205f 100644 --- a/app/auth_service/internal/server/auth_service_server.go +++ b/app/auth_service/internal/server/auth_service_server.go @@ -37,3 +37,9 @@ func (s *AuthServiceServer) GetAccessToken(ctx context.Context, in *auth.GetAcce l := logic.NewGetAccessTokenLogic(ctx, s.svcCtx) return l.GetAccessToken(in) } + +// 刷新AccessToken +func (s *AuthServiceServer) RefreshAccessToken(ctx context.Context, in *auth.RefreshAccessTokenRequest) (*auth.RefreshAccessTokenResponse, error) { + l := logic.NewRefreshAccessTokenLogic(ctx, s.svcCtx) + return l.RefreshAccessToken(in) +} diff --git a/app/auth_service/internal/svc/service_context.go b/app/auth_service/internal/svc/service_context.go index 08f3768..37efb68 100644 --- a/app/auth_service/internal/svc/service_context.go +++ b/app/auth_service/internal/svc/service_context.go @@ -14,6 +14,7 @@ import ( "github.com/spf13/viper" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/discov" + "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/zrpc" clientv3 "go.etcd.io/etcd/client/v3" @@ -26,6 +27,7 @@ type ServiceContext struct { Cli cli2.Helper UserManagerClient userservice.UserService Query *query.Query + dwCache *cache.Redis } func NewServiceContext(c config.Config) *ServiceContext { @@ -75,7 +77,7 @@ const AppDataWatchKey = "/youtu/appData" func (svc *ServiceContext) InitClient() { - dwCache := redisCache.NewRedis(context.Background(), &redisCache.RedisOpts{Host: svc.Config.Redis[0].Host}) + svc.dwCache = redisCache.NewRedis(context.Background(), &redisCache.RedisOpts{Host: svc.Config.Redis[0].Host}) cli, err := clientv3.NewFromURL(viper.GetString(config.EtcdAddrKey)) if err != nil { @@ -93,7 +95,7 @@ func (svc *ServiceContext) InitClient() { logx.Errorf("etcd watch %s json.Unmarshal: %v", AppDataWatchKey, err) continue } - svc.SaveDW(appDataArr, dwCache) + svc.SaveDW(appDataArr, svc.dwCache) } }() @@ -115,3 +117,8 @@ func (svc *ServiceContext) SaveDW(arr []AppData, dwCache cache.Cache) { svc.Cli.Set(v.AppId, c) } } + +func (svc *ServiceContext) DeleteDWCache(appId string) (err error) { + err = svc.dwCache.Delete(appId) + return err +} diff --git a/app/ecpm_service/internal/logic/get_ecpm_logic.go b/app/ecpm_service/internal/logic/get_ecpm_logic.go index 8667c33..be6aae9 100644 --- a/app/ecpm_service/internal/logic/get_ecpm_logic.go +++ b/app/ecpm_service/internal/logic/get_ecpm_logic.go @@ -41,15 +41,30 @@ func (l *GetEcpmLogic) GetEcpm(in *ecpm.GetEcpmRequest) (response *ecpm.GetEcpmR } //获取抖音ECPM数据 - list, err := GetEcpmData(res.AccessToken, in.AppId, in.OpenId, time.Now().Format(time.DateOnly)) + list, errNo, err := GetEcpmData(res.AccessToken, in.AppId, in.OpenId, time.Now().Format(time.DateOnly)) if err != nil { - return nil, err + if errNo == 28001003 { + l.Logger.Infof("err:%s 重试", err.Error()) + rRes, err := l.svcCtx.AuthServiceClient.RefreshAccessToken(l.ctx, &auth.RefreshAccessTokenRequest{ + AppId: in.AppId, + }) + if err != nil { + return nil, err + } + list, errNo, err = GetEcpmData(rRes.AccessToken, in.AppId, in.OpenId, time.Now().Format(time.DateOnly)) + if err != nil { + return nil, err + } + } else { + return nil, err + } } + ipu := len(list) //计算ECPM具体值 ecpmVal := CalcEcpm(list) - l.Logger.Infof("user:%s,ecpm:%f", in.OpenId, ecpmVal) + l.Logger.Infof("appId:%s,user:%s,ecpm:%f,ipu:%d", in.AppId, in.OpenId, ecpmVal, ipu) //获取后端配置的ECPM值 config, ok := l.svcCtx.EcpmConfig.Get(in.AppId) @@ -90,8 +105,8 @@ func CalcEcpm(res []Record) (ecpm float64) { // appId: 小程序id // openId: 抖音openId // dateHour: 日期 -func GetEcpmData(accessToken, appId, openId, dateHour string) (list []Record, err error) { - list, err = GetEcpm(GetEcpmParams{ +func GetEcpmData(accessToken, appId, openId, dateHour string) (list []Record, errNo int, err error) { + list, errNo, err = GetEcpm(GetEcpmParams{ AppId: appId, OpenId: openId, AccessToken: accessToken, @@ -107,7 +122,7 @@ const getEcpm = "https://minigame.zijieapi.com/mgplatform/api/apps/data/get_ecpm // GetEcpm 获取ECPM // https://bytedance.larkoffice.com/docx/Vg4yd0RDSovZINxJDyIc6THhnod // 根据分页大小循环获取,聚合总数返回 -func GetEcpm(params GetEcpmParams) (list []Record, err error) { +func GetEcpm(params GetEcpmParams) (list []Record, errNo int, err error) { fullURL := fmt.Sprintf("%s?open_id=%s&mp_id=%s&access_token=%s&date_hour=%s&page_size=500&page_no=", getEcpm, params.OpenId, params.AppId, params.AccessToken, params.DateHour) for { fullURL += strconv.Itoa(params.PageNo) @@ -139,6 +154,7 @@ func GetEcpm(params GetEcpmParams) (list []Record, err error) { // 检查 API 是否返回错误 if apiResponse.ErrNo != 0 { + errNo = apiResponse.ErrNo err = fmt.Errorf("抖音 API 返回错误: %s (错误码: %d)", apiResponse.ErrMsg, apiResponse.ErrNo) return } diff --git a/go.sum b/go.sum index a766800..0fda924 100644 --- a/go.sum +++ b/go.sum @@ -452,9 +452,8 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.18 h1:mZPOYw4h8rTk7TeJ5+3udUkfVGBqc+GCjOJYd68 go.etcd.io/etcd/client/pkg/v3 v3.5.18/go.mod h1:BxVf2o5wXG9ZJV+/Cu7QNUiJYk4A29sAhoI5tIRsCu4= go.etcd.io/etcd/client/v2 v2.305.15 h1:VG2xbf8Vz1KJh65Ar2V5eDmfkp1bpzkSEHlhJM3usp8= go.etcd.io/etcd/client/v2 v2.305.15/go.mod h1:Ad5dRjPVb/n5yXgAWQ/hXzuXXkBk0Y658ocuXYaUU48= +go.etcd.io/etcd/client/v3 v3.5.15 h1:23M0eY4Fd/inNv1ZfU3AxrbbOdW79r9V9Rl62Nm6ip4= go.etcd.io/etcd/client/v3 v3.5.15/go.mod h1:CLSJxrYjvLtHsrPKsy7LmZEE+DK2ktfd2bN4RhBMwlU= -go.etcd.io/etcd/client/v3 v3.5.18 h1:nvvYmNHGumkDjZhTHgVU36A9pykGa2K4lAJ0yY7hcXA= -go.etcd.io/etcd/client/v3 v3.5.18/go.mod h1:kmemwOsPU9broExyhYsBxX4spCTDX3yLgPMWtpBXG6E= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=