🆕授权登陆接口
This commit is contained in:
@@ -2,7 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"wireguard-dashboard/compoment"
|
||||
"wireguard-dashboard/constant"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
"wireguard-dashboard/model/vo"
|
||||
"wireguard-dashboard/repository"
|
||||
"wireguard-dashboard/utils"
|
||||
)
|
||||
|
||||
@@ -22,5 +27,65 @@ func (u user) Login(c *gin.Context) {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
utils.GinResponse(c).OKWithData("不准")
|
||||
|
||||
// 校验验证码
|
||||
pass := compoment.CaptchaStore{}.Verify(p.CaptchaId, p.CaptchaAnswer, true)
|
||||
if !pass {
|
||||
utils.GinResponse(c).FailedWithMsg("验证码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验用户是否存在
|
||||
user, err := repository.User().GetUserByAccount(p.Account)
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("账户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
if user.Status != constant.Normal {
|
||||
utils.GinResponse(c).FailedWithMsg("账户状态异常")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验密码
|
||||
if !utils.Password().ComparePassword(user.Password, p.Password) {
|
||||
utils.GinResponse(c).FailedWithMsg("密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 生成token
|
||||
token, err := compoment.JWT().GenerateToken(user.Id)
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("登陆失败")
|
||||
return
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OKWithData(map[string]any{
|
||||
"token": token,
|
||||
"type": "Bearer",
|
||||
})
|
||||
}
|
||||
|
||||
// GetUser
|
||||
// @description: 获取登陆用户信息
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) GetUser(c *gin.Context) {
|
||||
user, ok := c.Get("user")
|
||||
if !ok {
|
||||
utils.GinResponse(c).FailedWithMsg("获取信息失败")
|
||||
return
|
||||
}
|
||||
data := &vo.User{
|
||||
Id: user.(*entity.User).Id,
|
||||
Name: user.(*entity.User).Name,
|
||||
Avatar: user.(*entity.User).Avatar,
|
||||
Account: user.(*entity.User).Account,
|
||||
Email: user.(*entity.User).Email,
|
||||
IsAdmin: user.(*entity.User).IsAdmin,
|
||||
Status: user.(*entity.User).Status,
|
||||
CreatedAt: user.(*entity.User).CreatedAt,
|
||||
UpdatedAt: user.(*entity.User).UpdatedAt,
|
||||
}
|
||||
utils.GinResponse(c).OKWithData(data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user