/
wechat_controller.go
64 lines (56 loc) · 1.43 KB
/
wechat_controller.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
package controllers
import (
"crypto/sha1"
"fmt"
"github.com/astaxie/beego"
wx "github.com/ckeyer/beewechat/wechat"
"io"
"log"
// "net/http"
"sort"
)
type WeChatController struct {
beego.Controller
}
func (this *WeChatController) Get() {
//Start 微信服务器认证部分
signature := this.GetString("signature")
timestamp := this.GetString("timestamp")
nonce := this.GetString("nonce")
echostr := this.GetString("echostr")
tmps := []string{config.WECHAT_TOKEN, timestamp, nonce}
sort.Strings(tmps)
tmpStr := tmps[0] + tmps[1] + tmps[2]
tmp := func(data string) string {
t := sha1.New()
io.WriteString(t, data)
return fmt.Sprintf("%x", t.Sum(nil))
}(tmpStr)
if tmp == signature {
this.Ctx.WriteString(echostr)
return
}
//Over 微信服务器认证部分
//Start 微信网页认证部分
code := this.GetString("code")
log.Println(this.GetString("code"))
log.Println(this.GetString("status"))
if code != "" {
wat := wx.GetWebAccessToken(code)
u := wat.GetUserInfo()
u.Insert()
}
//Over 微信网页认证部分
// this.Data["QRImgUrl"] = wx.GetTempTicket(120, 1214, "FUNX_HOME")
// this.Data["PageTitle"] = "FANX-HOME"
// this.Data["Website"] = "beego.me"
// this.Data["Email"] = "astaxie@gmail.com"
// this.TplNames = ".tpl"
}
func (this *WeChatController) Post() {
s := fmt.Sprintf("%s", this.Ctx.Input.CopyBody())
// log.Println(s)
r := wx.ReceiveMsg(s)
// log.Println(r)
this.Ctx.WriteString(r)
}