2024-07-19 09:43:17 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2024-08-30 15:40:45 +08:00
|
|
|
|
"fmt"
|
2024-07-19 09:43:17 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-08-30 15:40:45 +08:00
|
|
|
|
"io"
|
2025-05-02 21:58:40 +08:00
|
|
|
|
"log"
|
2024-07-19 09:43:17 +08:00
|
|
|
|
"net/http"
|
2024-08-30 15:40:45 +08:00
|
|
|
|
"os"
|
2024-08-30 15:26:39 +08:00
|
|
|
|
"strconv"
|
2024-11-07 15:45:27 +08:00
|
|
|
|
"time"
|
2024-07-19 09:43:17 +08:00
|
|
|
|
"videoplayer/dao"
|
|
|
|
|
|
"videoplayer/proto"
|
2024-07-21 11:00:08 +08:00
|
|
|
|
"videoplayer/service"
|
2024-11-07 15:45:27 +08:00
|
|
|
|
"videoplayer/worker"
|
2024-07-19 09:43:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type SetRedisReq struct {
|
|
|
|
|
|
Option string `json:"option" form:"option"`
|
|
|
|
|
|
Key string `json:"key" form:"key"`
|
|
|
|
|
|
Value string `json:"value" form:"value"`
|
|
|
|
|
|
Expire int `json:"expire" form:"expire"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:45:27 +08:00
|
|
|
|
type SetDeviceStatusReq struct {
|
|
|
|
|
|
ID string `json:"id" form:"id"` //设备编码
|
|
|
|
|
|
Status string `json:"status" form:"status"` //设备状态
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 13:29:33 +08:00
|
|
|
|
type GetFileListReq struct {
|
|
|
|
|
|
Type string `json:"type" form:"type"` //请求类型,1:按md5查询,2:按文件名查询;3:查询待删除文件
|
|
|
|
|
|
Md5 string `json:"md5" form:"md5"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-02 15:39:05 +08:00
|
|
|
|
type SendMailReq struct {
|
|
|
|
|
|
Title string `json:"title" form:"title"`
|
|
|
|
|
|
Content string `json:"content" form:"content"`
|
|
|
|
|
|
To string `json:"to" form:"to"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-19 09:43:17 +08:00
|
|
|
|
func SetUpToolGroup(router *gin.Engine) {
|
|
|
|
|
|
toolGroup := router.Group("/tool")
|
|
|
|
|
|
toolGroup.POST("/set_redis", SetRedis)
|
2024-07-21 11:00:08 +08:00
|
|
|
|
toolGroup.POST("/get_redis", GetRedis)
|
2024-08-30 11:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
//文件上传、下载
|
|
|
|
|
|
toolGroup.POST("/upload", UploadFile)
|
2024-08-30 15:41:51 +08:00
|
|
|
|
toolGroup.GET("/download", DownloadFile)
|
2024-12-27 17:41:45 +08:00
|
|
|
|
toolGroup.GET("/file/:filename", GetFile)
|
2024-12-28 13:29:33 +08:00
|
|
|
|
toolGroup.POST("/file_list", GetFileList)
|
2024-08-30 21:46:28 +08:00
|
|
|
|
//文件管理
|
|
|
|
|
|
toolGroup.POST("/file_del", DelFile)
|
2024-11-07 15:45:27 +08:00
|
|
|
|
//服务器、设备状态接口
|
|
|
|
|
|
toolGroup.POST("/monitor", SetDeviceStatusV2)
|
2025-06-06 14:36:59 +08:00
|
|
|
|
toolGroup.GET("/get_monitor_list", GetMonitorList) //获取设备监控列表
|
|
|
|
|
|
toolGroup.POST("/update_monitor", UpdateMonitor) //设置设备状态
|
|
|
|
|
|
toolGroup.POST("/del_monitor", DelMonitor) //删除设备监控
|
2025-01-02 15:39:05 +08:00
|
|
|
|
//发送邮件
|
|
|
|
|
|
toolGroup.POST("/send_mail", SendMailTool)
|
2024-11-07 15:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 14:36:59 +08:00
|
|
|
|
func GetMonitorList(c *gin.Context) {
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
userId := int(id.(float64))
|
|
|
|
|
|
var resp proto.GeneralResp
|
|
|
|
|
|
monitorDeviceList, err := service.GetMonitorDeviceListWithStatus(userId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
resp.Code = proto.OperationFailed
|
|
|
|
|
|
resp.Message = err.Error()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp.Code = proto.SuccessCode
|
|
|
|
|
|
resp.Message = "success"
|
|
|
|
|
|
resp.Data = monitorDeviceList
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
|
}
|
2025-06-06 14:53:14 +08:00
|
|
|
|
|
|
|
|
|
|
type UpdateMonitorDeviceStatusReq struct {
|
|
|
|
|
|
Devices []proto.GetMonitorDeviceStatus `json:"devices" form:"devices"` //设备状态列表
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 14:36:59 +08:00
|
|
|
|
func UpdateMonitor(c *gin.Context) {
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
2025-06-06 14:53:14 +08:00
|
|
|
|
var req UpdateMonitorDeviceStatusReq
|
|
|
|
|
|
var resp proto.GeneralResp
|
|
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
|
|
|
err2 := service.UpdateMonitorDeviceListWithStatus(id1, req.Devices)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
resp.Code = proto.OperationFailed
|
|
|
|
|
|
resp.Message = "更新设备状态失败:" + err2.Error()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp.Code = proto.SuccessCode
|
|
|
|
|
|
resp.Message = "更新设备状态成功"
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp.Code = proto.ParameterError
|
|
|
|
|
|
resp.Message = "参数解析失败:" + err.Error()
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
2025-06-06 14:36:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
func DelMonitor(c *gin.Context) {
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
2025-06-06 14:59:25 +08:00
|
|
|
|
var req UpdateMonitorDeviceStatusReq
|
|
|
|
|
|
var resp proto.GeneralResp
|
|
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
|
|
|
delSuccess, err2 := service.DelMonitorDeviceListWithStatus(id1, req.Devices)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
resp.Code = proto.OperationFailed
|
|
|
|
|
|
resp.Message = "更新设备状态失败:" + err2.Error()
|
|
|
|
|
|
resp.Data = delSuccess
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp.Code = proto.SuccessCode
|
|
|
|
|
|
resp.Message = "更新设备状态成功"
|
|
|
|
|
|
resp.Data = delSuccess
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
resp.Code = proto.ParameterError
|
|
|
|
|
|
resp.Message = "参数解析失败:" + err.Error()
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
2025-06-06 14:36:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 15:45:27 +08:00
|
|
|
|
func SetDeviceStatusV2(c *gin.Context) {
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
var req SetDeviceStatusReq
|
2025-05-02 21:58:40 +08:00
|
|
|
|
var resp proto.GeneralResp
|
2024-11-07 15:45:27 +08:00
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2025-05-02 21:58:40 +08:00
|
|
|
|
resp.Code = proto.ParameterError
|
|
|
|
|
|
resp.Message = "参数解析失败:" + err.Error()
|
2024-11-07 15:45:27 +08:00
|
|
|
|
} else {
|
2024-11-24 15:19:37 +08:00
|
|
|
|
token := c.Request.Header.Get("token")
|
|
|
|
|
|
if token == "" {
|
2025-05-02 21:58:40 +08:00
|
|
|
|
resp.Code = proto.TokenIsNull //token为空
|
|
|
|
|
|
} else {
|
|
|
|
|
|
devices := worker.GetRedisSetMembers(token)
|
|
|
|
|
|
if len(devices) == 0 {
|
|
|
|
|
|
resp.Code = proto.MonitorServerIDIsNull
|
|
|
|
|
|
resp.Message = "服务器设备监控为空!"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
isExit := false
|
|
|
|
|
|
for _, v := range devices {
|
|
|
|
|
|
if v == req.ID {
|
|
|
|
|
|
// 继续处理请求
|
|
|
|
|
|
//是否是暂停之后第一次上线,如果是则发送邮件通知
|
|
|
|
|
|
deviceStatus := worker.GetRedis("monitor_" + req.ID)
|
|
|
|
|
|
isExist := worker.IsContainKey("monitor_" + req.ID)
|
|
|
|
|
|
if deviceStatus == "2" || !isExist {
|
|
|
|
|
|
//发送邮件通知
|
|
|
|
|
|
title := "设备上线"
|
|
|
|
|
|
content := "设备上线\n设备:" + req.ID + "\t状态:" + req.Status + "\t时间:" + time.Now().String()
|
|
|
|
|
|
go SendMail(title, content)
|
|
|
|
|
|
}
|
|
|
|
|
|
worker.SetRedisWithExpire("monitor_"+req.ID, "1", time.Second*300)
|
|
|
|
|
|
resp.Code = proto.SuccessCode
|
|
|
|
|
|
resp.Message = "success"
|
|
|
|
|
|
isExit = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if isExit == false {
|
|
|
|
|
|
resp.Code = proto.MonitorServerIDNotFound
|
|
|
|
|
|
resp.Message = "设备不存在!"
|
|
|
|
|
|
log.Println("设备不存在,id:", req.ID, "\ttoken:", token, "\tdevices:", devices)
|
2025-03-03 13:49:12 +08:00
|
|
|
|
}
|
2024-11-07 15:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-02 21:58:40 +08:00
|
|
|
|
c.JSON(http.StatusOK, resp)
|
2024-08-30 21:46:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 13:29:33 +08:00
|
|
|
|
func GetFileList(c *gin.Context) {
|
|
|
|
|
|
//解析请求参数
|
|
|
|
|
|
var req GetFileListReq
|
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
|
if req.Type == "1" {
|
|
|
|
|
|
//按md5查询
|
|
|
|
|
|
if req.Md5 == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "md5 is empty", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
file := dao.FindFileByMd5(req.Md5)
|
|
|
|
|
|
if file.ID == 0 {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": file})
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-30 21:46:28 +08:00
|
|
|
|
func DelFile(c *gin.Context) {
|
|
|
|
|
|
//先查看是否有权限
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
|
|
|
|
|
|
|
|
|
|
|
file_id, _ := strconv.Atoi(c.PostForm("id"))
|
|
|
|
|
|
|
|
|
|
|
|
file_ := dao.FindFileByID(file_id, id1)
|
|
|
|
|
|
if file_.ID == 0 {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//删除文件
|
|
|
|
|
|
err := os.Remove(file_.FilePath + "/" + file_.FileStoreName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "delete file failed", "code": proto.DeleteFileFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//删除文件信息
|
|
|
|
|
|
if res := dao.DeleteFileById(file_id); !res {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "delete file info failed", "code": proto.DeleteFileInfoFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
|
|
|
|
|
|
2024-08-30 11:28:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-27 17:41:45 +08:00
|
|
|
|
func GetFile(c *gin.Context) {
|
|
|
|
|
|
//先查看是否有权限
|
|
|
|
|
|
filename := c.Param("filename")
|
|
|
|
|
|
if filename == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "filename is empty", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//查询文件信息
|
|
|
|
|
|
file := dao.FindFileByName(filename)
|
|
|
|
|
|
if file.ID == 0 {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//下载文件
|
|
|
|
|
|
if file.NeedAuth == false {
|
|
|
|
|
|
c.File(file.FilePath + "/" + file.FileStoreName)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "file must auth", "data": "file must auth"})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-30 11:28:27 +08:00
|
|
|
|
func UploadFile(c *gin.Context) {
|
|
|
|
|
|
//先查看是否有权限
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
|
|
|
|
|
//从请求头获取upload_type
|
2024-08-30 15:13:23 +08:00
|
|
|
|
uploadType := c.PostForm("upload_type")
|
2024-12-27 17:41:45 +08:00
|
|
|
|
authType := c.PostForm("auth_type")
|
2024-12-27 20:36:04 +08:00
|
|
|
|
md5_ := c.PostForm("md5")
|
2024-08-30 15:13:23 +08:00
|
|
|
|
if uploadType == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-08-30 11:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
user := dao.FindUserByUserID(id1)
|
|
|
|
|
|
if user.Upload == false {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "code": proto.NoUploadPermissions, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//上传文件
|
|
|
|
|
|
file, err := c.FormFile("file")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "upload file failed", "code": proto.UploadFileFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-12-27 20:27:20 +08:00
|
|
|
|
//计算文件md5值
|
2024-12-27 20:36:04 +08:00
|
|
|
|
if md5_ == "" {
|
|
|
|
|
|
file_, _ := file.Open()
|
|
|
|
|
|
md5_ = service.CalculateFileMd5(file_)
|
|
|
|
|
|
if md5_ == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-12-27 20:27:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
//查询文件是否已存在
|
2024-12-27 20:36:04 +08:00
|
|
|
|
fileExist := dao.FindFileByMd5(md5_)
|
2024-12-27 20:27:20 +08:00
|
|
|
|
if fileExist.ID != 0 {
|
|
|
|
|
|
fileExist.FilePath = ""
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-08-30 11:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
//保存文件
|
2024-08-30 21:46:28 +08:00
|
|
|
|
filePath, fileStoreName, err := service.SaveFile(c, file, uploadType)
|
2024-08-30 11:28:27 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "save file failed", "code": proto.SaveFileFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//保存文件信息
|
|
|
|
|
|
fileSize := int(file.Size)
|
|
|
|
|
|
fileName := file.Filename
|
|
|
|
|
|
fileType := file.Header.Get("file_type")
|
2024-12-27 17:41:45 +08:00
|
|
|
|
var auth_type_ bool
|
|
|
|
|
|
if authType == "public" || authType == "" {
|
|
|
|
|
|
auth_type_ = false
|
|
|
|
|
|
} else if authType == "private" {
|
|
|
|
|
|
auth_type_ = true
|
|
|
|
|
|
}
|
2024-12-27 20:36:04 +08:00
|
|
|
|
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, md5_, fileSize, id1, auth_type_)
|
2024-12-27 20:30:05 +08:00
|
|
|
|
if file_record.ID == 0 {
|
2024-08-30 11:28:27 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-12-27 20:30:05 +08:00
|
|
|
|
file_record.FilePath = ""
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": file_record})
|
2024-08-30 11:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DownloadFile(c *gin.Context) {
|
|
|
|
|
|
//参数
|
2024-08-30 15:26:39 +08:00
|
|
|
|
//filename := c.Param("filename")
|
|
|
|
|
|
file_id, _ := strconv.Atoi(c.Query("id"))
|
2024-08-30 11:28:27 +08:00
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
//查询文件信息
|
2024-08-30 15:26:39 +08:00
|
|
|
|
//file := dao.FindFileByNames(file_id, int(id.(float64)))
|
2024-08-30 15:40:45 +08:00
|
|
|
|
file_ := dao.FindFileByID(file_id, int(id.(float64)))
|
|
|
|
|
|
if file_.ID == 0 {
|
2024-08-30 11:28:27 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//下载文件
|
2024-08-30 15:40:45 +08:00
|
|
|
|
// 打开文件
|
|
|
|
|
|
file, err := os.Open(file_.FilePath + "/" + file_.FileStoreName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to open file"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
|
|
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file_.FileName))
|
|
|
|
|
|
|
|
|
|
|
|
// 发送文件内容
|
|
|
|
|
|
_, err = io.Copy(c.Writer, file)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to send file"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-08-30 15:47:57 +08:00
|
|
|
|
|
|
|
|
|
|
c.Status(http.StatusOK)
|
2024-07-19 09:43:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SetRedis(c *gin.Context) {
|
|
|
|
|
|
//先查看是否有权限
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
|
|
|
|
|
user := dao.FindUserByUserID(id1)
|
|
|
|
|
|
if user.Redis == false {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//解析请求参数
|
|
|
|
|
|
var req SetRedisReq
|
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
2024-07-21 11:00:08 +08:00
|
|
|
|
var code int
|
|
|
|
|
|
var message string
|
|
|
|
|
|
if req.Option == "list" {
|
|
|
|
|
|
code, message = service.SetToolRedisList(req.Key, req.Value, req.Expire)
|
|
|
|
|
|
} else if req.Option == "set" {
|
|
|
|
|
|
code, message = service.SetToolRedisSet(req.Key, req.Value, req.Expire)
|
|
|
|
|
|
} else if req.Option == "kv" {
|
|
|
|
|
|
code, message = service.SetToolRedisKV(req.Key, req.Value, req.Expire)
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": code, "message": message})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-19 09:43:17 +08:00
|
|
|
|
|
2024-07-21 11:00:08 +08:00
|
|
|
|
func GetRedis(c *gin.Context) {
|
|
|
|
|
|
//先查看是否有权限
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
|
|
|
|
|
user := dao.FindUserByUserID(id1)
|
|
|
|
|
|
if user.Redis == false {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "no redis Permissions", "code": proto.NoRedisPermissions, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//解析请求参数
|
|
|
|
|
|
var req SetRedisReq
|
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
2024-12-21 17:16:38 +08:00
|
|
|
|
if req.Option == "one" {
|
|
|
|
|
|
code, message := service.GetToolRedis(req.Key)
|
|
|
|
|
|
req.Value = message
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
|
|
|
|
|
|
} else if req.Option == "all" {
|
|
|
|
|
|
code, message, data := service.GetAllRedis()
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": data})
|
|
|
|
|
|
}
|
2024-07-19 09:43:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-07 15:45:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 服务器、设备状态扫描
|
|
|
|
|
|
func ScanDeviceStatus() {
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
// 检查设备状态
|
|
|
|
|
|
// 如果设备状态异常, 则发送邮件通知
|
|
|
|
|
|
devices := worker.GetRedisSetMembers("627gyf3488h")
|
|
|
|
|
|
offline := ""
|
|
|
|
|
|
for _, v := range devices {
|
2024-11-24 15:50:11 +08:00
|
|
|
|
c := worker.IsContainKey("monitor_" + v)
|
|
|
|
|
|
if c == false {
|
2025-03-03 14:04:59 +08:00
|
|
|
|
worker.SetRedisWithExpire("monitor_"+v, "2", time.Hour*24)
|
2024-11-24 15:50:11 +08:00
|
|
|
|
offline += v + ","
|
2024-11-07 15:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if offline != "" {
|
|
|
|
|
|
title := "设备状态异常"
|
|
|
|
|
|
content := "设备状态异常\n设备: " + offline + "\t时间:" + time.Now().String()
|
|
|
|
|
|
go SendMail(title, content)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SendMail(title, content string) {
|
2024-11-27 11:28:23 +08:00
|
|
|
|
//捕获异常
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
fmt.Errorf("tool send mail error: %s", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2024-11-07 15:45:27 +08:00
|
|
|
|
// 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(title, content, []string{"3236990479@qq.com", "lijun@ljsea.top"})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-02 15:39:05 +08:00
|
|
|
|
|
|
|
|
|
|
func SendMailTool(c *gin.Context) {
|
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
|
id1 := int(id.(float64))
|
|
|
|
|
|
|
|
|
|
|
|
var req SendMailReq
|
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
|
user := dao.FindUserByUserID(id1)
|
|
|
|
|
|
if user.ID == 0 {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "user not found", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//目标邮箱地址是否合法
|
|
|
|
|
|
if !service.CheckEmail(req.To) {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "email address is invalid", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Title == "" || req.Content == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "title or content is empty", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//发送邮件
|
|
|
|
|
|
if user.Role == "admin" {
|
|
|
|
|
|
go service.SendEmail(req.To, req.Title, req.Content)
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": "mail will be sent"})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": "no send mail permission", "code": proto.PermissionDenied, "message": "failed"})
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|