|
@@ -1,8 +1,15 @@
|
|
|
package service
|
|
|
|
|
|
import (
|
|
|
+ "crypto/sha1"
|
|
|
+ "encoding/xml"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "sort"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
"github.com/pkg/errors"
|
|
|
"kpt.notice/apiserver/model"
|
|
|
"kpt.notice/apiserver/model/http"
|
|
@@ -48,6 +55,15 @@ func (svc *Service) UpdateMessage(msg *model.Message) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (svc *Service) UpdateMessageStatus(id int) (err error) {
|
|
|
+ err = svc.DB.Exec(` update message set status = -1 where id = ? `, id).Error
|
|
|
+ if err != nil {
|
|
|
+ log.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func (svc *Service) QueryMsg() (msgs []model.Message, err error) {
|
|
|
|
|
|
sql := `select m.* from message m join message_type mt
|
|
@@ -124,6 +140,15 @@ func (svc *Service) InsertMessage(msg *http.MessageReq, body []byte) (resp *http
|
|
|
data[v] = msg.Content[i]
|
|
|
}
|
|
|
tmp["data"] = data
|
|
|
+
|
|
|
+ status := -1
|
|
|
+ for _, target := range targetList {
|
|
|
+ if target != "" {
|
|
|
+ status = 0
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
msgDb := model.Message{
|
|
|
SysName: msg.SysName,
|
|
|
PastureID: msg.PastureID,
|
|
@@ -133,6 +158,7 @@ func (svc *Service) InsertMessage(msg *http.MessageReq, body []byte) (resp *http
|
|
|
MsgContent: util.MarshalToString(tmp),
|
|
|
UpdatedAt: time.Now(),
|
|
|
BodyData: string(body),
|
|
|
+ Status: status,
|
|
|
}
|
|
|
|
|
|
if err = svc.DB.Create(&msgDb).Error; err != nil {
|
|
@@ -217,3 +243,160 @@ func (svc *Service) QueryMsgType(req *http.MessageTypeReq) (m []http.MessageType
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+type WechatMessage struct {
|
|
|
+ ToUserName string `xml:"ToUserName"`
|
|
|
+ FromUserName string `xml:"FromUserName"`
|
|
|
+ CreateTime int64 `xml:"CreateTime"`
|
|
|
+ MsgType string `xml:"MsgType"`
|
|
|
+ Content string `xml:"Content"`
|
|
|
+}
|
|
|
+
|
|
|
+// type WechatTransferResponse struct {
|
|
|
+// XMLName xml.Name `xml:"xml"`
|
|
|
+// ToUserName string `xml:"ToUserName"`
|
|
|
+// FromUserName string `xml:"FromUserName"`
|
|
|
+// CreateTime int64 `xml:"CreateTime"`
|
|
|
+// MsgType string `xml:"MsgType"`
|
|
|
+// // TransInfo TransInfo `xml:"TransInfo"`
|
|
|
+// }
|
|
|
+
|
|
|
+type WechatResponse struct {
|
|
|
+ XMLName xml.Name `xml:"xml"`
|
|
|
+ ToUserName string `xml:"ToUserName"`
|
|
|
+ FromUserName string `xml:"FromUserName"`
|
|
|
+ CreateTime int64 `xml:"CreateTime"`
|
|
|
+ MsgType string `xml:"MsgType"`
|
|
|
+ Content string `xml:"Content"`
|
|
|
+}
|
|
|
+
|
|
|
+// func main() {
|
|
|
+// r := gin.Default()
|
|
|
+// r.GET("/wechat", handleVerification)
|
|
|
+// r.POST("/wechat", handleMessage)
|
|
|
+// fmt.Println("Server started on :80")
|
|
|
+// r.Run(":80")
|
|
|
+// }
|
|
|
+
|
|
|
+func (svc *Service) HandleVerification(c *gin.Context) (bool, string) {
|
|
|
+ signature := c.Query("signature")
|
|
|
+ timestamp := c.Query("timestamp")
|
|
|
+ nonce := c.Query("nonce")
|
|
|
+ echostr := c.Query("echostr")
|
|
|
+ fmt.Println(signature)
|
|
|
+ fmt.Println(timestamp)
|
|
|
+ fmt.Println(nonce)
|
|
|
+ fmt.Println(echostr)
|
|
|
+
|
|
|
+ return checkSignature(signature, timestamp, nonce), echostr
|
|
|
+}
|
|
|
+
|
|
|
+func (svc *Service) HandleMessage(c *gin.Context) []byte {
|
|
|
+ body, _ := ioutil.ReadAll(c.Request.Body)
|
|
|
+ fmt.Println(string(body))
|
|
|
+ defer c.Request.Body.Close()
|
|
|
+
|
|
|
+ var msg WechatMessage
|
|
|
+ xml.Unmarshal(body, &msg)
|
|
|
+
|
|
|
+ var count int64
|
|
|
+ svc.DB.Table("public").Where("`create` between DATE_SUB(NOW(), INTERVAL 10 MINUTE) and NOW() ").Where(" wxid = ? ", msg.ToUserName).Count(&count)
|
|
|
+
|
|
|
+ p := new(http.Public)
|
|
|
+ p.Wxid = msg.ToUserName
|
|
|
+ p.Context = msg.Content
|
|
|
+ p.Create = time.Now().Format("2006-01-02 15:04:05")
|
|
|
+ svc.DB.Table("public").Create(p)
|
|
|
+
|
|
|
+ var content string
|
|
|
+ if count == 0 {
|
|
|
+ content = `您好!感谢您关注科湃腾!您哪个系统需要服务?
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=精准饲喂&msgmenuid=1">👉精准饲喂</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=智能脖环&msgmenuid=1">👉智能脖环</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=智能喷淋&msgmenuid=1">👉智能喷淋</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=系统开发服务&msgmenuid=1">👉系统开发服务</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=其他&msgmenuid=1">👉其他</a>
|
|
|
+专线: 021- 6630 1586`
|
|
|
+ } else if p.Context == "精准饲喂" || p.Context == "智能脖环" || p.Context == "智能喷淋" || p.Context == "系统开发服务" || p.Context == "其他" {
|
|
|
+
|
|
|
+ content = `您所属那个大区?
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=东北大区&msgmenuid=1">👉东北大区</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=西北大区&msgmenuid=1">👉西北大区</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=华北大区&msgmenuid=1">👉华北大区</a>
|
|
|
+<a href="weixin://bizmsgmenu?msgmenucontent=南方大区&msgmenuid=1">👉南方大区</a>`
|
|
|
+
|
|
|
+ } else if p.Context == "东北大区" || p.Context == "西北大区" || p.Context == "华北大区" || p.Context == "南方大区" {
|
|
|
+ content = `东北大区服务老师联系方式
|
|
|
+谭佳悦:18888309870
|
|
|
+喻旭超:13594927924
|
|
|
+您可通过电话进行咨询,若您现在不方便电话联系,可以留下您的联系方式,简单描述一下问题或者拍照,我们安排服务人员在4小时内联系您,请您留意电话接听,祝您生活愉快!`
|
|
|
+
|
|
|
+ // new(notice.MessageRequest)
|
|
|
+ // notice.SendMessage(nil)
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ response := WechatResponse{
|
|
|
+ ToUserName: msg.FromUserName,
|
|
|
+ FromUserName: msg.ToUserName,
|
|
|
+ CreateTime: time.Now().Unix(),
|
|
|
+ MsgType: "text",
|
|
|
+ Content: content,
|
|
|
+ }
|
|
|
+
|
|
|
+ responseXML, _ := xml.Marshal(response)
|
|
|
+ return responseXML
|
|
|
+}
|
|
|
+
|
|
|
+func checkSignature(signature, timestamp, nonce string) bool {
|
|
|
+ token := "e10adc3949ba59abbe56e057f20f883e"
|
|
|
+ tmpList := []string{token, timestamp, nonce}
|
|
|
+ sort.Strings(tmpList)
|
|
|
+ tmpStr := strings.Join(tmpList, "")
|
|
|
+ h := sha1.New()
|
|
|
+ h.Write([]byte(tmpStr))
|
|
|
+ digest := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
+ return digest == signature
|
|
|
+}
|
|
|
+
|
|
|
+type Message struct {
|
|
|
+ ToUser string `json:"touser"`
|
|
|
+ MsgType string `json:"msgtype"`
|
|
|
+ Text struct {
|
|
|
+ Content string `json:"content"`
|
|
|
+ } `json:"text"`
|
|
|
+}
|
|
|
+
|
|
|
+// func sendCustomMessage(accessToken, openID, content string) error {
|
|
|
+// url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", accessToken)
|
|
|
+
|
|
|
+// message := Message{
|
|
|
+// ToUser: openID,
|
|
|
+// MsgType: "text",
|
|
|
+// Text: struct {
|
|
|
+// Content string `json:"content"`
|
|
|
+// }{
|
|
|
+// Content: content,
|
|
|
+// },
|
|
|
+// }
|
|
|
+
|
|
|
+// payload, err := json.Marshal(message)
|
|
|
+// if err != nil {
|
|
|
+// return err
|
|
|
+// }
|
|
|
+
|
|
|
+// resp, err := http1.Post(url, "application/json", bytes.NewBuffer(payload))
|
|
|
+// if err != nil {
|
|
|
+// return err
|
|
|
+// }
|
|
|
+// defer resp.Body.Close()
|
|
|
+
|
|
|
+// var result map[string]interface{}
|
|
|
+// if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
|
+// return err
|
|
|
+// }
|
|
|
+
|
|
|
+// fmt.Println("Response:", result)
|
|
|
+// return nil
|
|
|
+// }
|