67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package handle
|
|
|
|
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) {
|
|
// 绑定请求体
|
|
var req struct {
|
|
OrgID string `json:"orgId"`
|
|
UserID string `json:"userId"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
if err := c.BindJSON(&req); err != nil {
|
|
response.Error(c).Message("请求体不能为空").Send()
|
|
return
|
|
}
|
|
|
|
if req.OrgID == "" {
|
|
response.Error(c).Message("机构码不能为空").Send()
|
|
return
|
|
}
|
|
|
|
// 参数校验
|
|
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()
|
|
}
|