3 Star 9 Fork 1

os-lee/easy-paas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
admin_login.go 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
package controller
import (
"errors"
"gitee.com/os-lee/easy-paas/admin/internal/cache"
"gitee.com/os-lee/easy-paas/admin/internal/dao"
"gitee.com/os-lee/easy-paas/admin/internal/dto"
"gitee.com/os-lee/easy-paas/common"
"gitee.com/os-lee/easy-paas/common/e"
"gitee.com/os-lee/easy-paas/common/jwt"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"time"
)
type AdminLoginController struct{}
func AdminLoginRegister(group *gin.RouterGroup) {
adminLogin := &AdminLoginController{}
group.POST("/login", adminLogin.AdminLogin)
}
func (adminLogin *AdminLoginController) AdminLogin(c *gin.Context) {
params := &dto.AdminLoginInput{}
if err := params.BindValidParam(c); err != nil {
common.ResponseError(c, e.ErrParams, err)
return
}
//1. params.UserName 取得管理员信息
adminDao := dao.NewAdminDao(c)
admin, err := adminDao.Find(&dao.Admin{UserName: params.UserName, IsDelete: 0})
if err != nil {
common.ResponseError(c, e.ErrAdmin, errors.New("用户信息不存在"))
return
}
err = bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(params.Password))
if err != nil {
common.ResponseError(c, e.ErrAdmin, errors.New("密码错误,请重新输入"))
return
}
token, err := jwt.GenerateUserToken(admin.Id, admin.UserName)
if err != nil {
common.ResponseError(c, e.ErrToken, errors.New("JWT加密错误"))
return
}
// 将token 放入redis中
cache.RedisCli.Set(c, token, admin.UserName, time.Hour*24*30)
out := &dto.AdminLoginOutput{Token: token}
common.ResponseSuccess(c, out)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/os-lee/easy-paas.git
git@gitee.com:os-lee/easy-paas.git
os-lee
easy-paas
easy-paas
6cf1638f64c0

搜索帮助