Compare commits
4 Commits
00c23de1ca
...
9319390bcd
| Author | SHA1 | Date |
|---|---|---|
|
|
9319390bcd | |
|
|
b1c060baf4 | |
|
|
51dd15182b | |
|
|
4cd65ca0ab |
|
|
@ -18,7 +18,7 @@ type Device struct {
|
||||||
|
|
||||||
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) uint {
|
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) uint {
|
||||||
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
|
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
|
||||||
result := DB.Debug().Create(&device)
|
result := DB.Create(&device)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
fmt.Println("CreateDevice failed", result.Error)
|
fmt.Println("CreateDevice failed", result.Error)
|
||||||
return 0
|
return 0
|
||||||
|
|
@ -27,7 +27,7 @@ func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, devic
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteDeviceByID(id, user int) bool {
|
func DeleteDeviceByID(id, user int) bool {
|
||||||
res := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, user).Delete(&Device{})
|
res := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, user).Delete(&Device{})
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -36,18 +36,18 @@ func DeleteDeviceByID(id, user int) bool {
|
||||||
|
|
||||||
func FindDeviceByID(id, auth_id int) Device {
|
func FindDeviceByID(id, auth_id int) Device {
|
||||||
var device Device
|
var device Device
|
||||||
DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).First(&device)
|
DB.Where("id = ? and auth_id = ?", id, auth_id).First(&device)
|
||||||
return device
|
return device
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDeviceByAuthID(auth_id int) []Device {
|
func FindDeviceByAuthID(auth_id int) []Device {
|
||||||
var devices []Device
|
var devices []Device
|
||||||
DB.Debug().Where("auth_id = ?", auth_id).Find(&devices)
|
DB.Where("auth_id = ?", auth_id).Find(&devices)
|
||||||
return devices
|
return devices
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDeviceStatus(status string, id, auth_id int) bool {
|
func SetDeviceStatus(status string, id, auth_id int) bool {
|
||||||
result := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
result := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +78,7 @@ func UpdateDeviceByID(id, auth_id int, deviceName, deviceType, deviceStatus, dev
|
||||||
deviceInfo = pd.DeviceInfo
|
deviceInfo = pd.DeviceInfo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo})
|
res := DB.Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo})
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,20 +29,20 @@ func DeleteUserByID(id int) int {
|
||||||
|
|
||||||
func FindUserByID(id int) User {
|
func FindUserByID(id int) User {
|
||||||
var user User
|
var user User
|
||||||
DB.Debug().First(&user, id)
|
DB.First(&user, id)
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindUserByName(name string) User {
|
func FindUserByName(name string) User {
|
||||||
var user User
|
var user User
|
||||||
fmt.Println("name:", name)
|
fmt.Println("name:", name)
|
||||||
DB.Debug().Where("name = ?", name).First(&user)
|
DB.Where("name = ?", name).First(&user)
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindUserByEmail(email string) User {
|
func FindUserByEmail(email string) User {
|
||||||
var user User
|
var user User
|
||||||
DB.Debug().Where("email = ?", email).First(&user)
|
DB.Where("email = ?", email).First(&user)
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
24
dao/video.go
24
dao/video.go
|
|
@ -21,35 +21,35 @@ type Video struct {
|
||||||
|
|
||||||
func FindWillDelVideoList(id int) []Video {
|
func FindWillDelVideoList(id int) []Video {
|
||||||
var videos []Video
|
var videos []Video
|
||||||
DB.Debug().Where("auth_id = ?", id).Where("delete_time<=now()").Where("isdelete=0").Find(&videos)
|
DB.Where("auth_id = ?", id).Where("delete_time<=now()").Where("isdelete=0").Find(&videos)
|
||||||
return videos
|
return videos
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete int, createTime, endTime, deleteTime string, fileSize int) uint {
|
func CreateVideo(videoPath, videoName string, cameraID, authID, human, isDelete int, createTime, endTime, deleteTime string, fileSize int) uint {
|
||||||
video := Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, DeleteTime: deleteTime, FileSize: fileSize}
|
video := Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, DeleteTime: deleteTime, FileSize: fileSize}
|
||||||
res := DB.Debug().Create(&video)
|
res := DB.Create(&video)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if deleteTime == "" {
|
if deleteTime == "" {
|
||||||
DB.Debug().Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=?", video.ID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
|
DB.Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=?", video.ID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
|
||||||
}
|
}
|
||||||
return video.ID
|
return video.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteVideoByID(id, user int) int {
|
func DeleteVideoByID(id, user int) int {
|
||||||
delete_time := time.Now().Format("2006-01-02 15:04:05")
|
delete_time := time.Now().Format("2006-01-02 15:04:05")
|
||||||
DB.Debug().Where("id = ? and auth_id = ?", id, user).Updates(&Video{DeleteTime: delete_time, IsDelete: 1})
|
DB.Where("id = ? and auth_id = ?", id, user).Updates(&Video{DeleteTime: delete_time, IsDelete: 1})
|
||||||
DB.Debug().Where("id = ? and auth_id = ?", id, user).Delete(&Video{})
|
DB.Where("id = ? and auth_id = ?", id, user).Delete(&Video{})
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human, isDelete int, createTime, endTime string, fileSize int) bool {
|
func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human, isDelete int, createTime, endTime string, fileSize int) bool {
|
||||||
res := DB.Debug().Model(&Video{}).Where("id = ? and auth_id = ?", videoID, authID).Updates(Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize})
|
res := DB.Model(&Video{}).Where("id = ? and auth_id = ?", videoID, authID).Updates(Video{VideoPath: videoPath, VideoName: videoName, CameraID: cameraID, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize})
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
res = DB.Debug().Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=? and auth_id=?", videoID, authID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
|
res = DB.Exec("update videos set delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY) where id=? and auth_id=?", videoID, authID) //delete_time= DATE_ADD(NOW(), INTERVAL 20 DAY)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -58,26 +58,26 @@ func UpdateVideo(videoPath, videoName string, cameraID, videoID, authID, human,
|
||||||
|
|
||||||
func FindVideoByID(id, auth_id int) Video {
|
func FindVideoByID(id, auth_id int) Video {
|
||||||
var video Video
|
var video Video
|
||||||
DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).Where("isdelete = ?", 0).First(&video)
|
DB.Where("id = ? and auth_id = ?", id, auth_id).Where("isdelete = ?", 0).First(&video)
|
||||||
return video
|
return video
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据用户id查找视频列表,返回最新30条
|
// 根据用户id查找视频列表,返回最新30条
|
||||||
func FindVideoListsByAuthID(auth_id int) []Video {
|
func FindVideoListsByAuthID(auth_id int) []Video {
|
||||||
var videos []Video
|
var videos []Video
|
||||||
DB.Debug().Where("auth_id = ? and isdelete =? ", auth_id, 0).Order("create_time DESC").Limit(30).Find(&videos)
|
DB.Where("auth_id = ? and isdelete =? ", auth_id, 0).Order("create_time DESC").Limit(30).Find(&videos)
|
||||||
return videos
|
return videos
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindVideoListByTime(auth_id int, startTime, endTime string) []Video {
|
func FindVideoListByTime(auth_id int, startTime, endTime string) []Video {
|
||||||
var videos []Video
|
var videos []Video
|
||||||
DB.Debug().Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos)
|
DB.Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos)
|
||||||
return videos
|
return videos
|
||||||
}
|
}
|
||||||
|
|
||||||
// id 为视频id,auth_id为用户id,day为延长天数,返回修改的行数
|
// id 为视频id,auth_id为用户id,day为延长天数,返回修改的行数
|
||||||
func DelayVideo(id, auth_id, day int) int {
|
func DelayVideo(id, auth_id, day int) int {
|
||||||
res := DB.Debug().Exec("update videos set delete_time = date_add(delete_time, interval ? day) where id = ? and auth_id = ? and isdelete=0", day, id, auth_id)
|
res := DB.Exec("update videos set delete_time = date_add(delete_time, interval ? day) where id = ? and auth_id = ? and isdelete=0", day, id, auth_id)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +86,7 @@ func DelayVideo(id, auth_id, day int) int {
|
||||||
|
|
||||||
// id 为用户id,day为延长天数,返回修改的行数
|
// id 为用户id,day为延长天数,返回修改的行数
|
||||||
func DelayAllVideo(id, day int) int {
|
func DelayAllVideo(id, day int) int {
|
||||||
res := DB.Debug().Exec("update videos set delete_time = date_add(delete_time, interval ? day) where auth_id = ? and isdelete = 0", day, id)
|
res := DB.Exec("update videos set delete_time = date_add(delete_time, interval ? day) where auth_id = ? and isdelete = 0", day, id)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,34 +8,39 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeviceAddReq struct {
|
type DeviceAddReq struct {
|
||||||
DeviceName string `json:"device_name"`
|
DeviceName string `json:"device_name" form:"device_name" binding:"required"`
|
||||||
DeviceIP string `json:"device_ip"`
|
DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"`
|
||||||
DeviceStatus string `json:"device_status"`
|
DeviceStatus string `json:"device_status" form:"device_status" binding:"required"`
|
||||||
AuthID int `json:"auth_id"`
|
AuthID int `json:"auth_id" form:"auth_id" binding:"required"`
|
||||||
DeviceInfo string `json:"device_info"`
|
DeviceInfo string `json:"device_info" form:"device_info" binding:"required"`
|
||||||
DeviceType string `json:"device_type"`
|
DeviceType string `json:"device_type" form:"device_type" binding:"required"`
|
||||||
DeviceLocation string `json:"device_location"`
|
DeviceLocation string `json:"device_location" form:"device_location" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceUpdateReq struct {
|
type DeviceUpdateReq struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id" form:"id"`
|
||||||
DeviceName string `json:"device_name"`
|
DeviceName string `json:"device_name" form:"device_name" binding:"required"`
|
||||||
DeviceIP string `json:"device_ip"`
|
DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"`
|
||||||
DeviceStatus string `json:"device_status"`
|
DeviceStatus string `json:"device_status" form:"device_status" binding:"required"`
|
||||||
AuthID int `json:"auth_id"`
|
AuthID int `json:"auth_id" form:"auth_id" binding:"required"`
|
||||||
DeviceInfo string `json:"device_info"`
|
DeviceInfo string `json:"device_info" form:"device_info" binding:"required"`
|
||||||
DeviceType string `json:"device_type"`
|
DeviceType string `json:"device_type" form:"device_type" binding:"required"`
|
||||||
DeviceLocation string `json:"device_location"`
|
DeviceLocation string `json:"device_location" form:"device_location" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceStatus struct {
|
type DeviceStatus struct {
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip" form:"ip" binding:"required"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status" form:"status" binding:"required"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id" form:"id" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeviceDelReq struct {
|
type DeviceDelReq struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id" form:"id" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeviceRestartReq struct {
|
||||||
|
ID int `json:"id" form:"id" binding:"required"`
|
||||||
|
Option string `json:"option" form:"option" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetUpDeviceGroup(router *gin.Engine) {
|
func SetUpDeviceGroup(router *gin.Engine) {
|
||||||
|
|
@ -142,23 +147,38 @@ func GetDeviceList(c *gin.Context) {
|
||||||
|
|
||||||
func RestartDevice(c *gin.Context) {
|
func RestartDevice(c *gin.Context) {
|
||||||
user_id, _ := c.Get("id")
|
user_id, _ := c.Get("id")
|
||||||
var req DeviceDelReq
|
var req DeviceRestartReq
|
||||||
if err := c.ShouldBind(&req); err != nil {
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
device_id := req.ID
|
device_id := req.ID
|
||||||
device := service.GetDevice(device_id, int(user_id.(float64)))
|
if req.Option == "one" {
|
||||||
if device.ID != 0 {
|
device := service.GetDevice(device_id, int(user_id.(float64)))
|
||||||
if device.DeviceIP != "" {
|
if device.ID != 0 {
|
||||||
if Restart(device.DeviceIP) {
|
if device.DeviceIP != "" {
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
if Restart(device.DeviceIP) {
|
||||||
} else {
|
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
||||||
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
|
} else if req.Option == "all" {
|
||||||
|
devices := service.GetDeviceList(int(user_id.(float64)))
|
||||||
|
if len(devices) > 0 {
|
||||||
|
for _, device := range devices {
|
||||||
|
if device.DeviceIP != "" {
|
||||||
|
if !Restart(device.DeviceIP) {
|
||||||
|
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
||||||
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package handler
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
@ -184,7 +183,6 @@ func registerHandler(c *gin.Context) {
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"error": err.Error()})
|
c.JSON(200, gin.H{"error": err.Error()})
|
||||||
}
|
}
|
||||||
fmt.Println(req_data)
|
|
||||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||||
// 返回令牌
|
// 返回令牌
|
||||||
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User})
|
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User})
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -105,7 +104,6 @@ func GetVideo(c *gin.Context) {
|
||||||
var vp_req videoPReq
|
var vp_req videoPReq
|
||||||
user_id, _ := c.Get("id")
|
user_id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindQuery(&vp_req); err == nil {
|
if err := c.ShouldBindQuery(&vp_req); err == nil {
|
||||||
fmt.Println(vp_req)
|
|
||||||
video := service.GetVideo(vp_req.ID, int(user_id.(float64)))
|
video := service.GetVideo(vp_req.ID, int(user_id.(float64)))
|
||||||
name := video.VideoName
|
name := video.VideoName
|
||||||
path := video.VideoPath
|
path := video.VideoPath
|
||||||
|
|
@ -136,7 +134,6 @@ func CreateVideo(c *gin.Context) {
|
||||||
var video_req videoReq
|
var video_req videoReq
|
||||||
user_id, _ := c.Get("id")
|
user_id, _ := c.Get("id")
|
||||||
if err := c.ShouldBind(&video_req); err == nil {
|
if err := c.ShouldBind(&video_req); err == nil {
|
||||||
fmt.Println(video_req)
|
|
||||||
id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.DeleteTime, video_req.FileSize)
|
id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.CameraID, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.DeleteTime, video_req.FileSize)
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": 1, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "create video failed", "code": 1, "message": "failed"})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue