2024-05-18 17:12:24 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-21 14:57:45 +08:00
|
|
|
"crypto/md5"
|
|
|
|
|
"encoding/hex"
|
2024-06-14 17:59:53 +08:00
|
|
|
"encoding/json"
|
2024-05-18 17:12:24 +08:00
|
|
|
"fmt"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/golang-jwt/jwt"
|
2024-06-01 16:22:18 +08:00
|
|
|
"github.com/google/uuid"
|
2024-05-18 17:12:24 +08:00
|
|
|
"time"
|
2024-06-25 09:38:21 +08:00
|
|
|
"videoplayer/proto"
|
2024-05-18 17:12:24 +08:00
|
|
|
"videoplayer/service"
|
|
|
|
|
"videoplayer/worker"
|
|
|
|
|
)
|
|
|
|
|
|
2024-06-25 09:38:21 +08:00
|
|
|
var signingKey = []byte(proto.TOKEN_SECRET)
|
2024-05-18 17:12:24 +08:00
|
|
|
|
|
|
|
|
func SetUpUserGroup(router *gin.Engine) {
|
|
|
|
|
userGroup := router.Group("/user")
|
|
|
|
|
userGroup.POST("/register", registerHandler)
|
|
|
|
|
userGroup.POST("/login", loginHandler)
|
2024-06-13 11:33:27 +08:00
|
|
|
userGroup.POST("/uuid", GetScanUUID)
|
2024-06-01 16:22:18 +08:00
|
|
|
userGroup.POST("/gqr", GetQRStatus)
|
|
|
|
|
userGroup.POST("/sqr", SetQRStatus)
|
2024-06-07 20:16:08 +08:00
|
|
|
userGroup.POST("/confirm", ConfirmQRLogin)
|
2024-06-28 10:10:23 +08:00
|
|
|
userGroup.POST("/search", SearchHandler)
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RLReq struct {
|
2024-05-28 15:15:02 +08:00
|
|
|
User string `json:"username" form:"username"`
|
|
|
|
|
Email string `json:"email" form:"email"`
|
|
|
|
|
Password string `json:"password" form:"password"`
|
2024-06-05 11:47:03 +08:00
|
|
|
Age int `json:"age" form:"age"`
|
|
|
|
|
Gender string `json:"gender" form:"gender"`
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-01 16:22:18 +08:00
|
|
|
type QRReq struct {
|
2024-06-11 18:38:41 +08:00
|
|
|
UUID string `json:"uuid" form:"uuid"`
|
|
|
|
|
Address string `json:"address" form:"address"`
|
|
|
|
|
IP string `json:"ip" form:"ip"`
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-28 10:10:23 +08:00
|
|
|
type SearchReq struct {
|
|
|
|
|
Keyword string `json:"keyword" form:"keyword"`
|
|
|
|
|
ID int `json:"id" form:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 11:33:27 +08:00
|
|
|
func GetScanUUID(c *gin.Context) {
|
2024-06-12 10:22:52 +08:00
|
|
|
var ReqData QRReq
|
|
|
|
|
if err := c.ShouldBind(&ReqData); err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
|
2024-06-12 10:22:52 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-14 17:59:53 +08:00
|
|
|
data := map[string]interface{}{"status": "0", "address": ReqData.Address, "ip": c.ClientIP()}
|
|
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-01 16:22:18 +08:00
|
|
|
id := uuid.New()
|
2024-06-14 17:59:53 +08:00
|
|
|
res := worker.SetRedisWithExpire(id.String(), string(jsonData), time.Minute*30)
|
2024-06-12 10:22:52 +08:00
|
|
|
if res {
|
2024-06-14 17:59:53 +08:00
|
|
|
var retrievedData map[string]interface{}
|
|
|
|
|
if err2 := json.Unmarshal([]byte(worker.GetRedis(id.String())), &retrievedData); err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": retrievedData, "data": id.String()})
|
2024-06-12 10:22:52 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "qr code invalid", "data": "1"})
|
2024-06-12 10:22:52 +08:00
|
|
|
}
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetQRStatus(c *gin.Context) {
|
2024-06-07 20:16:08 +08:00
|
|
|
var qrsetReq QRReq
|
2024-06-13 11:33:27 +08:00
|
|
|
if err := c.ShouldBind(&qrsetReq); err == nil && qrsetReq.UUID != "" {
|
2024-06-12 17:43:15 +08:00
|
|
|
if worker.IsContainKey(qrsetReq.UUID) == false {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found in server", "data": "0"})
|
2024-06-12 17:43:15 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-14 17:59:53 +08:00
|
|
|
var retrievedData map[string]interface{}
|
|
|
|
|
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
retrievedData["status"] = "1"
|
|
|
|
|
jsonData, err2 := json.Marshal(retrievedData)
|
|
|
|
|
if err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
res := worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*30)
|
2024-06-07 20:16:08 +08:00
|
|
|
if res {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": retrievedData})
|
2024-06-07 20:16:08 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "qr code invalid", "data": "1"})
|
2024-06-07 20:16:08 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "2"})
|
2024-06-07 20:16:08 +08:00
|
|
|
}
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确认返回token数据
|
|
|
|
|
func ConfirmQRLogin(c *gin.Context) {
|
2024-06-07 20:16:08 +08:00
|
|
|
var qrsetReq QRReq
|
2024-06-13 18:34:18 +08:00
|
|
|
if err := c.ShouldBind(&qrsetReq); err == nil && qrsetReq.UUID != "" {
|
2024-06-07 20:16:08 +08:00
|
|
|
//user_id, _ := c.Get("id")
|
|
|
|
|
user_name, _ := c.Get("username")
|
|
|
|
|
if user_name != "" {
|
|
|
|
|
key := "user_" + user_name.(string)
|
|
|
|
|
token := worker.GetRedis(key)
|
2024-06-12 10:22:52 +08:00
|
|
|
if token == "" {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.RedisGetError, "message": "Token不存在", "data": "20"})
|
2024-06-12 10:22:52 +08:00
|
|
|
}
|
2024-06-13 11:23:58 +08:00
|
|
|
if worker.IsContainKey(qrsetReq.UUID) == false {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found in server", "data": "0"})
|
2024-06-13 11:23:58 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-14 17:59:53 +08:00
|
|
|
var retrievedData map[string]interface{}
|
|
|
|
|
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrsetReq.UUID)), &retrievedData); err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
retrievedData["status"] = token
|
|
|
|
|
jsonData, err2 := json.Marshal(retrievedData)
|
|
|
|
|
if err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-14 17:59:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if worker.SetRedisWithExpire(qrsetReq.UUID, string(jsonData), time.Minute*10) {
|
2024-06-12 10:22:52 +08:00
|
|
|
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"})
|
2024-06-07 20:16:08 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.RedisSetError, "message": "设置Token失败", "data": "8"})
|
2024-06-07 20:16:08 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.RedisGetError, "message": "failed", "data": "20"})
|
2024-06-07 20:16:08 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err, "data": "3"})
|
2024-06-07 20:16:08 +08:00
|
|
|
}
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetQRStatus(c *gin.Context) {
|
|
|
|
|
var qrReq QRReq
|
|
|
|
|
if err := c.ShouldBind(&qrReq); err == nil {
|
2024-06-14 17:59:53 +08:00
|
|
|
var retrievedData map[string]interface{}
|
|
|
|
|
if err2 := json.Unmarshal([]byte(worker.GetRedis(qrReq.UUID)), &retrievedData); err2 != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": err2, "data": "2"})
|
2024-06-11 18:38:41 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-06-14 17:59:53 +08:00
|
|
|
str := retrievedData["status"].(string)
|
2024-06-01 16:22:18 +08:00
|
|
|
switch str {
|
2024-06-12 10:22:52 +08:00
|
|
|
case "":
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.UUIDNotFound, "message": "uuid not found", "data": "0"}) //空值
|
2024-06-01 16:22:18 +08:00
|
|
|
case "0":
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "0"}) //空值
|
2024-06-01 16:22:18 +08:00
|
|
|
case "1":
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "1"}) //已扫描待确认
|
2024-06-01 16:22:18 +08:00
|
|
|
default:
|
|
|
|
|
// 解析 JWT 令牌
|
|
|
|
|
token, err := jwt.Parse(str, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
|
return signingKey, nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenParseError, "message": "error"})
|
2024-06-01 16:22:18 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 返回令牌
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
data["id"] = token.Claims.(jwt.MapClaims)["id"]
|
|
|
|
|
data["username"] = token.Claims.(jwt.MapClaims)["username"]
|
|
|
|
|
data["email"] = token.Claims.(jwt.MapClaims)["email"]
|
|
|
|
|
data["token"] = str
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data}) //确认返回token数据
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
|
2024-06-01 16:22:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 10:10:23 +08:00
|
|
|
func SearchHandler(c *gin.Context) {
|
|
|
|
|
var req_data SearchReq
|
|
|
|
|
if err := c.ShouldBind(&req_data); err == nil {
|
|
|
|
|
if req_data.ID != -1 {
|
|
|
|
|
user := service.GetUserByID(req_data.ID)
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": user})
|
2024-06-28 10:38:55 +08:00
|
|
|
return
|
2024-06-28 10:10:23 +08:00
|
|
|
} else if req_data.Keyword != "" {
|
|
|
|
|
users := service.GetUserByNameLike(req_data.Keyword)
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": users})
|
2024-06-28 10:38:55 +08:00
|
|
|
return
|
2024-06-28 10:10:23 +08:00
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "error", "data": "无ID 与 关键字"})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "error"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 17:12:24 +08:00
|
|
|
func loginHandler(c *gin.Context) {
|
|
|
|
|
var req_data RLReq
|
|
|
|
|
tokenString := ""
|
2024-05-28 15:15:02 +08:00
|
|
|
if err := c.ShouldBind(&req_data); err == nil {
|
2024-06-10 12:23:34 +08:00
|
|
|
if len(req_data.Password) != 32 {
|
2024-05-21 14:57:45 +08:00
|
|
|
hasher := md5.New()
|
|
|
|
|
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
|
|
|
|
req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值
|
|
|
|
|
}
|
2024-06-07 20:16:08 +08:00
|
|
|
user := service.GetUser(req_data.User, req_data.Password, req_data.Password)
|
2024-05-18 17:12:24 +08:00
|
|
|
if user.ID != 0 {
|
2024-06-09 11:22:58 +08:00
|
|
|
key := "user_" + user.Name
|
|
|
|
|
redis_token := worker.GetRedis(string(key))
|
2024-05-31 15:18:24 +08:00
|
|
|
if redis_token == "" {
|
|
|
|
|
// 生成 JWT 令牌
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
"username": user.Name,
|
|
|
|
|
"id": user.ID,
|
|
|
|
|
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 10小时后过期
|
|
|
|
|
})
|
|
|
|
|
tokenString, err = token.SignedString(signingKey)
|
|
|
|
|
if err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
|
2024-05-31 15:18:24 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入
|
|
|
|
|
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
2024-06-11 18:38:41 +08:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
data["id"] = user.ID
|
|
|
|
|
data["username"] = user.Name
|
|
|
|
|
data["email"] = user.Email
|
|
|
|
|
worker.SetHash(tokenString, data) // 将用户信息存入
|
2024-05-31 15:18:24 +08:00
|
|
|
} else {
|
|
|
|
|
tokenString = redis_token
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
// 返回令牌
|
2024-05-28 15:15:02 +08:00
|
|
|
data := make(map[string]interface{})
|
2024-05-31 15:18:24 +08:00
|
|
|
data["id"] = user.ID
|
|
|
|
|
data["username"] = user.Name
|
|
|
|
|
data["email"] = user.Email
|
|
|
|
|
data["token"] = tokenString
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
|
2024-05-18 17:12:24 +08:00
|
|
|
} else {
|
2024-06-07 20:16:08 +08:00
|
|
|
//用户名或密码错误
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": "用户名或密码错误", "code": proto.UsernameOrPasswordError, "message": "error"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func registerHandler(c *gin.Context) {
|
|
|
|
|
var req_data RLReq
|
|
|
|
|
tokenString := ""
|
|
|
|
|
if err := c.ShouldBindJSON(&req_data); err == nil {
|
2024-06-10 12:23:34 +08:00
|
|
|
if len(req_data.Password) != 32 {
|
2024-05-21 14:57:45 +08:00
|
|
|
hasher := md5.New()
|
|
|
|
|
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
|
|
|
|
req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值
|
|
|
|
|
}
|
2024-05-21 15:13:01 +08:00
|
|
|
if service.ContainsUser(req_data.User, req_data.Email) == true {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": "user already exists", "code": proto.UsernameExists, "message": "error"})
|
2024-05-21 15:13:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-05-18 17:12:24 +08:00
|
|
|
id := service.CreateUser(req_data.User, req_data.Password, req_data.Email)
|
|
|
|
|
// 生成 JWT 令牌
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
"username": req_data.User,
|
|
|
|
|
"id": id,
|
2024-05-21 11:10:21 +08:00
|
|
|
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
2024-05-18 17:12:24 +08:00
|
|
|
})
|
|
|
|
|
tokenString, err = token.SignedString(signingKey)
|
|
|
|
|
if err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.TokenGenerationError, "message": "error"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.DeviceRestartFailed, "message": "error"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|
|
|
|
|
fmt.Println(req_data)
|
2024-06-12 10:22:52 +08:00
|
|
|
res := worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
|
|
|
|
if !res {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"error": "set token error", "code": proto.RedisSetError, "message": "error"})
|
2024-06-12 10:22:52 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-05-18 17:12:24 +08:00
|
|
|
// 返回令牌
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": proto.SuccessCode, "message": "success"})
|
2024-05-18 17:12:24 +08:00
|
|
|
}
|