u
This commit is contained in:
1 parent
ed8bee3d77
commit
7552670e4a
23 files changed
+380
-154
No files matched your search
Whitespace-only changes.
@@ -1 +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)
|
||||
}
|
||||
@@ -1,31 +1,66 @@
|
||||
package handle
|
||||
|
||||
import "base-framework/pkg/router"
|
||||
import (
|
||||
"base-framework/pkg/config"
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/utils/jwt"
|
||||
"base-framework/pkg/utils/response"
|
||||
)
|
||||
|
||||
// Login 登录接口
|
||||
func Login(c *router.Context) {
|
||||
// 直接用 BindJSON 绑定请求体 JSON 到结构体
|
||||
// 绑定请求体
|
||||
var req struct {
|
||||
OrgID string `json:"orgID"`
|
||||
UserID string `json:"userID"`
|
||||
OrgID string `json:"orgId"`
|
||||
UserID string `json:"userId"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
c.JSON(400, map[string]string{"error": "请求体格式错误"})
|
||||
response.Error(c).Message("请求体不能为空").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 模拟校验
|
||||
if req.UserID == "" || req.Password == "" {
|
||||
c.JSON(400, map[string]string{"error": "账号或密码不能为空"})
|
||||
if req.OrgID == "" {
|
||||
response.Error(c).Message("机构码不能为空").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 登录成功,返回 token 示例
|
||||
c.JSON(200, map[string]interface{}{
|
||||
"code": "0000",
|
||||
"message": "登录成功",
|
||||
"data": map[string]string{
|
||||
"token": "这里是token字符串",
|
||||
},
|
||||
})
|
||||
// 参数校验
|
||||
if req.UserID == "" {
|
||||
response.Error(c).Message("账户不能为空").Send()
|
||||
return
|
||||
}
|
||||
|
||||
if req.Password == "" {
|
||||
response.Error(c).Message("密码不能为空").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否有该机构号
|
||||
hasOrg := false
|
||||
for k := range config.GetDBConfigs() {
|
||||
if k == req.OrgID {
|
||||
hasOrg = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasOrg {
|
||||
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 生成Token
|
||||
token, err := jwt.CreateToken(req.OrgID, req.UserID)
|
||||
|
||||
if err != nil {
|
||||
response.Error(c).Message("生成令牌失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c).Data(
|
||||
map[string]string{
|
||||
"token": token,
|
||||
}).Send()
|
||||
}
|
||||
@@ -10,14 +10,14 @@ func InitAppRouter() *router.Router {
|
||||
r := router.NewRouter()
|
||||
|
||||
r.Use(middleware.Recover())
|
||||
r.Use(middleware.Auth()).ExcludePaths("/login")
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user