登录模块修改

This commit is contained in:
李志强 2026-03-31 17:49:09 +08:00
parent 36929736c1
commit ae786d5157
3 changed files with 54 additions and 41 deletions

View File

@ -3,7 +3,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"net/http"
"server/services" "server/services"
@ -11,7 +10,7 @@ import (
) )
type platformLoginRequest struct { type platformLoginRequest struct {
Username string `json:"username"` Account string `json:"account"`
Password string `json:"password"` Password string `json:"password"`
} }
@ -27,70 +26,76 @@ func (c *PlatformAuthController) Login() {
// 支持前端以 JSON body 方式提交 // 支持前端以 JSON body 方式提交
body, err := io.ReadAll(c.Ctx.Request.Body) body, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil { if err != nil {
c.Ctx.Output.SetStatus(http.StatusBadRequest)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 400,
"message": "参数错误", "msg": "参数错误",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
return return
} }
if err := json.Unmarshal(body, &req); err != nil { if err := json.Unmarshal(body, &req); err != nil {
c.Ctx.Output.SetStatus(http.StatusBadRequest)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 400,
"message": "参数错误", "msg": "参数错误",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
return return
} }
if req.Username == "" || req.Password == "" { if req.Account == "" || req.Password == "" {
c.Ctx.Output.SetStatus(http.StatusBadRequest)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 400,
"message": "用户名或密码不能为空", "msg": "用户名或密码不能为空",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
return return
} }
// 控制器只做 HTTP 解析与响应编排,业务逻辑放 services 层 // 控制器只做 HTTP 解析与响应编排,业务逻辑放 services 层
token, err := services.PlatformLogin(req.Username, req.Password) token, err := services.PlatformLogin(req.Account, req.Password)
if err != nil { if err != nil {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 401,
"message": "未实现", "msg": err.Error(),
} }
_ = c.ServeJSON() _ = c.ServeJSON()
return return
} }
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": true, "code": 200,
"msg": "登录成功",
"data": map[string]interface{}{
"token": token, "token": token,
// user 结构用于前端 authStore 兼容旧格式
"user": map[string]interface{}{
"id": 1,
"account": req.Account,
"name": "平台管理员",
"group_id": "",
"tid": "",
"avatar": "",
},
},
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// SendLoginCode 发送登录验证码(占位实现) // SendLoginCode 发送登录验证码(占位实现)
func (c *PlatformAuthController) SendLoginCode() { func (c *PlatformAuthController) SendLoginCode() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "发送登录验证码暂未实现", "msg": "发送登录验证码暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// LoginBySms 手机号验证码登录(占位实现) // LoginBySms 手机号验证码登录(占位实现)
func (c *PlatformAuthController) LoginBySms() { func (c *PlatformAuthController) LoginBySms() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "手机号验证码登录暂未实现", "msg": "手机号验证码登录暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
@ -98,57 +103,60 @@ func (c *PlatformAuthController) LoginBySms() {
// Logout 平台退出登录(占位实现,当前为无状态直接返回成功) // Logout 平台退出登录(占位实现,当前为无状态直接返回成功)
func (c *PlatformAuthController) Logout() { func (c *PlatformAuthController) Logout() {
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": true, "code": 200,
"msg": "退出成功",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// GetGeetest3Infos 获取极验3.0配置(占位实现) // GetGeetest3Infos 获取极验3.0配置(占位实现)
func (c *PlatformAuthController) GetGeetest3Infos() { func (c *PlatformAuthController) GetGeetest3Infos() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "极验3.0暂未实现", "msg": "极验3.0暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// GetGeetest4Infos 获取极验4.0配置(占位实现) // GetGeetest4Infos 获取极验4.0配置(占位实现)
func (c *PlatformAuthController) GetGeetest4Infos() { func (c *PlatformAuthController) GetGeetest4Infos() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "极验4.0暂未实现", "msg": "极验4.0暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// GetOpenVerify 判断是否开启登录验证(占位实现) // GetOpenVerify 判断是否开启登录验证(占位实现)
func (c *PlatformAuthController) GetOpenVerify() { func (c *PlatformAuthController) GetOpenVerify() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 200,
"message": "登录验证开关暂未实现", "msg": "ok",
// data 为配置项数组这里固定关闭验证openVerify=0
"data": []map[string]string{
{
"label": "openVerify",
"value": "0",
},
},
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// ResetPassword 忘记密码重置(占位实现) // ResetPassword 忘记密码重置(占位实现)
func (c *PlatformAuthController) ResetPassword() { func (c *PlatformAuthController) ResetPassword() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "重置密码暂未实现", "msg": "重置密码暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }
// SendResetCode 发送找回密码验证码(占位实现) // SendResetCode 发送找回密码验证码(占位实现)
func (c *PlatformAuthController) SendResetCode() { func (c *PlatformAuthController) SendResetCode() {
c.Ctx.Output.SetStatus(http.StatusNotImplemented)
c.Data["json"] = map[string]interface{}{ c.Data["json"] = map[string]interface{}{
"success": false, "code": 501,
"message": "发送找回密码验证码暂未实现", "msg": "发送找回密码验证码暂未实现",
} }
_ = c.ServeJSON() _ = c.ServeJSON()
} }

Binary file not shown.

View File

@ -9,11 +9,16 @@ import (
// PlatformLogin 平台登录业务 // PlatformLogin 平台登录业务
// TODO: 后续接真实用户/租户表,这里先做最小可用实现。 // TODO: 后续接真实用户/租户表,这里先做最小可用实现。
func PlatformLogin(username, password string) (string, error) { func PlatformLogin(username, password string) (string, error) {
// 临时简单校验:用户名和密码非空即可 // 临时简单校验:用户名和密码非空
if username == "" || password == "" { if username == "" || password == "" {
return "", errors.New("用户名或密码不能为空") return "", errors.New("用户名或密码不能为空")
} }
// 测试账号admin / admin123
if username != "admin" || password != "admin123" {
return "", errors.New("用户名或密码错误")
}
// 这里后续应: // 这里后续应:
// 1. 从平台用户表查询用户 // 1. 从平台用户表查询用户
// 2. 校验密码(含加盐加密) // 2. 校验密码(含加盐加密)