sso

forget_password_controller.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package controllers
  2. import (
  3. "time"
  4. "SSO/enums"
  5. "SSO/service"
  6. "SSO/utils"
  7. )
  8. type ForgetPwdController struct {
  9. BaseController
  10. }
  11. // /password/forget [get]
  12. func (this *ForgetPwdController) ForgetPassword() {
  13. redisClient := service.RedisClient()
  14. defer redisClient.Close()
  15. req := this.Ctx.Request
  16. addr := utils.GetIP(req)
  17. cur_time := time.Now().Format("2006-01-02")
  18. _, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
  19. if err != nil {
  20. redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
  21. }
  22. //将客户端的ip加密传给前端,作为短信验证的密钥,来验证短信发送的IP地址
  23. aespass := utils.AESEncrypt(addr)
  24. returnURL := this.GetString("return_url")
  25. this.Data["return_url"] = returnURL
  26. this.Data["aespass"] = aespass
  27. this.SetTpl("new_main/forget_password.html")
  28. }
  29. // /password/modify [post]
  30. // @param mobile:string
  31. // @param code:string
  32. // @param password:string
  33. func (this *ForgetPwdController) ModifyPassword() {
  34. mobile := this.GetString("mobile")
  35. code := this.GetString("code")
  36. password := this.GetString("password")
  37. checkErr := this.checkParams(mobile, code, password)
  38. if checkErr != nil {
  39. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(checkErr)
  40. this.ServeJSON()
  41. return
  42. }
  43. modifyErr := service.ModifyPassword(mobile, password)
  44. if modifyErr != nil {
  45. utils.ErrorLog("修改mobile=%v的用户的密码时失败: %v", mobile, modifyErr)
  46. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  47. this.ServeJSON()
  48. return
  49. } else {
  50. // 修改成功后验证码就要使其失效
  51. redisClient := service.RedisClient()
  52. defer redisClient.Close()
  53. redisClient.Del("code_msg_" + mobile)
  54. this.Data["json"] = enums.MakeSuccessResponseJSON(nil)
  55. this.ServeJSON()
  56. return
  57. }
  58. }
  59. func (this *ForgetPwdController) checkParams(mobile string, code string, password string) *enums.SGJError {
  60. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  61. return &enums.SGJError{Code: enums.ErrorCodeMobileFormat}
  62. }
  63. if len(code) == 0 {
  64. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  65. }
  66. if len(password) == 0 {
  67. return &enums.SGJError{Code: enums.ErrorCodePasswordEmpty}
  68. }
  69. if service.IsMobileRegister(mobile) == false {
  70. return &enums.SGJError{Code: enums.ErrorCodeMobileNotExit}
  71. }
  72. redisClient := service.RedisClient()
  73. defer redisClient.Close()
  74. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  75. if cache_code != code {
  76. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  77. }
  78. return nil
  79. }