-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
72 lines (65 loc) · 1.71 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package goi
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// 内置响应数据格式
type Data struct {
Status int `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type Request struct {
Object *http.Request
Context context.Context // 请求上下文
PathParams metaValues // 路由参数
QueryParams metaValues // 查询字符串参数
BodyParams metaValues // Body 传参
}
// 解析请求参数
func (request *Request) parseRequestParams() {
var err error
// 解析 Query 参数
for name, values := range request.Object.URL.Query() {
request.QueryParams[name] = append(request.QueryParams[name], values...)
}
// 解析 Body 参数
err = request.Object.ParseMultipartForm(32 << 20)
if err != nil {
err = request.Object.ParseForm()
if err != nil {
panic(fmt.Sprintf("解析 Body 错误: %v\n", err))
}
}
for name, values := range request.Object.Form {
request.BodyParams[name] = append(request.BodyParams[name], values...)
}
// 解析 json 数据
body, err := io.ReadAll(request.Object.Body)
if err != nil {
panic(fmt.Sprintf("读取 Body 错误: %v\n", err))
}
if len(body) != 0 {
jsonData := make(map[string]interface{})
err = json.Unmarshal(body, &jsonData)
if err != nil {
panic(fmt.Sprintf("解析 json 错误: %v\n", err))
}
for name, value := range jsonData {
stringValue, ok := value.(string)
if !ok {
// 如果无法转换为字符串,则使用 fmt.Sprint 将其转换为字符串
stringValue = fmt.Sprint(value)
}
request.BodyParams[name] = append(request.BodyParams[name], stringValue)
}
}
}
// 请求响应数据
type Response struct {
Status int
Data interface{}
}