新增登陆接口
This commit is contained in:
parent
4038110e2f
commit
7f39c483db
@ -1,14 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/api/service"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Basic struct{}
|
||||
@ -42,13 +40,22 @@ func (b *Basic) Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
isValid := utils.VerifyCaptcha(login)
|
||||
if !isValid {
|
||||
// 验证码错误
|
||||
responses.FailWithMessage("验证码错误", c)
|
||||
// isValid := utils.VerifyCaptcha(login)
|
||||
// if !isValid {
|
||||
// // 验证码错误
|
||||
// responses.FailWithMessage("验证码错误", c)
|
||||
// return
|
||||
// }
|
||||
|
||||
// 登陆
|
||||
Basic := service.Basic{}
|
||||
token, err := Basic.Login(login)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
|
||||
responses.OkWithData(token, c)
|
||||
}
|
||||
|
||||
// GetCaptchaTest 获取验证码
|
||||
@ -100,20 +107,3 @@ func (b *Basic) GetCaptchaTest(c *gin.Context) {
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
func KeyMatch2(key1 string, key2 string) bool {
|
||||
key2 = strings.Replace(key2, "/*", "/.*", -1)
|
||||
fmt.Println(key2)
|
||||
re := regexp.MustCompile(`:[^/]+`)
|
||||
key2 = re.ReplaceAllString(key2, "$1[^/]+$2")
|
||||
|
||||
return RegexMatch(key1, "^"+key2+"$")
|
||||
}
|
||||
|
||||
func RegexMatch(key1 string, key2 string) bool {
|
||||
res, err := regexp.MatchString(key2, key1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
type AdminUser struct {
|
||||
}
|
||||
|
||||
// GetAdminUserFirstById 用户id获取用户数据
|
||||
// GetAdminUserFirstById 获取用户数据-用户id
|
||||
// roleId 用户id
|
||||
func (r *AdminUser) GetAdminUserFirstById(userId int64) (m model.AdminUser, err error) {
|
||||
err = global.Db.First(&m, userId).Error
|
||||
@ -17,3 +17,12 @@ func (r *AdminUser) GetAdminUserFirstById(userId int64) (m model.AdminUser, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetAdminUserFirstByAccess 获取用户数据-用户账号
|
||||
func (r *AdminUser) GetAdminUserFirstByAccess(username string) (m model.AdminUser, err error) {
|
||||
err = global.Db.Where("access = ?", username).First(&m).Error
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ func Auth() gin.HandlerFunc {
|
||||
// 获取用户数据
|
||||
AdminUserDao := dao.AdminUser{}
|
||||
adminUser, err := AdminUserDao.GetAdminUserFirstById(userId)
|
||||
if err != nil || adminUser.UserId == 0 {
|
||||
if err != nil || adminUser.UserID == 0 {
|
||||
responses.FailWithMessage("用户数据错误", c)
|
||||
c.Abort()
|
||||
return
|
||||
|
||||
@ -1,28 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// AdminUser 后台-用户表
|
||||
type AdminUser struct {
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:主键id" json:"user_id"`
|
||||
UserName string `gorm:"column:user_name;type:varchar(64);comment:用户名" json:"user_name"`
|
||||
Password string `gorm:"column:password;type:varchar(128);comment:密码" json:"password"`
|
||||
Salt string `gorm:"column:salt;type:varchar(255);comment:密码掩码" json:"salt"`
|
||||
Status int `gorm:"column:status;type:tinyint(1);default:2;comment:状态(1:正常 2:审核中 3:删除)" json:"status"`
|
||||
NickName string `gorm:"column:nick_name;type:varchar(255);comment:昵称" json:"nick_name"`
|
||||
Phone string `gorm:"column:phone;type:varchar(11);comment:手机号" json:"phone"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
|
||||
Sex int `gorm:"column:sex;type:tinyint(1);comment:性别(1:男 2:女)" json:"sex"`
|
||||
Email string `gorm:"column:email;type:varchar(100);comment:邮箱" json:"email"`
|
||||
RoleId int64 `gorm:"column:role_id;type:bigint(19);comment:角色表" json:"role_id"`
|
||||
DeptId int64 `gorm:"column:dept_id;type:bigint(19);comment:部门id" json:"dept_id"`
|
||||
PostId int64 `gorm:"column:post_id;type:bigint(19);comment:岗位id" json:"post_id"`
|
||||
CreateBy int64 `gorm:"column:create_by;type:bigint(19);comment:创建者id(用户表id)" json:"create_by"`
|
||||
UpdateBy int64 `gorm:"column:update_by;type:bigint(19);comment:更新者id(用户表id)" json:"update_by"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
||||
Model
|
||||
UserID int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:'主键id'" json:"user_id"`
|
||||
Access string `gorm:"column:access;type:varchar(64);comment:'账号'" json:"access"`
|
||||
Password string `gorm:"column:password;type:varchar(128);comment:'密码'" json:"password"`
|
||||
Salt string `gorm:"column:salt;type:varchar(255);comment:'密码掩码'" json:"salt"`
|
||||
Status int `gorm:"column:status;type:tinyint(1);default:2;comment:'状态(1:正常 2:审核中 3:审核失败)'" json:"status"`
|
||||
IsDeleted int `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:'是否被删除(0:否 1:是)'" json:"is_deleted"`
|
||||
IsDisabled int `gorm:"column:is_disabled;type:tinyint(1);default:0;comment:'是否被禁用(0:否 1:是)'" json:"is_disabled"`
|
||||
NickName string `gorm:"column:nick_name;type:varchar(255);comment:'昵称'" json:"nick_name"`
|
||||
Phone string `gorm:"column:phone;type:varchar(11);comment:'手机号'" json:"phone"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:'头像'" json:"avatar"`
|
||||
Sex int `gorm:"column:sex;type:tinyint(1);comment:'性别(1:男 2:女)'" json:"sex"`
|
||||
Email string `gorm:"column:email;type:varchar(100);comment:'邮箱'" json:"email"`
|
||||
RoleID int64 `gorm:"column:role_id;type:bigint(19);comment:'角色表'" json:"role_id"`
|
||||
DeptID int64 `gorm:"column:dept_id;type:bigint(19);comment:'部门id'" json:"dept_id"`
|
||||
PostID int64 `gorm:"column:post_id;type:bigint(19);comment:'岗位id'" json:"post_id"`
|
||||
}
|
||||
|
||||
func (m *AdminUser) TableName() string {
|
||||
|
||||
@ -6,8 +6,8 @@ type Basic struct {
|
||||
|
||||
// Login 登陆
|
||||
type Login struct {
|
||||
Username string `json:"username" form:"username" validate:"required" label:"用户名"` // 用户名
|
||||
Password string `json:"password" form:"password" validate:"required"` // 密码
|
||||
Captcha string `json:"captcha" form:"captcha" validate:"required"` // 验证码
|
||||
CaptchaId string `json:"captchaId" form:"captchaId" validate:"required"` // 验证码ID
|
||||
Access string `json:"access" form:"access" validate:"required" label:"用户名"` // 用户名
|
||||
Password string `json:"password" form:"password" validate:"required"` // 密码
|
||||
Captcha string `json:"captcha" form:"captcha" validate:"required"` // 验证码
|
||||
CaptchaId string `json:"captchaId" form:"captchaId" validate:"required"` // 验证码ID
|
||||
}
|
||||
|
||||
21
api/responses/basic.go
Normal file
21
api/responses/basic.go
Normal file
@ -0,0 +1,21 @@
|
||||
package responses
|
||||
|
||||
import "hospital-admin-api/config"
|
||||
|
||||
type Basic struct {
|
||||
Login // 登陆
|
||||
}
|
||||
|
||||
// Login 登陆
|
||||
type Login struct {
|
||||
UserId int64 `json:"user_id"` // 用户id
|
||||
NickName string `json:"nick_name"` // 昵称
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Token string `json:"token"` // 用户名
|
||||
}
|
||||
|
||||
// GetFullAvatar 返回带有指定字符串的头像路径
|
||||
func (l *Login) GetFullAvatar() Login {
|
||||
l.Avatar = config.C.Oss.OssCustomDomainName + "/" + l.Avatar
|
||||
return Login{}
|
||||
}
|
||||
@ -1,8 +1,64 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/utils"
|
||||
)
|
||||
|
||||
type Basic struct{}
|
||||
|
||||
// Login 登陆
|
||||
func (b *Basic) Login() {
|
||||
func (b *Basic) Login(login requests.Login) (responses.Login, error) {
|
||||
// 获取用户信息
|
||||
AdminUserDao := dao.AdminUser{}
|
||||
adminUser, err := AdminUserDao.GetAdminUserFirstByAccess(login.Access)
|
||||
if err != nil || adminUser.UserID == 0 {
|
||||
return responses.Login{}, errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
// 检测用户密码
|
||||
password := md5.Sum([]byte(login.Password + adminUser.Salt))
|
||||
// 将哈希值转换为16进制字符串
|
||||
passwordString := hex.EncodeToString(password[:])
|
||||
|
||||
if passwordString != adminUser.Password {
|
||||
return responses.Login{}, errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
// 检测用户状态
|
||||
if adminUser.IsDeleted == 1 {
|
||||
return responses.Login{}, errors.New("非法用户")
|
||||
}
|
||||
|
||||
if adminUser.IsDisabled == 1 {
|
||||
return responses.Login{}, errors.New("您的账号已被禁用,请联系管理员处理")
|
||||
}
|
||||
|
||||
token := &utils.Token{
|
||||
UserId: adminUser.UserID,
|
||||
RoleId: adminUser.RoleID,
|
||||
DeptId: adminUser.DeptID,
|
||||
PostId: adminUser.PostID,
|
||||
}
|
||||
|
||||
jwt, err := token.NewJWT()
|
||||
if err != nil {
|
||||
return responses.Login{}, errors.New("登陆失败")
|
||||
}
|
||||
|
||||
// 生成jwt
|
||||
result := responses.Login{
|
||||
UserId: adminUser.UserID,
|
||||
NickName: adminUser.NickName,
|
||||
Avatar: adminUser.Avatar,
|
||||
Token: jwt,
|
||||
}
|
||||
result.GetFullAvatar()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -29,4 +29,11 @@ jwt:
|
||||
sign-key: 123456 # 私钥
|
||||
ttl : 48 # 过期时间 小时
|
||||
algo : HS256 # 加密方式
|
||||
|
||||
|
||||
oss:
|
||||
oss-access-key: LTAI5tKmFrVCghcxX7yHyGhm
|
||||
oss-access-key-secret: q1aiIZCJJuf92YbKk2cSXnPES4zx26
|
||||
oss-bucket: gdxz-hospital
|
||||
oss-endpoint: oss-cn-chengdu.aliyuncs.com
|
||||
oss-custom-domain-name: https://img.applets.igandanyiyuan.com
|
||||
oss-env: applet-dev
|
||||
@ -9,4 +9,5 @@ type Config struct {
|
||||
Log Log `mapstructure:"log" json:"log" yaml:"log"`
|
||||
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
|
||||
Jwt Jwt `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
|
||||
Oss Oss `mapstructure:"oss" json:"oss" yaml:"oss"`
|
||||
}
|
||||
|
||||
10
config/oss.go
Normal file
10
config/oss.go
Normal file
@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
type Oss struct {
|
||||
OssAccessKey string `mapstructure:"oss-access-key" json:"oss-access-key" yaml:"oss-access-key"`
|
||||
OssAccessKeySecret string `mapstructure:"oss-access-key-secret" json:"oss-access-key-secret" yaml:"oss-access-key-secret"`
|
||||
OssBucket string `mapstructure:"oss-bucket" json:"oss-bucket" yaml:"oss-bucket"`
|
||||
OssEndpoint string `mapstructure:"oss-endpoint" json:"oss-endpoint" yaml:"oss-endpoint"`
|
||||
OssCustomDomainName string `mapstructure:"oss-custom-domain-name" json:"oss-custom-domain-name" yaml:"oss-custom-domain-name"`
|
||||
OssEnv string `mapstructure:"oss-env" json:"oss-env" yaml:"oss-env"`
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user