浏览代码

完成response的文档编写

yjp 1 年之前
父节点
当前提交
d3f446bfc7
共有 2 个文件被更改,包括 120 次插入65 次删除
  1. 120 58
      framework/core/api/response/response.go
  2. 0 7
      framework/core/api/response/response_data.go

+ 120 - 58
framework/core/api/response/response.go

@@ -4,26 +4,42 @@ import (
 	"encoding/json"
 	"git.sxidc.com/go-framework/baize/framework/core/api"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
-	"git.sxidc.com/service-supports/websocket"
 	"github.com/pkg/errors"
 )
 
+// SendResponseFunc 发送响应函数
 type SendResponseFunc[O any] func(c *api.Context, statusCode int, data O, err error)
 
+// NoResponse 零值占位响应函数
 func NoResponse(_ *api.Context, _ int, _ any, _ error) {
 	return
 }
 
+// SendMsgResponse 发送MsgResponse的响应函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: 无效
+// - err: 错误
+// 返回值: 无
 func SendMsgResponse(c *api.Context, statusCode int, _ any, err error) {
 	msgResp := formMsgResponse(err)
 	c.JSON(statusCode, msgResp)
 }
 
+// IDResponse 响应ID的响应结构
 type IDResponse struct {
 	MsgResponse
 	ID string `json:"id"`
 }
 
