From afdefd0632fcf067da50222390755a3a3ac0c0ed Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Mon, 30 Aug 2021 10:57:10 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=80=82=E9=85=8DPOST=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=EF=BC=8C=E5=85=BC=E5=AE=B9serverchan?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=BC=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/go.mod | 1 + go-wecomchan/go.sum | 2 ++ go-wecomchan/wecomchan.go | 32 +++++++++++++++++++++----------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/go-wecomchan/go.mod b/go-wecomchan/go.mod index 388cf93..88016bf 100644 --- a/go-wecomchan/go.mod +++ b/go-wecomchan/go.mod @@ -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 diff --git a/go-wecomchan/go.sum b/go-wecomchan/go.sum index 97595ea..44249e0 100644 --- a/go-wecomchan/go.sum +++ b/go-wecomchan/go.sum @@ -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= diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index d808b7a..ba96920 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -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) { - // 获取token - accessToken := GetAccessToken() - // 默认token有效 - tokenValid := true - + wecomChan := func(res http.ResponseWriter, req *http.Request, ps httprouter.Params) { _ = req.ParseForm() - sendkey := req.FormValue("sendkey") + 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 错误,请检查") } - msgContent := req.FormValue("msg") - msgType := req.FormValue("msg_type") + 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 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)) } From f9b71fa7331683ff08d6a222bf57f925e597b8bd Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Mon, 30 Aug 2021 11:19:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8text?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/wecomchan.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index ba96920..a6a4368 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -253,6 +253,9 @@ func main() { msgContent = req.Form.Get("title") + "\n" + req.Form.Get("desc") } msgType := req.Form.Get("msg_type") + if len(msgType) == 0 { + msgType = "text" + } log.Println("mes_type=", msgType) // 默认mediaId为空 mediaId := "" From 808cc276e80da50d8649e9394f9fe40bf91ca3e9 Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Mon, 30 Aug 2021 11:24:46 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/wecomchan.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index a6a4368..bdade89 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -12,6 +12,7 @@ import ( "net/http" "os" "reflect" + "strings" "time" "github.com/go-redis/redis/v8" @@ -250,7 +251,11 @@ func main() { req.ParseForm() msgContent := req.Form.Get("msg") if len(msgContent) == 0 { - msgContent = req.Form.Get("title") + "\n" + req.Form.Get("desc") + title := req.Form.Get("title") + text := req.Form.Get("text") + desp := req.Form.Get("desp") + msgContent = strings.Join([]string{title, text, desp}, "\n") + log.Println("msgContent=", msgContent) } msgType := req.Form.Get("msg_type") if len(msgType) == 0 { From be8576cbcd2fcf80c55f2b85391d589d5ba816e5 Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Mon, 30 Aug 2021 15:26:52 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/wecomchan.go | 1 - 1 file changed, 1 deletion(-) diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index bdade89..40cfe49 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -248,7 +248,6 @@ func main() { if sendkey != Sendkey { log.Panicln("sendkey 错误,请检查") } - req.ParseForm() msgContent := req.Form.Get("msg") if len(msgContent) == 0 { title := req.Form.Get("title") From b9f603452a209412768d240800a2627121d9bd29 Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Tue, 31 Aug 2021 09:19:34 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=B8=8D=E5=85=A8=E6=97=B6=E4=BC=9A=E4=BA=A7=E7=94=9F=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E7=9A=84=E6=8D=A2=E8=A1=8C=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/wecomchan.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index 40cfe49..f68e728 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -250,11 +250,24 @@ func main() { } msgContent := req.Form.Get("msg") if len(msgContent) == 0 { + textArray := make([]string, 3) + lenOfArr := 0 title := req.Form.Get("title") + if (len(title)) > 0 { + textArray[lenOfArr] = title + lenOfArr++ + } text := req.Form.Get("text") + if (len(text)) > 0 { + textArray[lenOfArr] = text + lenOfArr++ + } desp := req.Form.Get("desp") - msgContent = strings.Join([]string{title, text, desp}, "\n") - log.Println("msgContent=", msgContent) + if (len(desp)) > 0 { + textArray[lenOfArr] = desp + lenOfArr++ + } + msgContent = strings.Join(textArray[0:lenOfArr], "\n") } msgType := req.Form.Get("msg_type") if len(msgType) == 0 {