103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"errors"
|
|
"hospital-admin-api/api/dao"
|
|
"hospital-admin-api/api/dto"
|
|
"hospital-admin-api/api/requests"
|
|
"hospital-admin-api/extend/aliyun"
|
|
"hospital-admin-api/utils"
|
|
"strconv"
|
|
)
|
|
|
|
type AdminService struct {
|
|
}
|
|
|
|
// Login 登陆
|
|
func (b *AdminService) Login(LoginRequest requests.Login) (*dto.Login, error) {
|
|
// 获取用户信息
|
|
AdminUserDao := dao.AdminUserDao{}
|
|
adminUser, err := AdminUserDao.GetAdminUserFirstByAccess(LoginRequest.Access)
|
|
if err != nil || adminUser == nil {
|
|
return nil, errors.New("用户名或密码错误")
|
|
}
|
|
|
|
// 检测用户密码
|
|
password := md5.Sum([]byte(LoginRequest.Password + adminUser.Salt))
|
|
// 将哈希值转换为16进制字符串
|
|
passwordString := hex.EncodeToString(password[:])
|
|
|
|
if passwordString != adminUser.Password {
|
|
return nil, errors.New("用户名或密码错误")
|
|
}
|
|
|
|
// 检测用户状态
|
|
if adminUser.IsDeleted == 1 {
|
|
return nil, errors.New("非法用户")
|
|
}
|
|
|
|
if adminUser.IsDisabled == 1 {
|
|
return nil, errors.New("您的账号已被禁用,请联系管理员处理")
|
|
}
|
|
|
|
token := &utils.Token{
|
|
UserId: strconv.FormatInt(adminUser.UserID, 10),
|
|
RoleId: strconv.FormatInt(adminUser.RoleID, 10),
|
|
DeptId: strconv.FormatInt(adminUser.DeptID, 10),
|
|
PostId: strconv.FormatInt(adminUser.PostID, 10),
|
|
}
|
|
|
|
// 生成jwt
|
|
jwt, err := token.NewJWT()
|
|
if err != nil {
|
|
return nil, errors.New("登陆失败")
|
|
}
|
|
|
|
result := &dto.Login{
|
|
UserId: strconv.FormatInt(adminUser.UserID, 10),
|
|
NickName: adminUser.NickName,
|
|
Avatar: adminUser.Avatar,
|
|
Token: jwt,
|
|
}
|
|
|
|
result.GetLoginFullAvatar()
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// GetOssSign 获取oss签名
|
|
func (a *AdminService) GetOssSign(getOssSignRequest requests.GetOssSign) (*aliyun.GetOssSignResponse, error) {
|
|
var dir string
|
|
if getOssSignRequest.UserType == 1 {
|
|
dir = "applet/patient/"
|
|
} else if getOssSignRequest.UserType == 2 {
|
|
dir = "applet/doctor/"
|
|
} else if getOssSignRequest.UserType == 3 {
|
|
dir = "applet/pharmacist/"
|
|
} else if getOssSignRequest.UserType == 4 {
|
|
dir = "applet/admin/"
|
|
}
|
|
|
|
if dir != "" {
|
|
if getOssSignRequest.Scene == 1 {
|
|
dir = dir + "avatar/"
|
|
} else if getOssSignRequest.Scene == 2 {
|
|
dir = dir + "cert/"
|
|
} else if getOssSignRequest.Scene == 3 {
|
|
dir = dir + "card/"
|
|
} else if getOssSignRequest.Scene == 3 {
|
|
dir = dir + "article/"
|
|
}
|
|
}
|
|
|
|
if dir == "" {
|
|
return nil, errors.New("获取签名失败")
|
|
}
|
|
|
|
// 生成签名
|
|
ossSign, _ := aliyun.GetOssSign(dir)
|
|
return ossSign, nil
|
|
}
|