适配POST模式传参,兼容serverchan模式传参

This commit is contained in:
zhiyang7 2021-08-30 10:57:10 +08:00
parent 2960280f51
commit afdefd0632
3 changed files with 27 additions and 14 deletions

View File

@ -3,3 +3,4 @@ module go/wecomchan
go 1.16
require github.com/go-redis/redis/v8 v8.10.0
require github.com/julienschmidt/httprouter v1.3.0

View File

@ -22,6 +22,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

View File

@ -15,6 +15,7 @@ import (
"time"
"github.com/go-redis/redis/v8"
"github.com/julienschmidt/httprouter"
)
/*------------------------------- 环境变量配置 begin -------------------------------*/
@ -236,22 +237,29 @@ func InitJsonData(msgType string) JsonData {
func main() {
// 设置日志内容显示文件名和行号
log.SetFlags(log.LstdFlags | log.Lshortfile)
wecomChan := func(res http.ResponseWriter, req *http.Request) {
wecomChan := func(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
_ = req.ParseForm()
sendkey := req.Form.Get("sendkey")
urlpath := ps.ByName("urlpath")
if len(sendkey) == 0 && len(urlpath) > 6 && urlpath != "wecomchan" {
sendkey = urlpath[1 : len(urlpath)-5]
}
if sendkey != Sendkey {
log.Panicln("sendkey 错误,请检查")
}
req.ParseForm()
msgContent := req.Form.Get("msg")
if len(msgContent) == 0 {
msgContent = req.Form.Get("title") + "\n" + req.Form.Get("desc")
}
msgType := req.Form.Get("msg_type")
log.Println("mes_type=", msgType)
// 默认mediaId为空
mediaId := ""
// 获取token
accessToken := GetAccessToken()
// 默认token有效
tokenValid := true
_ = req.ParseForm()
sendkey := req.FormValue("sendkey")
if sendkey != Sendkey {
log.Panicln("sendkey 错误,请检查")
}
msgContent := req.FormValue("msg")
msgType := req.FormValue("msg_type")
log.Println("mes_type=", msgType)
// 默认mediaId为空
mediaId := ""
if msgType != "image" {
log.Println("消息类型不是图片")
} else {
@ -297,6 +305,8 @@ func main() {
res.Header().Set("Content-type", "application/json")
_, _ = res.Write([]byte(postStatus))
}
http.HandleFunc("/wecomchan", wecomChan)
log.Fatal(http.ListenAndServe(":8080", nil))
router := httprouter.New()
router.GET("/*urlpath", wecomChan)
router.POST("/*urlpath", wecomChan)
log.Fatal(http.ListenAndServe(":8080", router))
}