Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8561b0

Browse files
committed
微信接入
1 parent 36f9d04 commit b8561b0

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

src/http/controller/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ func RegisterRoutes(g *echo.Group) {
3434
new(OAuthController).RegisterRoute(g)
3535
new(WebsocketController).RegisterRoute(g)
3636

37+
new(WechatController).RegisterRoute(g)
38+
3739
new(InstallController).RegisterRoute(g)
3840
}

src/http/controller/wechat.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of self source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package controller
8+
9+
import (
10+
"fmt"
11+
"io/ioutil"
12+
"logic"
13+
"net/http"
14+
15+
"github.com/labstack/echo"
16+
)
17+
18+
type WechatController struct{}
19+
20+
// 注册路由
21+
func (self WechatController) RegisterRoute(g *echo.Group) {
22+
g.Any("/wechat/autoreply", self.AutoReply)
23+
}
24+
25+
func (self WechatController) AutoReply(ctx echo.Context) error {
26+
fmt.Printf("form values:%+v\n", ctx.FormParams())
27+
fmt.Printf("query values:%+v\n", ctx.QueryParams())
28+
29+
// 配置微信(不校验,直接返回成功)
30+
if ctx.QueryParam("echostr") != "" {
31+
return ctx.String(http.StatusOK, ctx.QueryParam("echostr"))
32+
}
33+
34+
body, err := ioutil.ReadAll(ctx.Request().Body())
35+
if err != nil {
36+
return ctx.String(http.StatusOK, "failure")
37+
}
38+
39+
if len(body) == 0 {
40+
return ctx.String(http.StatusOK, "没有获取到消息内容")
41+
}
42+
43+
respContent, err := logic.DefaultWechat.AutoReply(ctx, body)
44+
if err != nil {
45+
return ctx.String(http.StatusOK, "对不起服务错误!")
46+
}
47+
48+
return ctx.String(http.StatusOK, respContent)
49+
}

src/logic/rank.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ func (self RankLogic) FindDAURank(ctx context.Context, num int, ymds ...string)
155155
weights = append(weights, weight)
156156
}
157157

