package controller import ( model3 "SciencesServer/app/api/manage/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() 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}} } }