wxoffce.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/fastwego/offiaccount/type/type_message"
  10. "kpt.notice/apiserver/model"
  11. "kpt.notice/middleware/notice"
  12. "kpt.notice/pkg/http"
  13. "kpt.notice/pkg/log"
  14. )
  15. // 获取公众号临时二维码
  16. func CreateQRCode(sceneID string) (body []byte, err error) {
  17. resp, e := notice.CreateQRCode(sceneID)
  18. if err != nil {
  19. log.Errorf("service.CreateQRCode. %v", e)
  20. return
  21. }
  22. m := make(map[string]string)
  23. json.Unmarshal(resp, &m)
  24. m = map[string]string{
  25. "ticket": m["ticket"],
  26. }
  27. url := "https://mp.weixin.qq.com/cgi-bin/showqrcode"
  28. body, err = http.HttpGet(url, m)
  29. if err != nil {
  30. log.Errorf("service.CreateQRCode. %v", err)
  31. }
  32. return
  33. }
  34. func ReceiveMessage(body []byte) (output *type_message.ReplyMessageText, err error) {
  35. log.Info()
  36. msg := new(model.WxMessageReq)
  37. if e := xml.Unmarshal(body, msg); e != nil {
  38. err = errors.Wrap(e, "service.ReceiveMessage.Unmarshal")
  39. }
  40. switch msg.MsgType {
  41. case "text":
  42. output, err = ReceiveText(msg)
  43. case "event":
  44. output, err = ReceiveEvent(msg)
  45. }
  46. log.Infof("service.ReceiveMessage{WxMessageReq====} %+v", msg)
  47. return
  48. }
  49. /*
  50. 接收微信服务器推送的事件
  51. */
  52. func ReceiveEvent(msg *model.WxMessageReq) (output *type_message.ReplyMessageText, err error) {
  53. if msg.Event == "subscribe" || msg.Event == "SCAN" {
  54. key := msg.EventKey
  55. if msg.Event == "subscribe" {
  56. key = strings.Split(key, "qrscene_")[1]
  57. }
  58. keys := strings.Split(key, ".")
  59. // m := map[string]interface{}{
  60. // "userinfo": key,
  61. // "openid": msg.FromUserName,
  62. // }
  63. // body, e := json.Marshal(m)
  64. // if e != nil {
  65. // err = errors.Wrap(e, "service.ReceiveEvent.Marshal")
  66. // }
  67. // var url string
  68. // if svc.Con.Env == "dev" {
  69. // url = "http://kpttest.kptyun.com/userwxopenid/binding"
  70. // } else {
  71. // con := model.SysPastureUrl{
  72. // SysName: keys[0],
  73. // Pasture: keys[1],
  74. // }
  75. // res, e := svc.QuerySysPastureUrl(&con)
  76. // if e != nil {
  77. // err = errors.Wrap(e, "service.ReceiveEvent.QuerySysPastureUrl{没有匹配的url}=====")
  78. // }
  79. // url = res.Url
  80. // }
  81. // resp, e := http.HttpPost(url, "application/json", string(body))
  82. // if e != nil {
  83. // err = errors.Wrap(e, "service.ReceiveEvent-")
  84. // }
  85. var err error
  86. if len(keys) == 2 {
  87. err = svc.InsertSysUser(msg.FromUserName, keys[1], keys[0], "0")
  88. } else {
  89. err = svc.InsertSysUser(msg.FromUserName, keys[2], keys[0], keys[1])
  90. }
  91. respContent := "公众号绑定系统成功"
  92. if err != nil {
  93. respContent = "公众号绑定系统失败,请联系管理员"
  94. }
  95. output = WxResponse(type_message.CDATA(msg.FromUserName), type_message.CDATA(msg.ToUserName))
  96. output.Content = type_message.CDATA(respContent)
  97. }
  98. return
  99. }
  100. /*
  101. 接收微信服务器推送的文本消息
  102. */
  103. func ReceiveText(msg *model.WxMessageReq) (resp *type_message.ReplyMessageText, err error) {
  104. text := msg.Content
  105. openid := msg.FromUserName
  106. err = svc.InsertUser(text, openid)
  107. var respText string = "手机号绑定成功,欢迎使用"
  108. if err != nil {
  109. respText = "手机号绑定失败,请重新输入"
  110. }
  111. resp = WxResponse(type_message.CDATA(msg.FromUserName), type_message.CDATA(msg.ToUserName))
  112. resp.Content = type_message.CDATA(respText)
  113. return
  114. }
  115. func WxResponse(from, to type_message.CDATA) *type_message.ReplyMessageText {
  116. return &type_message.ReplyMessageText{
  117. ReplyMessage: type_message.ReplyMessage{
  118. ToUserName: from,
  119. FromUserName: to,
  120. CreateTime: strconv.FormatInt(time.Now().Unix(), 10),
  121. MsgType: type_message.ReplyMsgTypeText,
  122. },
  123. }
  124. }