158+
if len(uids) == 0 {
159+
return nil
160+
}
161+
158162
userMap := DefaultUser.FindDAUUsers(ctx, uids)
159163
users := make([]*model.User, len(userMap))
160164
for i, uid := range uids {

src/logic/wechat.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
package logic
8+
9+
import (
10+
"encoding/xml"
11+
"fmt"
12+
"model"
13+
"strings"
14+
15+
"golang.org/x/net/context"
16+
17+
"github.com/polaris1119/config"
18+
)
19+
20+
type WechatLogic struct{}
21+
22+
var DefaultWechat = WechatLogic{}
23+
24+
func (self WechatLogic) AutoReply(ctx context.Context, reqData []byte) (string, error) {
25+
objLog := GetLogger(ctx)
26+
27+
wechatMsg := &model.WechatMsg{}
28+
err := xml.Unmarshal(reqData, wechatMsg)
29+
if err != nil {
30+
objLog.Errorln("wechat autoreply xml unmarshal error:", err)
31+
return "", err
32+
}
33+
34+
switch wechatMsg.MsgType {
35+
case model.WeMsgTypeText:
36+
if strings.Contains(wechatMsg.Content, "晨读") {
37+
return self.readingContent(ctx, wechatMsg)
38+
} else {
39+
return self.searchContent(ctx, wechatMsg)
40+
}
41+
case model.WeMsgTypeEvent:
42+
switch wechatMsg.Event {
43+
case model.WeEventSubscribe:
44+
return config.ConfigFile.MustValue("wechat", "subscribe"), nil
45+
}
46+
}
47+
48+
return "", nil
49+
}
50+
51+
func (self WechatLogic) readingContent(ctx context.Context, wechatMsg *model.WechatMsg) (string, error) {
52+
53+
var formatContent = func(reading *model.MorningReading) string {
54+
if reading.Inner == 0 {
55+
return fmt.Sprintf("%s\n%s", reading.Content, reading.Url)
56+
}
57+
58+
host := "http://"
59+
if WebsiteSetting.OnlyHttps {
60+
host = "https://"
61+
}
62+
return fmt.Sprintf("%s\n%s%s/articles/%d", reading.Content, host, WebsiteSetting.Domain, reading.Inner)
63+
}
64+
65+
var readings []*model.MorningReading
66+
if wechatMsg.Content == "最新晨读" {
67+
readings = DefaultReading.FindBy(ctx, 1, model.RtypeGo)
68+
if len(readings) == 0 {
69+
return "没有找到您要的内容", nil
70+
}
71+
72+
return formatContent(readings[0]), nil
73+
}
74+
75+
readings = DefaultReading.FindBy(ctx, 5, model.RtypeGo)
76+
77+
respContentSlice := make([]string, len(readings))
78+
for i, reading := range readings {
79+
respContentSlice[i] = formatContent(reading)
80+
}
81+
82+
return strings.Join(respContentSlice, "\n\n"), nil
83+
}
84+
85+
func (self WechatLogic) searchContent(ctx context.Context, wechatMsg *model.WechatMsg) (string, error) {
86+
objLog := GetLogger(ctx)
87+
88+
respBody, err := DefaultSearcher.SearchByField("title", wechatMsg.Content, 0, 5)
89+
if err != nil {
90+
objLog.Errorln("wechat search by field error:", err)
91+
return "", err
92+
}
93+
94+
if respBody.NumFound == 0 {
95+
return "没有找到您要的内容", nil
96+
}
97+
98+
host := WebsiteSetting.Domain
99+
if WebsiteSetting.OnlyHttps {
100+
host = "https://" + host
101+
} else {
102+
host = "http://" + host
103+
}
104+
105+
respContentSlice := make([]string, len(respBody.Docs))
106+
for i, doc := range respBody.Docs {
107+
url := ""
108+
109+
switch doc.Objtype {
110+
case model.TypeTopic:
111+
url = fmt.Sprintf("%s/topics/%d", host, doc.Objid)
112+
case model.TypeArticle:
113+
url = fmt.Sprintf("%s/articles/%d", host, doc.Objid)
114+
case model.TypeProject:
115+
url = fmt.Sprintf("%s/p/%d", host, doc.Objid)
116+
case model.TypeWiki:
117+
url = fmt.Sprintf("%s/wiki/%d", host, doc.Objid)
118+
case model.TypeBook:
119+
url = fmt.Sprintf("%s/book/%d", host, doc.Objid)
120+
}
121+
respContentSlice[i] = fmt.Sprintf("《%s》 %s", doc.Title, url)
122+
}
123+
124+
return strings.Join(respContentSlice, "\n"), nil
125+
}

src/model/wechat_msg.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package model
8+
9+
const (
10+
WeMsgTypeText = "text"
11+
WeMsgTypeImage = "image"
12+
WeMsgTypeVoice = "voice"
13+
WeMsgTypeVideo = "video"
14+
WeMsgTypeShortVideo = "shortvideo"
15+
WeMsgTypeLocation = "location"
16+
WeMsgTypeLink = "link"
17+
WeMsgTypeEvent = "event"
18+
19+
WeEventSubscribe = "subscribe"
20+
WeEventUnsubscribe = "unsubscribe"
21+
)
22+
23+
type WechatMsg struct {
24+
ToUserName string
25+
FromUserName string
26+
CreateTime int
27+
MsgType string
28+
Content string
29+
MsgId int64
30+
31+
// 图片消息
32+
PicUrl string
33+
MediaId string
34+
35+
// 音频消息
36+
Format string
37+
38+
// 视频或短视频
39+
ThumbMediaId string
40+
41+
// 地理位置消息
42+
Location_X float64
43+
Location_Y float64
44+
Scale int
45+
Label string
46+
47+
// 链接消息
48+
Title string
49+
Description string
50+
Url string
51+
52+
// 事件
53+
Event string
54+
}

0 commit comments

Comments
 (0)