+// SendIDResponse 发送ID响应的函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - id: 发送的id
+// - err: 错误
+// 返回值: 无
 func SendIDResponse(c *api.Context, statusCode int, id string, err error) {
 	msgResp := formMsgResponse(err)
 	c.JSON(statusCode, IDResponse{
@@ -32,11 +48,19 @@ func SendIDResponse(c *api.Context, statusCode int, id string, err error) {
 	})
 }
 
+// InfoResponse 响应Info的响应结构
 type InfoResponse[T any] struct {
 	MsgResponse
 	Info T `json:"info"`
 }
 
+// SendInfoResponse 发送Info响应的函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - info: 发送的Info
+// - err: 错误
+// 返回值: 无
 func SendInfoResponse[T any](c *api.Context, statusCode int, info T, err error) {
 	msgResp := formMsgResponse(err)
 	c.JSON(statusCode, InfoResponse[T]{
@@ -45,11 +69,28 @@ func SendInfoResponse[T any](c *api.Context, statusCode int, info T, err error)
 	})
 }
 
+// InfosResponse 响应Infos的响应结构
 type InfosResponse[T any] struct {
 	MsgResponse
 	InfosData[T]
 }
 
+// InfosData Infos响应的数据结构
+// 类型参数:
+// - T: Info结构类型
+type InfosData[T any] struct {
+	Infos      []T   `json:"infos"`
+	TotalCount int64 `json:"totalCount"`
+	PageNo     int   `json:"pageNo"`
+}
+
+// SendInfosResponse 发送Infos响应的函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: InfosData[T]结构,T是包含的Info类型
+// - err: 错误
+// 返回值: 无
 func SendInfosResponse[T any](c *api.Context, statusCode int, data InfosData[T], err error) {
 	msgResp := formMsgResponse(err)
 	c.JSON(statusCode, InfosResponse[T]{
@@ -58,6 +99,11 @@ func SendInfosResponse[T any](c *api.Context, statusCode int, data InfosData[T],
 	})
 }
 
+// StructToMap 将结构转换为map的函数(用于作为map响应函数的发送数据)
+// 参数:
+// - originStruct: 原始结构
+// 返回值:
+// - 转换为的map
 func StructToMap(originStruct any) map[string]any {
 	jsonBytes, err := json.Marshal(originStruct)
 	if err != nil {
@@ -73,6 +119,13 @@ func StructToMap(originStruct any) map[string]any {
 	return retMap
 }
 
+// SendMapResponse 发送map响应的函数(实际响应的是Json)
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: map响应数据
+// - err: 错误
+// 返回值: 无
 func SendMapResponse(c *api.Context, statusCode int, data map[string]any, err error) {
 	msgRespMap := formMapMsgResponse(err)
 	for key, value := range data {
@@ -82,63 +135,13 @@ func SendMapResponse(c *api.Context, statusCode int, data map[string]any, err er
 	c.JSON(statusCode, msgRespMap)
 }
 
-type MsgResponse struct {
-	Success bool   `json:"success"`
-	ErrCode int    `json:"errCode"`
-	Msg     string `json:"msg"`
-}
-
-func formMsgResponse(err error) MsgResponse {
-	if err != nil {
-		logger.GetInstance().Error(err)
-
-		return MsgResponse{
-			Success: false,
-			ErrCode: 1,
-			Msg:     err.Error(),
-		}
-	}
-
-	return MsgResponse{
-		Success: true,
-		ErrCode: 0,
-		Msg:     "操作成功",
-	}
-}
-
-func formMapMsgResponse(err error) map[string]any {
-	resp := make(map[string]any)
-	if err != nil {
-		logger.GetInstance().Error(err)
-
-		resp["success"] = false
-		resp["errCode"] = 1
-		resp["msg"] = err.Error()
-
-		return resp
-	}
-
-	resp["success"] = true
-	resp["errCode"] = 0
-	resp["msg"] = "操作成功"
-
-	return resp
-}
-
-func WSSendMessage(groupID string, obj any) {
-	message, err := json.Marshal(obj)
-	if err != nil {
-		logger.GetInstance().Error(errors.New(err.Error()))
-		return
-	}
-
-	err = websocket.GetInstance().BroadCast(groupID, message)
-	if err != nil {
-		logger.GetInstance().Error(errors.New(err.Error()))
-		return
-	}
-}
-
+// SendString 发送字符串响应的函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: 字符串响应数据
+// - err: 错误
+// 返回值: 无
 func SendString(c *api.Context, statusCode int, data string, err error) {
 	if err != nil {
 		c.String(statusCode, err.Error())
@@ -148,6 +151,13 @@ func SendString(c *api.Context, statusCode int, data string, err error) {
 	c.String(statusCode, data)
 }
 
+// WriteBytes 发送字节响应的函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: 字节响应数据
+// - err: 错误
+// 返回值: 无
 func WriteBytes(c *api.Context, statusCode int, bytes []byte, err error) {
 	if err != nil {
 		c.AbortWithStatus(statusCode)
@@ -164,11 +174,19 @@ func WriteBytes(c *api.Context, statusCode int, bytes []byte, err error) {
 	c.Writer.Flush()
 }
 
+// WXOrderResponse 微信订单响应结构
 type WXOrderResponse struct {
 	ReturnCode string `xml:"return_code"`
 	ReturnMsg  string `xml:"return_msg"`
 }
 
+// SendWXOrderResponse 微信订单响应函数
+// 参数:
+// - c: 上下文
+// - statusCode: HTTP状态码
+// - data: 无效
+// - err: 错误
+// 返回值: 无
 func SendWXOrderResponse(c *api.Context, statusCode int, _ any, err error) {
 	if err != nil {
 		c.XML(statusCode, &WXOrderResponse{
@@ -182,3 +200,47 @@ func SendWXOrderResponse(c *api.Context, statusCode int, _ any, err error) {
 		ReturnCode: "SUCCESS",
 	})
 }
+
+// MsgResponse 响应头
+type MsgResponse struct {
+	Success bool   `json:"success"`
+	ErrCode int    `json:"errCode"`
+	Msg     string `json:"msg"`
+}
+
+func formMsgResponse(err error) MsgResponse {
+	if err != nil {
+		logger.GetInstance().Error(err)
+
+		return MsgResponse{
+			Success: false,
+			ErrCode: 1,
+			Msg:     err.Error(),
+		}
+	}
+
+	return MsgResponse{
+		Success: true,
+		ErrCode: 0,
+		Msg:     "操作成功",
+	}
+}
+
+func formMapMsgResponse(err error) map[string]any {
+	resp := make(map[string]any)
+	if err != nil {
+		logger.GetInstance().Error(err)
+
+		resp["success"] = false
+		resp["errCode"] = 1
+		resp["msg"] = err.Error()
+
+		return resp
+	}
+
+	resp["success"] = true
+	resp["errCode"] = 0
+	resp["msg"] = "操作成功"
+
+	return resp
+}

+ 0 - 7
framework/core/api/response/response_data.go

@@ -1,7 +0,0 @@
-package response
-
-type InfosData[T any] struct {
-	Infos      []T   `json:"infos"`
-	TotalCount int64 `json:"totalCount"`
-	PageNo     int   `json:"pageNo"`
-}