sso

register_controller.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package controllers
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "SSO/enums"
  7. "SSO/service"
  8. "SSO/utils"
  9. )
  10. type RegisterController struct {
  11. BaseController
  12. }
  13. // /register [get]
  14. func (this *RegisterController) Register() {
  15. mobile := this.GetString("mobile")
  16. redisClient := service.RedisClient()
  17. defer redisClient.Close()
  18. req := this.Ctx.Request
  19. addr := utils.GetIP(req)
  20. cur_time := time.Now().Format("2006-01-02")
  21. _, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
  22. if err != nil {
  23. redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
  24. }
  25. //将客户端的ip加密传给前端,作为短信验证的密钥,来验证短信发送的IP地址
  26. aespass := utils.AESEncrypt(addr)
  27. returnURL := this.GetString("return_url")
  28. this.Data["return_url"] = returnURL
  29. this.Data["aespass"] = aespass
  30. this.Data["mobile"] = mobile
  31. this.SetTpl("new_main/register.html")
  32. }
  33. // /register/submit [post]
  34. func (this *RegisterController) RegisterSubmit() {
  35. mobile := this.GetString("phone")
  36. pwd := this.GetString("password")
  37. code := this.GetString("code")
  38. // 判断手机号是否存在
  39. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  40. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  41. this.ServeJSON()
  42. return
  43. }
  44. if service.IsMobileRegister(mobile) == true {
  45. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  46. this.ServeJSON()
  47. return
  48. }
  49. if len(pwd) == 0 {
  50. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  51. this.ServeJSON()
  52. return
  53. }
  54. if len(code) == 0 {
  55. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  56. this.ServeJSON()
  57. return
  58. }
  59. redisClient := service.RedisClient()
  60. defer redisClient.Close()
  61. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  62. if cache_code != code {
  63. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  64. this.ServeJSON()
  65. return
  66. }
  67. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  68. if err != nil {
  69. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  70. this.ServeJSON()
  71. return
  72. } else {
  73. // 保存登录令牌
  74. token := utils.GenerateLoginToken(mobile)
  75. expiration, _ := beego.AppConfig.Int64("login_token_expiration_second")
  76. this.Ctx.SetCookie("sso_token_cookie", token, expiration)
  77. if isDebuggingSSO, _ := beego.AppConfig.Bool("is_sso_use_session_id_key"); isDebuggingSSO {
  78. share_session_id := this.Ctx.Input.CruSession.SessionID()
  79. this.Ctx.SetCookie("s", share_session_id, expiration, "/", beego.AppConfig.String("cookie_rootdomain"))
  80. redisClient.Set(fmt.Sprintf("sso_token_%v", share_session_id), token, time.Duration(expiration)*time.Second)
  81. } else {
  82. redisClient.Set(fmt.Sprintf("sso_token_%v", mobile), token, time.Duration(expiration)*time.Second)
  83. }
  84. this.SetSession("admin_user", admin)
  85. // 注册成功后验证码就要使其失效
  86. redisClient.Del("code_msg_" + mobile)
  87. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  88. "result": true,
  89. "id": admin.Id,
  90. })
  91. this.ServeJSON()
  92. return
  93. }
  94. }