/
httpserver_httprouter_negroni.go
executable file
·235 lines (183 loc) · 7.59 KB
/
httpserver_httprouter_negroni.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// httpserver.go
package main
import (
"fmt"
"net/http"
"log"
"github.com/yujinliang/wechat/mp"
"github.com/yujinliang/wechat/mp/request"
"github.com/yujinliang/wechat/mp/oauth2web"
"github.com/codegangsta/negroni"
"github.com/goincremental/negroni-sessions"
"github.com/goincremental/negroni-sessions/cookiestore"
"github.com/julienschmidt/httprouter"
)
const (
token = "weixin_@#$_x"
appId = "wx84c5988f9d70106d"
appSecret = "c888cc4fe859e212b2fa8642a3056861"
encodingAESKey = "yujinliangyujinliangyujinliangyujinliang123"
)
func HandleVoiceMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
//test menu.
//createMenu(wx)
//test oauth2
oauthConfig := oauth2web.NewOAuth2Config(wx.GetAppId(), wx.GetAppSecret(), "http://webapp.jinliangyu_weinxin_dev.tunnel.mobi/showuserinfo", "snsapi_userinfo")
oauthUrl := oauthConfig.AuthCodeURL("testOauth2")
//oClient := &oauth2web.Client{OAuth2Config:oauthConfig}
//oClient.CheckAccessTokenValid()
replyText := wx.ReplyText(oauthUrl, r)
w.Write([]byte(replyText))
}
func HandleTextMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("文本消息!", r)
//data, _ := wx.MakeEncryptResponse([]byte(replyText), timestamp, nonce)
w.Write([]byte(replyText))
//send custom message
//wx.PostText(r.FromUserName, "我是客服消息, 你好!", "")
}
func HandleImgeMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("图片消息!", r)
w.Write([]byte(replyText))
}
func HandleVideoMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("视频消息!", r)
w.Write([]byte(replyText))
}
func HandleLocationMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("位置消息!", r)
w.Write([]byte(replyText))
}
func HandleLinkMsg(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("link消息!", r)
w.Write([]byte(replyText))
}
func HandleSubscribeEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("订阅事件!", r)
w.Write([]byte(replyText))
}
func HandleUnSubscribeEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("取消订阅事件!", r)
w.Write([]byte(replyText))
}
func HandleScanEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("扫二维码事件!", r)
w.Write([]byte(replyText))
}
func HandleLocationEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("位置事件!", r)
w.Write([]byte(replyText))
}
func HandleMenuClickEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("菜单点击事件!", r)
w.Write([]byte(replyText))
}
func HandleMenuViewEvent(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
replyText := wx.ReplyText("打开网页事件!", r)
w.Write([]byte(replyText))
}
//test menu
func createMenu(wx *mp.WeiXin) {
menu := &mp.Menu{make([]mp.MenuButton,3)}
menu.Buttons[0].Name = "我要打七"
menu.Buttons[0].Type = mp.ButtonTypeView
menu.Buttons[0].Url = "https://mp.weixin.qq.com"
menu.Buttons[1].Name = "结缘法宝"
menu.Buttons[1].Type = mp.ButtonTypeView
menu.Buttons[1].Url = "https://mp.weixin.qq.com"
menu.Buttons[2].Name = "打七论坛"
menu.Buttons[2].SubButtons = make([]mp.MenuButton,1)
menu.Buttons[2].SubButtons[0].Name = "分享"
menu.Buttons[2].SubButtons[0].Type = mp.ButtonTypeClick
menu.Buttons[2].SubButtons[0].Key = "TestKey001"
err := wx.CreateMenu(menu)
if err != nil {
log.Println(err)
}
}
//-----------
//切换至不同的域名 start
type HostSwitch map[string]http.Handler
// Implement the ServerHTTP method on our new type
func (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check if a http.Handler is registered for the given host.
// If yes, use it to handle the request.
if handler := hs[r.Host]; handler != nil {
handler.ServeHTTP(w, r)
} else {
// Handle host names for wich no handler is registered
http.Error(w, r.Host, 403) // Or Redirect?
}
}
//切换至不同的域名 end
//微网站 start
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
session := sessions.GetSession(r)
iName := session.Get("name")
if iName != nil {
str, ok := iName.(string)
if ok && str == ps.ByName("name") {
fmt.Fprintf(w, "同主机,同名字,您又来了,%s", str)
} else {
session.Set("name", ps.ByName("name"))
fmt.Fprintf(w, "新来的的名字, %s", ps.ByName("name"))
}
} else {
session.Set("name", ps.ByName("name"))
fmt.Fprintf(w, "新来的主机, %s", ps.ByName("name"))
}
}
func ShowUserInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.ParseForm()
code := r.FormValue("code")
state := r.FormValue("state")
fmt.Fprintf(w, "code: %s, state: %s\n", code, state)
//get access token.
oauthConfig := oauth2web.NewOAuth2Config(appId, appSecret, "http://webapp.jinliangyu_weinxin_dev.tunnel.mobi/showuserinfo", "snsapi_userinfo")
oClient := &oauth2web.Client{OAuth2Config:oauthConfig}
oClient.ExchangeOAuth2AccessTokenByCode(code)
info, _ := oClient.UserInfo("zh_CN")
fmt.Fprintf(w, "openid:%s, nickname:%s, sex:%s, city:%s, province:%s, country:%s,UnionId:%s, HeadImageURL:%s, Privilege:%v", info.OpenId,info.Nickname, info.Sex, info.City, info.Province, info.Country, info.UnionId, info.HeadImageURL,info.Privilege )
}
//微网站 end
func main() {
//http.HandleFunc("/", InvalidateForWeiXin)
//err := http.ListenAndServe(":8080", nil)
wx := mp.New(token, "wx84c5988f9d70106d", "c888cc4fe859e212b2fa8642a3056861" ,"yujinliangyujinliangyujinliangyujinliang123", "")
wx.HandleFunc(mp.MsgTypeText, HandleTextMsg)
wx.HandleFunc(mp.MsgTypeVoice, HandleVoiceMsg)
wx.HandleFunc(mp.MsgTypeImage, HandleImgeMsg)
wx.HandleFunc(mp.MsgTypeVideo, HandleVideoMsg)
wx.HandleFunc(mp.MsgTypeLocation, HandleLocationMsg)
wx.HandleFunc(mp.MsgTypeLink, HandleLinkMsg)
//event.
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventSubscribe), HandleSubscribeEvent)
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventUnsubscribe), HandleUnSubscribeEvent)
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventScan), HandleScanEvent)
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventLocation), HandleLocationEvent)
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventClick), HandleMenuClickEvent)
wx.HandleFunc(mp.GenHttpRouteKey(mp.MsgTypeEvent, mp.EventView), HandleMenuViewEvent)
//mux bind.
router := httprouter.New()
router.GET("/hello/:name", Hello)
router.GET("/showuserinfo", ShowUserInfo)
//mux chain.
muxchain := make(HostSwitch)
muxchain["wechat.jinliangyu_weinxin_dev.tunnel.mobi"] = wx
muxchain["webapp.jinliangyu_weinxin_dev.tunnel.mobi"] = router
n := negroni.Classic()
store := cookiestore.New([]byte("secret123"))
n.Use(sessions.Sessions("my_session", store))
n.UseHandler(muxchain)
//create menu.
createMenu(wx)
//launch weixin http server.
n.Run(":8080")
//http.Handle("/wechat",wx)
//err := http.ListenAndServe(":8080", muxchain)
//if err != nil {
//
// log.Fatal("ListenAndServe: ", err)
//
//}
}