You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.6 KiB
78 lines
1.6 KiB
package live
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/user"
|
|
"recook/tools"
|
|
"strconv"
|
|
)
|
|
|
|
// 验证用户信息
|
|
func UserInfo(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Request.Header.Get("X-Recook-ID"))
|
|
if id <= 0 || err != nil {
|
|
back.Fail(c, "游客无法使用该功能,请登录")
|
|
return
|
|
}
|
|
userId := getUserId(uint(id))
|
|
if userId != "" {
|
|
uid, _ := strconv.ParseUint(userId, 10, 64)
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"userId": uid,
|
|
})
|
|
return
|
|
} else {
|
|
login := &user.Login{}
|
|
dbc.DB.First(login, "id=?", id)
|
|
if login.UserID != 0 {
|
|
setUserId(login)
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"userId": login.UserID,
|
|
})
|
|
return
|
|
}
|
|
}
|
|
back.Fail(c, "登录超时,请登录")
|
|
return
|
|
}
|
|
|
|
// 获取userId缓存
|
|
func getUserId(id uint) string {
|
|
k := fmt.Sprintf("live_user_id_%d", id)
|
|
val, _ := dbc.Rds.Get(k).Result()
|
|
return val
|
|
}
|
|
|
|
// 缓存userId
|
|
func setUserId(l *user.Login) {
|
|
k := fmt.Sprintf("live_user_id_%d", l.ID)
|
|
err := dbc.Rds.Set(k, l.UserID, 0).Err()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type argsUsers struct {
|
|
Ids []int `json:"ids" form:"ids"`
|
|
}
|
|
|
|
// 根据ids批量获取会员基础信息
|
|
func GetUsers(c *gin.Context) {
|
|
argsList := argsUsers{}
|
|
if err := tools.ParseParams(&argsList, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if len(argsList.Ids) == 0 {
|
|
back.Fail(c, "参数不全")
|
|
return
|
|
}
|
|
information := []user.Information{}
|
|
dbc.DB.Find(&information, "id in (?)", argsList.Ids)
|
|
back.Suc(c, "操作成功", information)
|
|
return
|
|
}
|