|
@@ -7,21 +7,22 @@ import (
|
|
|
"github.com/astaxie/beego/context"
|
|
"github.com/astaxie/beego/context"
|
|
|
"github.com/yaotian/gowechat"
|
|
"github.com/yaotian/gowechat"
|
|
|
"github.com/yaotian/gowechat/mp/message"
|
|
"github.com/yaotian/gowechat/mp/message"
|
|
|
|
|
+ "github.com/yaotian/gowechat/mp/oauth"
|
|
|
"github.com/yaotian/gowechat/wxcontext"
|
|
"github.com/yaotian/gowechat/wxcontext"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var appURL = "http://localhost:8001"
|
|
var appURL = "http://localhost:8001"
|
|
|
|
|
+var config = wxcontext.Config{
|
|
|
|
|
+ AppID: "your app id",
|
|
|
|
|
+ AppSecret: "your app secret",
|
|
|
|
|
+ Token: "your token",
|
|
|
|
|
+ EncodingAESKey: "your encoding aes key",
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
func hello(ctx *context.Context) {
|
|
func hello(ctx *context.Context) {
|
|
|
//配置微信参数
|
|
//配置微信参数
|
|
|
- config := wxcontext.Config{
|
|
|
|
|
- AppID: "your app id",
|
|
|
|
|
- AppSecret: "your app secret",
|
|
|
|
|
- Token: "your token",
|
|
|
|
|
- EncodingAESKey: "your encoding aes key",
|
|
|
|
|
- }
|
|
|
|
|
wc := gowechat.NewWechat(config)
|
|
wc := gowechat.NewWechat(config)
|
|
|
-
|
|
|
|
|
|
|
+ //微信平台mp
|
|
|
mp, err := wc.MpMgr()
|
|
mp, err := wc.MpMgr()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return
|
|
return
|
|
@@ -29,10 +30,9 @@ func hello(ctx *context.Context) {
|
|
|
|
|
|
|
|
// 传入request和responseWriter
|
|
// 传入request和responseWriter
|
|
|
msgHandler := mp.GetMsgHandler(ctx.Request, ctx.ResponseWriter)
|
|
msgHandler := mp.GetMsgHandler(ctx.Request, ctx.ResponseWriter)
|
|
|
- fmt.Println("msgHandler:", msgHandler)
|
|
|
|
|
|
|
+
|
|
|
//设置接收消息的处理方法
|
|
//设置接收消息的处理方法
|
|
|
msgHandler.SetHandleMessageFunc(func(msg message.MixMessage) *message.Reply {
|
|
msgHandler.SetHandleMessageFunc(func(msg message.MixMessage) *message.Reply {
|
|
|
-
|
|
|
|
|
//回复消息:演示回复用户发送的消息
|
|
//回复消息:演示回复用户发送的消息
|
|
|
text := message.NewText(msg.Content)
|
|
text := message.NewText(msg.Content)
|
|
|
return &message.Reply{message.MsgTypeText, text}
|
|
return &message.Reply{message.MsgTypeText, text}
|
|
@@ -42,31 +42,38 @@ func hello(ctx *context.Context) {
|
|
|
err = msgHandler.Handle()
|
|
err = msgHandler.Handle()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
fmt.Println(err)
|
|
|
- return
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//wxOAuth 微信公众平台,网页授权
|
|
//wxOAuth 微信公众平台,网页授权
|
|
|
func wxOAuth(ctx *context.Context) {
|
|
func wxOAuth(ctx *context.Context) {
|
|
|
//配置微信参数
|
|
//配置微信参数
|
|
|
- config := wxcontext.Config{
|
|
|
|
|
- AppID: "your app id",
|
|
|
|
|
- AppSecret: "your app secret",
|
|
|
|
|
- Token: "your token",
|
|
|
|
|
- EncodingAESKey: "your encoding aes key",
|
|
|
|
|
- }
|
|
|
|
|
wc := gowechat.NewWechat(config)
|
|
wc := gowechat.NewWechat(config)
|
|
|
-
|
|
|
|
|
|
|
+ //微信公众平台
|
|
|
mp, err := wc.MpMgr()
|
|
mp, err := wc.MpMgr()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
oauthHandler := mp.GetPageOAuthHandler(ctx.Request, ctx.ResponseWriter, appURL+"/oauth")
|
|
oauthHandler := mp.GetPageOAuthHandler(ctx.Request, ctx.ResponseWriter, appURL+"/oauth")
|
|
|
|
|
+
|
|
|
|
|
+ oauthHandler.SetFuncCheckOpenIDExisting(func(openID string) bool {
|
|
|
|
|
+ //看自己的系统中是否已经存在此openID的用户
|
|
|
|
|
+ //如果已经存在, 调用自己的Login 方法,设置cookie等,return true
|
|
|
|
|
+ //如果还不存在,return false, handler会自动去取用户信息
|
|
|
|
|
+ return true
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ oauthHandler.SetFuncAfterGetUserInfo(func(user oauth.UserInfo) {
|
|
|
|
|
+ //已获得用户信息,这里用信息做注册使用
|
|
|
|
|
+ //调用自己的Login方法,设置cookie等
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
oauthHandler.Handle()
|
|
oauthHandler.Handle()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
|
beego.Any("/", hello)
|
|
beego.Any("/", hello)
|
|
|
- beego.Any("/oauth", wxOAuth)
|
|
|
|
|
|
|
+ beego.Any("/oauth", wxOAuth) //需要网页授权的页面url /oauth?target=url
|
|
|
beego.Run(":8001")
|
|
beego.Run(":8001")
|
|
|
}
|
|
}
|