扫码登录uuid,redis hash设置
This commit is contained in:
parent
9cf0306744
commit
4bbcbb3531
|
|
@ -34,20 +34,23 @@ type RLReq struct {
|
||||||
|
|
||||||
type QRReq struct {
|
type QRReq struct {
|
||||||
UUID string `json:"uuid" form:"uuid"`
|
UUID string `json:"uuid" form:"uuid"`
|
||||||
|
Address string `json:"address" form:"address"`
|
||||||
|
IP string `json:"ip" form:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanUUID(c *gin.Context) {
|
func ScanUUID(c *gin.Context) {
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
worker.SetRedisWithExpire(id.String(), "0", time.Minute*3)
|
worker.SetHash(id.String(), map[string]interface{}{"status": "0", "address": "", "ip": ""})
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data": id.String()})
|
c.JSON(200, gin.H{"code": 0, "message": "success", "data": id.String()})
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetQRStatus(c *gin.Context) {
|
func SetQRStatus(c *gin.Context) {
|
||||||
var qrsetReq QRReq
|
var qrsetReq QRReq
|
||||||
if err := c.ShouldBind(&qrsetReq); err == nil {
|
if err := c.ShouldBind(&qrsetReq); err == nil {
|
||||||
res := worker.SetRedis(qrsetReq.UUID, "1")
|
res := worker.SetHashWithField(qrsetReq.UUID, "status", "1")
|
||||||
if res {
|
if res {
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"})
|
data := worker.GetHashAll(qrsetReq.UUID)
|
||||||
|
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"code": 1, "message": "该二维码数据无效", "data": "1"})
|
c.JSON(200, gin.H{"code": 1, "message": "该二维码数据无效", "data": "1"})
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +84,13 @@ 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 {
|
||||||
str := worker.GetRedis(qrReq.UUID)
|
end := worker.SetHashWithField(qrReq.UUID, "address", qrReq.Address)
|
||||||
|
end_ := worker.SetHashWithField(qrReq.UUID, "ip", qrReq.IP)
|
||||||
|
if !end || !end_ {
|
||||||
|
c.JSON(200, gin.H{"code": 1, "message": "failed", "data": "2"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
str := worker.GetHash(qrReq.UUID, "status")
|
||||||
switch str {
|
switch str {
|
||||||
case "0":
|
case "0":
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"}) //空值
|
c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"}) //空值
|
||||||
|
|
@ -137,7 +146,11 @@ func loginHandler(c *gin.Context) {
|
||||||
|
|
||||||
worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入
|
worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入
|
||||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||||
worker.SetHash(tokenString, int(user.ID), user.Name, user.Email) // 将用户信息存入
|
data := make(map[string]interface{})
|
||||||
|
data["id"] = user.ID
|
||||||
|
data["username"] = user.Name
|
||||||
|
data["email"] = user.Email
|
||||||
|
worker.SetHash(tokenString, data) // 将用户信息存入
|
||||||
} else {
|
} else {
|
||||||
tokenString = redis_token
|
tokenString = redis_token
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ func SetRedis(key string, value string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置hash
|
// 设置hash
|
||||||
func SetHash(key string, id int, name, email string) bool {
|
func SetHashWithTime(key string, id int, name, email string, duration time.Duration) bool {
|
||||||
//捕获错误,如果错误返回
|
//捕获错误,如果错误返回
|
||||||
|
|
||||||
ctx := context.Background() // 创建一个上下文
|
ctx := context.Background() // 创建一个上下文
|
||||||
|
|
@ -85,17 +85,50 @@ func SetHash(key string, id int, name, email string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取hash
|
// 设置redis hash,设置过期时间
|
||||||
func GetHash(key string) RUser {
|
func SetHash(key string, data map[string]interface{}) bool {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
val, err := redisClient.HGetAll(ctx, key).Result() // 从 Redis 读取哈希表, 如果键不存在则返回空字符串, 如果出现错误则返回错误
|
err := redisClient.HMSet(ctx, key, data).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error getting key: %v", err)
|
fmt.Println("%v :Error setting hash: %v", key, err)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
id, _ := strconv.Atoi(val["id"])
|
err = redisClient.Expire(ctx, key, time.Minute*3).Err()
|
||||||
name, _ := val["name"]
|
if err != nil {
|
||||||
email, _ := val["email"]
|
fmt.Println("%v :Error setting expire: %v", key, err)
|
||||||
return RUser{ID: id, Name: name, Email: email}
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetHashWithField(key string, field string, value string) bool {
|
||||||
|
ctx := context.Background()
|
||||||
|
err := redisClient.HSet(ctx, key, field, value).Err()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error setting key: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetHash(key string, field string) string {
|
||||||
|
ctx := context.Background()
|
||||||
|
val, err := redisClient.HGet(ctx, key, field).Result()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting hash: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetHashAll(key string) map[string]string {
|
||||||
|
ctx := context.Background()
|
||||||
|
val, err := redisClient.HGetAll(ctx, key).Result()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting hash: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置redis
|
// 设置redis
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue