zhangmeng
2024-01-26 b835c4020a3a18a29e236d7a9ea031cc579410ea
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
73
package bhomeclient
 
import (
    "errors"
    jsoniter "github.com/json-iterator/go"
)
 
type Request struct {
    Path        string              `json:"path"`
    Method      string              `json:"method"`
    ContentType string              `json:"contentType"`
    HeaderMap   map[string][]string `json:"headerMap"`
    QueryMap    map[string][]string `json:"queryMap"`
    FormMap     map[string][]string `json:"formMap"`
    PostFormMap map[string][]string `json:"postFormMap"`
    Body        []byte              `json:"body"`
    File        FileArg             `json:"file"`
    MultiFiles  []FileArg           `json:"multiFiles"`
 
    SrcProc ProcInfo `json:"srcProc"` //请求来源进程
}
 
type FileArg struct {
    Name  string `json:"name"`
    Size  int64  `json:"size"`
    Bytes []byte `json:"bytes"`
}
 
type Reply struct {
    Success bool        `json:"success"`
    Msg     string      `json:"msg"`
    Data    interface{} `json:"data"`
}
 
func (r *Request) Header(key string) string {
    if values, ok := r.HeaderMap[key]; ok {
        return values[0]
    }
    return ""
}
 
func (r *Request) Query(key string) string {
    if values, ok := r.QueryMap[key]; ok {
        return values[0]
    }
    return ""
}
 
func (r *Request) PostForm(key string) string {
    if values, ok := r.PostFormMap[key]; ok {
        return values[0]
    }
    return ""
}
 
func (r *Request) BindJSON(v interface{}) error {
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    return json.Unmarshal(r.Body, &v)
}
 
func (r *Request) FormFile() (*FileArg, error) {
    if r.File.Name != "" && r.File.Size > 0 && r.File.Bytes != nil {
        return &r.File, nil
    }
    return nil, errors.New("file not found")
}
 
func (r *Request) FormFiles() (*[]FileArg, error) {
    if r.MultiFiles != nil && len(r.MultiFiles) > 0 {
        return &r.MultiFiles, nil
    }
    return nil, errors.New("multi files not found")
}