✨ 重新初始化项目
This commit is contained in:
71
utils/jwt.go
71
utils/jwt.go
@@ -1,71 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Lee-WineList/config"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
type JWT struct {
|
||||
claims jwt.MapClaims
|
||||
}
|
||||
|
||||
// GenerateToken 根据UserId生成并返回Token
|
||||
func GenerateToken(userId uint64) string {
|
||||
jwtConfig := config.Scd.JWT
|
||||
now := time.Now().Unix()
|
||||
claims := make(jwt.MapClaims)
|
||||
claims["exp"] = now + jwtConfig.AccessExpire
|
||||
claims["iat"] = now
|
||||
claims["rft"] = now + jwtConfig.RefreshAfter
|
||||
claims["userId"] = userId
|
||||
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
token.Claims = claims
|
||||
tokenStr, _ := token.SignedString([]byte(jwtConfig.AccessSecret))
|
||||
|
||||
return tokenStr
|
||||
}
|
||||
|
||||
// ParseToken 解析Token
|
||||
func ParseToken(tokenStr string) (*JWT, error) {
|
||||
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.Scd.JWT.AccessSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &JWT{claims: token.Claims.(jwt.MapClaims)}, nil
|
||||
}
|
||||
|
||||
// Valid 验证token是否有效
|
||||
func (jwt *JWT) Valid() bool {
|
||||
if err := jwt.claims.Valid(); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CheckRefresh 检查是否可以刷新
|
||||
func (jwt *JWT) CheckRefresh() bool {
|
||||
if time.Now().Unix() > jwt.GetRefreshTime() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (jwt *JWT) RefreshToken() string {
|
||||
return GenerateToken(jwt.GetUserId())
|
||||
}
|
||||
|
||||
// GetUserId 从Token中解析userId
|
||||
func (jwt *JWT) GetUserId() uint64 {
|
||||
return uint64(jwt.claims["userId"].(float64))
|
||||
}
|
||||
|
||||
// GetRefreshTime 从Token中解析refreshTime
|
||||
func (jwt *JWT) GetRefreshTime() int64 {
|
||||
return int64(jwt.claims["rft"].(float64))
|
||||
}
|
104
utils/timer/timed_task.go
Normal file
104
utils/timer/timed_task.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package timer
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Timer interface {
|
||||
AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error)
|
||||
AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error)
|
||||
FindCron(taskName string) (*cron.Cron, bool)
|
||||
StartTask(taskName string)
|
||||
StopTask(taskName string)
|
||||
Remove(taskName string, id int)
|
||||
Clear(taskName string)
|
||||
Close()
|
||||
}
|
||||
|
||||
// timer 定时任务管理
|
||||
type timer struct {
|
||||
taskList map[string]*cron.Cron
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// AddTaskByFunc 通过函数的方法添加任务
|
||||
func (t *timer) AddTaskByFunc(taskName string, spec string, task func(), option ...cron.Option) (cron.EntryID, error) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if _, ok := t.taskList[taskName]; !ok {
|
||||
t.taskList[taskName] = cron.New(option...)
|
||||
}
|
||||
id, err := t.taskList[taskName].AddFunc(spec, task)
|
||||
t.taskList[taskName].Start()
|
||||
return id, err
|
||||
}
|
||||
|
||||
// AddTaskByJob 通过接口的方法添加任务
|
||||
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }, option ...cron.Option) (cron.EntryID, error) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if _, ok := t.taskList[taskName]; !ok {
|
||||
t.taskList[taskName] = cron.New(option...)
|
||||
}
|
||||
id, err := t.taskList[taskName].AddJob(spec, job)
|
||||
t.taskList[taskName].Start()
|
||||
return id, err
|
||||
}
|
||||
|
||||
// FindCron 获取对应taskName的cron 可能会为空
|
||||
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
v, ok := t.taskList[taskName]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// StartTask 开始任务
|
||||
func (t *timer) StartTask(taskName string) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if v, ok := t.taskList[taskName]; ok {
|
||||
v.Start()
|
||||
}
|
||||
}
|
||||
|
||||
// StopTask 停止任务
|
||||
func (t *timer) StopTask(taskName string) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if v, ok := t.taskList[taskName]; ok {
|
||||
v.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
// Remove 从taskName 删除指定任务
|
||||
func (t *timer) Remove(taskName string, id int) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if v, ok := t.taskList[taskName]; ok {
|
||||
v.Remove(cron.EntryID(id))
|
||||
}
|
||||
}
|
||||
|
||||
// Clear 清除任务
|
||||
func (t *timer) Clear(taskName string) {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
if v, ok := t.taskList[taskName]; ok {
|
||||
v.Stop()
|
||||
delete(t.taskList, taskName)
|
||||
}
|
||||
}
|
||||
|
||||
// Close 释放资源
|
||||
func (t *timer) Close() {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
for _, v := range t.taskList {
|
||||
v.Stop()
|
||||
}
|
||||
}
|
||||
func NewTimerTask() Timer {
|
||||
return &timer{taskList: make(map[string]*cron.Cron)}
|
||||
}
|
61
utils/timer/timed_task_test.go
Normal file
61
utils/timer/timed_task_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package timer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var job = mockJob{}
|
||||
|
||||
type mockJob struct{}
|
||||
|
||||
func (job mockJob) Run() {
|
||||
mockFunc()
|
||||
}
|
||||
func mockFunc() {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("1s...")
|
||||
}
|
||||
func TestNewTimerTask(t *testing.T) {
|
||||
tm := NewTimerTask()
|
||||
_tm := tm.(*timer)
|
||||
{
|
||||
_, err := tm.AddTaskByFunc("func", "@every 1s", mockFunc)
|
||||
assert.Nil(t, err)
|
||||
_, ok := _tm.taskList["func"]
|
||||
if !ok {
|
||||
t.Error("no find func")
|
||||
}
|
||||
}
|
||||
{
|
||||
_, err := tm.AddTaskByJob("job", "@every 1s", job)
|
||||
assert.Nil(t, err)
|
||||
_, ok := _tm.taskList["job"]
|
||||
if !ok {
|
||||
t.Error("no find job")
|
||||
}
|
||||
}
|
||||
{
|
||||
_, ok := tm.FindCron("func")
|
||||
if !ok {
|
||||
t.Error("no find func")
|
||||
}
|
||||
_, ok = tm.FindCron("job")
|
||||
if !ok {
|
||||
t.Error("no find job")
|
||||
}
|
||||
_, ok = tm.FindCron("none")
|
||||
if ok {
|
||||
t.Error("find none")
|
||||
}
|
||||
}
|
||||
{
|
||||
tm.Clear("func")
|
||||
_, ok := tm.FindCron("func")
|
||||
if ok {
|
||||
t.Error("find func")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user