package grpcHandler import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/domain/service" "finclip-app-manager/infrastructure/config" "finclip-app-manager/infrastructure/logger" pb "finclip-app-manager/infrastructure/protobuf/golang" "finclip-app-manager/infrastructure/utility" "strings" "gitlab.finogeeks.club/finclip-backend/apm" "net/http" ) var ( log = logger.GetLogger() ) type GrpcServer struct { } func (g GrpcServer) RuleEngineGetAppInfo(ctx context.Context, req *pb.RuleEngineGetAppInfoReq) (*pb.RuleEngineGetAppInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.RuleEngineGetAppInfo") defer span.End() svr := service.NewAppService() rspData, rsp := svr.RuntimeGetPubAppInfo(ctx, req.GetSdkKey(), req.GetAppId(), req.GetSdkVer()) log.Infof("RuleEngineGetAppInfo service rsp data:%+v,rsp:%v", rspData, rsp) result := pb.RuleEngineGetAppInfoRsp{} if rsp.ErrCode != utility.OK { utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } result.Data = service.CovertAppToPbAppInfoRsp(&rspData) utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } func (g GrpcServer) RuleEngineGetAppVerInfo(ctx context.Context, req *pb.RuleEngineGetAppVerInfoReq) (*pb.RuleEngineGetAppVerInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.RuleEngineGetAppVerInfo") defer span.End() svr := service.NewAppService() rspData, rsp := svr.RuleEngineGetAppVerInfo(ctx, req.AppId, int(req.GetVersion()), req.GetSdkKey(), req.GetSdkVer()) log.Infof("RuleEngineGetAppVerInfo service rsp data:%+v,rsp:%v", rspData, rsp) result := pb.RuleEngineGetAppVerInfoRsp{} if rsp.ErrCode != utility.OK { utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } result.Data = service.CovertAppToPbAppInfoRsp(&rspData) utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } func (g GrpcServer) OpenApiGetAppVerInfo(ctx context.Context, req *pb.OpenApiGetAppVerInfoReq) (*pb.OpenApiGetAppVerInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.OpenApiGetAppVerInfo") defer span.End() result := pb.OpenApiGetAppVerInfoRsp{} if req.GetAppId() == "" || req.GetSdkKey() == "" { log.Errorf("InternalGetAppVerInfo sdk key or appid empty,appId:%s,sdkKey:%s", req.GetAppId(), req.GetSdkKey()) switch { case req.GetAppId() == "": utility.MakeGrpcCommonResult(http.StatusBadRequest, utility.FS_LACK_OF_APPID, &result.Result) case req.GetSdkKey() == "": utility.MakeGrpcCommonResult(http.StatusBadRequest, utility.FS_LACK_OF_SDKKEY, &result.Result) default: utility.MakeGrpcCommonResult(http.StatusBadRequest, utility.FS_LACK_OF_APPID, &result.Result) } return &result, nil } //sdkKey白名单的校验 if !utility.InArry(req.SdkKey, config.WhiteSDKArry) { if config.Cfg.PublishEnv == entity.ENV_PRIVATE { svr := service.NewBindingService() //if !svr.CheckReviewBySdkKey(ctx, req.SdkKey) { checkStatus := svr.CheckReviewBySdkKey(ctx, req.SdkKey) if checkStatus != utility.OK { log.Errorf("RuntimeGetAppVersionInfo sdk not in review list,sdk-key:%s", req.SdkKey) utility.MakeGrpcCommonResult(http.StatusForbidden, checkStatus, &result.Result) return &result, nil } } else { log.Errorf("RuntimeGetAppVersionInfo sdk not in white sdk arry,sdk-key:%s", req.SdkKey) utility.MakeGrpcCommonResult(http.StatusForbidden, utility.FS_COOPERATION_TERMINATED, &result.Result) return &result, nil } } svr := service.NewAppService() rspData, rsp := svr.OpenApiGetAppVerInfo(ctx, req.GetAppId(), int(req.GetVersion()), req.GetSdkVer()) log.Infof("OpenApiGetAppVerInfo rsp data:%+v,rsp:%+v", rspData, rsp) if rsp.ErrCode != utility.OK { utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } result.Data = service.CovertInternalAppToPbAppInfoRsp(&rspData) result.Data.GroupName = g.ChangeGroupName(result.Data.GroupName) utility.MakeGrpcCommonResult(rsp.HttpStatus, rsp.ErrCode, &result.Result) return &result, nil } func (g GrpcServer) ChangeGroupName(groupName string) string { if len(groupName) == 18 && strings.Contains(groupName, "个人-") { phone := groupName[7:] if isOK := utility.IsMobile(phone); isOK { newName := "个人-" for i := 0; i < len(phone); i++ { if i >= 3 && i <= 6 { newName += "*" } else { newName += string(phone[i]) } } groupName = newName } } return groupName } func (g GrpcServer) GetOrganIdBySdkKey(ctx context.Context, req *pb.GetOrganIdBySdkKeyReq) (*pb.GetOrganIdBySdkKeyRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.GetOrganIdBySdkKey") defer span.End() rsp := pb.GetOrganIdBySdkKeyRsp{} rsp.Data = new(pb.GetOrganIdBySdkKeyRsp_DATA) rsp.Data.OrganId = "" rsp.Data.IsWhite = false if req.SdkKey == "" { log.Errorf("GetOrganIdBySdkKey sdk-key empty!!!") utility.MakeGrpcCommonResult(http.StatusBadRequest, utility.FS_LACK_OF_SDKKEY, &rsp.Result) return &rsp, nil } //sdkKey白名单的校验 if utility.InArry(req.SdkKey, config.WhiteSDKArry) { log.Errorf("GetOrganIdBySdkKey is white list,sdk key:%s", req.SdkKey) rsp.Data.IsWhite = true utility.MakeGrpcCommonResult(http.StatusOK, utility.OK, &rsp.Result) return &rsp, nil } //非白名单 //svr := service.NewBindingService() //t := db.NewTable(db.TableBinding) //result := model.Binding{} //err := t.GetOne(ctx, bson.M{"bundleInfos.SDKKey": bson.M{"$in": []string{req.SdkKey}}}, &result) //if err != nil { // log.Errorf("GetOrganIdBySdkKey get binding info error:%s", err.Error()) // common.MakeGrpcCommonResult(http.StatusInternalServerError, common.FS_SERVICE_UNAVAILABLE, &rsp.Result) // return &rsp, nil //} //rsp.Data.OrganId = result.GroupID //rsp.Data.IsWhite = false //log.Infof("GetOrganIdBySdkKey sdkKey:%s,result:%+v", req.SdkKey, result) //common.MakeGrpcCommonResult(http.StatusOK, common.OK, &rsp.Result) return &rsp, nil } func (g GrpcServer) BatchGetAppAndVerInfo(ctx context.Context, req *pb.BatchGetAppAndVerInfoReq) (*pb.BatchGetAppAndVerInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.BatchGetAppAndVerInfo") defer span.End() rsp := new(pb.BatchGetAppAndVerInfoRsp) rsp.Data = new(pb.BatchGetAppAndVerInfoRsp_DATA) rsp.Data.AppFailList = make([]string, 0) rsp.Data.AppVerFailList = make([]string, 0) rsp.Data.AppDataList = make([]*pb.AppInfoRspData, 0) rsp.Data.AppVerDataList = make([]*pb.AppInfoRspData, 0) log.Infof("BatchGetAppAndVerInfo req:%+v", req) //首先获取小程序详情 rs := service.NewAppService() for _, appId := range req.AppList { data, tempRsp := rs.RuntimeGetPubAppInfo(ctx, req.SdkKey, appId, req.SdkVer) if tempRsp.ErrCode != utility.OK { log.Errorf("RuleEngineBatchGetApp get info data:%v,resp:%+v", data, tempRsp) rsp.Data.AppFailList = append(rsp.Data.AppFailList, appId) continue } log.Debugf("RuleEngineBatchGetApp info:%+v", data.CustomData.SourceFile) log.Debugf("RuleEngineBatchGetApp info:%+v", utility.InterfaceToJsonString(data)) rsp.Data.AppDataList = append(rsp.Data.AppDataList, service.CovertAppToPbAppInfoRsp(&data)) } //获取小程序版本详情 for _, appVer := range req.AppVerList { rspData, tempRsp := rs.RuleEngineGetAppVerInfo(ctx, appVer.AppId, int(appVer.Sequence), req.SdkKey, req.SdkVer) if tempRsp.ErrCode != utility.OK { rsp.Data.AppVerFailList = append(rsp.Data.AppVerFailList, appVer.AppId) continue } rsp.Data.AppVerDataList = append(rsp.Data.AppVerDataList, service.CovertAppToPbAppInfoRsp(&rspData)) } utility.MakeGrpcCommonResult(http.StatusOK, utility.OK, &rsp.Result) log.Infof("grpc BatchGetAppAndVerInfo resp:", utility.InterfaceToJsonString(rsp)) return rsp, nil } func (g GrpcServer) OpenApiGetQRcodeAppVerInfo(ctx context.Context, req *pb.OpenApiGetQRcodeAppVerInfoReq) (*pb.OpenApiGetAppVerInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.OpenApiGetAppVerInfo") defer span.End() rsp := pb.OpenApiGetAppVerInfoRsp{} rsp.Data = new(pb.AppInfoRspData) if req.RedisKey == "" { log.Errorf("InternalGetAppVerInfo redisKey empty!!!") return &rsp, nil } if req.SdkKey == "" { log.Errorf("InternalGetAppVerInfo sdk-key empty!!!") return &rsp, nil } //sdkKey白名单的校验 if !utility.InArry(req.SdkKey, config.WhiteSDKArry) { if config.Cfg.PublishEnv == entity.ENV_PRIVATE { svr := service.NewBindingService() checkStatus := svr.CheckReviewBySdkKey(ctx, req.SdkKey) if checkStatus != utility.OK { log.Errorf("RuntimeGetAppVersionInfo sdk not in review list,sdk-key:%s", req.SdkKey) return &rsp, nil } } else { log.Errorf("RuntimeGetAppVersionInfo sdk not in white sdk arry,sdk-key:%s", req.SdkKey) return &rsp, nil } } rspInfo := service.InternalGetAppVerInfoRsp{} //dataBytes, err := cache.NewQRcodeAppCache().GetQRcodeAppInfo(ctx, req.RedisKey) //fmt.Println("dataBytes------------", dataBytes) //if err != nil { // if len(dataBytes) == 0 { // log.Debugln("qrcode is refresh:") // common.MakeGrpcCommonResult(http.StatusOK, common.FS_QRCODE_HAS_EXPIRE, &rsp.Result) // return &rsp, nil // // } else { // log.Errorf("redis error:", err.Error()) // return &rsp, nil // } //} //err = json.Unmarshal(dataBytes, &rspInfo) //if err != nil { // log.Errorf("json unmarshal error:", err.Error()) // return &rsp, nil //} rsp.Data.AppClass = rspInfo.AppClass rsp.Data.AppId = rspInfo.AppID rsp.Data.AppType = rspInfo.AppType rsp.Data.CoreDescription = rspInfo.CoreDescription rsp.Data.CorporationId = rspInfo.CorporationID rsp.Data.Created = rspInfo.Created rsp.Data.CreatedBy = rspInfo.CreatedBy rsp.Data.CustomData = service.ConvertRpcCustomDataToPb(rspInfo.CustomData) rsp.Data.DeveloperId = rspInfo.DeveloperID rsp.Data.GroupId = rspInfo.GroupID rsp.Data.GroupName = rspInfo.GroupName rsp.Data.InGrayRelease = rspInfo.InGrayRelease rsp.Data.Logo = rspInfo.Logo rsp.Data.Name = rspInfo.Name rsp.Data.Sequence = int32(rspInfo.Sequence) rsp.Data.Version = rspInfo.Version if &rspInfo.Status != nil { rsp.Data.Status = new(pb.Status) rsp.Data.Status.Value = rspInfo.Status.Value rsp.Data.Status.LastUpdated = rspInfo.Status.LastUpdated rsp.Data.Status.ModifiedBy = rspInfo.Status.ModifiedBy rsp.Data.Status.Reason = rspInfo.Status.Reason } rsp.Data.IsTemp = rspInfo.IsTemp rsp.Data.IsUserLimit = false rsp.Data.NeedCrt = rspInfo.NeedCrt rsp.Data.WechatLoginInfo = service.ConvertWechatLoginInfoToPb(&rspInfo.WechatLoginInfo) rsp.Data.PrivacySettingType = int32(rspInfo.PrivacySettingType) rsp.Data.ProjectType = int32(rspInfo.ProjectType) rsp.Data.AppTag = rspInfo.AppTag log.Debugln("appver info:", rspInfo, ",data:", rsp.Data) utility.MakeGrpcCommonResult(http.StatusOK, utility.OK, &rsp.Result) return &rsp, nil } func (g GrpcServer) GetBuildAppInfo(ctx context.Context, req *pb.GetBuildAppInfoReq) (*pb.GetBuildAppInfoRsp, error) { span, ctx := apm.ApmClient().CreateGRpcEntrySpan(ctx, "GrpcServer.GetBuildAppInfo") defer span.End() log.Infof("grpc GetBuildAppInfo req:%+v", req) svr := service.NewAppService() svrReq := apiproto.GetBuildAppInfoReq{ Type: req.GetType(), CodeId: req.GetCodeId(), } rspData, rsp := svr.GetBuildInfo(ctx, svrReq, req.GetSdkKey()) //buildInfo, svrRsp := svr.GetBuildInfo(ctx, svrReq, req.GetSdkKey()) log.Infof("grp GetBuildAppInfo rsp data:%+v,rsp:%+v", rspData, rsp) result := new(pb.GetBuildAppInfoRsp) result.Data = rspData utility.MakeGrpcCommonResultNoLoc(rsp.HttpStatus, rsp.ErrCode, rsp.ErrMsg, &result.Result) return result, nil }