send.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package public_msg
  2. import (
  3. "context"
  4. "git.getensh.com/common/gopkgs/database"
  5. dbmodel "property-household/model"
  6. "property-household/parser"
  7. "property-household/pb"
  8. pb_v1 "property-household/pb/v1"
  9. "time"
  10. )
  11. type ServiceInfo struct {
  12. Name string
  13. State string
  14. }
  15. func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
  16. ret := map[int64]pb_v1.GardenItem{}
  17. if len(ids) == 0 {
  18. return ret, nil
  19. }
  20. mreq := pb_v1.GardenInfosRequest{
  21. Ids: ids,
  22. }
  23. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  24. if err != nil {
  25. return nil, err
  26. }
  27. for _, v := range mreply.List {
  28. ret[v.Id] = *v
  29. }
  30. return ret, nil
  31. }
  32. // 发送公众号消息
  33. func SendMsgWx(uid int64, gardenId int64, info ServiceInfo) {
  34. remark := "详情请进入小程序查看"
  35. if parser.Conf.ThirdParty.Wx.PublicStateMsgTmpId == "" {
  36. return
  37. }
  38. p := dbmodel.TUser{}
  39. where := map[string]interface{}{
  40. "id": uid,
  41. }
  42. p.Find(database.DB(), where)
  43. if p.PublicOpenId == "" {
  44. return
  45. }
  46. gardenInfos, err := getGardenInfos([]int64{gardenId})
  47. if err != nil {
  48. return
  49. }
  50. gardenName := gardenInfos[gardenId].GardenName
  51. popenId := p.PublicOpenId
  52. content := []*pb_v1.WxPublicMsgKeyValue{
  53. &pb_v1.WxPublicMsgKeyValue{Key: "first", Value: "亲爱的用户,您在" + gardenName + "的服务状态发生变更"},
  54. &pb_v1.WxPublicMsgKeyValue{Key: "keyword1", Value: info.Name},
  55. &pb_v1.WxPublicMsgKeyValue{Key: "keyword2", Value: info.State},
  56. &pb_v1.WxPublicMsgKeyValue{Key: "keyword3", Value: time.Now().Format("2006-01-02 15:04:05")},
  57. &pb_v1.WxPublicMsgKeyValue{Key: "remark", Value: remark},
  58. }
  59. mreq := pb_v1.WxPublicMsgSendRequest{
  60. PublicOpenId: popenId,
  61. Content: content,
  62. TemplateId: parser.Conf.ThirdParty.Wx.PublicStateMsgTmpId,
  63. }
  64. _, err = pb.Thirdparty.WxPublicMsgSend(context.Background(), &mreq)
  65. if err == nil {
  66. return
  67. }
  68. }