2023-12-25 02:19:00 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2023-12-26 15:34:38 +08:00
|
|
|
"PersonalMain/internal/service/TokenService"
|
2023-12-25 02:32:46 +08:00
|
|
|
"PersonalMain/utility/ErrorCode"
|
|
|
|
"PersonalMain/utility/ResultUtil"
|
2023-12-26 16:29:04 +08:00
|
|
|
"context"
|
2023-12-25 02:19:00 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2023-12-26 16:29:04 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2023-12-25 02:19:00 +08:00
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultHandlerResponse is the default implementation of HandlerResponse.
|
|
|
|
type DefaultHandlerResponse struct {
|
2023-12-26 15:34:38 +08:00
|
|
|
Output string `json:"output" dc:"Output data for certain request according API definition"`
|
2023-12-25 02:19:00 +08:00
|
|
|
Code int `json:"code" dc:"Error code"`
|
|
|
|
Message string `json:"message" dc:"Error message"`
|
|
|
|
Data interface{} `json:"data" dc:"Result data for certain request according API definition"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimestampMiddleware
|
|
|
|
//
|
|
|
|
// 全局中间件
|
|
|
|
// 检查时间戳误差是否在
|
|
|
|
func TimestampMiddleware(r *ghttp.Request) {
|
|
|
|
// 检查时间戳误差是否在
|
2023-12-26 16:29:04 +08:00
|
|
|
timestampString := r.GetHeader("timestamp")
|
|
|
|
if len(timestampString) >= 13 && len(timestampString) <= 14 {
|
|
|
|
timestamp, err := strconv.ParseInt(timestampString, 10, 64)
|
|
|
|
if gregex.IsMatch(`^[0-9]+$`, []byte(strconv.FormatInt(timestamp, 10))) {
|
|
|
|
if timestamp+int64(2000) > time.Now().UnixMilli() && timestamp-int64(2000) < time.Now().UnixMilli() {
|
|
|
|
r.Middleware.Next()
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
ResultUtil.ErrorNoData(r, ErrorCode.TimestampExpired)
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 02:19:00 +08:00
|
|
|
} else {
|
|
|
|
if err != nil {
|
2023-12-26 16:29:04 +08:00
|
|
|
ResultUtil.ErrorNoData(r, ErrorCode.TimestampVerifyFailed)
|
2023-12-25 02:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-26 16:29:04 +08:00
|
|
|
ResultUtil.ErrorNoData(r, ErrorCode.TimestampVerifyFailed)
|
2023-12-25 02:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 15:34:38 +08:00
|
|
|
// VerifyTokenMiddleware
|
|
|
|
//
|
|
|
|
// 校验 TokenServiceImpl 是否有效
|
|
|
|
func VerifyTokenMiddleware(r *ghttp.Request) {
|
|
|
|
// 校验 token
|
|
|
|
tokenService := TokenService.NewTokenService()
|
|
|
|
getToken := tokenService.GetToken(r)
|
|
|
|
if getToken != nil {
|
|
|
|
// 检查 TokenServiceImpl 是否有效
|
|
|
|
if tokenService.VerifyToken(getToken) {
|
|
|
|
r.Middleware.Next()
|
|
|
|
} else {
|
|
|
|
ResultUtil.ErrorNoData(r, ErrorCode.TokenExpired)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ResultUtil.ErrorNoData(r, ErrorCode.TokenNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 02:19:00 +08:00
|
|
|
func JsonResponseMiddleware(r *ghttp.Request) {
|
|
|
|
r.Middleware.Next()
|
|
|
|
|
|
|
|
// There's custom buffer content, it then exits current handler.
|
|
|
|
if r.Response.BufferLength() > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-12-26 15:34:38 +08:00
|
|
|
output = r.GetRequest("output").String()
|
|
|
|
msg = r.GetRequest("message").String()
|
|
|
|
err = r.GetError()
|
|
|
|
res = r.GetHandlerResponse()
|
|
|
|
code int
|
2023-12-25 02:19:00 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if r.GetRequest("code") == nil {
|
|
|
|
code = 500
|
|
|
|
}
|
|
|
|
msg = err.Error()
|
|
|
|
} else {
|
|
|
|
if r.Response.Status > 0 && r.Response.Status != 200 {
|
|
|
|
msg = http.StatusText(r.Response.Status)
|
|
|
|
switch r.Response.Status {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
code = 404
|
|
|
|
case http.StatusForbidden:
|
|
|
|
code = 403
|
|
|
|
default:
|
|
|
|
code = r.GetRequest("code").Int()
|
|
|
|
}
|
|
|
|
// 处理错误
|
|
|
|
err = gerror.New(r.GetRequest("message").String())
|
|
|
|
r.SetError(err)
|
|
|
|
} else {
|
2023-12-26 15:34:38 +08:00
|
|
|
code = ErrorCode.ServerUnknownError.Code()
|
2023-12-25 02:19:00 +08:00
|
|
|
if msg == "" {
|
2023-12-26 15:34:38 +08:00
|
|
|
msg = ErrorCode.ServerUnknownError.Message()
|
|
|
|
output = ErrorCode.ServerUnknownError.Output()
|
2023-12-25 02:19:00 +08:00
|
|
|
}
|
2023-12-26 16:29:04 +08:00
|
|
|
g.Log().Cat("Unknown").Info(context.Background(), output, msg)
|
2023-12-25 02:19:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Response.WriteJson(DefaultHandlerResponse{
|
2023-12-26 15:34:38 +08:00
|
|
|
Output: output,
|
2023-12-25 02:19:00 +08:00
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
Data: res,
|
|
|
|
})
|
|
|
|
}
|