102 lines
3.0 KiB
Go
102 lines
3.0 KiB
Go
package account
|
||
|
||
import (
|
||
"SciencesServer/app/api/enterprise/model"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/service"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/config"
|
||
"SciencesServer/utils"
|
||
"errors"
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
type Instance struct{}
|
||
|
||
type InstanceHandle func() *Instance
|
||
|
||
type InstanceLoginCallback func(params *InstanceLoginParams) *InstanceLoginReturn
|
||
|
||
type (
|
||
InstanceLoginParams struct {
|
||
UID uint64
|
||
Avatar, Name, Mobile string
|
||
Vip uint64
|
||
VipStatus bool
|
||
VipDeadline time.Time
|
||
Currency float64
|
||
Identity, SelectIdentity int
|
||
Status model2.AccountStatusKind
|
||
}
|
||
InstanceLoginReturn struct {
|
||
Token string `json:"token"`
|
||
TokenEffectTime int `json:"token_effect_time"`
|
||
}
|
||
)
|
||
|
||
func (c *Instance) Login() InstanceLoginCallback {
|
||
return func(params *InstanceLoginParams) *InstanceLoginReturn {
|
||
token := utils.JWTEncrypt(utils.StringToInt(config.SystemConfig[config.SysTokenEffectTime]), map[string]interface{}{
|
||
config.TokenForUID: fmt.Sprintf("%d", params.UID),
|
||
})
|
||
_session := session.NewEnterprise()
|
||
_session.Token = token
|
||
_session.UID = params.UID
|
||
_session.Avatar = params.Avatar
|
||
_session.Name = params.Name
|
||
_session.Mobile = params.Mobile
|
||
_session.Vip = int(params.Vip)
|
||
_session.VipStatus = params.VipStatus
|
||
_session.VipDeadline = params.VipDeadline
|
||
_session.Currency = params.Currency
|
||
_session.Identity = params.Identity
|
||
_session.SelectIdentity = params.SelectIdentity
|
||
|
||
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccountEnterprise, _session.GetStringUID(), _session)
|
||
|
||
return &InstanceLoginReturn{Token: token, TokenEffectTime: utils.StringToInt(config.SystemConfig[config.SysTokenEffectTime])}
|
||
}
|
||
}
|
||
|
||
// ResetPassword 重置密码
|
||
func (c *Instance) ResetPassword(token, password, repeatPass string) error {
|
||
tokenInfo := utils.JWTDecrypt(token)
|
||
|
||
if tokenInfo == nil || len(tokenInfo) <= 0 {
|
||
return errors.New("操作错误,Token无效")
|
||
}
|
||
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
|
||
expTime := time.Unix(expTimestamp, 0)
|
||
ok := expTime.After(time.Now())
|
||
|
||
if !ok {
|
||
return errors.New("操作错误,Token过期")
|
||
}
|
||
if password != repeatPass {
|
||
return errors.New("操作错误,两次密码不一致")
|
||
}
|
||
mUserInstance := model.NewUserInstance()
|
||
|
||
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "name", "mobile", "status"},
|
||
model2.NewWhere("mobile", tokenInfo["mobile"]))
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,系统中未找到此手机用户")
|
||
}
|
||
mUserInstance.Password = password
|
||
mUserInstance.SetPasswordAttribute()
|
||
|
||
return model2.Updates(mUserInstance.UserInstance, map[string]interface{}{
|
||
"password": mUserInstance.Password, "salt": mUserInstance.Salt, "updated_at": time.Now(),
|
||
})
|
||
}
|
||
|
||
func NewInstance() InstanceHandle {
|
||
return func() *Instance {
|
||
return &Instance{}
|
||
}
|
||
}
|