You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.7 KiB
111 lines
2.7 KiB
package internal |
|
|
|
import ( |
|
"context" |
|
"encoding/json" |
|
"fmt" |
|
mqtt "github.com/eclipse/paho.mqtt.golang" |
|
"go.mongodb.org/mongo-driver/bson" |
|
"strings" |
|
"tyj_admin/internal/model/entity" |
|
) |
|
|
|
func GetMails(ctx context.Context, uid string, serverId string, createTime1 int64, createTime2 int64) (mailList []*entity.Mail, err error) { |
|
filter := bson.M{} |
|
if len(uid) > 0 { |
|
filter["to"] = uid |
|
} |
|
if len(serverId) > 0 { |
|
filter["serverId"] = serverId |
|
} |
|
if createTime1 >= 0 && createTime2 > createTime1 { |
|
filter["c_date"] = bson.M{"$lte": createTime2} |
|
} |
|
err = MongoDatabaseList[0].Collection("mail").Find(ctx, filter).All(&mailList) |
|
return |
|
|
|
} |
|
|
|
func AddMail(ctx context.Context, mail entity.Mail) (err error) { |
|
ss, err := MongoDatabaseList[0].Collection("mail").InsertOne(ctx, mail) |
|
fmt.Println("ss %v", ss) |
|
return |
|
} |
|
|
|
type MailBody struct { |
|
entity.Mail |
|
ModuleType string `json:"moduleType"` |
|
} |
|
|
|
type MqttMail struct { |
|
ReqId int64 `json:"reqId"` |
|
ModuleId string `json:"moduleId"` |
|
Body MailBody `json:"body"` |
|
} |
|
|
|
func SendMqttMail(msg interface{}, c chan bool, server string) { |
|
|
|
registerFunc := func(client mqtt.Client, qtmsg mqtt.Message) { |
|
ss := string(msg.([]byte)) |
|
fmt.Println(ss) |
|
err := ClientSend("client", 0, false, ss) |
|
if err != nil { |
|
c <- false |
|
} |
|
//ClientSend("client", 0, false, `{"reqId":3,"moduleId":"webadmin","body":{"uids":"lq0001","gm":1,"moduleType":"changeGM"}}`) |
|
} |
|
|
|
mailFunc := func(client mqtt.Client, qtmsg mqtt.Message) { |
|
res := &MqttResult{} |
|
client.Disconnect(1) |
|
ss := string(qtmsg.Payload()) |
|
ss = ss[1 : len(ss)-1] |
|
ss = strings.Replace(ss, "\\", "", -1) |
|
err := json.Unmarshal([]byte(ss), res) |
|
if err == nil && res.RespId == 1 { |
|
c <- true |
|
return |
|
} |
|
fmt.Println("error" + err.Error()) |
|
c <- false |
|
|
|
} |
|
if NewMqttClient(registerFunc, mailFunc, server) != nil { |
|
c <- false |
|
} |
|
|
|
} |
|
|
|
type MqttResult struct { |
|
RespId int `json:"respId"` |
|
Error interface{} `json:"error"` |
|
} |
|
|
|
//func Mailcallback(c mqtt.Client, msg mqtt.Message) { |
|
// res := &MqttResult{} |
|
// c.Disconnect(1) |
|
// fmt.Println(string(msg.Payload())) |
|
// ss := string(msg.Payload()) |
|
// ss = ss[1 : len(ss)-1] |
|
// ss = strings.Replace(ss, "\\", "", -1) |
|
// fmt.Println(ss) |
|
// err := json.Unmarshal([]byte(ss), res) |
|
// if err == nil && res.RespId == 1 { |
|
// ch <- true |
|
// return |
|
// } |
|
// fmt.Println("error" + err.Error()) |
|
// ch <- false |
|
// |
|
//} |
|
// |
|
//func RegisterCallback(c mqtt.Client, msg mqtt.Message) { |
|
// ss := string(Payload.([]byte)) |
|
// fmt.Println(ss) |
|
// err := ClientSend("client", 0, false, ss) |
|
// if err != nil { |
|
// ch <- false |
|
// } |
|
// //ClientSend("client", 0, false, `{"reqId":3,"moduleId":"webadmin","body":{"uids":"lq0001","gm":1,"moduleType":"changeGM"}}`) |
|
// |
|
//}
|
|
|