123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package util
- import (
- "encoding/json"
- "fmt"
- "github.com/vmihailenco/msgpack/v5"
- "log"
- "time"
- )
- // oZMR-6SQHsxGdO9mMmvsQEz4lyHA/hy2246102093|
- type WxMessagetemplate struct {
- Openid string //微信ID
- Templateid string // 模板id
- Parameter string // 参数
- Reqdata string //消息体
- Ttime int64 // 发布时间
- }
- // **
- // {{character_string14.DATA}}
- // 企业名称
- // {{thing13.DATA}}
- // 设备名称
- // {{thing3.DATA}}
- // 传感器名称
- // {{thing45.DATA}}
- // 预警时间
- // {{time2.DATA}}
- type WxStruct struct {
- Character string `json:"character_string14"`
- Thing13 string `json:"thing13"`
- Thing3 string `json:"thing3"`
- Thing45 string `json:"thing45"`
- Time2 string `json:"time2"`
- }
- func Send(openid string, message WxStruct) {
- data := map[string]interface{}{
- "character_string14": map[string]interface{}{
- "value": message.Character,
- },
- "thing13": map[string]interface{}{
- "value": message.Thing13,
- },
- "thing3": map[string]interface{}{
- "value": message.Thing3,
- },
- "thing45": map[string]interface{}{
- "value": message.Thing45,
- },
- "time2": map[string]interface{}{
- "value": message.Time2,
- },
- }
- marshal, _ := json.Marshal(data)
- fmt.Println(string(marshal))
- b, _ := msgpack.Marshal(&WxMessagetemplate{
- Openid: openid,
- Templateid: "4jtNEZ--zp1QcfkN--lhJMCULgz3fxiYwRqR8dTG-WA",
- Parameter: "index",
- Reqdata: string(marshal),
- Ttime: time.Now().Unix(),
- })
- msg, err := Nats.Request("Wx_MessageTemplate", b, 30*time.Second) // 30 秒, 订阅端 超时 30秒 将丢弃消息
- if err != nil {
- fmt.Println(err)
- log.Printf("Wx_MessageTemplate : %s\n", err)
- }
- fmt.Printf("Wx_MessageTemplate : %s\n", string(msg.Data))
- }
- func Intercept(str string) string {
- if len(str) > 20 {
- return str[:20] + "..."
- }
- return str
- }
|