royalrick / weapp

微信小程序服务端 SDK (for Golang)
https://pkg.go.dev/github.com/medivhzhan/weapp/v3
Apache License 2.0
1.47k stars 281 forks source link

GET不响应 #61

Open JiajunDou opened 3 years ago

JiajunDou commented 3 years ago
package main

import (
    "fmt"
    "net/http"

    "github.com/medivhzhan/weapp/v3/server"
)

func main() {

    http.HandleFunc("/sub", func(w http.ResponseWriter, r *http.Request) {
        srv, err := server.NewServer("appid", "", "", "mchID", "apiKey", false, nil)
        if err != nil {
            fmt.Printf("init server error: %s", err)
        }

        // 当用户触发订阅消息弹框后
        srv.OnSubscribeMsgPopup(func(msg *server.SubscribeMsgPopupEvent) {

            // Do something cool ...

        })

        // 当用户通过设置界面改变订阅消息事件内容
        srv.OnSubscribeMsgChange(func(msg *server.SubscribeMsgChangeEvent) {
            // Do something cool ...

        })

        if err := srv.Serve(w, r); err != nil {
            fmt.Println(r)
            return
        }
    })

    http.ListenAndServe(":8082", nil)
}
JiajunDou commented 3 years ago
http.HandleFunc("/wechat/notify", func(w http.ResponseWriter, r *http.Request) {
        srv, err := server.NewServer("appid", "token", "aesKey", "mchID", "apiKey", false, nil)
        if err != nil {
            //lof.Fatalf("init server error: %s", err)
        }

        // 当用户触发订阅消息弹框后
        srv.OnSubscribeMsgPopup(func(msg *server.SubscribeMsgPopupEvent) {

            // Do something cool ...
            //return nil
        })

        // 当用户通过设置界面改变订阅消息事件内容
        srv.OnSubscribeMsgChange(func(msg *server.SubscribeMsgChangeEvent) {
            // Do something cool ...
            //return nil
        })

        if err := srv.Serve(w, r); err != nil {
            // 处理微信返回错误信息
            return
        }
        fmt.Println(w)
        fmt.Println(r.URL.Query())
    })
    http.ListenAndServe(":8083", nil)

GET signature=74fa2420b718af62bd92e3f84a32ffa09127027c&timestamp=1&nonce=1&echostr=1 伪造一段GET无响应

royalrick commented 3 years ago

如果开启服务器校验,那么GET请求会返回 query 中的 echostr。 否则不会返回任何消息。

JiajunDou commented 3 years ago

所以NewServer里面倒数第二个要把false改成true 时间很紧 读代码真心累