修复用户二维码登录redis设置后消失问题,修改logger错误输出,实现视频按日期及小时精度查找。

This commit is contained in:
junleea 2024-06-14 17:59:53 +08:00
parent a8a1826d9c
commit 062fb6f4b6
4 changed files with 76 additions and 33 deletions

View File

@ -3,6 +3,7 @@ package handler
import ( import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
@ -44,11 +45,21 @@ func GetScanUUID(c *gin.Context) {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"}) c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"})
return return
} }
data := map[string]interface{}{"status": "0", "address": ReqData.Address, "ip": c.ClientIP()}
jsonData, err := json.Marshal(data)
if err != nil {
c.JSON(200, gin.H{"code": 9, "message": err, "data": "2"})
return
}
id := uuid.New() id := uuid.New()
res := worker.SetHash(id.String(), map[string]interface{}{"status": "0", "address": ReqData.Address, "ip": c.ClientIP()}) res := worker.SetRedisWithExpire(id.String(), string(jsonData), time.Minute*30)
if res { if res {
data := worker.GetHashAll(id.String()) var retrievedData map[string]interface{}
c.JSON(200, gin.H{"code": 0, "message": data, "data": id.String()}) if err2 := json.Unmarshal([]byte(worker.GetRedis(id.String())), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
return
}
c.JSON(200, gin.H{"code": 0, "message": retrievedData, "data": id.String()})
} else { } else {
c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"}) c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"})
} }
@ -61,10 +72,20 @@ func SetQRStatus(c *gin.Context) {
c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"}) c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"})
return return
} }
res := worker.SetHashWithField(qrsetReq.UUID, "status", "1") var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
return
}
retrievedData["status"] = "1"
jsonData, err2 := json.Marshal(retrievedData)
if err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
return
}
res := worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*30)
if res { if res {
data := worker.GetHashAll(qrsetReq.UUID) c.JSON(200, gin.H{"code": 0, "message": "success", "data": retrievedData})
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data})
} else { } else {
c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"}) c.JSON(200, gin.H{"code": 8, "message": "qr code invalid", "data": "1"})
} }
@ -89,7 +110,18 @@ func ConfirmQRLogin(c *gin.Context) {
c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"}) c.JSON(200, gin.H{"code": 18, "message": "uuid not found in server", "data": "0"})
return return
} }
if worker.SetHashWithField(qrsetReq.UUID, "status", token) { var retrievedData map[string]interface{}
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
return
}
retrievedData["status"] = token
jsonData, err2 := json.Marshal(retrievedData)
if err2 != nil {
c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
return
}
if worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*10) {
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"}) c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"})
} else { } else {
c.JSON(200, gin.H{"code": 8, "message": "设置Token失败", "data": "8"}) c.JSON(200, gin.H{"code": 8, "message": "设置Token失败", "data": "8"})
@ -105,13 +137,12 @@ func ConfirmQRLogin(c *gin.Context) {
func GetQRStatus(c *gin.Context) { func GetQRStatus(c *gin.Context) {
var qrReq QRReq var qrReq QRReq
if err := c.ShouldBind(&qrReq); err == nil { if err := c.ShouldBind(&qrReq); err == nil {
end := worker.SetHashWithField(qrReq.UUID, "address", qrReq.Address) var retrievedData map[string]interface{}
end_ := worker.SetHashWithField(qrReq.UUID, "ip", qrReq.IP) if err2 := json.Unmarshal([]byte(worker.GetRedis(qrReq.UUID)), &retrievedData); err2 != nil {
if !end || !end_ { c.JSON(200, gin.H{"code": 9, "message": err2, "data": "2"})
c.JSON(200, gin.H{"code": 8, "message": "set redis failed", "data": "8"})
return return
} }
str := worker.GetHash(qrReq.UUID, "status") str := retrievedData["status"].(string)
switch str { switch str {
case "": case "":
c.JSON(200, gin.H{"code": 18, "message": "uuid not found", "data": "0"}) //空值 c.JSON(200, gin.H{"code": 18, "message": "uuid not found", "data": "0"}) //空值

View File

@ -150,9 +150,10 @@ func CreateVideo(c *gin.Context) {
func GetVideoList(c *gin.Context) { func GetVideoList(c *gin.Context) {
var gvl_req gvlReq var gvl_req gvlReq
gvl_req.Hour = "33"
id, _ := c.Get("id") id, _ := c.Get("id")
if err := c.ShouldBind(&gvl_req); err == nil { if err := c.ShouldBind(&gvl_req); err == nil {
videos := service.GetVideoList(int(id.(float64)), gvl_req.StartTime, gvl_req.EndTime) videos := service.GetVideoList(int(id.(float64)), gvl_req.StartTime, gvl_req.EndTime, gvl_req.Hour)
c.JSON(http.StatusOK, gin.H{"data": videos, "code": 0, "message": "success"}) c.JSON(http.StatusOK, gin.H{"data": videos, "code": 0, "message": "success"})
} else { } else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"}) c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": 9, "message": "failed"})

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"io" "io"
@ -45,10 +44,7 @@ func writeLogger(c *gin.Context) {
params = string(bodyBytes) params = string(bodyBytes)
} }
} }
res := dao.InsertLogToDB(path, ip, method, params) go dao.InsertLogToDB(path, ip, method, params)
if res == 0 {
fmt.Println("insert log failed")
}
} }
func JWTAuthMiddleware() gin.HandlerFunc { func JWTAuthMiddleware() gin.HandlerFunc {

View File

@ -1,8 +1,7 @@
package service package service
import ( import (
"fmt" "strings"
"time"
"videoplayer/dao" "videoplayer/dao"
) )
@ -14,23 +13,39 @@ func GetWillDelVideoList(id int) []dao.Video {
return dao.FindWillDelVideoList(id) return dao.FindWillDelVideoList(id)
} }
func GetVideoList(auth_id int, start, end string) []dao.Video { func GetVideoList(auth_id int, start, end, hour string) []dao.Video {
if start == "" { if start == "" {
return dao.FindVideoListsByAuthID(auth_id) return dao.FindVideoListsByAuthID(auth_id)
} else { } else {
s, err := time.Parse("2006/1/02 15:04:05", start) //s, err := time.Parse("2006/1/02 15:04:05", start)
if err != nil { //if err != nil {
s, err = time.Parse("2006/01/02 15:04:05", start) // s, err = time.Parse("2006/01/02 15:04:05", start)
} //}
e, err2 := time.Parse("2006/1/02 15:04:05", end) //e, err2 := time.Parse("2006/1/02 15:04:05", end)
if err2 != nil { //if err2 != nil {
e, err2 = time.Parse("2006/01/02 15:04:05", end) // e, err2 = time.Parse("2006/01/02 15:04:05", end)
} //}
if s.After(e) || err != nil || err2 != nil { //if s.After(e) || err != nil || err2 != nil {
fmt.Println(err) // fmt.Println(err)
fmt.Println(err2) // fmt.Println(err2)
return []dao.Video{} // return []dao.Video{}
//}
start = strings.Replace(start, "/", "-", -1)
end = strings.Replace(end, "/", "-", -1)
start = strings.Replace(start, " ", " ", -1)
end = strings.Replace(end, " ", " ", -1)
start = start[0:5] + "0" + start[5:]
end = end[0:5] + "0" + end[5:]
if hour != "33" {
ss := strings.Split(start, " ")
ss1 := strings.Split(ss[1], ":")
start = ss[0] + " " + hour + ":" + ss1[1] + ":" + ss1[2]
es := strings.Split(end, " ")
es1 := strings.Split(es[1], ":")
end = es[0] + " " + hour + ":" + es1[1] + ":" + es1[2]
} }
return dao.FindVideoListByTime(auth_id, start, end) return dao.FindVideoListByTime(auth_id, start, end)
} }
} }