mirror of
https://github.com/yv1ing/gin-admin.git
synced 2025-10-24 10:12:05 +08:00
完成基本框架搭建及系统用户登录流程
This commit is contained in:
58
internal/api/system/user_api.go
Normal file
58
internal/api/system/user_api.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
systemmodel "gin-admin/internal/model/system"
|
||||
systemservice "gin-admin/internal/service/system"
|
||||
)
|
||||
|
||||
// @Author: yv1ing
|
||||
// @Author: me@yvling.cn
|
||||
// @Date: 2025/8/28 16:29
|
||||
// @Desc: 系统用户服务接口
|
||||
|
||||
type SysUserLoginReq struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func SysUserLoginHandler(ctx *gin.Context) {
|
||||
var req SysUserLoginReq
|
||||
|
||||
err := ctx.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, systemmodel.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Info: "请求参数非法",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
jwtToken, err := systemservice.SysUserLogin(req.Username, req.Password)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, systemmodel.Response{
|
||||
Code: http.StatusInternalServerError,
|
||||
Info: "登录请求失败:" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if jwtToken == "" {
|
||||
ctx.AbortWithStatusJSON(http.StatusForbidden, systemmodel.Response{
|
||||
Code: http.StatusForbidden,
|
||||
Info: "登录请求失败:账号或密码错误",
|
||||
})
|
||||
return
|
||||
} else {
|
||||
ctx.AbortWithStatusJSON(http.StatusOK, systemmodel.Response{
|
||||
Code: http.StatusOK,
|
||||
Info: "登录请求成功",
|
||||
Data: gin.H{
|
||||
"token": jwtToken,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user