feat: 个人中心发布
This commit is contained in:
+39
-13
@@ -2,37 +2,63 @@ package async
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime/debug"
|
||||
"sundynix-go/global"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// AsyncTask 定义了异步任务的函数原型
|
||||
type AsyncTask func(ctx context.Context)
|
||||
|
||||
// TaskRunner 任务收集器
|
||||
type namedTask struct {
|
||||
name string
|
||||
fn AsyncTask
|
||||
}
|
||||
|
||||
type TaskRunner struct {
|
||||
tasks []AsyncTask
|
||||
mu sync.Mutex
|
||||
tasks []namedTask
|
||||
}
|
||||
|
||||
// Add 添加一个任务到队列中
|
||||
func (tr *TaskRunner) Add(task AsyncTask) {
|
||||
tr.tasks = append(tr.tasks, task)
|
||||
// Add 添加任务
|
||||
func (tr *TaskRunner) Add(name string, task AsyncTask) {
|
||||
if task == nil {
|
||||
return
|
||||
}
|
||||
tr.mu.Lock()
|
||||
defer tr.mu.Unlock()
|
||||
tr.tasks = append(tr.tasks, namedTask{name: name, fn: task})
|
||||
}
|
||||
|
||||
// RunAll 安全地启动所有任务
|
||||
// RunAll 安全执行
|
||||
func (tr *TaskRunner) RunAll() {
|
||||
for _, task := range tr.tasks {
|
||||
t := task // 避免闭包变量捕获问题
|
||||
tr.mu.Lock()
|
||||
todoTasks := tr.tasks
|
||||
tr.tasks = nil
|
||||
tr.mu.Unlock()
|
||||
|
||||
for _, task := range todoTasks {
|
||||
t := task
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
global.Logger.Info("[AsyncError] 任务执行崩溃", zap.Any("recover", r))
|
||||
// 使用全局 Zap 记录结构化日志
|
||||
// 这里的 global.Logger 替换为你实际的全局变量名
|
||||
global.Logger.Error("异步任务异常崩溃",
|
||||
zap.String("task_name", t.name),
|
||||
zap.Any("panic_info", r),
|
||||
zap.String("stack", string(debug.Stack())),
|
||||
)
|
||||
}
|
||||
}()
|
||||
// 异步任务通常使用 Background,避免受主请求超时影响
|
||||
// 也可以自定义一个更长的超时 context
|
||||
t(context.Background())
|
||||
|
||||
// 异步任务执行,设置独立的超时控制
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
t.fn(ctx)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system/request"
|
||||
"sundynix-go/utils"
|
||||
"sundynix-go/utils/timer"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -32,8 +32,8 @@ func NewJWT() *JWT {
|
||||
|
||||
// CreateClaims 创建Claims
|
||||
func (j *JWT) CreateClaims(baseClaims request.BaseClaims) request.CustomClaims {
|
||||
bf, _ := utils.ParseDuration(global.Config.JWT.BufferTime)
|
||||
ep, _ := utils.ParseDuration(global.Config.JWT.ExpiresTime)
|
||||
bf, _ := timer.ParseDuration(global.Config.JWT.BufferTime)
|
||||
ep, _ := timer.ParseDuration(global.Config.JWT.ExpiresTime)
|
||||
claims := request.CustomClaims{
|
||||
BaseClaims: baseClaims,
|
||||
BufferTime: int64(bf / time.Second), // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package timer
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// GetMiniAccessToken 获取小程序的access_token
|
||||
func GetMiniAccessToken() string {
|
||||
ak, err := global.Redis.Get(context.Background(), "mini_access_token").Result()
|
||||
ak, err := global.Redis.Get(context.Background(), "zeeq_mini_access_token").Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
// 从微信服务器获取
|
||||
//重新从微信服务器获取
|
||||
@@ -44,7 +44,7 @@ func GetMiniAccessToken() string {
|
||||
}
|
||||
ak = data["access_token"].(string)
|
||||
ex := data["expires_in"].(float64)
|
||||
global.Redis.Set(context.Background(), "mini_access_token", ak, time.Duration(ex)*time.Second) //秒
|
||||
global.Redis.Set(context.Background(), "zeeq_mini_access_token", ak, time.Duration(ex)*time.Second) //秒
|
||||
} else if err != nil {
|
||||
log.Fatalf("Error getting access token from Redis: %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user