Files

71 lines
2.0 KiB
Go
Raw Normal View History

2021-09-28 11:47:19 +08:00
package controller
import (
2021-10-15 15:06:02 +08:00
model3 "SciencesServer/app/api/manage/model"
2021-09-28 11:47:19 +08:00
"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("验证码错误")
}
2021-10-15 15:06:02 +08:00
mSysUser := model3.NewSysUser()
2021-09-28 11:47:19 +08:00
isExist, err := mSysUser.GetByAccountOrMobile(account)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("该帐号信息不存在")
}
2021-11-01 11:19:49 +08:00
2021-09-28 11:47:19 +08:00
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()
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 {
2021-11-24 09:59:29 +08:00
if c.Session != nil && c.UID > 0 {
service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccount, utils.UintToString(c.UID))
}
2021-09-28 11:47:19 +08:00
return nil
}
func NewAccount() AccountHandle {
return func(session *service.Session) *Account {
return &Account{Platform: &Platform{Session: session}}
}
}