登录模块修改

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

Binary file not shown.

View File

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