mirror of
				https://github.com/bufanyun/hotgo.git
				synced 2025-11-04 08:13:45 +08:00 
			
		
		
		
	完善文档
This commit is contained in:
		@@ -28,8 +28,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 平台简介
 | 
					## 平台简介
 | 
				
			||||||
* 基于全新Go Frame 2+Vue3+Naive UI+UinApp开发的全栖框架,为二次开发而生,适合中小型完整应用开发。
 | 
					* 基于全新Go Frame 2+Vue3+Naive UI+uniapp开发的全栖框架,为二次开发而生,适合中小型完整应用开发。
 | 
				
			||||||
* 前端采用Naive-Ui-Admin、Vue、Naive UI、UinApp。
 | 
					* 前端采用Naive-Ui-Admin、Vue、Naive UI、uniapp。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 演示地址
 | 
					## 演示地址
 | 
				
			||||||
-  [https://hotgo.facms.cn/admin](https://hotgo.facms.cn/admin)
 | 
					-  [https://hotgo.facms.cn/admin](https://hotgo.facms.cn/admin)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								docs/guide-zh-CN/images/sys-middleware-com-response.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								docs/guide-zh-CN/images/sys-middleware-com-response.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 10 KiB  | 
@@ -4,7 +4,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
- 服务端
 | 
					- 服务端
 | 
				
			||||||
- web前端
 | 
					- web前端
 | 
				
			||||||
- uinapp端(待开放)
 | 
					- uniapp端(待开放)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### 服务端
 | 
					#### 服务端
 | 
				
			||||||
@@ -169,7 +169,7 @@
 | 
				
			|||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### uinapp端
 | 
					#### uniapp端
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
// 待开放
 | 
					// 待开放
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
@@ -73,9 +73,50 @@ func main()  {
 | 
				
			|||||||
- 文件路径:server/internal/logic/middleware/response.go
 | 
					- 文件路径:server/internal/logic/middleware/response.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### 常用响应类型
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- hotgo为一些常用的响应类型做了统一格式封装,例如:`application/json`、`text/xml`、`text/html`、`text/event-stream`等,默认使用`application/json`。
 | 
				
			||||||
 | 
					- 下面我们以`text/xml`为例简单演示几种使用方法:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. 当你使用规范化路由时,可直接在XxxRes结构体的`g.Meta`中声明响应类型:
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					type HelloReq struct {
 | 
				
			||||||
 | 
					    g.Meta `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
 | 
				
			||||||
 | 
					    Name   string `json:"name" d:"hotgo" dc:"名字"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type HelloRes struct {
 | 
				
			||||||
 | 
					    g.Meta `mime:"text/xml" type:"string"`
 | 
				
			||||||
 | 
					    Tips string `json:"tips"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2. 在响应前设置响应头:
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
					    Hello = cHello{}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type cHello struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *cHello) Hello(ctx context.Context, req *user.HelloReq) (res *user.HelloRes, err error) {
 | 
				
			||||||
 | 
					    r := ghttp.RequestFromCtx(ctx)
 | 
				
			||||||
 | 
					    r.Response.Header().Set("Content-Type", "text/xml")
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					    res = &user.HelloRes{
 | 
				
			||||||
 | 
					        Tips: fmt.Sprintf("hello %v, this is the api for %v applications.", req.Name, simple.AppName(ctx)),
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- 浏览器中访问响应内容如下:
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### 自定义响应
 | 
					#### 自定义响应
 | 
				
			||||||
- 由于响应中间件是全局的,并且是统一使用json格式进行响应的,但是在实际的开发中可能存在一些需要使用非json格式的响应,所以你需要进行单独的处理。
 | 
					- 在实际开发中,可能需要使用自定义的响应类型,由于响应中间件是全局的,因此您需要对其进行单独处理。
 | 
				
			||||||
- 推荐以下几种处理方式,可做参考:
 | 
					- 推荐以下几种处理方案,可做参考:
 | 
				
			||||||
1. 使用`ghttp.ExitAll()`,需要注意的是此方法会终止后续所有的http处理
 | 
					1. 使用`ghttp.ExitAll()`,需要注意的是此方法会终止后续所有的http处理
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```go
 | 
					```go
 | 
				
			||||||
@@ -102,6 +143,7 @@ func main()  {
 | 
				
			|||||||
2. 在`server/internal/logic/middleware/response.go`中根据请求的独有特征进行单独的处理,兼容后续http处理。
 | 
					2. 在`server/internal/logic/middleware/response.go`中根据请求的独有特征进行单独的处理,兼容后续http处理。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### 重写响应错误提示
 | 
					#### 重写响应错误提示
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- 在实际开发中,我们可能想要隐藏一些敏感错误,返回给客户端友好的错误提示,但开发者同时又想需要看到真实的敏感错误。对此hotgo已经进行了过滤处理,下面是一个简单的例子:
 | 
					- 在实际开发中,我们可能想要隐藏一些敏感错误,返回给客户端友好的错误提示,但开发者同时又想需要看到真实的敏感错误。对此hotgo已经进行了过滤处理,下面是一个简单的例子:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,6 @@ type NotifyQQPayReq struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type NotifyQQPayRes struct {
 | 
					type NotifyQQPayRes struct {
 | 
				
			||||||
	g.Meta `mime:"text/xml" type:"string" example:"<html/>"`
 | 
						g.Meta `mime:"text/xml" type:"string"`
 | 
				
			||||||
	payin.PayNotifyModel
 | 
						payin.PayNotifyModel
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,9 +10,9 @@ import (
 | 
				
			|||||||
	"github.com/gogf/gf/v2/net/ghttp"
 | 
						"github.com/gogf/gf/v2/net/ghttp"
 | 
				
			||||||
	"github.com/gogf/gf/v2/os/gctx"
 | 
						"github.com/gogf/gf/v2/os/gctx"
 | 
				
			||||||
	"github.com/gogf/gf/v2/os/gtime"
 | 
						"github.com/gogf/gf/v2/os/gtime"
 | 
				
			||||||
 | 
						"github.com/gogf/gf/v2/util/gconv"
 | 
				
			||||||
	"hotgo/internal/library/contexts"
 | 
						"hotgo/internal/library/contexts"
 | 
				
			||||||
	"hotgo/internal/model"
 | 
						"hotgo/internal/model"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// JsonExit 返回JSON数据并退出当前HTTP执行函数
 | 
					// JsonExit 返回JSON数据并退出当前HTTP执行函数
 | 
				
			||||||
@@ -21,6 +21,36 @@ func JsonExit(r *ghttp.Request, code int, message string, data ...interface{}) {
 | 
				
			|||||||
	r.Exit()
 | 
						r.Exit()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// RXml xml
 | 
				
			||||||
 | 
					func RXml(r *ghttp.Request, code int, message string, data ...interface{}) {
 | 
				
			||||||
 | 
						responseData := interface{}(nil)
 | 
				
			||||||
 | 
						if len(data) > 0 {
 | 
				
			||||||
 | 
							responseData = data[0]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						res := &model.Response{
 | 
				
			||||||
 | 
							Code:      code,
 | 
				
			||||||
 | 
							Message:   message,
 | 
				
			||||||
 | 
							Timestamp: gtime.Timestamp(),
 | 
				
			||||||
 | 
							TraceID:   gctx.CtxId(r.Context()),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果不是正常的返回,则将data转为error
 | 
				
			||||||
 | 
						if gcode.CodeOK.Code() == code {
 | 
				
			||||||
 | 
							res.Data = responseData
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							res.Error = responseData
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 清空响应
 | 
				
			||||||
 | 
						r.Response.ClearBuffer()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 写入响应
 | 
				
			||||||
 | 
						r.Response.WriteXml(gconv.Map(res))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 加入到上下文
 | 
				
			||||||
 | 
						contexts.SetResponse(r.Context(), res)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RJson 标准返回结果数据结构封装
 | 
					// RJson 标准返回结果数据结构封装
 | 
				
			||||||
func RJson(r *ghttp.Request, code int, message string, data ...interface{}) {
 | 
					func RJson(r *ghttp.Request, code int, message string, data ...interface{}) {
 | 
				
			||||||
	responseData := interface{}(nil)
 | 
						responseData := interface{}(nil)
 | 
				
			||||||
@@ -30,7 +60,7 @@ func RJson(r *ghttp.Request, code int, message string, data ...interface{}) {
 | 
				
			|||||||
	res := &model.Response{
 | 
						res := &model.Response{
 | 
				
			||||||
		Code:      code,
 | 
							Code:      code,
 | 
				
			||||||
		Message:   message,
 | 
							Message:   message,
 | 
				
			||||||
		Timestamp: time.Now().Unix(),
 | 
							Timestamp: gtime.Timestamp(),
 | 
				
			||||||
		TraceID:   gctx.CtxId(r.Context()),
 | 
							TraceID:   gctx.CtxId(r.Context()),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,6 @@ func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
 | 
				
			|||||||
		s.responseXml(r)
 | 
							s.responseXml(r)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	case consts.HTTPContentTypeStream:
 | 
						case consts.HTTPContentTypeStream:
 | 
				
			||||||
		// ...
 | 
					 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		responseJson(r)
 | 
							responseJson(r)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -56,16 +55,8 @@ func (s *sMiddleware) responseHtml(r *ghttp.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// responseXml xml响应
 | 
					// responseXml xml响应
 | 
				
			||||||
func (s *sMiddleware) responseXml(r *ghttp.Request) {
 | 
					func (s *sMiddleware) responseXml(r *ghttp.Request) {
 | 
				
			||||||
	code, message, resp := parseResponse(r)
 | 
						code, message, data := parseResponse(r)
 | 
				
			||||||
	r.Response.ClearBuffer()
 | 
						response.RXml(r, code, message, data)
 | 
				
			||||||
	r.Response.Write(`<?xml version="1.0" encoding="UTF-8"?>`)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	switch code {
 | 
					 | 
				
			||||||
	case gcode.CodeOK.Code():
 | 
					 | 
				
			||||||
		r.Response.WriteXml(g.Map{"code": code, "message": message, "data": resp})
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		r.Response.WriteXml(g.Map{"code": code, "message": message, "error": resp})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -127,7 +127,7 @@ viewer:
 | 
				
			|||||||
setting:
 | 
					setting:
 | 
				
			||||||
  title:       "HotGo"
 | 
					  title:       "HotGo"
 | 
				
			||||||
  keywords:    "中后台解决方案,gf框架,vue3"
 | 
					  keywords:    "中后台解决方案,gf框架,vue3"
 | 
				
			||||||
  description: "hotgo 是一个基于 goframe2,vue3,vite2,TypeScript,uinapp 的中后台解决方案,它可以帮助你快速搭建企业级中后台项目,相信不管是从新技术使用还是其他方面,都能帮助到你,持续更新中。"
 | 
					  description: "hotgo 是一个基于 goframe2,vue3,vite2,TypeScript,uniapp 的中后台解决方案,它可以帮助你快速搭建企业级中后台项目,相信不管是从新技术使用还是其他方面,都能帮助到你,持续更新中。"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 路由配置
 | 
					# 路由配置
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
  <div>
 | 
					  <div>
 | 
				
			||||||
    <div class="n-layout-page-header">
 | 
					    <div class="n-layout-page-header">
 | 
				
			||||||
      <n-card :bordered="false" title="关于">
 | 
					      <n-card :bordered="false" title="关于">
 | 
				
			||||||
        {{ name }} 是一个基于 goframe2,vue3,vite2,TypeScript,uinapp
 | 
					        {{ name }} 是一个基于 goframe2,vue3,vite2,TypeScript,uniapp
 | 
				
			||||||
        的中后台解决方案,它可以帮助你快速搭建企业级中后台项目,相信不管是从新技术使用还是其他方面,都能帮助到你,持续更新中。
 | 
					        的中后台解决方案,它可以帮助你快速搭建企业级中后台项目,相信不管是从新技术使用还是其他方面,都能帮助到你,持续更新中。
 | 
				
			||||||
      </n-card>
 | 
					      </n-card>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user