123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package service
- import (
- "encoding/json"
- "encoding/xml"
- "strconv"
- "strings"
- "time"
- "github.com/pkg/errors"
- "github.com/fastwego/offiaccount/type/type_message"
- "kpt.notice/apiserver/model"
- "kpt.notice/middleware/notice"
- "kpt.notice/pkg/http"
- "kpt.notice/pkg/log"
- )
- // 获取公众号临时二维码
- func CreateQRCode(sceneID string) (body []byte, err error) {
- resp, e := notice.CreateQRCode(sceneID)
- if err != nil {
- log.Errorf("service.CreateQRCode. %v", e)
- return
- }
- m := make(map[string]string)
- json.Unmarshal(resp, &m)
- m = map[string]string{
- "ticket": m["ticket"],
- }
- url := "https://mp.weixin.qq.com/cgi-bin/showqrcode"
- body, err = http.HttpGet(url, m)
- if err != nil {
- log.Errorf("service.CreateQRCode. %v", err)
- }
- return
- }
- func ReceiveMessage(body []byte) (output *type_message.ReplyMessageText, err error) {
- log.Info()
- msg := new(model.WxMessageReq)
- if e := xml.Unmarshal(body, msg); e != nil {
- err = errors.Wrap(e, "service.ReceiveMessage.Unmarshal")
- }
- switch msg.MsgType {
- case "text":
- output, err = ReceiveText(msg)
- case "event":
- output, err = ReceiveEvent(msg)
- }
- log.Infof("service.ReceiveMessage{WxMessageReq====} %+v", msg)
- return
- }
- /*
- 接收微信服务器推送的事件
- */
- func ReceiveEvent(msg *model.WxMessageReq) (output *type_message.ReplyMessageText, err error) {
- if msg.Event == "subscribe" || msg.Event == "SCAN" {
- key := msg.EventKey
- if msg.Event == "subscribe" {
- key = strings.Split(key, "qrscene_")[1]
- }
- keys := strings.Split(key, ".")
- // m := map[string]interface{}{
- // "userinfo": key,
- // "openid": msg.FromUserName,
- // }
- // body, e := json.Marshal(m)
- // if e != nil {
- // err = errors.Wrap(e, "service.ReceiveEvent.Marshal")
- // }
- // var url string
- // if svc.Con.Env == "dev" {
- // url = "http://kpttest.kptyun.com/userwxopenid/binding"
- // } else {
- // con := model.SysPastureUrl{
- // SysName: keys[0],
- // Pasture: keys[1],
- // }
- // res, e := svc.QuerySysPastureUrl(&con)
- // if e != nil {
- // err = errors.Wrap(e, "service.ReceiveEvent.QuerySysPastureUrl{没有匹配的url}=====")
- // }
- // url = res.Url
- // }
- // resp, e := http.HttpPost(url, "application/json", string(body))
- // if e != nil {
- // err = errors.Wrap(e, "service.ReceiveEvent-")
- // }
- var err error
- if len(keys) == 2 {
- err = svc.InsertSysUser(msg.FromUserName, keys[1], keys[0], "0")
- } else {
- err = svc.InsertSysUser(msg.FromUserName, keys[2], keys[0], keys[1])
- }
- respContent := "公众号绑定系统成功"
- if err != nil {
- respContent = "公众号绑定系统失败,请联系管理员"
- }
- output = WxResponse(type_message.CDATA(msg.FromUserName), type_message.CDATA(msg.ToUserName))
- output.Content = type_message.CDATA(respContent)
- }
- return
- }
- /*
- 接收微信服务器推送的文本消息
- */
- func ReceiveText(msg *model.WxMessageReq) (resp *type_message.ReplyMessageText, err error) {
- text := msg.Content
- openid := msg.FromUserName
- err = svc.InsertUser(text, openid)
- var respText string = "手机号绑定成功,欢迎使用"
- if err != nil {
- respText = "手机号绑定失败,请重新输入"
- }
- resp = WxResponse(type_message.CDATA(msg.FromUserName), type_message.CDATA(msg.ToUserName))
- resp.Content = type_message.CDATA(respText)
- return
- }
- func WxResponse(from, to type_message.CDATA) *type_message.ReplyMessageText {
- return &type_message.ReplyMessageText{
- ReplyMessage: type_message.ReplyMessage{
- ToUserName: from,
- FromUserName: to,
- CreateTime: strconv.FormatInt(time.Now().Unix(), 10),
- MsgType: type_message.ReplyMsgTypeText,
- },
- }
- }
|