浏览代码

优化代码

shao 1 天之前
父节点
当前提交
a24e55bacf

+ 1 - 1
api/v1/config.go

@@ -23,7 +23,7 @@ func GetConfigOptions(c *gin.Context) {
 	flag := c.Query("flag")
 	enable := cast.ToBool(c.Query("enable"))
 
-	res, err := service.ResourceService.GetConfigOptionList(common.GC2GM(c), flag, enable)
+	res, err := service.ResourceClient.GetConfigOptionList(common.GC2GM(c), flag, enable)
 	if err != nil {
 		common.HttpErr(c, err)
 		return

+ 1 - 1
api/v1/dicom.go

@@ -13,7 +13,7 @@ import (
 func GenerateUniqueIdentifier(c *gin.Context) {
 	flag := c.Query("flag")
 	number := cast.ToInt32(c.Query("number"))
-	res, err := service.DcmtkService.GenerateUniqueIdentifier(common.GC2GM(c), flag, number)
+	res, err := service.DcmtkClient.GenerateUniqueIdentifier(common.GC2GM(c), flag, number)
 	if err != nil {
 		common.HttpErr(c, err)
 		return

+ 8 - 8
api/v1/protocol.go

@@ -9,8 +9,8 @@ import (
 	"auth-server/service"
 )
 
-func GetPatientType(c *gin.Context) {
-	res, err := service.ProtocolService.GetPatientType(common.GC2GM(c), common.IsEnabled(c))
+func GetPatientTypeList(c *gin.Context) {
+	res, err := service.ProtocolClient.GetPatientTypeList(common.GC2GM(c), common.IsEnabled(c))
 	if err != nil {
 		common.HttpErr(c, err)
 		return
@@ -18,9 +18,9 @@ func GetPatientType(c *gin.Context) {
 	common.HttpSuccess(c, res)
 }
 
-func GetBodyPart(c *gin.Context) {
+func GetBodyPartList(c *gin.Context) {
 
-	res, err := service.ProtocolService.GetBodyPart(common.GC2GM(c),
+	res, err := service.ProtocolClient.GetBodyPartList(common.GC2GM(c),
 		common.GetQueryString(c, "patient_type"),
 		common.GetQueryString(c, "modality"),
 		common.IsEnabled(c))
@@ -31,8 +31,8 @@ func GetBodyPart(c *gin.Context) {
 	common.HttpSuccess(c, res)
 }
 
-func GetProcedure(c *gin.Context) {
-	res, err := service.ProtocolService.GetProcedure(common.GC2GM(c),
+func GetProcedureList(c *gin.Context) {
+	res, err := service.ProtocolClient.GetProcedureList(common.GC2GM(c),
 		common.GetQueryString(c, "patient_type"),
 		common.GetQueryString(c, "body_part"),
 		common.IsEnabled(c))
@@ -43,8 +43,8 @@ func GetProcedure(c *gin.Context) {
 	common.HttpSuccess(c, res)
 }
 
-func GetView(c *gin.Context) {
-	res, err := service.ProtocolService.GetView(common.GC2GM(c),
+func GetViewList(c *gin.Context) {
+	res, err := service.ProtocolClient.GetViewList(common.GC2GM(c),
 		common.GetQueryString(c, "patient_type"),
 		common.GetQueryString(c, "body_part"),
 		common.GetQueryString(c, "procedure_id"),

+ 4 - 4
api/v1/public.go

@@ -14,10 +14,10 @@ func Ping(c *gin.Context) {
 }
 
 func SoftwareInfo(c *gin.Context) {
-	rssi := service.ResourceService.GetSoftwareInfo()
-	sssi := service.StudyService.GetSoftwareInfo()
-	pssi := service.ProtocolService.GetSoftwareInfo()
-	dssi := service.DcmtkService.GetSoftwareInfo()
+	rssi := service.ResourceClient.GetSoftwareInfo()
+	sssi := service.StudyClient.GetSoftwareInfo()
+	pssi := service.ProtocolClient.GetSoftwareInfo()
+	dssi := service.DcmtkClient.GetSoftwareInfo()
 	common.HttpSuccess(c, map[string]interface{}{
 		"server": map[string]interface{}{
 			"auth-server": map[string]interface{}{

+ 16 - 3
api/v1/study.go

@@ -4,16 +4,29 @@ import (
 	"auth-server/common"
 	pb "auth-server/rpc_idl/dr_study_pb"
 	"auth-server/service"
+	"fmt"
 	"github.com/gin-gonic/gin"
+	"google.golang.org/protobuf/encoding/protojson"
+	"net/http"
 )
 
 func CreateStudy(c *gin.Context) {
 	request := pb.StudyRequest{}
-	if err := c.ShouldBindJSON(&request); err != nil {
-		common.HttpErr(c, err)
+	reqBytes, err := c.GetRawData()
+	if err != nil {
+		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"})
+		return
+	}
+	unmarshaler := protojson.UnmarshalOptions{
+		AllowPartial:   true, // 允许部分解析,即使缺少必需字段
+		DiscardUnknown: true, // 丢弃 JSON 中 Protobuf 消息未定义的字段
+	}
+	err = unmarshaler.Unmarshal(reqBytes, &request)
+	if err != nil {
+		c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Failed to unmarshal JSON to Protobuf: %v", err)})
 		return
 	}
-	res, err := service.StudyService.CreateStudy(common.GC2GM(c), &request)
+	res, err := service.StudyClient.CreateStudy(common.GC2GM(c), &request)
 	if err != nil {
 		common.HttpErr(c, err)
 		return

+ 5 - 5
cmd/httpserver/server.go

@@ -66,11 +66,11 @@ func setup() {
 	models.SetupGorm(true)
 	//4. 初始化i18n
 	common.SetupTrans()
-	//5. 初始化service
-	service.ResourceService.Setup()
-	service.StudyService.Setup()
-	service.ProtocolService.Setup()
-	service.DcmtkService.Setup()
+	//5. 初始化client
+	service.ResourceClient.Setup()
+	service.StudyClient.Setup()
+	service.ProtocolClient.Setup()
+	service.DcmtkClient.Setup()
 
 	slog.Info(`starting api server`, "pid", os.Getpid())
 }

+ 4 - 11
common/rescode.go

@@ -1,7 +1,6 @@
 package common
 
 import (
-	"fmt"
 	"log/slog"
 )
 
@@ -35,8 +34,8 @@ var (
 	InvalidProductError  ResDesc = "ErrCode_InvalidProductError"
 
 	// 业务 错误
-	InvalidIdError ResDesc = "InvalidIdError"
-	FieldTypeError ResDesc = "FieldTypeError"
+	InvalidIdError ResDesc = "Error_InvalidIdError"
+	FieldTypeError ResDesc = "Error_FieldTypeError"
 )
 
 var (
@@ -98,19 +97,13 @@ func convertParamsToMap(params ...interface{}) map[string]interface{} {
 }
 
 func (p *ResStatus) SetParam(params ...interface{}) *ResStatus {
-	return &ResStatus{p.Code, p.Description, convertParamsToMap(params), p.Solution}
+	return &ResStatus{p.Code, p.Description, convertParamsToMap(params...), p.Solution}
 }
 
 func (p *ResStatus) Translator(localizer *i18n.Localizer) *ResStatus {
-	params := []string{}
-	if len(p.Params) > 0 {
-		for _, param := range p.Params {
-			params = append(params, fmt.Sprintf("%v", param))
-		}
-	}
 	t, err := localizer.Localize(&i18n.LocalizeConfig{
 		MessageID:    string(p.Description),
-		TemplateData: params,
+		TemplateData: p.Params,
 	})
 	if err != nil {
 		slog.Error("translator fail\n", "desc", p.Description, "err", err)

+ 1 - 1
common/response.go

@@ -71,7 +71,7 @@ func ErrToH(err interface{}, locale string) gin.H {
 		h = err.(*ResStatus).Translator(GetTrans(locale)).H()
 	case *json.UnmarshalTypeError:
 		utErr := err.(*json.UnmarshalTypeError)
-		h = InvalidParam.Desc(FieldTypeError).SetParam(utErr.Field, utErr.Type.String()).Translator(GetTrans(locale)).H()
+		h = InvalidParam.Desc(FieldTypeError).SetParam("Field", utErr.Field, "Type", utErr.Type.String(), "Value", utErr.Value).Translator(GetTrans(locale)).H()
 	case validator.ValidationErrors:
 		for _, fieldError := range err.(validator.ValidationErrors) {
 			return InvalidParam.Desc(ResDesc(fieldError.Error())).Translator(GetTrans(locale)).H()

+ 4 - 4
router/router.go

@@ -33,10 +33,10 @@ func InitRouter() *gin.Engine {
 		}
 		protocolV1 := authV1.Group("/protocol")
 		{
-			protocolV1.GET("patient_type", apiv1.GetPatientType)
-			protocolV1.GET("body_part", apiv1.GetBodyPart)
-			protocolV1.GET("procedure", apiv1.GetProcedure)
-			protocolV1.GET("view", apiv1.GetView)
+			protocolV1.GET("patient_type", apiv1.GetPatientTypeList)
+			protocolV1.GET("body_part", apiv1.GetBodyPartList)
+			protocolV1.GET("procedure", apiv1.GetProcedureList)
+			protocolV1.GET("view", apiv1.GetViewList)
 		}
 		studyV1 := authV1.Group("/study")
 		{

+ 1 - 1
rpc_idl

@@ -1 +1 @@
-Subproject commit b247d632b2cae8508da0642e7843868df389980a
+Subproject commit d8ae00be16f5956d36e997467d63134062a0cbab

+ 2 - 2
service/pb_dcmtk.go → service/pb_dcmtk_client.go

@@ -15,10 +15,10 @@ import (
 	pb "auth-server/rpc_idl/dr_dcmtk_pb"
 )
 
-var DcmtkService *Dcmtk
+var DcmtkClient *Dcmtk
 
 func init() {
-	DcmtkService = new(Dcmtk)
+	DcmtkClient = new(Dcmtk)
 }
 
 type Dcmtk struct {

+ 18 - 18
service/pb_protocol.go → service/pb_protocol_client.go

@@ -15,10 +15,10 @@ import (
 	pb "auth-server/rpc_idl/dr_protocol_pb"
 )
 
-var ProtocolService *Protocol
+var ProtocolClient *Protocol
 
 func init() {
-	ProtocolService = new(Protocol)
+	ProtocolClient = new(Protocol)
 }
 
 type Protocol struct {
@@ -55,54 +55,54 @@ func (s *Protocol) GetSoftwareInfo() *pb.SoftwareInfoReply {
 	return r
 }
 
-func (s *Protocol) GetPatientType(ctx context.Context, isEnabled *bool) (*pb.PatientTypeReply, error) {
-	slog.Info("[rpc]GetPatientType...")
-	r, err := s.protocolClient.GetPatientType(ctx, &pb.PatientTypeRequest{
+func (s *Protocol) GetPatientTypeList(ctx context.Context, isEnabled *bool) (*pb.PatientTypeReply, error) {
+	slog.Info("[rpc]GetPatientTypeList...")
+	r, err := s.protocolClient.GetPatientTypeList(ctx, &pb.PatientTypeRequest{
 		IsEnabled: isEnabled,
 	})
 	if err != nil {
 		slog.Error("[rpc]GetPatientType failed", "err", err)
 	}
-	slog.Info("[rpc]GetPatientType result:", "rows", len(r.PatientTypeList))
+	slog.Info("[rpc]GetPatientTypeList result:", "rows", len(r.PatientTypeList))
 	return r, err
 }
 
-func (s *Protocol) GetBodyPart(ctx context.Context, patientType *string, modality *string, isEnabled *bool) (*pb.BodyPartReply, error) {
-	slog.Info("[rpc]GetBodyPart...")
-	r, err := s.protocolClient.GetBodyPart(ctx, &pb.BodyPartRequest{
+func (s *Protocol) GetBodyPartList(ctx context.Context, patientType *string, modality *string, isEnabled *bool) (*pb.BodyPartReply, error) {
+	slog.Info("[rpc]GetBodyPartList...")
+	r, err := s.protocolClient.GetBodyPartList(ctx, &pb.BodyPartRequest{
 		PatientType: patientType,
 		Modality:    modality,
 		IsEnabled:   isEnabled,
 	})
 	if err != nil {
-		slog.Error("[rpc]GetBodyPart failed", "err", err)
+		slog.Error("[rpc]GetBodyPartList failed", "err", err)
 	}
 	return r, err
 }
 
-func (s *Protocol) GetProcedure(ctx context.Context, patientType *string, bodyPart *string, isEnabled *bool) (*pb.ProcedureReply, error) {
-	slog.Info("[rpc]GetProcedure...")
-	r, err := s.protocolClient.GetProcedure(ctx, &pb.ProcedureRequest{
+func (s *Protocol) GetProcedureList(ctx context.Context, patientType *string, bodyPart *string, isEnabled *bool) (*pb.ProcedureReply, error) {
+	slog.Info("[rpc]GetProcedureList...")
+	r, err := s.protocolClient.GetProcedureList(ctx, &pb.ProcedureRequest{
 		PatientType: patientType,
 		BodyPartId:  bodyPart,
 		IsEnabled:   isEnabled,
 	})
 	if err != nil {
-		slog.Error("[rpc]GetProcedure failed", "err", err)
+		slog.Error("[rpc]GetProcedureList failed", "err", err)
 	}
 	return r, err
 }
 
-func (s *Protocol) GetView(ctx context.Context, patientType *string, bodyPart *string, procedureID *string, isEnabled *bool) (*pb.ViewReply, error) {
-	slog.Info("[rpc]GetView...")
-	r, err := s.protocolClient.GetView(ctx, &pb.ViewRequest{
+func (s *Protocol) GetViewList(ctx context.Context, patientType *string, bodyPart *string, procedureID *string, isEnabled *bool) (*pb.ViewReply, error) {
+	slog.Info("[rpc]GetViewList...")
+	r, err := s.protocolClient.GetViewList(ctx, &pb.ViewRequest{
 		PatientType: patientType,
 		BodyPartId:  bodyPart,
 		ProcedureId: procedureID,
 		IsEnabled:   isEnabled,
 	})
 	if err != nil {
-		slog.Error("[rpc]GetView failed", "err", err)
+		slog.Error("[rpc]GetViewList failed", "err", err)
 	}
 	return r, err
 }

+ 2 - 2
service/pb_resource.go → service/pb_resource_client.go

@@ -15,10 +15,10 @@ import (
 	pb "auth-server/rpc_idl/dr_resource_pb"
 )
 
-var ResourceService *Resource
+var ResourceClient *Resource
 
 func init() {
-	ResourceService = new(Resource)
+	ResourceClient = new(Resource)
 }
 
 type Resource struct {

+ 4 - 3
service/pb_study.go → service/pb_study_client.go

@@ -15,10 +15,10 @@ import (
 	pb "auth-server/rpc_idl/dr_study_pb"
 )
 
-var StudyService *Study
+var StudyClient *Study
 
 func init() {
-	StudyService = new(Study)
+	StudyClient = new(Study)
 }
 
 type Study struct {
@@ -35,6 +35,7 @@ func (s *Study) Setup() {
 	slog.Info("study conn success", "conn", conn.GetState())
 
 	s.basicClient = pb.NewBasicClient(conn)
+	s.studyClient = pb.NewStudyClient(conn)
 }
 
 func (s *Study) GetSoftwareInfo() *pb.SoftwareInfoReply {
@@ -55,7 +56,7 @@ func (s *Study) GetSoftwareInfo() *pb.SoftwareInfoReply {
 }
 
 func (s *Study) CreateStudy(ctx context.Context, in *pb.StudyRequest) (*pb.StudyReply, error) {
-	r, err := s.studyClient.CreateStudy(context.Background(), in)
+	r, err := s.studyClient.CreateStudy(ctx, in)
 	if err != nil {
 		slog.Error("[rpc]CreateStudy failed", "err", err)
 		return r, err

+ 3 - 0
static/locale/common.en.toml

@@ -9,4 +9,7 @@ ErrCode_ExistsError = "Already Exists"
 ErrCode_NotAvailableError = "Not Available"
 ErrCode_UnknownError = "Service exception, please contact the administrator"
 
+Error_FieldTypeError = "json: cannot unmarshal {{.Value}} into Go struct field {{.Field}} of type {{.Type}}"
+
 ErrCode_InvalidUsernameOrPasswdError = "Invalid username or password"
+

+ 2 - 0
static/locale/common.zh.toml

@@ -9,4 +9,6 @@ ErrCode_ExistsError = "已存在"
 ErrCode_NotAvailableError = "不可用"
 ErrCode_UnknownError = "服务异常,请联系管理员"
 
+Error_FieldTypeError = "无法将{{.Value}}类型解码成{{.Field}}字段的{{.Type}}类型"
+
 ErrCode_InvalidUsernameOrPasswdError = "无效的用户名或密码"