-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
63 lines (56 loc) · 1.48 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Path: internal/apiserver/service
// FileName: token.go
// Created by dkedTeam
// Author: GJing
// Date: 2022/12/27$ 17:49$
package service
import (
"github.com/gjing1st/gin-admin-frame/internal/apiserver/model/entity"
"github.com/gjing1st/gin-admin-frame/internal/pkg/utils"
"github.com/gjing1st/gin-admin-frame/internal/pkg/utils/rand"
"github.com/gjing1st/gin-admin-frame/pkg/errcode"
"strconv"
)
type TokenService struct {
}
const tokenPrefix = "admin:"
// GenerateToken
// @description: 生成token
// @param:
// @author: GJing
// @email: guojing@tna.cn
// @date: 2022/4/15 14:37
// @success:
func (td TokenService) GenerateToken(u *entity.UserTokenInfo) (token string, errCode errcode.Err) {
data := map[string]interface{}{
"id": u.Id,
"name": u.Name,
"role_id": u.RoleId,
}
uuid := rand.GoogleUUID32()
token = "L_admin_" + strconv.Itoa(int(u.Id)) + "_" + uuid
//放入缓存
hKey := tokenPrefix + token
errCode = gCache.RemoveSet(hKey, data)
return
}
// GetInfo
// @description: 获取token对应的信息
// @param:
// @author: GJing
// @email: gjing1st@gmail.com
// @date: 2022/12/27 18:22
// @success:
func (td TokenService) GetInfo(token string) (u entity.UserTokenInfo, errCode errcode.Err) {
hKey := tokenPrefix + token
v, errCode1 := gCache.Get(hKey)
if errCode != 0 || v == nil {
errCode = errCode1
return
}
m := v.(map[string]interface{})
u.Id = uint(utils.Int(m["id"]))
u.Name = utils.String(m["name"])
u.RoleId = utils.Int(m["role_id"])
return
}