140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"time"
|
|
"videoplayer/proto"
|
|
"videoplayer/worker"
|
|
)
|
|
|
|
func SetToolRedisList(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.PushRedisList(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "push redis list failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.PushRedisListWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "push redis list with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func SetToolRedisSet(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.SetRedis(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func SetToolRedisKV(key string, value string, expire int) (code int, message string) {
|
|
if expire == 0 {
|
|
if worker.SetRedis(key, value) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis failed"
|
|
}
|
|
} else if expire > 0 {
|
|
if worker.SetRedisWithExpire(key, value, time.Duration(expire)) {
|
|
return proto.SuccessCode, "success"
|
|
} else {
|
|
return proto.OperationFailed, "set redis with expire failed"
|
|
}
|
|
} else {
|
|
return proto.ParameterError, "expire time can not be negative"
|
|
}
|
|
}
|
|
|
|
func GetToolRedis(key string) (code int, message string) {
|
|
val := worker.GetRedis(key)
|
|
if val == "" {
|
|
return proto.OperationFailed, "get redis failed"
|
|
} else {
|
|
return proto.SuccessCode, val
|
|
}
|
|
}
|
|
|
|
func GetAllRedis() (code int, msg string, data []worker.RedisInfo) {
|
|
data, err := worker.GetAllRedisInfo()
|
|
if err != nil {
|
|
return proto.OperationFailed, err.Error(), nil
|
|
}
|
|
return proto.SuccessCode, "success", data
|
|
}
|
|
|
|
func SendEmail(email, subject, body string) {
|
|
//捕获异常
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
fmt.Errorf("tool send mail error: %s", err)
|
|
}
|
|
}()
|
|
// TODO
|
|
// 发送邮件
|
|
// 邮件内容
|
|
// 邮件标题
|
|
// 收件人
|
|
// 发送邮件
|
|
// 发送邮件通知
|
|
// 发送邮件通知
|
|
var em worker.MyEmail
|
|
em.SmtpPassword = "nihzazdkmucnbhid"
|
|
em.SmtpHost = "pop.qq.com:587"
|
|
em.SmtpUserName = "354425203@qq.com"
|
|
em.SmtpPort = 587
|
|
em.ImapPort = 993
|
|
err := em.Send(subject, body, []string{email})
|
|
if err != nil {
|
|
fmt.Println("send mail error:", err)
|
|
}
|
|
}
|
|
|
|
// 地址校验
|
|
func CheckEmail(email string) bool {
|
|
//正则表达式判断是否是邮箱
|
|
pattern := `^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$`
|
|
reg := regexp.MustCompile(pattern)
|
|
return reg.MatchString(email)
|
|
}
|
|
|
|
// 获取监控设备及状态
|
|
func GetMonitorDeviceListWithStatus(userId int) ([]proto.GetMonitorDeviceStatus, error) {
|
|
var deviceStatus []proto.GetMonitorDeviceStatus
|
|
user := GetUserByIDFromUserCenter(userId)
|
|
if user.Role != "admin" {
|
|
return deviceStatus, errors.New("user is not admin, can not get monitor device list")
|
|
} else {
|
|
devices := worker.GetRedisSetMembers(proto.Config.MONITOR_SERVER_TOKEN)
|
|
for _, device := range devices {
|
|
status := worker.GetRedis("monitor_" + device)
|
|
var deviceInfo proto.GetMonitorDeviceStatus
|
|
deviceInfo.ID = device
|
|
if status == "" {
|
|
deviceInfo.Status = "offline"
|
|
} else {
|
|
deviceInfo.Status = status
|
|
}
|
|
deviceStatus = append(deviceStatus, deviceInfo)
|
|
}
|
|
}
|
|
return deviceStatus, nil
|
|
}
|