diff --git a/app/api/enterprise/api/account.go b/app/api/enterprise/api/account.go index 140a942..185889d 100644 --- a/app/api/enterprise/api/account.go +++ b/app/api/enterprise/api/account.go @@ -77,3 +77,18 @@ func (*Account) Logout(c *gin.Context) { err := account.NewLogout()(_session).Launch() api.APIResponse(err)(c) } + +// ResetPassword 重置密码 +func (*Account) ResetPassword(c *gin.Context) { + form := &struct { + Token string `json:"token" form:"token" binding:"required"` + Password string `json:"password" form:"password" binding:"required"` + RepeatPass string `json:"repeat_pass" form:"repeat_pass" binding:"required"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + err := account.NewInstance()().ResetPassword(form.Token, form.Password, form.RepeatPass) + api.APIResponse(err)(c) +} diff --git a/app/api/enterprise/api/config.go b/app/api/enterprise/api/config.go deleted file mode 100644 index bdf19a2..0000000 --- a/app/api/enterprise/api/config.go +++ /dev/null @@ -1,3 +0,0 @@ -package api - -type Config struct{} diff --git a/app/api/enterprise/controller/account/instance.go b/app/api/enterprise/controller/account/instance.go index 63b558c..b31d955 100644 --- a/app/api/enterprise/controller/account/instance.go +++ b/app/api/enterprise/controller/account/instance.go @@ -1,11 +1,13 @@ package account import ( - "SciencesServer/app/common/model" + "SciencesServer/app/api/enterprise/model" + model2 "SciencesServer/app/common/model" "SciencesServer/app/service" "SciencesServer/app/session" "SciencesServer/config" "SciencesServer/utils" + "errors" "fmt" "time" ) @@ -20,12 +22,12 @@ type ( InstanceLoginParams struct { UID uint64 Avatar, Name, Mobile string - Vip model.UserInstanceVipKind + Vip model2.UserInstanceVipKind VipStatus bool VipDeadline time.Time Currency float64 Identity, SelectIdentity int - Status model.AccountStatusKind + Status model2.AccountStatusKind } InstanceLoginReturn struct { Token string `json:"token"` @@ -57,6 +59,41 @@ func (c *Instance) Login() InstanceLoginCallback { } } +// 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{} diff --git a/app/basic/api/sms.go b/app/basic/api/sms.go index c298e1b..c923a25 100644 --- a/app/basic/api/sms.go +++ b/app/basic/api/sms.go @@ -7,7 +7,7 @@ import ( type Sms struct{} -func (a *Sms) sCaptcha(c *gin.Context) { +func (*Sms) Captcha(c *gin.Context) { form := &struct { Mobile string `json:"mobile" form:"mobile" binding:"required"` }{} @@ -16,5 +16,18 @@ func (a *Sms) sCaptcha(c *gin.Context) { return } err := controller.NewSms()().Captcha(form.Mobile) - APIResponse(err) + APIResponse(err)(c) +} + +func (*Sms) CaptchaValidate(c *gin.Context) { + form := &struct { + Mobile string `json:"mobile" form:"mobile" binding:"required"` + Captcha string `json:"captcha" form:"captcha" binding:"required"` + }{} + if err := Bind(form)(c); err != nil { + APIFailure(err.(error))(c) + return + } + data, err := controller.NewSms()().CaptchaValidate(form.Mobile, form.Captcha) + APIResponse(err, data)(c) } diff --git a/app/basic/controller/sms.go b/app/basic/controller/sms.go index 428ef63..c423c8f 100644 --- a/app/basic/controller/sms.go +++ b/app/basic/controller/sms.go @@ -4,18 +4,26 @@ import ( "SciencesServer/app/handle" "SciencesServer/utils" "errors" + "fmt" ) type Sms struct{} type SmsHandle func() *Sms -func (c *Sms) captchaCallback(mobile, code string) error { - //content := fmt.Sprintf("【汇安科技】您的验证码是%s,三分钟内有效", code) - // 发送短信 - //_ = platform.NewSms().Send()(platform.SmsSendModeForGroup, &platform.SmsParam{ - // Mobile: []string{mobile}, Content: content, - //}) +type ( + CaptchaCallback struct { + Token string `json:"token"` + EffectTime int `json:"effect_time"` + } +) + +// tokenEffectTime Token有效期,单位s +var tokenEffectTime = 5 * 60 + +func (c *Sms) captchaCallback(mobile, code string, callback func() error) error { + content := fmt.Sprintf("【中科云】您的验证码是%s,三分钟内有效", code) + fmt.Println(content) // 执行保存数据库 //mSysSmsLogs := model.NewSysSmsLogs() //mSysSmsLogs.Mobile = mobile @@ -23,6 +31,14 @@ func (c *Sms) captchaCallback(mobile, code string) error { //mSysSmsLogs.Usage = model2.SysSmsLogsUsageForCaptcha // //return model2.Create(mSysSmsLogs.SysSmsLogs) + + // 发送短信 + //_ = platform.NewSms().Send()(platform.SmsSendModeForGroup, &platform.SmsParam{ + // Mobile: []string{mobile}, Content: content, + //}) + if callback != nil { + //_ = callback() + } return nil } @@ -32,11 +48,32 @@ func (c *Sms) Captcha(mobile string) error { return errors.New("手机格式不正确") } if err := handle.NewCaptcha().Sms(6, mobile, c.captchaCallback); err != nil { - return errors.New("操作错误") + return err } return nil } +func (c *Sms) CaptchaValidate(mobile, captcha string) (*CaptchaCallback, error) { + if !utils.ValidateMobile(mobile) { + return nil, errors.New("手机格式不正确") + } + pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{ + Mobile: mobile, + Captcha: captcha, + }) + if err != nil { + return nil, err + } else if !pass { + return nil, errors.New("手机号或验证码错误") + } + return &CaptchaCallback{ + Token: utils.JWTEncrypt(tokenEffectTime, map[string]interface{}{ + "mobile": mobile, + }), + EffectTime: tokenEffectTime, + }, nil +} + func NewSms() SmsHandle { return func() *Sms { return &Sms{} diff --git a/app/handle/captcha.go b/app/handle/captcha.go index a87d6a7..5b6206e 100644 --- a/app/handle/captcha.go +++ b/app/handle/captcha.go @@ -28,11 +28,11 @@ type ( } ) -type SmsCallback func(mobile, code string) error +type SmsCallback func(mobile, code string, function func() error) error const ( // SmsCaptchaEffectiveTime 短信验证码有效时间 - SmsCaptchaEffectiveTime int = 3600 * 3 + SmsCaptchaEffectiveTime int = 60 * 3 ) func (this *CaptchaSms) validate() (bool, error) { @@ -55,13 +55,12 @@ func (this *CaptchaImage) validate() (bool, error) { // Sms 短信 func (this *Captcha) Sms(length int, mobile string, callback SmsCallback) error { code := utils.GetRandomCode(length) - // 存储redis - if err := cache.Cache.Set(mobile, code, SmsCaptchaEffectiveTime); err != nil { - return err - } // 发送短信 //NewSms().Handle(mobile) - return callback(mobile, code) + return callback(mobile, code, func() error { + // 存储redis + return cache.Cache.Set(mobile, code, SmsCaptchaEffectiveTime) + }) } // Image 图形 diff --git a/app/service/es_achievement.go b/app/service/es_achievement.go index 2db0362..449b12a 100644 --- a/app/service/es_achievement.go +++ b/app/service/es_achievement.go @@ -56,7 +56,7 @@ func (this *ESAchievement) Search(page, pageSize int) (interface{}, int64, error if this.Keyword != "" { mustParams["keyword"] = this.Keyword } - termParams["is_show"] = 1 + //termParams["is_show"] = 1 return es.Search(this, this.Index(), &es.SearchParams{ TermParams: termParams, diff --git a/router/address.go b/router/address.go index c1a594a..d285999 100644 --- a/router/address.go +++ b/router/address.go @@ -424,6 +424,9 @@ func registerEnterpriseAPI(app *gin.Engine) { apiPrefix + "/v1/account/login", apiPrefix + "/v1/account/register", apiPrefix + "/v1/account/authorize", + apiPrefix + "/v1/account/reset/password", + apiPrefix + "/v1/sms/captcha", + apiPrefix + "/v1/sms/captcha/validate", }...))) v1.Use(NeedAuthIdentity()) @@ -438,6 +441,13 @@ func registerEnterpriseAPI(app *gin.Engine) { configV1.GET("/identity", _api.Identity) configV1.GET("/industry", _api.Industry) } + // SMS 短信管理 + smsV1 := v1.Group("/sms") + { + _api := new(api.Sms) + smsV1.POST("/captcha", _api.Captcha) + smsV1.POST("/captcha/validate", _api.CaptchaValidate) + } // Account 账号管理 accountV1 := v1.Group("/account") { @@ -446,6 +456,7 @@ func registerEnterpriseAPI(app *gin.Engine) { accountV1.POST("/register", _api.Register) accountV1.POST("/authorize", _api.Authorize) accountV1.POST("/logout", _api.Logout) + accountV1.POST("/reset/password", _api.ResetPassword) } // User 用户管理 userV1 := v1.Group("/user") diff --git a/router/auth.go b/router/auth.go index 7aaea5f..fb4cbf1 100644 --- a/router/auth.go +++ b/router/auth.go @@ -28,7 +28,6 @@ func AddSkipperURL(url ...string) SkipperURL { // NeedLogin 需要登录 func NeedLogin(key string, session logic.ISession, skipperURL ...SkipperURL) gin.HandlerFunc { return func(c *gin.Context) { - fmt.Println(c.Request.URL.Path) if len(skipperURL) > 0 && skipperURL[0](c) { c.Next() return