-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwt.go
53 lines (49 loc) · 1.02 KB
/
gwt.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
// Package gwt provides ...
package gwt
import (
"github.com/fdgo/leaseapp/middleware/jwt"
"github.com/fdgo/leaseapp/models"
"log"
"net/http"
"time"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
type LoginResult struct {
Token string `json:"token"`
model.Employee
}
//生成token
func GenerateToken(c *gin.Context, emp model.Employee) {
j := &jwt.JWT{
[]byte("martin"),
}
claims := jwt.Customclaims{
emp.EmpName,
emp.Phone,
emp.Role,
jwtgo.StandardClaims{
NotBefore: int64(time.Now().Unix() - 1000), //签名生效时间
ExpiresAt: int64(time.Now().Unix() + 3600), //签名过期时间 一小时
Issuer: "martin", //签名发行者
},
}
token, err := j.CreateToken(claims)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": -1,
"msg": err.Error(),
})
}
log.Println(token)
data := LoginResult{
Token: token,
Employee: emp,
}
c.JSON(http.StatusOK, gin.H{
"status": 0,
"msg": "登录成功",
"data": data,
})
return
}