-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.go
110 lines (92 loc) · 1.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package wechat
import "net/http"
type Context interface {
Wechat() *Wechat
Request() Request
Response() Response
SetHandler(h Handler)
}
type Request interface {
ToUserName() string
FromUserName() string
CreateTime() int
MsgType() MsgType
Content() string
MsgId() int64
PicUrl() string
MediaId() string
Format() string
Recognition() string
ThumbMediaId() string
LocationX() float64
LocationY() float64
Scale() int
Label() string
Title() string
Description() string
Url() string
Event() string
EventKey() string
Ticket() string
Latitude() float32
Longitude() float32
Precision() float32
MenuId() int64
ScanCodeInfo() ScanCodeInfo
SendPicsInfo() SendPicsInfo
SendLocationInfo() SendLocationInfo
Status() string
}
type Response interface {
Success() error
String(s string) error
Bytes(b []byte) error
Response(data interface{}) error
Text(content string) error
Image(mediaID string) error
Voice(mediaID string) error
Video(video Video) error
Music(music Music) error
Article(articles ...ArticleItem) error
}
type context struct {
dft *defaultRequestMessage
r *http.Request
w http.ResponseWriter
wc *Wechat
dr defaultResponse
handler Handler
}
func newContext(w http.ResponseWriter, r *http.Request, wc *Wechat) (c *context) {
c = &context{
r: r,
w: w,
wc: wc,
dft: &defaultRequestMessage{},
}
c.dr = defaultResponse{c: c, w: w}
return
}
func (c *context) parse() (err error) {
data, err := c.wc.body(c.r)
if err != nil {
return
}
err = c.dft.Unmarshal(data)
return
}
func (c *context) Handler() Handler {
return c.handler
}
func (c *context) SetHandler(h Handler) {
c.handler = h
}
func (c *context) Request() Request {
return c.dft
}
func (c *context) Wechat() *Wechat {
return c.wc
}
func (c *context) Response() Response {
return c.dr
}