This commit is contained in:
oneao committed 2025-11-28 22:41:37 +08:00
1 parent cf4291652a
commit 8408cf779f
20 files changed
+1400 -33

No files matched your search

@@ -0,0 +1,27 @@
// Code generated by hertz generator. DO NOT EDIT.
package login
import (
login "cn/oneao/base-go/biz/handler/login"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_auth := root.Group("/auth", _authMw()...)
{
_login := _auth.Group("/login", _loginMw()...)
_login.POST("/default", append(_logindefaultMw(), login.LoginDefault)...)
}
}
}
@@ -0,0 +1,27 @@
// Code generated by hertz generator.
package login
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _authMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginMw() []app.HandlerFunc {
// your code...
return nil
}
func _logindefaultMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -1,17 +0,0 @@
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
// Error 中间件
func Error() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
c.Next(ctx)
if err := c.Errors.Last(); err != nil {
}
}
}
@@ -1,100 +0,0 @@
package middleware
import (
"context"
"log"
"net/http"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/jwt"
)
var identityKey = "UserId"
type loginForm struct {
Account string `form:"username,required" json:"username,required"`
Password string `form:"password,required" json:"password,required"`
Type int `form:"type,required" json:"type,required"`
}
type User struct {
UserId int64
}
var hertzJWTMiddleware *jwt.HertzJWTMiddleware
func init() {
middleware, err := jwt.New(&jwt.HertzJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: 30 * time.Minute,
MaxRefresh: 30 * 24 * time.Hour,
IdentityKey: identityKey,
// Token负载信息
PayloadFunc: func(data interface{}) jwt.MapClaims {
if v, ok := data.(*User); ok {
return jwt.MapClaims{
identityKey: v.UserId,
}
}
return jwt.MapClaims{}
},
// 解析Token取出用户信息
IdentityHandler: func(ctx context.Context, c *app.RequestContext) interface{} {
claims := jwt.ExtractClaims(ctx, c)
return &User{
UserId: claims[identityKey].(int64),
}
},
// 登录
Authenticator: func(ctx context.Context, c *app.RequestContext) (interface{}, error) {
c.Request.RequestURI()
return nil, jwt.ErrFailedAuthentication
},
// 权限控制
Authorizator: func(data interface{}, ctx context.Context, c *app.RequestContext) bool {
return true
},
// 未验证处理
Unauthorized: func(ctx context.Context, c *app.RequestContext, code int, message string) {
c.JSON(code, map[string]interface{}{
"code": code,
"message": message,
})
},
// 登录响应
LoginResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
// 登出响应
LogoutResponse: func(ctx context.Context, c *app.RequestContext, code int) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
})
},
// 刷新Token
RefreshResponse: func(ctx context.Context, c *app.RequestContext, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"token": token,
"expire": expire.Format(time.RFC3339),
})
},
TokenLookup: "header: Authorization",
})
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
hertzJWTMiddleware = middleware
}
func JWT() *jwt.HertzJWTMiddleware {
return hertzJWTMiddleware
}
@@ -1,35 +0,0 @@
package middleware
import (
"cn/oneao/base-go/logx"
"context"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/requestid"
"go.uber.org/zap"
)
func Log() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
reqId := requestid.Get(ctx)
c = context.WithValue(c, "RequestID", reqId)
start := time.Now() // 记录开始时间
// 执行后续处理
ctx.Next(c)
// 计算耗时
duration := time.Since(start).Milliseconds()
logx.CtxInfo(c, "请求完成",
zap.String("IP", ctx.ClientIP()),
zap.String("Method", string(ctx.Method())),
zap.String("Path", string(ctx.Path())),
zap.Int("Status", ctx.GetResponse().StatusCode()),
zap.Int64("Duration", duration),
)
}
}
@@ -0,0 +1,17 @@
// Code generated by hertz generator. DO NOT EDIT.
package router
import (
login "cn/oneao/base-go/biz/router/login"
register "cn/oneao/base-go/biz/router/register"
"github.com/cloudwego/hertz/pkg/app/server"
)
// GeneratedRegister registers routers generated by IDL.
func GeneratedRegister(r *server.Hertz) {
//INSERT_POINT: DO NOT DELETE THIS LINE!
register.Register(r)
login.Register(r)
}
@@ -0,0 +1,27 @@
// Code generated by hertz generator.
package register
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _authMw() []app.HandlerFunc {
// your code...
return nil
}
func _registerMw() []app.HandlerFunc {
// your code...
return nil
}
func _registerdefaultMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,27 @@
// Code generated by hertz generator. DO NOT EDIT.
package register
import (
register "cn/oneao/base-go/biz/handler/register"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_auth := root.Group("/auth", _authMw()...)
{
_register := _auth.Group("/register", _registerMw()...)
_register.POST("/default", append(_registerdefaultMw(), register.RegisterDefault)...)
}
}
}