This commit is contained in:
oneao committed 2025-08-25 17:22:54 +08:00
1 parent 76a6c81af2
commit b19d083031
68 files changed
+3889 -343

No files matched your search

@@ -0,0 +1,11 @@
package api
import (
"base-framework/internal/app/handle"
"base-framework/pkg/router"
)
func InitAuth(r *router.Router) {
group := r.Group("/auth")
group.POST("/login", handle.Login)
}
@@ -0,0 +1,11 @@
package api
import (
"base-framework/internal/app/handle"
"base-framework/pkg/router"
)
func InitTest(r *router.Router) {
group := r.Group("/test")
group.GET("/", handle.TestDB)
}
@@ -0,0 +1,66 @@
package handle
import (
"base-framework/pkg/config"
"base-framework/pkg/reponse"
"base-framework/pkg/router"
"base-framework/pkg/utils/jwt"
)
// Login 登录接口
func Login(c *router.Context) {
// 绑定请求体
var req struct {
OrgID string `json:"orgId"`
UserID string `json:"userId"`
Password string `json:"password"`
}
if err := c.BindJSON(&req); err != nil {
reponse.Error(c).Message("请求体不能为空").Send()
return
}
if req.OrgID == "" {
reponse.Error(c).Message("机构码不能为空").Send()
return
}
// 参数校验
if req.UserID == "" {
reponse.Error(c).Message("账户不能为空").Send()
return
}
if req.Password == "" {
reponse.Error(c).Message("密码不能为空").Send()
return
}
// 检查是否有该机构号
hasOrg := false
for k := range config.GetDBConfigs() {
if k == req.OrgID {
hasOrg = true
break
}
}
if !hasOrg {
reponse.Error(c).Code(reponse.CodeInvalidOrgCode).Send()
return
}
// 生成Token
token, err := jwt.CreateToken(req.OrgID, req.UserID)
if err != nil {
reponse.Error(c).Message("生成令牌失败")
return
}
reponse.Success(c).Data(
map[string]string{
"token": token,
}).Send()
}
@@ -0,0 +1,8 @@
package handle
import (
"base-framework/pkg/router"
)
func TestDB(c *router.Context) {
}
@@ -0,0 +1,24 @@
package app
import (
"base-framework/internal/app/api"
"base-framework/pkg/middleware"
"base-framework/pkg/router"
)
func InitAppRouter() *router.Router {
r := router.NewRouter()
r.Use(middleware.Routinex())
r.Use(middleware.Error())
r.Use(middleware.Auth()).ExcludePaths("/auth/login")
r.Use(middleware.Logger())
initApi(r)
return r
}
func initApi(r *router.Router) {
api.InitTest(r)
api.InitAuth(r)
}