109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package controller
|
|
|
|
import (
|
|
model3 "SciencesServer/app/api/manage/model"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/handle"
|
|
"SciencesServer/app/service"
|
|
"SciencesServer/config"
|
|
"SciencesServer/utils"
|
|
"errors"
|
|
)
|
|
|
|
type Account struct{ *Platform }
|
|
|
|
type AccountHandle func(session *service.Session) *Account
|
|
|
|
type (
|
|
AccountLoginResponse struct {
|
|
Token string `json:"token"`
|
|
EffectTime int `json:"effect_time"`
|
|
}
|
|
)
|
|
|
|
// Login 登录请求
|
|
func (c *Account) Login(account, password, captchaKey, captchaValue, equipment, ip string) (*AccountLoginResponse, error) {
|
|
// 验证验证码
|
|
if pass, _ := handle.NewCaptcha().Validate(&handle.CaptchaImage{Key: captchaKey, Captcha: captchaValue}); !pass {
|
|
return nil, errors.New("验证码错误")
|
|
}
|
|
mSysUser := model3.NewSysUser()
|
|
|
|
isExist, err := mSysUser.GetByAccountOrMobile(account)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !isExist {
|
|
return nil, errors.New("该帐号信息不存在")
|
|
}
|
|
if !mSysUser.ValidatePassword(password) {
|
|
return nil, errors.New("密码错误")
|
|
}
|
|
session := service.NewSession()
|
|
session.UID = mSysUser.UUID
|
|
session.Name = mSysUser.Name
|
|
session.Mobile = mSysUser.Mobile
|
|
session.IsAdmin = mSysUser.IsAdminUser()
|
|
// 非超级管理员
|
|
if !session.IsAdmin {
|
|
// 查询相应关系
|
|
mSysUserTenant := model3.NewSysUserTenant()
|
|
|
|
if isExist, err = model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "tenant_id", "identity", "status"},
|
|
model2.NewWhere("uid", mSysUser.UUID)); err != nil {
|
|
return nil, err
|
|
} else if !isExist {
|
|
return nil, errors.New("当前不属于任何租户,不可登录")
|
|
}
|
|
if mSysUserTenant.TenantID <= 0 {
|
|
goto Complete
|
|
}
|
|
mSysTenant := model3.NewSysTenant()
|
|
mSysTenant.ID = mSysUserTenant.TenantID
|
|
|
|
level := 0
|
|
|
|
for {
|
|
if isExist, err = model2.FirstField(mSysTenant, []string{"id", "key", "parent_id", "deadline", "status"}); err != nil {
|
|
return nil, err
|
|
} else if !isExist {
|
|
return nil, errors.New("租户信息不存在,不可登录")
|
|
} else if mSysTenant.IsInvalid() {
|
|
return nil, errors.New("租户协议已失效,不可登录")
|
|
}
|
|
if level <= 0 {
|
|
session.TenantID = mSysTenant.ID
|
|
//session.TenantKey = mSysTenant.Key
|
|
}
|
|
// 判断是否含有含有上级
|
|
if mSysTenant.ParentID <= 0 {
|
|
goto Complete
|
|
}
|
|
level++
|
|
mSysTenant.ID = mSysTenant.ParentID
|
|
}
|
|
}
|
|
Complete:
|
|
uid := mSysUser.UUIDString()
|
|
|
|
session.Token = utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{config.TokenForUID: uid})
|
|
|
|
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, uid, session)
|
|
|
|
service.Publish(config.EventForAccountLoginProduce, session.TenantID, session.UID, equipment, ip)
|
|
|
|
return &AccountLoginResponse{Token: session.Token, EffectTime: config.SettingInfo.TokenEffectTime}, nil
|
|
}
|
|
|
|
// Logout 退出请求
|
|
func (c *Account) Logout() error {
|
|
service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccount, utils.UintToString(c.UID))
|
|
return nil
|
|
}
|
|
|
|
func NewAccount() AccountHandle {
|
|
return func(session *service.Session) *Account {
|
|
return &Account{Platform: &Platform{Session: session}}
|
|
}
|
|
}
|