2022-08-12 18:33:37 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-10 10:34:56 +08:00
|
|
|
"html/template"
|
2022-08-12 18:33:37 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
"blog.bing89.com/go/wechat/wechat"
|
2022-08-12 18:33:37 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-10-10 10:34:56 +08:00
|
|
|
appID = "wxc39301065b66300c"
|
|
|
|
secret = "7fee3a1cba8c555cf6c4caec4f05844c"
|
|
|
|
token = "qwertyuiopasdfghjkl"
|
|
|
|
Secret = "0a05cb757a9e4fdc9b1aac4f0406a3df"
|
|
|
|
Appid = "wx2fa7fb238aa6896a"
|
|
|
|
Encodingaeskey = "Lw2YDq9Svzhi5HeTzldcG5ID7rlgsJkQ5tDv8wwa092"
|
2022-08-12 18:33:37 +08:00
|
|
|
)
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
type Server struct {
|
|
|
|
w *wechat.Wechat
|
|
|
|
users map[string]*wechat.UserInfo
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func NewServer() (*Server, error) {
|
|
|
|
w, err := wechat.NewWechat(Appid, Secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := Server{
|
|
|
|
w: w,
|
|
|
|
users: make(map[string]*wechat.UserInfo),
|
|
|
|
}
|
|
|
|
return &s, nil
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func (s *Server) Check(c *gin.Context) {
|
|
|
|
me := &wechat.MessageEntity{}
|
2022-08-12 18:33:37 +08:00
|
|
|
me.Signature = c.Query("signature")
|
|
|
|
me.Timestamp = c.Query("timestamp")
|
|
|
|
me.Nonce = c.Query("nonce")
|
|
|
|
me.EchoStr = c.Query("echostr")
|
|
|
|
log.Println("signature:", me.Signature, "timestamp:", me.Timestamp, "noce:", me.Nonce, "echostr:", me.EchoStr)
|
|
|
|
if !me.CheckSignature(token) {
|
|
|
|
log.Println("token is not ok")
|
|
|
|
c.AbortWithError(http.StatusForbidden, fmt.Errorf("token invalid"))
|
|
|
|
return
|
|
|
|
}
|
2022-10-10 10:34:56 +08:00
|
|
|
c.Set("echostr", me.EchoStr)
|
2022-08-12 18:33:37 +08:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func cdata(data string) string {
|
2022-08-12 18:33:37 +08:00
|
|
|
return fmt.Sprintf(`<![CDATA[%s]]>`, data)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func (s *Server) handleGet(c *gin.Context) {
|
|
|
|
echoStr := c.GetString("echostr")
|
2022-08-12 18:33:37 +08:00
|
|
|
log.Println("token is ok")
|
2022-10-10 10:34:56 +08:00
|
|
|
c.String(http.StatusOK, echoStr)
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
2022-10-10 10:34:56 +08:00
|
|
|
|
|
|
|
//关注
|
|
|
|
func (s *Server) handlePost(c *gin.Context) {
|
|
|
|
data := wechat.FoucsRequest{}
|
2022-08-12 18:33:37 +08:00
|
|
|
err := c.BindXML(&data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
log.Println("data: ", data)
|
|
|
|
openID := c.Query("openid")
|
|
|
|
log.Println("openid is ", openID)
|
2022-10-10 10:34:56 +08:00
|
|
|
res, err := s.w.SendText(data.FromUserName, "welcome to xk.design.")
|
|
|
|
log.Printf("res: %s\nerr:%v", string(res), err)
|
|
|
|
ui, err := s.w.GetUserInfo(data.FromUserName)
|
|
|
|
if err == nil {
|
|
|
|
s.users[ui.OpenID] = ui
|
|
|
|
ui.Show()
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-10-10 10:34:56 +08:00
|
|
|
log.Printf("get ui err:%v", err)
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
2022-10-10 10:34:56 +08:00
|
|
|
c.String(http.StatusOK, "success")
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func (s *Server) handlePut(c *gin.Context) {
|
|
|
|
data := wechat.FoucsRequest{}
|
2022-08-12 18:33:37 +08:00
|
|
|
err := c.BindXML(&data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
log.Println("data: ", data)
|
|
|
|
c.String(http.StatusOK, "success")
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
func (s *Server) handleDelete(c *gin.Context) {
|
|
|
|
data := wechat.FoucsRequest{}
|
2022-08-12 18:33:37 +08:00
|
|
|
err := c.BindXML(&data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
log.Println("data: ", data)
|
|
|
|
c.String(http.StatusOK, "success")
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
//@router /auth/login
|
|
|
|
func (s *Server) login(c *gin.Context) {
|
|
|
|
code := c.Query("code")
|
|
|
|
if code == "" {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no code"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ticket, err := s.w.CreateQRCode()
|
|
|
|
if err != nil {
|
2022-08-12 18:33:37 +08:00
|
|
|
|
2022-10-10 10:34:56 +08:00
|
|
|
}
|
|
|
|
log.Printf("ticket: %s\nurl: %s\n err:%v\n", ticket.Ticket, ticket.URL, ticket.ExpireSeconds)
|
|
|
|
log.Println("==========> code is: ", code)
|
|
|
|
c.HTML(http.StatusOK, "next", gin.H{"next": template.HTML(ticket.URL)})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Template(name string) *template.Template {
|
|
|
|
tmpl := template.New(name)
|
|
|
|
tmpl.Parse(s.html())
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) html() string {
|
|
|
|
return `<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Login</title>
|
|
|
|
</head><body></body><script>window.location.href='{{.next}}?#wechat_redirect';</script></html>`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run(addr string) error {
|
2022-08-12 18:33:37 +08:00
|
|
|
engine := gin.Default()
|
2022-10-10 10:34:56 +08:00
|
|
|
engine.SetHTMLTemplate(s.Template("next"))
|
|
|
|
apiGroup := engine.Group("/api/wechat")
|
|
|
|
apiGroup.Use(s.Check)
|
|
|
|
apiGroup.GET("/signature", s.handleGet)
|
|
|
|
apiGroup.POST("/signature", s.handlePost)
|
|
|
|
apiGroup.PUT("/signature", s.handlePut)
|
|
|
|
apiGroup.DELETE("/signature", s.handleDelete)
|
|
|
|
authGroup := engine.Group("/auth")
|
|
|
|
authGroup.GET("/login", s.login)
|
|
|
|
|
|
|
|
return engine.Run(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc39301065b66300c&redirect_uri=http://106.55.59.210:15043/auth/login&response_type=code&scope=snsapi_userinfo#wechat_redirect
|
|
|
|
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc39301065b66300c&redirect_uri=http%3A%2F%2F106.55.59.210%3A15043%2Fauth%2Flogin&response_type=code&scope=snsapi_userinfo&connect_redirect=1#wechat_redirect
|
|
|
|
func main() {
|
|
|
|
s, err := NewServer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Run("0.0.0.0:15043")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-08-12 18:33:37 +08:00
|
|
|
}
|