2024-03-31 20:09:20 +08:00

140 lines
3.3 KiB
Go

package controller
import (
"encoding/json"
"errors"
"fmt"
"github.com/loveuer/nf/nft/resp"
"github.com/loveuer/nfflow/internal/database"
"github.com/loveuer/nfflow/internal/model"
"github.com/loveuer/nfflow/internal/opt"
"github.com/loveuer/nfflow/internal/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"gorm.io/gorm"
"strings"
"time"
)
func (u uc) GetUser(id uint64) (*model.User, error) {
var (
err error
target = new(model.User)
key = fmt.Sprintf("%s:user:id:%d", opt.CachePrefix, id)
bs []byte
)
if opt.EnableUserCache {
if bs, err = u.c.Get(util.Timeout(3), key); err != nil {
logrus.Warnf("controller.GetUser: get user by cache key=%s err=%v", key, err)
goto ByDB
}
if err = json.Unmarshal(bs, target); err != nil {
logrus.Warnf("controller.GetUser: json unmarshal key=%s by=%s err=%v", key, string(bs), err)
goto ByDB
}
return target, nil
}
ByDB:
if err = u.db.Session(util.Timeout(3)).
Model(&model.User{}).
Where("id = ?", id).
Take(target).
Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// tips: 公开项目需要考虑击穿处理
return target, resp.NewError(400, "目标不存在", err, nil)
}
return target, resp.NewError(500, "", err, nil)
}
if opt.EnableUserCache {
if err = u.CacheUser(target); err != nil {
logrus.Warnf("controller.GetUser: cache user key=%s err=%v", key, err)
}
}
return target, nil
}
func (u uc) GetUserByToken(token string) (*model.User, error) {
strs := strings.Split(token, ".")
if len(strs) != 3 {
return nil, fmt.Errorf("controller.GetUserByToken: jwt token invalid, token=%s", token)
}
key := fmt.Sprintf("%s:user:token:%s", opt.CachePrefix, strs[2])
bs, err := u.c.Get(util.Timeout(3), key)
if err != nil {
return nil, err
}
logrus.Tracef("controller.GetUserByToken: key=%s cache bytes=%s", key, string(bs))
userId := cast.ToUint64(string(bs))
if userId == 0 {
return nil, fmt.Errorf("controller.GetUserByToken: bs=%s cast to uint64 err", string(bs))
}
var op *model.User
if op, err = u.GetUser(userId); err != nil {
return nil, err
}
return op, nil
}
func (u uc) CacheUser(target *model.User) error {
key := fmt.Sprintf("%s:user:id:%d", opt.CachePrefix, target.Id)
return u.c.Set(util.Timeout(3), key, target)
}
func (u uc) CacheToken(token string, user *model.User) error {
strs := strings.Split(token, ".")
if len(strs) != 3 {
return fmt.Errorf("controller.CacheToken: jwt token invalid")
}
key := fmt.Sprintf("%s:user:token:%s", opt.CachePrefix, strs[2])
return u.c.SetEx(util.Timeout(3), key, user.Id, opt.TokenTimeout)
}
func (u uc) RmUserCache(id uint64) error {
key := fmt.Sprintf("%s:user:id:%d", opt.CachePrefix, id)
return u.c.Del(util.Timeout(3), key)
}
func (u uc) DeleteUser(id uint64) error {
var (
err error
now = time.Now()
username = "CONCAT(username, '@del')"
)
if opt.Cfg.Database.Type == "sqlite" {
username = "username || '@del'"
}
if err = database.DB.Session(util.Timeout(5)).
Model(&model.User{}).
Where("id = ?", id).
Updates(map[string]any{
"deleted_at": now.UnixMilli(),
"username": gorm.Expr(username),
}).Error; err != nil {
return resp.NewError(500, "", err, nil)
}
if opt.EnableUserCache {
if err = u.RmUserCache(id); err != nil {
logrus.Warnf("controller.DeleteUser: rm user=%d cache err=%v", id, err)
}
}
return nil
}